예제 #1
0
        public virtual async Task <NetworkIncomingMessage <TReadPayloadBaseType> > ReadAsync(CancellationToken token)
        {
            try
            {
                using (await readSynObj.LockAsync(token).ConfigureAwait(false))
                {
                    //if was canceled the header reading probably returned null anyway
                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }

                    //Custom 317 Protocol:
                    //2 byte short payload length.
                    //1 byte opcode (in the payload data).

                    //payload length
                    await ReadAsync(PacketPayloadReadBuffer, 0, 2, token)
                    .ConfigureAwait(false);

                    //We read from the payload buffer 2 bytes, it's the size.
                    int payloadSize = PacketPayloadReadBuffer.Reinterpret <short>(0);

                    //If the token was canceled then the buffer isn't filled and we can't make a message
                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }

                    //We need to read enough bytes to deserialize the payload
                    await ReadAsync(PacketPayloadReadBuffer, 0, payloadSize, token)
                    .ConfigureAwait(false);                            //TODO: Should we timeout?

                    //If the token was canceled then the buffer isn't filled and we can't make a message
                    if (token.IsCancellationRequested)
                    {
                        return(null);
                    }

                    //Deserialize the bytes starting from the begining but ONLY read up to the payload size. We reuse this buffer and it's large
                    //so if we don't specify the length we could end up with an issue.
                    var payload = Serializer.Deserialize <TReadPayloadBaseType>(PacketPayloadReadBuffer, 0, payloadSize);

                    return(new NetworkIncomingMessage <TReadPayloadBaseType>(new HeaderlessPacketHeader(payloadSize), payload));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #2
0
        protected override async Task <IPacketHeader> BuildHeaderWithDecryption(CancellationToken token)
        {
            //Unlike 3.3.5 the 1.12.1 server header is simplier
            //It is just 2 byte size and 2 byte opcode.
            //But the caller really only wants size. Not opcode
            await ReadAsync(PacketPayloadReadBuffer, 0, 2, token)
            .ConfigureAwait(false);

            if (CryptoService.isInitialized)
            {
                CryptoService.DecryptionService.ProcessBytes(PacketPayloadReadBuffer, 0, 2, PacketPayloadReadBuffer, 0);
            }

            //It's big endian though so we need to reverse it
            //it is safe to do this hack because it will be overridden when more data is read.
            PacketPayloadReadBuffer[2] = PacketPayloadReadBuffer[0];

            ushort size = PacketPayloadReadBuffer.Reinterpret <ushort>(1);

            Console.WriteLine($"Recieved server packet header size: {size}");

            return(new ServerPacketHeader(size));
        }