コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        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;
                    }
                }
            }
        }