From 9beb35ead02e1b071ccebd1c1dbc3ed5e43111f1 Mon Sep 17 00:00:00 2001 From: gvvp Date: Sun, 26 Jul 2020 14:15:36 +0900 Subject: [PATCH] =?utf8?q?svn=E3=81=8B=E3=82=89=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/AssistRB.sln | 20 + src/AssistRB/App.xaml | 8 + src/AssistRB/App.xaml.cs | 16 + src/AssistRB/AssistRB.csproj | 162 ++++++ src/AssistRB/DlnaItemList.cs | 109 ++++ src/AssistRB/DynamicJson.cs | 431 +++++++++++++++ src/AssistRB/FindRecbox.cs | 271 ++++++++++ src/AssistRB/MainWindow.xaml | 96 ++++ src/AssistRB/MainWindow.xaml.cs | 403 ++++++++++++++ src/AssistRB/Properties/AssemblyInfo.cs | 55 ++ src/AssistRB/Properties/Resources.Designer.cs | 63 +++ src/AssistRB/Properties/Resources.resx | 117 ++++ src/AssistRB/Properties/Settings.Designer.cs | 26 + src/AssistRB/Properties/Settings.settings | 7 + src/AssistRB/RecboxController.cs | 751 ++++++++++++++++++++++++++ src/AssistRB/SortableBindingList.cs | 151 ++++++ src/AssistRB/ToolLog.cs | 100 ++++ src/AssistRB/TransferStatusChecker.cs | 277 ++++++++++ src/AssistRB/WebLowLayer.cs | 400 ++++++++++++++ src/AssistRB/WindowConnection.xaml | 20 + src/AssistRB/WindowConnection.xaml.cs | 160 ++++++ src/AssistRB/app.config | 3 + 22 files changed, 3646 insertions(+) create mode 100644 src/AssistRB.sln create mode 100644 src/AssistRB/App.xaml create mode 100644 src/AssistRB/App.xaml.cs create mode 100644 src/AssistRB/AssistRB.csproj create mode 100644 src/AssistRB/DlnaItemList.cs create mode 100644 src/AssistRB/DynamicJson.cs create mode 100644 src/AssistRB/FindRecbox.cs create mode 100644 src/AssistRB/MainWindow.xaml create mode 100644 src/AssistRB/MainWindow.xaml.cs create mode 100644 src/AssistRB/Properties/AssemblyInfo.cs create mode 100644 src/AssistRB/Properties/Resources.Designer.cs create mode 100644 src/AssistRB/Properties/Resources.resx create mode 100644 src/AssistRB/Properties/Settings.Designer.cs create mode 100644 src/AssistRB/Properties/Settings.settings create mode 100644 src/AssistRB/RecboxController.cs create mode 100644 src/AssistRB/SortableBindingList.cs create mode 100644 src/AssistRB/ToolLog.cs create mode 100644 src/AssistRB/TransferStatusChecker.cs create mode 100644 src/AssistRB/WebLowLayer.cs create mode 100644 src/AssistRB/WindowConnection.xaml create mode 100644 src/AssistRB/WindowConnection.xaml.cs create mode 100644 src/AssistRB/app.config diff --git a/src/AssistRB.sln b/src/AssistRB.sln new file mode 100644 index 0000000..5d1439b --- /dev/null +++ b/src/AssistRB.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssistRB", "AssistRB\AssistRB.csproj", "{87CC2E6A-739E-457B-B2C6-E6F6878CD6ED}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {87CC2E6A-739E-457B-B2C6-E6F6878CD6ED}.Debug|x86.ActiveCfg = Debug|x86 + {87CC2E6A-739E-457B-B2C6-E6F6878CD6ED}.Debug|x86.Build.0 = Debug|x86 + {87CC2E6A-739E-457B-B2C6-E6F6878CD6ED}.Release|x86.ActiveCfg = Release|x86 + {87CC2E6A-739E-457B-B2C6-E6F6878CD6ED}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/AssistRB/App.xaml b/src/AssistRB/App.xaml new file mode 100644 index 0000000..c9d8979 --- /dev/null +++ b/src/AssistRB/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/src/AssistRB/App.xaml.cs b/src/AssistRB/App.xaml.cs new file mode 100644 index 0000000..182bb5c --- /dev/null +++ b/src/AssistRB/App.xaml.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Windows; + +namespace AssistRB +{ + /// + /// App.xaml の相互作用ロジック + /// + public partial class App : Application + { + } +} diff --git a/src/AssistRB/AssistRB.csproj b/src/AssistRB/AssistRB.csproj new file mode 100644 index 0000000..df7aea8 --- /dev/null +++ b/src/AssistRB/AssistRB.csproj @@ -0,0 +1,162 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {87CC2E6A-739E-457B-B2C6-E6F6878CD6ED} + WinExe + Properties + AssistRB + AssistRB + v4.0 + + + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + true + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + + + + + + + WindowConnection.xaml + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + MainWindow.xaml + Code + + + Designer + MSBuild:Compile + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + + + + False + Microsoft .NET Framework 4 Client Profile %28x86 および x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows インストーラー 3.1 + true + + + + + \ No newline at end of file diff --git a/src/AssistRB/DlnaItemList.cs b/src/AssistRB/DlnaItemList.cs new file mode 100644 index 0000000..80362f1 --- /dev/null +++ b/src/AssistRB/DlnaItemList.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net; + +namespace AssistRB +{ + public class DlnaItem + { + public string Parent { get; set; } + public string Id { get; set; } + public string Title { get; set; } + public string Series { get; set; } + public string ClassType { get; set; } + public Int64 Size { get; set; } + public string Date { get; set; } + } + + public class DlnaItemList + { + private const string RootFolderId = "FS-Folder"; + + static DlnaItemList instance = null; + + private List dlnaItem = new List(); + private Dictionary folderDictionary = new Dictionary(); + private List invalidFolder = new List(); + + public delegate void ListChangedDelegate(); + public event ListChangedDelegate ListChanged; + + public static DlnaItemList GetInstance() + { + if (instance == null) + { + instance = new DlnaItemList(); + instance.InvalidateAll(); + } + + return instance; + } + + public void InvalidateAll() + { + invalidFolder.Clear(); + invalidFolder.Add(RootFolderId); + } + + public void Invalidate(string folder) + { + if (invalidFolder.Contains(RootFolderId)) + { + return; + } + + if (folder == RootFolderId) + { + InvalidateAll(); + } + else + { + if (!invalidFolder.Contains(folder)) + { + invalidFolder.Add(folder); + } + } + } + + public List GetList() + { + return this.dlnaItem; + } + + public Dictionary GetFolderList() + { + return this.folderDictionary; + } + + public List GetInvalidList() + { + return this.invalidFolder; + } + + public void UpdateList( + List newList) + { + this.dlnaItem = newList; + + this.invalidFolder.Clear(); + + // フォルダ辞書の更新 + folderDictionary.Clear(); + folderDictionary.Add("FS-Folder", "ROOT"); + foreach (DlnaItem item in this.dlnaItem) + { + if (item.ClassType == "container") + { + folderDictionary.Add(item.Id, item.Title); + } + } + + if (this.ListChanged != null) + { + this.ListChanged(); + } + } + } +} diff --git a/src/AssistRB/DynamicJson.cs b/src/AssistRB/DynamicJson.cs new file mode 100644 index 0000000..45771ed --- /dev/null +++ b/src/AssistRB/DynamicJson.cs @@ -0,0 +1,431 @@ +/*-------------------------------------------------------------------------- +* DynamicJson +* ver 1.2.0.0 (May. 21th, 2010) +* +* created and maintained by neuecc +* licensed under Microsoft Public License(Ms-PL) +* http://neue.cc/ +* http://dynamicjson.codeplex.com/ +*--------------------------------------------------------------------------*/ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Dynamic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization.Json; +using System.Text; +using System.Xml; +using System.Xml.Linq; + +namespace Codeplex.Data +{ + public class DynamicJson : DynamicObject + { + private enum JsonType + { + @string, number, boolean, @object, array, @null + } + + // public static methods + + /// from JsonSring to DynamicJson + public static dynamic Parse(string json) + { + return Parse(json, Encoding.Unicode); + } + + /// from JsonSring to DynamicJson + public static dynamic Parse(string json, Encoding encoding) + { + using (var reader = JsonReaderWriterFactory.CreateJsonReader(encoding.GetBytes(json), XmlDictionaryReaderQuotas.Max)) + { + return ToValue(XElement.Load(reader)); + } + } + + /// from JsonSringStream to DynamicJson + public static dynamic Parse(Stream stream) + { + using (var reader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max)) + { + return ToValue(XElement.Load(reader)); + } + } + + /// from JsonSringStream to DynamicJson + public static dynamic Parse(Stream stream, Encoding encoding) + { + using (var reader = JsonReaderWriterFactory.CreateJsonReader(stream, encoding, XmlDictionaryReaderQuotas.Max, _ => { })) + { + return ToValue(XElement.Load(reader)); + } + } + + /// create JsonSring from primitive or IEnumerable or Object({public property name:property value}) + public static string Serialize(object obj) + { + return CreateJsonString(new XStreamingElement("root", CreateTypeAttr(GetJsonType(obj)), CreateJsonNode(obj))); + } + + // private static methods + + private static dynamic ToValue(XElement element) + { + var type = (JsonType)Enum.Parse(typeof(JsonType), element.Attribute("type").Value); + switch (type) + { + case JsonType.boolean: + return (bool)element; + case JsonType.number: + return (double)element; + case JsonType.@string: + return (string)element; + case JsonType.@object: + case JsonType.array: + return new DynamicJson(element, type); + case JsonType.@null: + default: + return null; + } + } + + private static JsonType GetJsonType(object obj) + { + if (obj == null) return JsonType.@null; + + switch (Type.GetTypeCode(obj.GetType())) + { + case TypeCode.Boolean: + return JsonType.boolean; + case TypeCode.String: + case TypeCode.Char: + case TypeCode.DateTime: + return JsonType.@string; + case TypeCode.Int16: + case TypeCode.Int32: + case TypeCode.Int64: + case TypeCode.UInt16: + case TypeCode.UInt32: + case TypeCode.UInt64: + case TypeCode.Single: + case TypeCode.Double: + case TypeCode.Decimal: + case TypeCode.SByte: + case TypeCode.Byte: + return JsonType.number; + case TypeCode.Object: + return (obj is IEnumerable) ? JsonType.array : JsonType.@object; + case TypeCode.DBNull: + case TypeCode.Empty: + default: + return JsonType.@null; + } + } + + private static XAttribute CreateTypeAttr(JsonType type) + { + return new XAttribute("type", type.ToString()); + } + + private static object CreateJsonNode(object obj) + { + var type = GetJsonType(obj); + switch (type) + { + case JsonType.@string: + case JsonType.number: + return obj; + case JsonType.boolean: + return obj.ToString().ToLower(); + case JsonType.@object: + return CreateXObject(obj); + case JsonType.array: + return CreateXArray(obj as IEnumerable); + case JsonType.@null: + default: + return null; + } + } + + private static IEnumerable CreateXArray(T obj) where T : IEnumerable + { + return obj.Cast() + .Select(o => new XStreamingElement("item", CreateTypeAttr(GetJsonType(o)), CreateJsonNode(o))); + } + + private static IEnumerable CreateXObject(object obj) + { + return obj.GetType() + .GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Select(pi => new { Name = pi.Name, Value = pi.GetValue(obj, null) }) + .Select(a => new XStreamingElement(a.Name, CreateTypeAttr(GetJsonType(a.Value)), CreateJsonNode(a.Value))); + } + + private static string CreateJsonString(XStreamingElement element) + { + using (var ms = new MemoryStream()) + using (var writer = JsonReaderWriterFactory.CreateJsonWriter(ms, Encoding.Unicode)) + { + element.WriteTo(writer); + writer.Flush(); + return Encoding.Unicode.GetString(ms.ToArray()); + } + } + + // dynamic structure represents JavaScript Object/Array + + readonly XElement xml; + readonly JsonType jsonType; + + /// create blank JSObject + public DynamicJson() + { + xml = new XElement("root", CreateTypeAttr(JsonType.@object)); + jsonType = JsonType.@object; + } + + private DynamicJson(XElement element, JsonType type) + { + Debug.Assert(type == JsonType.array || type == JsonType.@object); + + xml = element; + jsonType = type; + } + + public bool IsObject { get { return jsonType == JsonType.@object; } } + + public bool IsArray { get { return jsonType == JsonType.array; } } + + /// has property or not + public bool IsDefined(string name) + { + return IsObject && (xml.Element(name) != null); + } + + /// has property or not + public bool IsDefined(int index) + { + return IsArray && (xml.Elements().ElementAtOrDefault(index) != null); + } + + /// delete property + public bool Delete(string name) + { + var elem = xml.Element(name); + if (elem != null) + { + elem.Remove(); + return true; + } + else return false; + } + + /// delete property + public bool Delete(int index) + { + var elem = xml.Elements().ElementAtOrDefault(index); + if (elem != null) + { + elem.Remove(); + return true; + } + else return false; + } + + /// mapping to Array or Class by Public PropertyName + public T Deserialize() + { + return (T)Deserialize(typeof(T)); + } + + private object Deserialize(Type type) + { + return (IsArray) ? DeserializeArray(type) : DeserializeObject(type); + } + + private dynamic DeserializeValue(XElement element, Type elementType) + { + var value = ToValue(element); + if (value is DynamicJson) + { + value = ((DynamicJson)value).Deserialize(elementType); + } + return Convert.ChangeType(value, elementType); + } + + private object DeserializeObject(Type targetType) + { + var result = Activator.CreateInstance(targetType); + var dict = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => p.CanWrite) + .ToDictionary(pi => pi.Name, pi => pi); + foreach (var item in xml.Elements()) + { + PropertyInfo propertyInfo; + if (!dict.TryGetValue(item.Name.LocalName, out propertyInfo)) continue; + var value = DeserializeValue(item, propertyInfo.PropertyType); + propertyInfo.SetValue(result, value, null); + } + return result; + } + + private object DeserializeArray(Type targetType) + { + if (targetType.IsArray) // Foo[] + { + var elemType = targetType.GetElementType(); + dynamic array = Array.CreateInstance(elemType, xml.Elements().Count()); + var index = 0; + foreach (var item in xml.Elements()) + { + array[index++] = DeserializeValue(item, elemType); + } + return array; + } + else // List + { + var elemType = targetType.GetGenericArguments()[0]; + dynamic list = Activator.CreateInstance(targetType); + foreach (var item in xml.Elements()) + { + list.Add(DeserializeValue(item, elemType)); + } + return list; + } + } + + // Delete + public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) + { + result = (IsArray) + ? Delete((int)args[0]) + : Delete((string)args[0]); + return true; + } + + // IsDefined, if has args then TryGetMember + public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) + { + if (args.Length > 0) + { + result = null; + return false; + } + + result = IsDefined(binder.Name); + return true; + } + + // Deserialize or foreach(IEnumerable) + public override bool TryConvert(ConvertBinder binder, out object result) + { + if (binder.Type == typeof(IEnumerable) || binder.Type == typeof(object[])) + { + var ie = (IsArray) + ? xml.Elements().Select(x => ToValue(x)) + : xml.Elements().Select(x => (dynamic)new KeyValuePair(x.Name.LocalName, ToValue(x))); + result = (binder.Type == typeof(object[])) ? ie.ToArray() : ie; + } + else + { + result = Deserialize(binder.Type); + } + return true; + } + + private bool TryGet(XElement element, out object result) + { + if (element == null) + { + result = null; + return false; + } + + result = ToValue(element); + return true; + } + + public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) + { + return (IsArray) + ? TryGet(xml.Elements().ElementAtOrDefault((int)indexes[0]), out result) + : TryGet(xml.Element((string)indexes[0]), out result); + } + + public override bool TryGetMember(GetMemberBinder binder, out object result) + { + return (IsArray) + ? TryGet(xml.Elements().ElementAtOrDefault(int.Parse(binder.Name)), out result) + : TryGet(xml.Element(binder.Name), out result); + } + + private bool TrySet(string name, object value) + { + var type = GetJsonType(value); + var element = xml.Element(name); + if (element == null) + { + xml.Add(new XElement(name, CreateTypeAttr(type), CreateJsonNode(value))); + } + else + { + element.Attribute("type").Value = type.ToString(); + element.ReplaceNodes(CreateJsonNode(value)); + } + + return true; + } + + private bool TrySet(int index, object value) + { + var type = GetJsonType(value); + var e = xml.Elements().ElementAtOrDefault(index); + if (e == null) + { + xml.Add(new XElement("item", CreateTypeAttr(type), CreateJsonNode(value))); + } + else + { + e.Attribute("type").Value = type.ToString(); + e.ReplaceNodes(CreateJsonNode(value)); + } + + return true; + } + + public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) + { + return (IsArray) + ? TrySet((int)indexes[0], value) + : TrySet((string)indexes[0], value); + } + + public override bool TrySetMember(SetMemberBinder binder, object value) + { + return (IsArray) + ? TrySet(int.Parse(binder.Name), value) + : TrySet(binder.Name, value); + } + + public override IEnumerable GetDynamicMemberNames() + { + return (IsArray) + ? xml.Elements().Select((x, i) => i.ToString()) + : xml.Elements().Select(x => x.Name.LocalName); + } + + /// Serialize to JsonString + public override string ToString() + { + // is can't serialize. replace to + foreach (var elem in xml.Descendants().Where(x => x.Attribute("type").Value == "null")) + { + elem.RemoveNodes(); + } + return CreateJsonString(new XStreamingElement("root", CreateTypeAttr(jsonType), xml.Elements())); + } + } +} \ No newline at end of file diff --git a/src/AssistRB/FindRecbox.cs b/src/AssistRB/FindRecbox.cs new file mode 100644 index 0000000..bc6b9db --- /dev/null +++ b/src/AssistRB/FindRecbox.cs @@ -0,0 +1,271 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net; +using System.Net.Sockets; +using System.IO; +using System.Diagnostics; +using System.Threading; + +namespace AssistRB +{ + /// + /// オンラインのRECBOXを探します + /// + public class FindRecbox + { + /// + /// 発見したRECBOXを表します + /// + public class RecboxInfo : IEquatable + { + private string mName; + private byte[] mMac; + private IPAddress mAddress; + private IPAddress mMask; + private IPAddress mRoute; + private UInt16 mPort; + + public string Name + { + get { return mName; } + } + public byte[] Mac + { + get { return mMac; } + } + public IPAddress Address + { + get { return mAddress; } + } + public IPAddress Mask + { + get { return mMask; } + } + public IPAddress Route + { + get { return mRoute; } + } + public UInt16 Port + { + get { return mPort; } + } + + public RecboxInfo( + string name, + byte[] mac, + byte[] address, + byte[] mask, + byte[] route, + UInt16 port) + { + Debug.Assert(mac.Length == 6); + Debug.Assert(address.Length == 4); + Debug.Assert(mask.Length == 4); + Debug.Assert(route.Length == 4); + + this.mName = name; + this.mMac = (byte[])(mac.Clone()); + this.mAddress = new IPAddress(address); + this.mMask = new IPAddress(mask); + this.mRoute = new IPAddress(route); + this.mPort = port; + + } + + public bool Equals(RecboxInfo other) + { + if (this.mName != other.mName) return false; + if (this.mMac.Length != other.mMac.Length) return false; + for (int i = 0; i < this.mMac.Length; ++i) + { + if (this.mMac[i] != other.mMac[i]) return false; + } + if (!this.mAddress.Equals(other.mAddress)) return false; + if (!this.mMask.Equals(other.mMask)) return false; + if (!this.mRoute.Equals(other.mRoute)) return false; + if (this.mPort != other.mPort) return false; + return true; + } + }; + + private List mRecboxList = new List(); + + private UInt32 requestSeq { get; set; } + private byte[] request = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x46, 0x52, 0x4f, 0x4d, 0x2d, 0x50, + 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x49, 0x4f, 0x2d, 0x44, 0x41, + 0x54, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + + /// + /// コンストラクタです + /// + public FindRecbox() + { + Random rand = new Random(); + requestSeq = (UInt32)rand.Next(0x7fffffff); + } + + /// + /// 発見したRECBOXのリストを返します + /// + /// + public List GetRecboxList() + { + return mRecboxList; + } + + /// + /// 実際に検索します + /// + /// 発見した数を返します。エラーが発生した場合は負数を返します。 + public int Search() + { + int count = 0; + const double SearchTimeout = 5; // seconds + + mRecboxList.Clear(); + + try + { + UdpClient udpclient = new UdpClient(); + + IPEndPoint broadcast = new IPEndPoint(IPAddress.Broadcast, 65); + udpclient.Connect(broadcast); + + request[1] = (byte)((requestSeq / 0x1000000) % 0x100); + request[2] = (byte)((requestSeq / 0x10000) % 0x100); + request[3] = (byte)((requestSeq / 0x100) % 0x100); + request[4] = (byte)(requestSeq % 0x100); + ++requestSeq; + + udpclient.Send(request, request.Length); + udpclient.Send(request, request.Length); + udpclient.Send(request, request.Length); + + DateTime limitTime = DateTime.Now.AddSeconds(SearchTimeout); + + State state = new State(); + state.e = new IPEndPoint(IPAddress.Any, 65); + state.u = new UdpClient(state.e); + + while (limitTime > DateTime.Now) + { + recieved = false; + state.u.BeginReceive(new AsyncCallback(processUdpResponse), state); + + while (limitTime > DateTime.Now && !recieved) + { + Thread.Sleep(100); + } + + } + + udpclient.Close(); + + count = mRecboxList.Count; + } + catch (Exception) + { + count = -1; + } + + return count; + } + + /// + /// 非同期処理用 + /// + private struct State + { + public IPEndPoint e; + public UdpClient u; + }; + + /// + /// 非同期処理用 + /// + private bool recieved; + + /// + /// 非同期処理用 + /// + private void processUdpResponse(IAsyncResult result) + { + UdpClient u = (UdpClient)((State)(result.AsyncState)).u; + IPEndPoint e = (IPEndPoint)((State)(result.AsyncState)).e; + + byte[] recieve = u.EndReceive(result, ref e); + + if (recieve.Length == 132) + { + const int IPAddressOffset = 14; + const int NetMaskOffset = 18; + const int DefaultOffset = 22; + const int PortOffset = 26; + const int MACOffset = 28; + const int NameOffset = 34; + const int NameMaxLength = 80; + const int VendorOffset = 115; + const int VendorMaxLength = 16; + + string name = Encoding.ASCII.GetString(recieve, NameOffset, NameMaxLength); + name = name.TrimEnd('\0'); + string vendor = Encoding.ASCII.GetString(recieve, VendorOffset, VendorMaxLength); + vendor = vendor.TrimEnd('\0'); + + byte[] mac = new byte[6]; + for (int i = 0; i < 6; ++i) + { + mac[i] = recieve[MACOffset + i]; + } + + byte[] address = new byte[4]; + for (int i = 0; i < 4; ++i) + { + address[i] = recieve[IPAddressOffset + i]; + } + + byte[] mask = new byte[4]; + for (int i = 0; i < 4; ++i) + { + mask[i] = recieve[NetMaskOffset + i]; + } + + byte[] route = new byte[4]; + for (int i = 0; i < 4; ++i) + { + route[i] = recieve[DefaultOffset + i]; + } + + UInt16 port = (UInt16)(recieve[PortOffset] * 0x100 + recieve[PortOffset + 1]); + + RecboxInfo recboxInfo = new RecboxInfo( + name, mac, address, mask, route, port); + + if (!mRecboxList.Contains(recboxInfo)) + { + mRecboxList.Add(recboxInfo); + } + } + + recieved = true; + } + } +} diff --git a/src/AssistRB/MainWindow.xaml b/src/AssistRB/MainWindow.xaml new file mode 100644 index 0000000..de95120 --- /dev/null +++ b/src/AssistRB/MainWindow.xaml @@ -0,0 +1,96 @@ + + + +