/// <summary>Initiates and keeps the server running.</summary> public static void Main() { // Settings for both the TCP and UDP processing threads. string myIP = "127.0.0.1"; int myPort = 64198; int myMaxClients = 256; int myBufferReadSize = 256; int myBufferWriteSize = 256; int myBufferAlignment = 1; int myPacketHeader = 137; // Creates a new thread for processing transmision control protocol(TCP). // Remove the three lines of code below if you do not wish to use a based TCP server. Thread myTcpThread = new Thread(() => TcpNetwork.TcpStart(myIP, myPort, myMaxClients, myBufferReadSize, myBufferWriteSize, myBufferAlignment, myPacketHeader)); myTcpThread.IsBackground = false; myTcpThread.Start(); Thread.Sleep(100); // Creates a new thread for processing user datagram protocol(UDP). // Remove the three lines of code below if you do not wish to use a based UDP server. Thread myUdpThread = new Thread(() => UdpNetwork.UdpStart(myPort, myBufferReadSize, myBufferWriteSize, myBufferAlignment, myPacketHeader)); myUdpThread.IsBackground = false; myUdpThread.Start(); Thread.Sleep(100); // Keeps the server from closing while the TCP and UDP threads are running. // However this will not keep the server from unexpectedly closing. // Also adds a command input and console stack for user input and request. CmdNetwork.StartCommands(); while (UdpNetwork.UdpServerIsOnline == true || TcpNetwork.TcpServerIsOnline == true) { string myCommand = System.Console.ReadLine(); CmdNetwork.RunCommand(myCommand); } CmdNetwork.EndCommands(); }
/// <summary>Handles all client data and receiving data from the client. The client will be sent a disconnection message(id 254) if the /// the client has connected when the server has reached maximum capacity(MaxClients).</summary> /// <param name="myClient">Socket of the specific TCP client.</param> private static async void TcpHandle(TcpClient myClient) { NetworkStream myStream = myClient.GetStream(); ByteBuffer myBufferR = new ByteBuffer(); myBufferR.Create(TcpBufferReadSize, TcpBufferAlignment); ByteBuffer myBufferW = new ByteBuffer(); myBufferW.Create(TcpBufferWriteSize, TcpBufferAlignment); if (TcpSocketList.Count <= TcpMaxClients) { CmdNetwork.AppendLog("Client Connected : " + (( IPEndPoint )myClient.Client.RemoteEndPoint).Address.ToString()); bool myThreading = true; while (myThreading) { try { if (myStream.DataAvailable == true) { Array.Clear(myBufferR.Buffer, 0, TcpBufferReadSize); Array.Clear(myBufferW.Buffer, 0, TcpBufferWriteSize); myBufferR.BytePeek = 0; myBufferW.BytePeek = 0; int mySize = myClient.Available; await myStream.ReadAsync(myBufferR.Buffer, 0, mySize); IPEndPoint myIPEndPoint = ( IPEndPoint )myClient.Client.RemoteEndPoint; TcpPackets.TcpPacketRead(ref myIPEndPoint, ref myStream, ref myThreading, ref myBufferR, ref myBufferW); } TcpPackets.TcpPacketSend(ref myStream, ref myThreading, ref myBufferW); } catch (Exception) { myThreading = false; } if (myClient.Connected == false) { myThreading = false; } } CmdNetwork.AppendLog("Client Disconnected : " + (( IPEndPoint )myClient.Client.RemoteEndPoint).Address.ToString()); } else { myBufferW.Writeu8(TcpPacketHeader); myBufferW.Writeu8(254); myBufferW.SendTcp(myStream); } int myID = TcpSocketList.FindIndex(x => x == myClient); TcpSocketList.RemoveAt(myID); myStream.Close(); myClient.Close(); }