Esempio n. 1
0
        private void OnData(IAsyncResult result)
        {
            try
            {
                _bytesToRead -= _ns.EndRead(result);
                if (_bytesToRead > 0)
                {
                    _ns.BeginRead(_buffer, _buffer.Length - _bytesToRead, _bytesToRead, OnData, null);
                    return;
                }
                // Combine the buffer for packetId and the data because we encrypt those together, we have to combine and decrypt them together
                _buffer = packetId.Concat(_buffer).ToArray();
                // Decrypt the buffer to retrieve the actual packetId and the data
                if (_encrypt)
                {
                    _buffer = this._crypt.Decrypt(_buffer).ToArray();
                }
                var packet  = new Packet(this, BitConverter.ToUInt16(_buffer, 0), _buffer.Skip(2).ToArray());
                var hexDump = SerializeWriter.HexDump(packet.Buffer);
                if (ServerMain.Instance.LogEnabled[1] && ServerMain.Instance.IPBlacklist.Contains(this.EndPoint.Address.ToString()) == false)
                {
                    Log.Info("Handling packet {0} ({1} id {2}, 0x{2:X}) on {3} at {4}.", "",
                             Packets.GetName(packet.Id), packet.Id, this.EndPoint.Port.ToString(), DateTime.Now.ToString());
                    Log.Info("HexDump {0}:{1}{2}", packet.Id, Environment.NewLine, hexDump);
                }
                _parent.Parse(packet);

                _buffer      = new byte[4];
                _bytesToRead = _buffer.Length;
                _ns.BeginRead(_buffer, 0, 4, OnHeader, null);
            }
            catch (Exception ex)
            {
                KillConnection(ex);
            }
        }