private async void ServerDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string request; using (DataReader dataReader = args.GetDataReader()) { request = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim(); } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => listbox_Logger.Items.Add(request)); // Echo the request back as the response. using (Stream outputStream = (await sender.GetOutputStreamAsync(args.RemoteAddress, "1337")).AsStreamForWrite()) { using (var streamWriter = new StreamWriter(outputStream)) { await streamWriter.WriteLineAsync(request); await streamWriter.FlushAsync(); } } //await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.listbox_Logger.Items.Add(string.Format("server sent back the response: \"{0}\"", request))); sender.Dispose(); // await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.listbox_Logger.Items.Add("server closed its socket")); }
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { using (var reader = args.GetDataReader()) { var buf = new byte[reader.UnconsumedBufferLength]; reader.ReadBytes(buf); MessageReceived?.Invoke(new DatagramSocketMessage(args.RemoteAddress.CanonicalName, buf)); } }
private void ClientDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string response; using (DataReader dataReader = args.GetDataReader()) { response = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim(); } sender.Dispose(); }
public static void ServerDatagramSocket_MessageReceived( Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string respond; using (DataReader dataReader = args.GetDataReader()) { respond = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim(); } StatusResponse = respond; }
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { uint dataLength = args.GetDataReader().UnconsumedBufferLength; byte[] result = new byte[dataLength]; args.GetDataReader().ReadBytes(result); SupportClass.PacketSupport packet = new SupportClass.PacketSupport(result, result.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(args.RemoteAddress.CanonicalName), int.Parse(args.RemotePort))); try { if (Enclosing_Instance.localHost.ShouldIgnorePacket(packet)) { return; } DNSIncoming msg = new DNSIncoming(packet); logger.Debug("SocketListener.run() JmDNS in:" + msg.Print(true)); lock (Enclosing_Instance.IOLock) { if (msg.Query) { if (packet.Port != DNSConstants.MDNS_PORT) { Enclosing_Instance.HandleQuery(msg, packet.Address, packet.Port); } Enclosing_Instance.HandleQuery(msg, Enclosing_Instance.Group, DNSConstants.MDNS_PORT); } else { Enclosing_Instance.HandleResponse(msg); } } } catch (IOException e) { logger.Warn("run() exception ", e); } }
private async void ClientDatagramSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string response; using (DataReader dataReader = args.GetDataReader()) { response = dataReader.ReadString(dataReader.UnconsumedBufferLength).Trim(); } await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.clientListBox.Items.Add(string.Format("client received the response: \"{0}\"", response))); sender.Dispose(); await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.clientListBox.Items.Add("client closed its socket")); }
private void _Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { using (var reader = args.GetDataReader()) { var data = new ReceivedUdpData() { ReceivedBytes = Convert.ToInt32(reader.UnconsumedBufferLength), ReceivedFrom = new UdpEndPoint() { IPAddress = args.RemoteAddress.RawName, Port = Convert.ToInt32(args.RemotePort) } }; data.Buffer = new byte[data.ReceivedBytes]; reader.ReadBytes(data.Buffer); _ReceivedData.Enqueue(data); _DataAvailableSignal.Set(); } }
public static void ClientDatagramSocket_MessageReceived( Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args) { string response; using (DataReader dataReader = args.GetDataReader()) { byte[] buffer = new byte[dataReader.UnconsumedBufferLength]; dataReader.ReadBytes(buffer); StringBuilder builder = new StringBuilder(); foreach (var item in buffer) { builder.Append((char)item); } response = builder.ToString(); } Debug.WriteLine("COMMAND RESPONSE:" + response); ReportMsg("COMMAND RESPONSE:" + response); CommandResponse = response; //iserror = (response != "ok"); //sender.Dispose(); }
/// <summary> /// Event hander when receiving response from the ntp server. /// </summary> /// <param name="socket">The udp socket object which triggered this event </param> /// <param name="eventArguments">event information</param> void OnNTPTimeReceived(Windows.Networking.Sockets.DatagramSocket socket, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs eventArguments) { int currentRTT = (int)ntpResponseMonitor.ElapsedMilliseconds; Debug.WriteLine($"NtpSync: current RTT {currentRTT}"); ntpResponseMonitor.Stop(); if (currentNtpQueryCount < MaxNtpRTTProbeQuery) { //we only trace 'min' RTT within the RTT probe attempts if (minNtpRTT == -1 || minNtpRTT > currentRTT) { minNtpRTT = currentRTT; if (minNtpRTT == 0) { minNtpRTT = 1; //in case we got response so fast, consider it to be 1ms. } } averageNtpRTT = (averageNtpRTT * (currentNtpQueryCount - 1) + currentRTT) / currentNtpQueryCount; if (averageNtpRTT < 1) { averageNtpRTT = 1; } RunOnUiThread(() => { ntpQueryTimer.Stop(); ntpRTTIntervalTimer.Start(); }); return; } //if currentRTT is good enough, e.g.: closer to minRTT, then, we don't have to continue to query. if (currentRTT > (averageNtpRTT + minNtpRTT) / 2) { RunOnUiThread(() => { ntpQueryTimer.Stop(); ntpRTTIntervalTimer.Start(); }); return; } byte[] ntpData = new byte[48]; eventArguments.GetDataReader().ReadBytes(ntpData); //Offset to get to the "Transmit Timestamp" field (time at which the reply //departed the server for the client, in 64-bit timestamp format." const byte serverReplyTime = 40; //Get the seconds part ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime); //Get the seconds fraction ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4); //Convert From big-endian to little-endian intPart = SwapEndianness(intPart); fractPart = SwapEndianness(fractPart); ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L); RunOnUiThread(() => { OnNTPTimeAvailable?.Invoke((long)milliseconds + currentRTT / 2); }); socket.Dispose(); ReportNtpSyncStatus(true, currentRTT); }