/// <summary> /// This will receive UDP messages. This thread will /// only run if UseUDP is set to true. /// </summary> public void UDP_Recv() { if (UsingUDP) { try { while (this.NetSystem.IsConnected) { if (IsDisconnecting) { break; } EndPoint tempUdpEp2 = UDPSocketS.RemoteEndPoint; //EndPoint tempUdpEp2 = new IPEndPoint(TCPSocket. int bytesRead = -10; string tempMessage = ""; while (this.NetSystem.IsConnected && (tempMessage == "" || bytesRead == 0)) //|| tempMessage.EndsWith("\n") == false { bytesRead = UDPSocketR.ReceiveFrom(UDPbuffer, 1024, SocketFlags.None, ref tempUdpEp2); tempMessage += Encoding.ASCII.GetString(UDPbuffer); UDPbuffer = new byte[1024]; } UDPMessage.Append(tempMessage); //Thread.Sleep(ThreadTimer); } } catch (SocketException e) { //Do nothing we have disconnected GenericNetworkCore.Logger("Socket Exception: " + e.ToString()); } } }
public void TCP_Recv() { //Must have IsConnected //Must have IsSever //Must have IsClient try { while (this.NetSystem.IsConnected) { if (IsDisconnecting) { break; } int byteRead = -10; string tempMessage = ""; while (this.NetSystem.IsConnected && (tempMessage == "" || byteRead == 0)) //|| tempMessage.EndsWith("\n") == false { byteRead += TCPSocket.Receive(TCPbuffer, 1024, SocketFlags.None); //TCPMessage.Append(Encoding.ASCII.GetString(TCPbuffer)); tempMessage += Encoding.ASCII.GetString(TCPbuffer); TCPbuffer = new byte[1024]; } TCPMessage.Append(tempMessage); string[] tempArgs = tempMessage.Split('\n'); foreach (string s in tempArgs) { if (s.StartsWith("DISCON")) { if (!IsDisconnecting) { TCP_Send("DISCON\n"); } IsDisconnecting = true; } if (s.StartsWith("IDPLEASE") && this.NetSystem.IsServer) { TCP_Send("ID#" + ConnectionID + "\n"); } if (s.StartsWith("ID#") && this.NetSystem.IsClient) { ConnectionID = int.Parse(s.Split('#')[1].Split('\n')[0]); GenericNetworkCore.Logger("The ID for the connection is: " + ConnectionID); DidClientRecvId = true; TCP_Send("ID#" + ConnectionID + "\n"); } if (s.StartsWith("ID#") && this.NetSystem.IsServer) { GenericNetworkCore.Logger("The Client Acknowledged their ID!"); DidClientRecvId = true; } if (s.StartsWith("LAT#")) { Latency = float.Parse(s.Split('#')[1].Split('\n')[0]); } if (s.StartsWith("OK")) { if (this.NetSystem.IsClient) { TCP_Send("OK\n"); } if (this.NetSystem.IsServer) { Latency = (float)(System.DateTime.Now - StartCall).TotalSeconds; TCP_Send("LAT#" + Latency.ToString("F2")); } } } Thread.Sleep(ThreadTimer); } } catch (SocketException e) { //Do nothing. We have disconnected. GenericNetworkCore.Logger("Socket Exception: " + e.ToString()); IsDisconnecting = true; } }