private void MKInfoAdd(MKInfo m) { lock (lockObj) { mkInfos.Add(m); } }
void ReadBytes(BinaryReader binaryReader, ref MKInfo mikroTikInfo) { byte[] Type = binaryReader.ReadBytes(2); Array.Reverse(Type); byte[] Length = binaryReader.ReadBytes(2); Array.Reverse(Length); ushort Length_Value = BitConverter.ToUInt16(Length); byte[] Value = binaryReader.ReadBytes(Length_Value); if (BitConverter.ToUInt16(Type) != TlvTypeUnknown) { switch (BitConverter.ToUInt16(Type)) { case TlvTypeMacAddr: mikroTikInfo.MacAddr = BitConverter.ToString(Value).Replace("-", ":"); break; case TlvTypeIdentity: mikroTikInfo.Identity = Encoding.Default.GetString(Value); break; case TlvTypeVersion: mikroTikInfo.Version = Encoding.Default.GetString(Value); break; case TlvTypePlatform: mikroTikInfo.Platform = Encoding.Default.GetString(Value); break; case TlvTypeUptime: mikroTikInfo.Uptime = TimeSpan.FromSeconds(BitConverter.ToUInt32(Value, 0)).ToString().Replace(".", "d"); break; case TlvTypeSoftwareID: mikroTikInfo.SoftwareID = Encoding.Default.GetString(Value); break; case TlvTypeBoard: mikroTikInfo.Board = Encoding.Default.GetString(Value); break; case TlvTypeUnpack: mikroTikInfo.Unpack = Encoding.Default.GetString(Value); break; case TlvTypeIPv6Addr: mikroTikInfo.IPv6Addr = Encoding.Default.GetString(Value); break; case TlvTypeInterface: mikroTikInfo.InterfaceName = Encoding.Default.GetString(Value); break; } ReadBytes(binaryReader, ref mikroTikInfo); } }
private void ReceiveMsg() { while (receiveFlag) { try { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); //ReceiveData:0000; if (receiveFlag) { if (receiveBytes.Length > 4) { if (RemoteIpEndPoint.Address.ToString() != IPAddress.Any.ToString()) { using MemoryStream memoryStream = new MemoryStream(receiveBytes); using BinaryReader binaryReader = new BinaryReader(memoryStream); string IPAddr = RemoteIpEndPoint.Address.ToString(); MKInfo mkInfo = new MKInfo() { IPAddr = IPAddr }; //TLV格式的数据指针偏移4 binaryReader.BaseStream.Position = 4; //开始读取TLV格式的数据 //递归方法读取二进制流的数据。 ReadBytes(binaryReader, ref mkInfo); foreach (MKInfo t in mkInfos) { if (t.IPAddr == mkInfo.IPAddr && t.MacAddr == mkInfo.MacAddr) { int i = mkInfos.IndexOf(t); ListRemove lr = new ListRemove(MKInfoRemove); lr(i); break; } } ListAdd la = new ListAdd(MKInfoAdd); la(mkInfo); } } } } catch (ObjectDisposedException) { } catch (SocketException) { } } udpClient.Dispose(); }