示例#1
0
        void Read_End(IAsyncResult ar)
        {
            Read_SO stateObj = (Read_SO)ar.AsyncState;

            try
            {
                stateObj.UpdateContext();
                int read = _socket.EndReceive(ar);

                stateObj.Read += read;
                if ((read > 0) && (stateObj.Read < stateObj.Size))
                {
                    _socket.BeginReceive(
                        stateObj.Buffer,
                        stateObj.Offset + stateObj.Read,
                        stateObj.Size - stateObj.Read,
                        new AsyncCallback(Read_End),
                        stateObj);
                }
                else
                {
                    stateObj.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (_disposed)
                {
                    stateObj.Exception = GetDisposedException();
                }
                else
                {
                    stateObj.Exception = e;
                }
                stateObj.SetCompleted();
            }

            /*
             * catch
             * {
             *  if(_disposed)
             *      stateObj.Exception = GetDisposedException();
             *  else
             *      stateObj.Exception = new SocketException(SockErrors.WSAECONNRESET);
             *  stateObj.SetCompleted();
             * }
             */
        }
        private void Read_End(IAsyncResult ar)
        {
            Read_SO asyncState = (Read_SO)ar.AsyncState;

            try
            {
                asyncState.UpdateContext();
                int num = this._socket.EndReceive(ar);
                asyncState.Read += num;
                if ((num > 0) && (asyncState.Read < asyncState.Size))
                {
                    this._socket.BeginReceive(asyncState.Buffer, asyncState.Offset + asyncState.Read, asyncState.Size - asyncState.Read, new AsyncCallback(this.Read_End), asyncState);
                }
                else
                {
                    asyncState.SetCompleted();
                }
            }
            catch (Exception exception)
            {
                if (this._disposed)
                {
                    asyncState.Exception = this.GetDisposedException();
                }
                else
                {
                    asyncState.Exception = exception;
                }
                asyncState.SetCompleted();
            }
            catch
            {
                if (this._disposed)
                {
                    asyncState.Exception = this.GetDisposedException();
                }
                else
                {
                    asyncState.Exception = new SocketException(0x2746);
                }
                asyncState.SetCompleted();
            }
        }