예제 #1
0
 public GameClient(TcpClient tcp, GameServer server, bool exchangeRequired)
 {
     _tcp                = tcp;
     _parent             = server;
     _ns                 = tcp.GetStream();
     _crypt              = new Supercrypt();
     _queue              = new Queue <Packet>();
     TimeSinceLastPacket = DateTime.Now.Ticks;
     _socketWorker       = new Timer(PingClient, this, 10000, 5000);
     IsAckReceived       = true;
     _connected          = true;
     _encrypt            = exchangeRequired;
     Log.Info("Client {0} connected.", tcp.Client.RemoteEndPoint.ToString());
     try
     {
         if (ServerMain.Instance.Server.IsTeacherComponentEnabled || exchangeRequired)
         {
             _buffer      = new byte[4];
             _bytesToRead = _buffer.Length;
             _ns.BeginRead(_buffer, 0, 4, OnHandShake, null);
         }
         else
         {
             _buffer      = new byte[4];
             _bytesToRead = _buffer.Length;
             _ns.BeginRead(_buffer, 0, 4, OnHeader, null);
         }
     }
     catch (Exception ex)
     {
         KillConnection(ex);
     }
 }
예제 #2
0
 private void OnKeyData(IAsyncResult ar)
 {
     try
     {
         _bytesToRead -= _ns.EndRead(ar);
         if (_bytesToRead > 0)
         {
             _ns.BeginRead(_buffer, _buffer.Length - _bytesToRead, _bytesToRead, OnKeyData, null);
             return;
         }
         var    reader = new SerializeReader(new MemoryStream(_buffer));
         byte[] key    = reader.ReadBytes(32);
         byte[] iv     = reader.ReadBytes(16);
         _crypt       = new Supercrypt(key, iv);
         _buffer      = new byte[4];
         _bytesToRead = _buffer.Length;
         _ns.BeginRead(_buffer, 0, _bytesToRead, OnHeader, null);
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine(ex);
     }
 }