/// <summary> /// Begins an asynchronous write to the network stream. /// </summary> /// <param name="buffer">Data to write.</param> /// <param name="offset">The position in the data <i>buffer</i> from which to begin writing.</param> /// <param name="size">The number of bytes to write.</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 write. /// </returns> /// /// <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> /// /// <remarks> /// The <b>BeginWrite</b> method starts an asynchronous /// write operation. /// It returns immediately and does not wait for /// the asynchronous call to complete. /// <para> /// The /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite"/> /// method is used to retrieve the results of /// the asynchronous call. It can be called /// any time after <b>BeginWrite</b>; if the asynchronous /// call has not completed, /// <b>EndWrite</b> /// will block until it completes. /// </para> /// <para> /// Write operation will not completed until all data, specified by <i>size</i> parameter, are sent. /// </para> /// /// <note> /// The <b>NetworkStreamEx</b> should have access right to write data to the network stream. /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite"/> property to check this. /// </note> /// /// </remarks> public override IAsyncResult BeginWrite( byte[] buffer, int offset, int size, AsyncCallback callback, object state ) { CheckDisposed(); _asyncCtx.SetProgress(true); try { Write_SO stateObj = new Write_SO( buffer, offset, size, callback, state); return(_socket.BeginSend( buffer, offset, size, new AsyncCallback(Send_End), stateObj)); } catch { _asyncCtx.SetProgress(false); CheckDisposed(); throw; } }
void Send_End(IAsyncResult ar) { Write_SO stateObj = (Write_SO)ar.AsyncState; try { stateObj.UpdateContext(); int sent = _socket.EndSend(ar); stateObj.Sent += sent; if (stateObj.Sent < stateObj.Size) { _socket.BeginSend( stateObj.Buffer, stateObj.Offset + stateObj.Sent, stateObj.Size - stateObj.Sent, new AsyncCallback(Send_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 BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state) { IAsyncResult result; this.CheckDisposed(); this._asyncCtx.SetProgress(true); try { Write_SO e_so = new Write_SO(buffer, offset, size, callback, state); result = this._socket.BeginSend(buffer, offset, size, new AsyncCallback(this.Send_End), e_so); } catch { this._asyncCtx.SetProgress(false); this.CheckDisposed(); throw; } return(result); }
private void Send_End(IAsyncResult ar) { Write_SO asyncState = (Write_SO)ar.AsyncState; try { asyncState.UpdateContext(); int num = this._socket.EndSend(ar); asyncState.Sent += num; if (asyncState.Sent < asyncState.Size) { this._socket.BeginSend(asyncState.Buffer, asyncState.Offset + asyncState.Sent, asyncState.Size - asyncState.Sent, new AsyncCallback(this.Send_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 write to the network stream. /// </summary> /// <param name="buffer">Data to write.</param> /// <param name="offset">The position in the data <i>buffer</i> from which to begin writing.</param> /// <param name="size">The number of bytes to write.</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 write. /// </returns> /// /// <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> /// /// <remarks> /// The <b>BeginWrite</b> method starts an asynchronous /// write operation. /// It returns immediately and does not wait for /// the asynchronous call to complete. /// <para> /// The /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite"/> /// method is used to retrieve the results of /// the asynchronous call. It can be called /// any time after <b>BeginWrite</b>; if the asynchronous /// call has not completed, /// <b>EndWrite</b> /// will block until it completes. /// </para> /// <para> /// Write operation will not completed until all data, specified by <i>size</i> parameter, are sent. /// </para> /// /// <note> /// The <b>NetworkStreamEx</b> should have access right to write data to the network stream. /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite"/> property to check this. /// </note> /// /// </remarks> public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state ) { CheckDisposed(); _asyncCtx.SetProgress(true); try { Write_SO stateObj = new Write_SO(buffer, offset, size, callback, state); return _socket.BeginSend(buffer, offset, size, new AsyncCallback(Send_End), stateObj); } catch { _asyncCtx.SetProgress(false); CheckDisposed(); throw; } }