public Response Query() { var e = new IPEndPoint(IPAddress.Any, Port); var u = new UdpClient(e); var UdpState = new UdpState() { Client = u, IpEndPoint = e }; try { var Timer = new Timer(2000); // Hook up the Elapsed event for the timer. Timer.Elapsed += (source, d) => { u.Close(); Timer.Dispose(); }; Timer.Start(); u.Connect(Host, Port); WriteData(UdpState, Handshake); var message = ReceiveMessages(UdpState); var challangeBytes = new byte[16]; Array.Copy(message, 5, challangeBytes, 0, message.Length - 5); var challengeInt = int.Parse(Encoding.ASCII.GetString(challangeBytes)); var token = BitConverter.GetBytes(challengeInt).Reverse().ToArray(); WriteData(UdpState, Stat, token, new byte[] { 0x00, 0x00, 0x00, 0x00 }); var response = ReceiveMessages(UdpState); return(new Response(response)); } finally { u.Close(); } }
public static void WriteData(UdpState s, byte cmd, byte[] append = null, byte[] append2 = null) { var cmdData = new byte[] { 0xFE, 0xFD, cmd, 0x01, 0x02, 0x03, 0x04 }; var dataLength = cmdData.Length + (append != null ? append.Length : 0) + (append2 != null ? append2.Length : 0); var data = new byte[dataLength]; cmdData.CopyTo(data, 0); if (append != null) { append.CopyTo(data, cmdData.Length); } if (append2 != null) { append2.CopyTo(data, cmdData.Length + (append != null ? append.Length : 0)); } s.Client.Send(data, data.Length); }
public static byte[] ReceiveMessages(UdpState s) { return(s.Client.Receive(ref s.IpEndPoint)); }