Пример #1
0
        private IAsyncResult StartInternalRead(Socket streamSocket, SocketStreamAsyncArgs readArgs, byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            IAsyncResult asyncResult;

            try
            {
                SocketStream.IoReq ioReq = new SocketStream.IoReq(state, callback);
                ioReq.RecordUserBuffer(buffer, offset, size);
                ioReq.InternalBuffer = readArgs.InternalBuffer;
                readArgs.SetBuffer(ioReq.InternalBuffer.Buffer, 0, size);
                readArgs.UserToken = ioReq;
                SocketStream.Tracer.TraceDebug((long)this.GetHashCode(), "ReceiveAsync called");
                if (!streamSocket.ReceiveAsync(readArgs))
                {
                    SocketStream.Tracer.TraceDebug((long)this.GetHashCode(), "ReceiveAsync sync completion");
                    this.IO_Completed(null, readArgs);
                }
                asyncResult = ioReq.AsyncResult;
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }
                throw new IOException(ex.Message, ex);
            }
            return(asyncResult);
        }
Пример #2
0
 private int EndReadFromInternalBuf(LazyAsyncResult result)
 {
     SocketStream.IoReq ioReq = result.AsyncObject as SocketStream.IoReq;
     if (ioReq == null)
     {
         throw new ArgumentException("AsyncObject corrupt");
     }
     result.InternalWaitForCompletion();
     if (ioReq.SocketError != SocketError.Success)
     {
         throw new SocketException((int)ioReq.SocketError);
     }
     return(ioReq.BytesTransferred);
 }
Пример #3
0
 private void ProcessReceive(SocketAsyncEventArgs e)
 {
     SocketStream.IoReq ioReq = e.UserToken as SocketStream.IoReq;
     if (ioReq == null)
     {
         throw new ArgumentException("UserToken must be ReadReq");
     }
     try
     {
         ioReq.SocketError      = e.SocketError;
         ioReq.BytesTransferred = e.BytesTransferred;
         Array.Copy(ioReq.InternalBuffer.Buffer, 0, ioReq.UserBuffer, ioReq.UserOffset, ioReq.BytesTransferred);
         ioReq.InternalBuffer = null;
         e.SetBuffer(null, 0, 0);
     }
     finally
     {
         SocketStream.Tracer.TraceDebug((long)this.GetHashCode(), "ProcessReceive marks read idle");
         Interlocked.Exchange(ref this.m_readIsBusy, 0);
         ioReq.Completed = true;
     }
     ioReq.AsyncResult.InvokeCallback();
 }