Example #1
0
        /// <inheritdoc />
        public override Task WriteAsync(byte[] bytes, int offset, int count)
        {
            //We are making the assumption they are writing a full payload
            //and opcode. So we only need to serialize ushort length
            //and then the length and opcode should be encrypted
            OutgoingClientPacketHeader header = new OutgoingClientPacketHeader(count - 2, (NetworkOperationCode)bytes.Reinterpret <short>(offset));

            //We subtract 2 from the payload data length because first 2 bytes are opcode and header contains opcode.
            //Then we reinterpet the first 2 bytes of the payload data because it's the opcode we need to use.
            byte[] clientPacketHeader = Serializer.Serialize(header);

            return(CryptAndSend(bytes, clientPacketHeader, offset, count));
        }
Example #2
0
        /// <inheritdoc />
        public virtual Task WriteAsync(TWritePayloadBaseType payload)
        {
            //Serializer the payload first so we can build the header
            byte[] payloadData = Serializer.Serialize(payload);

            OutgoingClientPacketHeader header = new OutgoingClientPacketHeader(payloadData.Length - 2, (NetworkOperationCode)payloadData.Reinterpret <short>(0));

            //Console.WriteLine($"New ClientHeader: OpCode: {header.OperationCode} PacketSize: {header.PacketSize} PayloadSize: {header.PayloadSize}");

            //We subtract 2 from the payload data length because first 2 bytes are opcode and header contains opcode.
            //Then we reinterpet the first 2 bytes of the payload data because it's the opcode we need to use.
            byte[] clientPacketHeader = Serializer.Serialize(header);

            return(CryptAndSend(payloadData, clientPacketHeader, 0, payloadData.Length));
        }
Example #3
0
        public async Task <NetworkIncomingMessage <TReadPayloadBaseType> > ReadAsync(CancellationToken token)
        {
            IPacketHeader        header  = null;
            TReadPayloadBaseType payload = null;

            using (await readSynObj.LockAsync(token).ConfigureAwait(false))
            {
                await ReadAsync(PacketPayloadReadBuffer, 0, 6, token)
                .ConfigureAwait(false);

                //Check crypto first. We may need to decrypt this header
                if (CryptoService.isInitialized)
                {
                    CryptoService.DecryptionService.ProcessBytes(PacketPayloadReadBuffer, 0, 6, PacketPayloadReadBuffer, 0);
                }

                OutgoingClientPacketHeader clientHeader = Serializer.Deserialize <OutgoingClientPacketHeader>(PacketPayloadReadBuffer, 0, 6);

                //TODO: Enable this logging on Debug
                //Console.WriteLine($"New ClientHeader: OpCode: {clientHeader.OperationCode} PacketSize: {clientHeader.PacketSize} PayloadSize: {clientHeader.PayloadSize}");

                header = clientHeader;

                //Console.WriteLine($"Recieved OpCode: {clientHeader.OperationCode}:{(ushort)clientHeader.OperationCode} from client Encrypted:{CryptoService.isInitialized}");

                //If the header is null it means the socket disconnected
                if (header == null)
                {
                    return(null);
                }

                //if was canceled the header reading probably returned null anyway
                if (token.IsCancellationRequested)
                {
                    return(null);
                }

                //We need to start reading at 2 bytes so we can manually insert the opcode
                //into the payload buffer
                await ReadAsync(PacketPayloadReadBuffer, 2, header.PayloadSize, token)
                .ConfigureAwait(false);                         //TODO: Should we timeout?

                //RACE CONDITION. WE NEED TO LOCK AROUND READING FRM THIS BUFF
                byte[] reinterpretedOpCode = ((short)clientHeader.OperationCode).Reinterpret();

                PacketPayloadReadBuffer[0] = reinterpretedOpCode[0];
                PacketPayloadReadBuffer[1] = reinterpretedOpCode[1];

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

                //TODO: Revisit thbis to check if +2 is ok
                //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.
                //We have to read for and additional two bytes due to the payyload data shifted 2 bytes forward
                payload = Serializer.Deserialize <TReadPayloadBaseType>(PacketPayloadReadBuffer, 0, header.PayloadSize + 2);
            }

            return(new NetworkIncomingMessage <TReadPayloadBaseType>(header, payload));
        }