/// <summary> /// Begins an asynchronous reading from the network stream. /// </summary> /// <param name="buffer">The buffer to store data to.</param> /// <param name="offset">The location in the <i>buffer</i> where to start storing the received data.</param> /// <param name="size">The number of bytes to read.</param> /// <param name="callback"> /// The <see cref="System.AsyncCallback">AsyncCallback</see> delegate. /// </param> /// <param name="state"> /// An object containing state information for this request. /// </param> /// <returns> /// An <see cref="System.IAsyncResult">IAsyncResult</see> /// that references the asynchronous read. /// </returns> /// <remarks> /// The <b>BeginRead</b> method starts an asynchronous /// read operation from the network stream. /// It returns immediately and does not wait for /// the asynchronous call to complete. /// <para> /// The /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndRead"/> /// method is used to retrieve the results of /// the asynchronous call. It can be called /// any time after <b>BeginRead</b>; if the asynchronous /// call has not completed, /// <b>EndRead</b> /// will block until it completes. /// </para> /// <para> /// The read operation will not completed until the number of bytes specified by <i>size</i> /// parameter is read from the stream. If the remote host shuts down the /// <see cref="BytesRoad.Net.Sockets.SocketEx"/> /// connection with the /// <see cref="BytesRoad.Net.Sockets.SocketEx.Shutdown"/> /// method, and all available data has been received, the <b>Read</b> method /// will complete and return number of bytes was read. /// </para> /// <note> /// The <b>NetworkStreamEx</b> should have access right to read data from the network stream. /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanRead"/> property to check this. /// </note> /// </remarks> /// /// <exception cref="System.ObjectDisposedException"> /// The <see cref="BytesRoad.Net.Sockets.NetworkStreamEx"/> object was disposed. /// </exception> /// /// <exception cref="System.ArgumentNullException"> /// <i>buffer</i> is a null reference (<b>Nothing</b> in Visual Basic). /// </exception> /// /// <exception cref="System.ArgumentOutOfRangeException"> /// <i>offset</i> is less than 0. /// <para>-or-</para> /// <i>offset</i> is greater than the length of <i>buffer</i>. /// <para>-or-</para> /// <i>size</i> is less than 0. /// <para>-or-</para> /// <i>size</i> is greater than the length of <i>buffer</i> minus /// the value of the <i>offset</i> parameter. /// </exception> public override IAsyncResult BeginRead( byte[] buffer, int offset, int size, AsyncCallback callback, object state ) { CheckDisposed(); _asyncCtx.SetProgress(true); try { Read_SO stateObj = new Read_SO( buffer, offset, size, callback, state); return(_socket.BeginReceive( buffer, offset, size, new AsyncCallback(Read_End), stateObj)); } catch { _asyncCtx.SetProgress(false); CheckDisposed(); throw; } }
public override int EndRead(IAsyncResult asyncResult) { AsyncBase.VerifyAsyncResult(asyncResult, typeof(Read_SO), "EndRead"); this._asyncCtx.HandleAsyncEnd(asyncResult, true); Read_SO d_so = (Read_SO)asyncResult; return(d_so.Read); }
/// <summary> /// Ends a pending asynchronous read. /// </summary> /// <param name="asyncResult"> /// An /// <see cref="System.IAsyncResult">IAsyncResult</see> /// that stores state information for this asynchronous operation. /// </param> /// <returns>The number of bytes read.</returns> /// /// <exception cref="System.ObjectDisposedException"> /// The <see cref="BytesRoad.Net.Sockets.NetworkStreamEx"/> object was disposed. /// </exception> /// /// <exception cref="System.ArgumentNullException"> /// <i>asyncResult</i> is a null reference /// (<b>Nothing</b> in Visual Basic). /// </exception> /// <exception cref="System.ArgumentException"> /// <i>asyncResult</i> was not returned by a call to the /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.BeginRead"/> /// method. /// </exception> /// <exception cref="System.InvalidOperationException"> /// <b>EndRead</b> was previously called for the /// asynchronous read. /// </exception> /// /// <exception cref="System.Net.Sockets.SocketException"> /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. /// </exception> /// /// <remarks> /// <b>EndRead</b> is a blocking method that completes the /// asynchronous read operation started in the /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.BeginRead"/> method. /// /// <para> /// The read operation will not completed until the number of bytes specified by <i>size</i> /// parameter (to <b>BeginRead</b> method) is read from the stream. If the remote host shuts down the /// <see cref="BytesRoad.Net.Sockets.SocketEx"/> /// connection with the /// <see cref="BytesRoad.Net.Sockets.SocketEx.Shutdown"/> /// method, and all available data has been received, the <b>Read</b> method /// will complete and return number of bytes was read. /// </para> /// /// <note> /// The <b>NetworkStreamEx</b> should have access right to read data from the network stream. /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanRead"/> property to check this. /// </note> /// /// </remarks> public override int EndRead(IAsyncResult asyncResult) { AsyncBase.VerifyAsyncResult(asyncResult, typeof(Read_SO), "EndRead"); _asyncCtx.HandleAsyncEnd(asyncResult, true); Read_SO stateObj = (Read_SO)asyncResult; return(stateObj.Read); }
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(); * } */ }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state) { IAsyncResult result; this.CheckDisposed(); this._asyncCtx.SetProgress(true); try { Read_SO d_so = new Read_SO(buffer, offset, size, callback, state); result = this._socket.BeginReceive(buffer, offset, size, new AsyncCallback(this.Read_End), d_so); } catch { this._asyncCtx.SetProgress(false); this.CheckDisposed(); throw; } return(result); }
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(); } }
/// <summary> /// Begins an asynchronous reading from the network stream. /// </summary> /// <param name="buffer">The buffer to store data to.</param> /// <param name="offset">The location in the <i>buffer</i> where to start storing the received data.</param> /// <param name="size">The number of bytes to read.</param> /// <param name="callback"> /// The <see cref="System.AsyncCallback">AsyncCallback</see> delegate. /// </param> /// <param name="state"> /// An object containing state information for this request. /// </param> /// <returns> /// An <see cref="System.IAsyncResult">IAsyncResult</see> /// that references the asynchronous read. /// </returns> /// <remarks> /// The <b>BeginRead</b> method starts an asynchronous /// read operation from the network stream. /// It returns immediately and does not wait for /// the asynchronous call to complete. /// <para> /// The /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndRead"/> /// method is used to retrieve the results of /// the asynchronous call. It can be called /// any time after <b>BeginRead</b>; if the asynchronous /// call has not completed, /// <b>EndRead</b> /// will block until it completes. /// </para> /// <para> /// The read operation will not completed until the number of bytes specified by <i>size</i> /// parameter is read from the stream. If the remote host shuts down the /// <see cref="BytesRoad.Net.Sockets.SocketEx"/> /// connection with the /// <see cref="BytesRoad.Net.Sockets.SocketEx.Shutdown"/> /// method, and all available data has been received, the <b>Read</b> method /// will complete and return number of bytes was read. /// </para> /// <note> /// The <b>NetworkStreamEx</b> should have access right to read data from the network stream. /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanRead"/> property to check this. /// </note> /// </remarks> /// /// <exception cref="System.ObjectDisposedException"> /// The <see cref="BytesRoad.Net.Sockets.NetworkStreamEx"/> object was disposed. /// </exception> /// /// <exception cref="System.ArgumentNullException"> /// <i>buffer</i> is a null reference (<b>Nothing</b> in Visual Basic). /// </exception> /// /// <exception cref="System.ArgumentOutOfRangeException"> /// <i>offset</i> is less than 0. /// <para>-or-</para> /// <i>offset</i> is greater than the length of <i>buffer</i>. /// <para>-or-</para> /// <i>size</i> is less than 0. /// <para>-or-</para> /// <i>size</i> is greater than the length of <i>buffer</i> minus /// the value of the <i>offset</i> parameter. /// </exception> public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state ) { CheckDisposed(); _asyncCtx.SetProgress(true); try { Read_SO stateObj = new Read_SO(buffer, offset, size, callback, state); return _socket.BeginReceive(buffer, offset, size, new AsyncCallback(Read_End), stateObj); } catch { _asyncCtx.SetProgress(false); CheckDisposed(); throw; } }