private async Task <bool> ReadAndDecrypt(byte[] buffer, int start, CancellationToken token, int extendedCount)
        {
            //We don't need to know the amount read, I think.
            int count = await DecoratedClient.ReadAsync(buffer, start, extendedCount, token)
                        .ConfigureAwait(false);

            //IF we read nothing then that means the socket disconnected
            if (count == 0)
            {
                return(false);
            }

            //Check cancel again, we want to fail quick
            if (token.IsCancellationRequested)
            {
                return(false);
            }

            //We throw above if we have an invalid size that can't be decrypted once read.
            //That means callers will need to be careful in what they request to read.
            DecryptionServiceProvider.Crypt(buffer, start, extendedCount);

            //Check cancel again, we want to fail quick
            if (token.IsCancellationRequested)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public async Task <IPacketHeader> ReadHeaderAsync(CancellationToken token)
        {
            //If the token is canceled just return null;
            if (token.IsCancellationRequested)
            {
                return(null);
            }

            //If we had access to the stream we could wrap it in a reader and use it
            //without knowing the size. Since we don't have access we must manually read
            int count = await DecoratedClient.ReadAsync(PacketHeaderBuffer, 0, PacketHeaderBuffer.Length, token)
                        .ConfigureAwait(false);        //TODO: How long should the timeout be if any?

            //This means the socket is disconnected
            if (count == 0)
            {
                return(null);
            }

            //If the token is canceled just return null;
            if (token.IsCancellationRequested)
            {
                return(null);
            }

            //This will deserialize
            return(Serializer.Deserialize <TPacketHeaderType>(PacketHeaderBuffer));
        }
        /// <inheritdoc />
        public async Task <NetworkIncomingMessage <TReadPayloadBaseType> > ReadAsync(CancellationToken token)
        {
            using (await ReadLock.LockAsync(token).ConfigureAwait(false))
            {
                //We want to clear the read buffers after reaiding a full message.
                NetworkIncomingMessage <TReadPayloadBaseType> message = await DecoratedClient.ReadAsync(token)
                                                                        .ConfigureAwait(false);

                //Do not call the object's ClearBuffer or we will deadlock; isn't re-enterant
                await DecoratedClient.ClearReadBuffers()
                .ConfigureAwait(false);

                //Could be null if the socket disconnected
                return(message);
            }
        }
Esempio n. 4
0
 /// <inheritdoc />
 public override Task <int> ReadAsync(byte[] buffer, int start, int count, CancellationToken token)
 {
     return(DecoratedClient.ReadAsync(buffer, start, count, token));
 }
 /// <inheritdoc />
 public override async Task <int> ReadAsync(byte[] buffer, int start, int count, CancellationToken token)
 {
     using (await ReadLock.LockAsync(token).ConfigureAwait(false))
         return(await DecoratedClient.ReadAsync(buffer, start, count, token)
                .ConfigureAwait(false));
 }