Exemple #1
0
        protected override void Encode(IChannelHandlerContext context, IPacket message, IByteBuffer output)
        {
            var socket = context.Channel.GetAttribute(NettyAttributes.SocketKey).Get();
            var dataLen = (short)message.Buffer.Length;
            var buffer = message.Buffer.ToArray();

            if (socket != null)
            {
                var seqSend = socket.SeqSend;
                var rawSeq = (short)((seqSend >> 16) ^ -(_transport.Version + 1));

                if (socket.EncryptData)
                {
                    dataLen ^= rawSeq;

                    ShandaCipher.EncryptTransform(buffer);
                    _aesCipher.Transform(buffer, seqSend);
                }

                output.WriteShortLE(rawSeq);
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);

                socket.SeqSend = _igCipher.Hash(seqSend, 4, 0);
            }
            else
            {
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);
            }
        }
Exemple #2
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            var socket = context.Channel.GetAttribute(NettyAttributes.SocketKey).Get();

            switch (State)
            {
            case NettyPacketState.DecodingHeader:
                if (socket != null)
                {
                    if (input.ReadableBytes < 4)
                    {
                        RequestReplay();
                        return;
                    }

                    var sequence = input.ReadShortLE();
                    var length   = input.ReadShortLE();

                    if (socket.EncryptData)
                    {
                        length ^= sequence;
                    }

                    _sequence = sequence;
                    _length   = length;
                }
                else
                {
                    if (input.ReadableBytes < 2)
                    {
                        RequestReplay();
                        return;
                    }

                    _length = input.ReadShortLE();
                }

                Checkpoint(NettyPacketState.DecodingPayload);
                return;

            case NettyPacketState.DecodingPayload:
                if (input.ReadableBytes < _length)
                {
                    RequestReplay();
                    return;
                }

                var buffer = new byte[_length];

                input.ReadBytes(buffer);
                Checkpoint(NettyPacketState.DecodingHeader);

                if (_length < 0x2)
                {
                    return;
                }

                if (socket != null)
                {
                    var seqRecv = socket.SeqRecv;
                    var version = (short)(seqRecv >> 16) ^ _sequence;

                    if (!(version == -(_transport.Version + 1) ||
                          version == _transport.Version))
                    {
                        return;
                    }

                    if (socket.EncryptData)
                    {
                        _aesCipher.Transform(buffer, seqRecv);
                        ShandaCipher.DecryptTransform(buffer);
                    }

                    socket.SeqRecv = _igCipher.Hash(seqRecv, 4, 0);
                }

                output.Add(new UnstructuredIncomingPacket(buffer));
                return;
            }
        }