private void receiveMessageCallback(IAsyncResult ar) { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); byte[] messageBuffer = ((UdpClient)ar.AsyncState).EndReceive(ar, ref endPoint); string decoded = Encoding.UTF8.GetString(messageBuffer); Console.WriteLine("decoded: " + decoded); InstrumentationPointExchangeMessage m = JsonConvert.DeserializeObject <InstrumentationPointExchangeMessage>(decoded); if (snapshots.ContainsKey(m.instrumentationPointName)) { m.value = snapshots[m.instrumentationPointName].Value; } if (watches.ContainsKey(m.instrumentationPointName)) { m.value = watches[m.instrumentationPointName][watches[m.instrumentationPointName].Count - 1]; } string responseString = JsonConvert.SerializeObject(m, Formatting.Indented); Console.WriteLine("Replying with: " + responseString); byte[] responseData = Encoding.UTF8.GetBytes(responseString); listener.Send(responseData, responseData.Length, endPoint); listener.BeginReceive(new AsyncCallback(receiveMessageCallback), listener); }
public void Start() { listener.Start(); isListening = true; Task.Run(() => { while (isListening) { using (TcpClient tc = listener.AcceptTcpClient()) using (NetworkStream ns = tc.GetStream()) using (StreamReader sr = new StreamReader(ns)) { string receivedJSON = sr.ReadToEnd(); InstrumentationPointExchangeMessage m = JsonConvert.DeserializeObject <InstrumentationPointExchangeMessage>(receivedJSON); Console.WriteLine(m); } } }); }
public object CaptureValueBlocking(InstrumentationPoint ip) { using (UdpClient client = new UdpClient(0)) { IPEndPoint responseEndPoint = new IPEndPoint(IPAddress.Any, 0); InstrumentationPointExchangeMessage requestContents = new InstrumentationPointExchangeMessage { instrumentationPointName = ip.Name, instrumentationPointType = ip.GetType().ToString(), value = "" }; byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject( requestContents, Formatting.Indented)); // This will block client.Send(requestData, requestData.Length, "127.0.0.1", RemoteTestingWrapper.RemoteConfigurations.RemoteListenerPort); byte[] encoded = client.Receive(ref responseEndPoint); InstrumentationPointExchangeMessage response = JsonConvert.DeserializeObject <InstrumentationPointExchangeMessage>( Encoding.UTF8.GetString(encoded)); return(response.value); } }