Example #1
0
        protected async Task <int> ReceiveAsync(byte[] buffer, int offset, int size, SocketFlags socketFlags)
        {
            if (!IsConnected)
            {
                return(-1);
            }
            if (buffer == null)
            {
                throw new NullReferenceException("Buffer cannot be null.");
            }
            else if (buffer.Length == 0 || size == 0)
            {
                return(0);
            }

            int read = -1;

            try
            {
                IAsyncResult result = Client.BeginReceive(buffer, offset, size, socketFlags, null, null);
                read = await Task.Factory.FromAsync(result, Client.EndReceive).ConfigureAwait(false);
            }
            catch { read = -1; }

            if (read > 0 && IsDecrypting && Decrypter != null)
            {
                Decrypter.RefParse(buffer, offset, read,
                                   socketFlags.HasFlag(SocketFlags.Peek));
            }
            return(read);
        }
Example #2
0
        protected async Task <int> ReceiveAsync(byte[] buffer, int offset, int size, SocketFlags socketFlags)
        {
            if (!IsConnected)
            {
                return(0);
            }

            IAsyncResult result = Client.BeginReceive(buffer, offset, size, socketFlags, null, null);
            int          read   = await Task.Factory.FromAsync(result, Client.EndReceive).ConfigureAwait(false);

            if (read > 0 && IsDecrypting && Decrypter != null)
            {
                Decrypter.RefParse(buffer, offset, read,
                                   socketFlags.HasFlag(SocketFlags.Peek));
            }
            return(read);
        }
Example #3
0
        protected int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags)
        {
            if (!IsConnected)
            {
                return(0);
            }

            try
            {
                int read = Client.Receive(buffer, offset, size, socketFlags);
                if (read > 0 && IsDecrypting && Decrypter != null)
                {
                    Decrypter.RefParse(buffer, offset, read,
                                       socketFlags.HasFlag(SocketFlags.Peek));
                }
                return(read);
            }
            catch { return(0); }
        }