Beispiel #1
7
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            ReadAsyncResult ar = new ReadAsyncResult(callback, state);
            bool shouldInvokeWriter;
            if (!TryCompleteReadRequest(buffer, offset, count, ar, out shouldInvokeWriter))
            {
                if (shouldInvokeWriter)
                {
                    InvokeWriter();
                }
            }
            return ar;
        }
Beispiel #2
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback?callback, object?state)
        {
            ReadAsyncResult result = new ReadAsyncResult(this, callback, state);

            result.Read(buffer, offset, count);
            return(result);
        }
Beispiel #3
0
            internal static int End(IAsyncResult result)
            {
                ReadAsyncResult thisPtr = (ReadAsyncResult)result;

                thisPtr.InternalWaitForCompletion();
                return(thisPtr._read);
            }
Beispiel #4
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            ReadAsyncResult ar = new ReadAsyncResult(callback, state);
            bool            shouldInvokeWriter;

            if (!TryCompleteReadRequest(buffer, offset, count, ar, out shouldInvokeWriter))
            {
                if (shouldInvokeWriter)
                {
                    InvokeWriter();
                }
            }
            return(ar);
        }
Beispiel #5
0
 public override int EndRead(IAsyncResult asyncResult)
 {
     if (asyncResult == null)
     {
         throw new ArgumentNullException("asyncResult");
     }
     return(ReadAsyncResult.End(asyncResult));
 }
Beispiel #6
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback?callback, object?state)
        {
            ValidateBufferArguments(buffer, offset, count);

            var result = new ReadAsyncResult(this, buffer, offset, count, callback, state);

            result.Read();
            return(result);
        }
Beispiel #7
0
            static void OnReadComplete(TransportAsyncCallbackArgs args)
            {
                ReadAsyncResult thisPtr = (ReadAsyncResult)args.UserToken;

                thisPtr.bufferOffset           = 0;
                thisPtr.bufferEnd              = args.BytesTransfered;
                thisPtr.completedSynchronously = args.CompletedSynchronously;
                thisPtr.Complete();
            }
Beispiel #8
0
        public WebSocketNetworkStream(Socket socket) : base(socket, ownsSocket: true)
        {
            m_socket = socket;

            m_readAsyncResult     = new ReadAsyncResult(this);
            m_readArgs            = new SocketAsyncEventArgs();
            m_readArgs.Completed += OnReadCompleted;

            m_writeAsyncResult     = new WriteAsyncResult(this);
            m_writeArgs            = new SocketAsyncEventArgs();
            m_writeArgs.Completed += OnWriteCompleted;
        }
Beispiel #9
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (SSLConfirmed && SSL)
            {
                if (Length > 0)
                {
                    var result = new ReadAsyncResult();
                    result.AsyncState             = state;
                    result.CompletedSynchronously = true;
                    result.Buffer      = buffer;
                    result.Offset      = offset;
                    result.Size        = size;
                    result.IsCompleted = true;
                    return(result);
                }
                else
                {
                    mReadAsyncResult                        = new ReadAsyncResult();
                    mReadAsyncResult.AsyncState             = state;
                    mReadAsyncResult.CompletedSynchronously = false;
                    mReadAsyncResult.Buffer                 = buffer;
                    mReadAsyncResult.Offset                 = offset;
                    mReadAsyncResult.Size                   = size;
                    mReadAsyncResult.IsCompleted            = false;
                    mReadAsyncResult.AsyncCallback          = callback;
                    return(mReadAsyncResult);
                }
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("size");
            }
            Socket streamSocket = Socket;

            if (streamSocket == null)
            {
                throw new BXException("PipeStream Socket is null!");
            }
            IAsyncResult asyncResult = streamSocket.BeginReceive(buffer, offset, size, SocketFlags.None, callback, state);

            return(asyncResult);
        }
Beispiel #10
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (bufferLength > 0)
            {
                int available = Math.Min(bufferLength, count);
                Buffer.BlockCopy(streamBuffer, bufferPos, buffer, offset, available);
                bufferPos    += available;
                bufferLength -= available;
                return(new ReadAsyncResult(buffer, offset, available, state, callback));
            }

            var result = new ReadAsyncResult(buffer, offset, 0, state, callback);

            result.BaseResult = baseStream.BeginRead(buffer, offset, count, readCallback, result);
            return(result);
        }
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if ((offset < 0) || (offset > buffer.Length))
     {
         throw new ArgumentOutOfRangeException("offset");
     }
     if ((offset + count) > buffer.Length)
     {
         throw new ArgumentOutOfRangeException("count");
     }
     ReadAsyncResult result = new ReadAsyncResult(this, buffer, offset, count, callback, state);
     result.Read();
     return result;
 }
Beispiel #12
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if ((offset < 0) || (offset > buffer.Length))
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if ((offset + count) > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            ReadAsyncResult result = new ReadAsyncResult(this, buffer, offset, count, callback, state);

            result.Read();
            return(result);
        }
Beispiel #13
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            var result = new ReadAsyncResult(this, buffer, offset, count, callback, state);

            result.Read();
            return(result);
        }
Beispiel #14
0
 private static void OnRead(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ReadAsyncResult thisPtr = (ReadAsyncResult)result.AsyncState;
         try
         {
             thisPtr._read += thisPtr._parent.BaseStream.EndRead(result);
             thisPtr.InvokeCallback();
         }
         catch (Exception e)
         {
             if (thisPtr.IsCompleted)
             {
                 throw;
             }
             thisPtr.InvokeCallback(e);
         }
     }
 }
Beispiel #15
0
 private static void OnRead(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ReadAsyncResult thisPtr = (ReadAsyncResult)result.AsyncState;
         try
         {
             if (!thisPtr.CompleteRead(result))
             {
                 thisPtr.Read();
             }
         }
         catch (Exception e)
         {
             if (thisPtr.IsCompleted)
             {
                 throw;
             }
             thisPtr.InvokeCallback(e);
         }
     }
 }
Beispiel #16
0
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     ReadAsyncResult result = new ReadAsyncResult(this, callback, state);
     result.Read(buffer, offset, count);
     return result;
 }
Beispiel #17
0
            public static int End(IAsyncResult result)
            {
                ReadAsyncResult thisPtr = AsyncResult.End <ReadAsyncResult>(result);

                return(thisPtr.bytesRead);
            }
Beispiel #18
0
 public override int EndRead(IAsyncResult asyncResult)
 {
     return(ReadAsyncResult.End(asyncResult));
 }
Beispiel #19
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            var result = new ReadAsyncResult(this, buffer, offset, count, callback, state);
            result.Read();
            return result;
        }
Beispiel #20
0
 public ReadRequestMessage(int channelId, byte[] buffer, int offset, int count, ReadAsyncResult completionHandle)
 {
     this.channelId = channelId;
     this.buffer = buffer;
     this.offset = offset;
     this.count = count;
     this.completionHandle = completionHandle;
 }
Beispiel #21
0
 public TransportStream(TransportBase transport)
 {
     this.transport   = transport;
     this.readResult  = new ReadAsyncResult(this.transport);
     this.writeResult = new WriteAsyncResult(this.transport);
 }
Beispiel #22
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            int read = ReadAsyncResult.End(asyncResult);

            return(read);
        }
Beispiel #23
0
 internal IAsyncResult BeginRead(int channelId, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     ReadAsyncResult completionHandle = new ReadAsyncResult(callback, state, this);
     this.actorManager.SendMessage(this.transportReceivingActor, new ReadRequestMessage(channelId, buffer, offset, count, completionHandle));
     return completionHandle;
 }
Beispiel #24
0
 public TransportStream(TransportBase transport)
 {
     this.transport = transport;
     this.readResult = new ReadAsyncResult(this.transport);
     this.writeResult = new WriteAsyncResult(this.transport);
 }
Beispiel #25
0
        public override int EndRead(IAsyncResult asyncResult)
        {
            ArgumentNullException.ThrowIfNull(asyncResult);

            return(ReadAsyncResult.End(asyncResult));
        }
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            Debug.WriteLine("BNS ({0}): BeginRead (buffer, {1}, {2}) [m_inputBuffer_pos = {3}, m_inputBuffer_read_ahead = {4}]",
                            NetworkStream.InternalSocket.InternalRemoteEndPoint, offset, count, m_inputBuffer_pos, m_inputBuffer_read_ahead);

            ReadAsyncResult rar;

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer is null");
            }
            int len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
            }
            if (count < 0 || offset + count > len)
            {
                throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
            }

            CheckObjectDisposedException();

            if (!m_stream.CanRead)
            {
                throw new NotSupportedException(Locale.GetText("Cannot read from stream"));
            }

            if (m_inputBuffer_pos < m_inputBuffer_read_ahead)
            {
                int curLength = m_inputBuffer_read_ahead - m_inputBuffer_pos;
                if (count <= curLength)
                {
                    Buffer.BlockCopy(m_inputBuffer, m_inputBuffer_pos, buffer, offset, count);
                    m_inputBuffer_pos += count;

                    var rsr = new ReadSyncResult
                    {
                        AsyncState   = state,
                        DataReceived = count
                    };

                    if (callback != null)
                    {
                        callback.BeginInvokeEx(rsr, null, null);
                    }

                    return(rsr);
                }

                if (curLength != 0)
                {
                    Buffer.BlockCopy(m_inputBuffer, m_inputBuffer_pos, buffer, offset, curLength);
                    count  -= curLength;
                    offset += curLength;

                    if (!m_stream.InternalSocket.DataAvailable)
                    {
                        m_inputBuffer_pos        = 0;
                        m_inputBuffer_read_ahead = 0;

                        var rsr = new ReadSyncResult
                        {
                            AsyncState   = state,
                            DataReceived = curLength
                        };

                        if (callback != null)
                        {
                            callback.BeginInvokeEx(rsr, null, null);
                        }

                        return(rsr);
                    }
                }

                rar = new ReadAsyncResult
                {
                    DataReceived = curLength,
                    AsyncState   = state
                };
            }
            else
            {
                rar = new ReadAsyncResult
                {
                    DataReceived = 0,
                    AsyncState   = state
                };
            }

            m_inputBuffer_pos        = 0;
            m_inputBuffer_read_ahead = 0;

            if (count > m_bufferSize / 2)
            {
                if (rar.DataReceived == 0)
                {
                    return(m_stream.BeginRead(buffer, offset, count, callback, state));
                }

                m_stream.BeginRead(buffer, offset, count, iar =>
                {
                    var rs = (ReadState)iar.AsyncState;

                    rs.readAsyncResult.DataReceived += m_stream.EndRead(iar);

                    rs.readAsyncResult.IsCompleted = true;
                    ((CEvent)rs.readAsyncResult.AsyncWaitHandle).Set();

                    if (rs.callback != null)
                    {
                        try
                        {
                            rs.callback(rs.readAsyncResult);
                        }
                        catch
                        {
                        }
                    }
                }, new ReadState
                {
                    callback        = callback,
                    readAsyncResult = rar
                });

                return(rar);
            }

            EnsureInputBuffer();

            m_stream.BeginRead(m_inputBuffer, 0, m_bufferSize, iar =>
            {
                var rs = (ReadState)iar.AsyncState;

                m_inputBuffer_read_ahead = m_stream.EndRead(iar);

                if (rs.count > m_inputBuffer_read_ahead)
                {
                    rs.count = m_inputBuffer_read_ahead;
                }

                rs.readAsyncResult.DataReceived += rs.count;

                if (rs.count != 0)
                {
                    Buffer.BlockCopy(m_inputBuffer, 0, rs.buffer, rs.offset, rs.count);
                    m_inputBuffer_pos = rs.count;
                }

                rs.readAsyncResult.IsCompleted = true;
                ((CEvent)rs.readAsyncResult.AsyncWaitHandle).Set();

                if (rs.callback != null)
                {
                    try
                    {
                        rs.callback(rs.readAsyncResult);
                    }
                    catch
                    {
                    }
                }
            }, new ReadState
            {
                callback        = callback,
                readAsyncResult = rar,
                buffer          = buffer,
                offset          = offset,
                count           = count
            });

            return(rar);
        }