private void HandleClient() { byte[] bytesFrom = new byte[4096]; NetworkStream networkStream = clientSocket.GetStream(); while (run) { while (!networkStream.DataAvailable) { Thread.Sleep(50); } networkStream.Read(bytesFrom, 0, 4096); ByteBuffer buffer = new ByteBuffer(); buffer.WriteBytes(bytesFrom); short id = buffer.ReadByte(); string token = buffer.ReadString(); if (!JwtTokens.EvaluateToken(token)) { SendDefaultRespose(false, clNo); } else { HandleID(id, bytesFrom); } } }
public void Start() { httpSocket.Start(); Task task = Task.Run(() => { while (run) { for (int i = 0; i < Globals.httpClient.Length; i++) { Globals.httpClient[i] = new TcpClient(); try { Globals.httpClient[i] = httpSocket.AcceptTcpClient(); } catch { break; } Console.WriteLine(">> New connection from http server"); HandleHttpClient httpClient = new HandleHttpClient(); httpClient.StartHttpClient(Globals.httpClient[i], i); } Thread.Sleep(1000); } }); int counter = 0; JwtTokens.LoadKey("key.txt"); serverstart: serverSocket.Start(); Globals.dicPlayers = new Dictionary <int, Player>(); Console.WriteLine(">> TCP IP Server Started on port " + Globals.port); while (Globals.mainRun) { for (int i = 0; i < Globals.clients.Length; i++) { if (Globals.clients[i] == null) { Globals.clients[i] = new TcpClient(); Globals.clients[i] = serverSocket.AcceptTcpClient(); if (!Globals.mainRun) { break; } Console.WriteLine(" >> Client No:" + Convert.ToString(counter) + " started! " + Globals.clients[i].Client.LocalEndPoint); HandleClinet client = new HandleClinet(); client.startClient(Globals.clients[i], counter, Globals.clients[i].Client.LocalEndPoint.ToString()); counter++; } } } CloseSocket(); Console.WriteLine(">> Closing Server Socket"); serverSocket.Stop(); if (Globals.restart) { Globals.restart = false; Globals.threadsRun = true; Globals.mainRun = true; LoadConfig(path); serverSocket = new TcpListener(IPAddress.Any, Globals.port); goto serverstart; } }
private void doClient() { int requestCount = 0; byte[] bytesFrom = new byte[4096]; requestCount = 0; NetworkStream networkStream = clientSocket.GetStream(); while (!networkStream.DataAvailable) { Thread.Sleep(50); } // waits for package with the auth key ByteBuffer buffer = new ByteBuffer(); networkStream.Read(bytesFrom, 0, 4096); buffer.WriteBytes(bytesFrom); buffer.ReadInt(); string token = buffer.ReadString(); if (!JwtTokens.EvaluateToken(token)) { byte[] bufferE = new byte[4]; bufferE[0] = (byte)Enums.AllEnums.SInvalid; networkStream.Write(bufferE, 0, 4); return; } Player pl = new Player(); pl.uName = buffer.ReadString(); pl.cName = buffer.ReadString(); pl.head = buffer.ReadInt(); pl.body = buffer.ReadInt(); pl.cloths = buffer.ReadInt(); pl.currentPlaytime = DateTime.Now; pl.playerIP = ip; pl.totalPlaytime = DB.getInstance("", "").GetPlayTime(pl.uName); pl.socketID = clNo; Globals.dicPlayers.Add(clNo, pl); Globals.clients[clNo].GetStream().Write(buffer.ToArray(), 0, buffer.ToArray().Length); networkStream.Flush(); NotifyAlreadyConnected(clNo, pl); NotifyMainPlayerOfAlreadyConnected(clNo); count++; while (Globals.threadsRun && run) { try { requestCount++; networkStream = clientSocket.GetStream(); if (networkStream.DataAvailable) { buffer = new ByteBuffer(); networkStream.Read(bytesFrom, 0, 4096); buffer.WriteBytes(bytesFrom); int packageID = buffer.ReadInt(); if (packageID == (int)Enums.AllEnums.SCloseConnection) { run = false; break; } HandleMessage(packageID, clNo, buffer.ToArray()); } Thread.Sleep(50); } catch (Exception ex) { try { if (!Globals.clients[clNo].Connected) { SCloseConnection(clNo); } Globals.clients[clNo] = null; } catch (Exception e) { } finally { byte[] closeBuffer = new byte[5]; closeBuffer[0] = 7; closeBuffer[4] = (byte)clNo; run = false; } } } }