private Envelope GetIncomingMessages() { TcpEnvelope newEnvelope = null; int incomingData = myTcpClient.Available; if (myTcpClient.Connected && (incomingData > 0)) { ////Get message var stream = myTcpClient.GetStream(); byte[] bytes = new byte[4]; stream.Read(bytes, 0, bytes.Length); var messageSize = BitConverter.ToInt32(bytes, 0); bytes = new byte[messageSize]; int totalBytesRead = 0; while (totalBytesRead != messageSize) { totalBytesRead += stream.Read(bytes, totalBytesRead, bytes.Length - totalBytesRead); } var message = MessageFactory.GetMessage(bytes); string key = ((IPEndPoint)myTcpClient.Client.RemoteEndPoint).ToString(); newEnvelope = new TcpEnvelope(message) { To = (IPEndPoint)myTcpClient.Client.RemoteEndPoint, Key = key }; } return(newEnvelope); }
private Envelope GetIncomingMessages() { TcpEnvelope newEnvelope = null; //Docs say that AcceptTcpClient() is a blocking call, so it won't just spin it's wheels. There is an asynch, AcceptTcpClientAsync. Log.Info($"Waiting for TCP connection on port ${((IPEndPoint)myTcpListenerClient.LocalEndpoint).Port}"); System.Net.Sockets.TcpClient client = myTcpListenerClient.AcceptTcpClient(); try { Log.Info($"Incoming TCP Connection established with {((IPEndPoint)client.Client.RemoteEndPoint).ToString()}"); ComService.AddTcpClient(client); } catch (Exception e) { Log.Error(e); } return(newEnvelope); }