public void SetComplete(Exception ex) { lock (locker) { if (completed) { return; } completed = true; if (handle != null) { handle.Set(); } if (_userCallback != null) #if SSHARP { _userCallback.BeginInvokeEx(this, null, null); } #else { _userCallback.BeginInvoke(this, null, null); } #endif _asyncException = ex; } }
private void SetComplete(Exception ex, byte[] resultingBuffer) { lock (locker) { if (completed) { return; } completed = true; _asyncException = ex; _resultingBuffer = resultingBuffer; if (handle != null) { handle.Set(); } if (_userCallback != null) #if SSHARP { _userCallback.BeginInvokeEx(this, null, null); } #else { _userCallback.BeginInvoke(this, null, null); } #endif } }
public void Complete() { lock (locker) { if (completed) { return; } completed = true; if (handle != null) { handle.Set(); } if (Callback != null) #if SSHARP { Callback.BeginInvokeEx(this, null, null); } #else { Callback.BeginInvoke(this, null, null); } #endif } }
private void SetComplete(Exception ex, int bytesRead) { lock (locker) { if (completed) { return; } completed = true; _asyncException = ex; _bytesRead = bytesRead; if (handle != null) { handle.Set(); } } if (_userCallback != null) #if SSHARP { _userCallback.BeginInvokeEx(this, null, null); } #else { _userCallback.BeginInvoke(this, null, null); } #endif }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { Debug.WriteLine("BNS ({0}): BeginWrite (buffer, {1}, {2}) [m_outputBuffer_pos = {3}]", NetworkStream.InternalSocket.InternalRemoteEndPoint, offset, count, m_outputBuffer_pos); 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.CanWrite) { throw new NotSupportedException(Locale.GetText("Cannot write to stream")); } if (m_nagleTimer == null) { return(m_stream.BeginWrite(buffer, offset, count, callback, state)); } var avail = m_bufferSize - m_outputBuffer_pos; if (count <= avail) { EnsureOutputBuffer(); m_lockFlush.Acquire(); try { Buffer.BlockCopy(buffer, offset, m_outputBuffer, m_outputBuffer_pos, count); m_outputBuffer_pos += count; if (m_outputBuffer_pos == m_bufferSize) { m_nagleTimer.Stop(); m_outputBuffer_pos = 0; return(m_stream.BeginWrite(m_outputBuffer, 0, m_bufferSize, callback, state)); } m_nagleTimer.Reset(200); var wsr = new WriteSyncResult { AsyncState = state }; if (callback != null) { callback.BeginInvokeEx(wsr, null, null); } return(wsr); } finally { m_lockFlush.Release(); } } if (m_outputBuffer_pos == 0 && count > m_bufferSize) { return(m_stream.BeginWrite(buffer, offset, count, callback, state)); } if (m_inputBuffer_pos != 0) { m_lockFlush.Acquire(); try { if (m_outputBuffer_pos != 0) { m_nagleTimer.Stop(); if (count <= m_bufferSize) { var tbuffer = new byte[m_outputBuffer_pos + count]; Buffer.BlockCopy(m_outputBuffer, 0, tbuffer, 0, m_outputBuffer_pos); Buffer.BlockCopy(buffer, offset, tbuffer, m_outputBuffer_pos, count); m_outputBuffer_pos = 0; return(m_stream.BeginWrite(tbuffer, 0, tbuffer.Length, callback, state)); } var war = new WriteAsyncResult { AsyncState = state }; m_stream.BeginWrite(m_outputBuffer, 0, m_outputBuffer_pos, iar => { var ws = (WriteState)iar.AsyncState; m_stream.EndWrite(iar); m_stream.BeginWrite(ws.buffer, ws.offset, ws.count, iar2 => { var ws2 = (WriteState)iar.AsyncState; m_stream.EndWrite(iar2); ws2.writeAsyncResult.IsCompleted = true; ((CEvent)ws2.writeAsyncResult.AsyncWaitHandle).Set(); if (ws2.callback != null) { try { ws2.callback(ws2.writeAsyncResult); } catch { } } }, ws); }, new WriteState { writeAsyncResult = war, callback = callback, buffer = buffer, offset = offset, count = count, }); m_outputBuffer_pos = 0; return(war); } } finally { m_lockFlush.Release(); } } return(m_stream.BeginWrite(buffer, offset, count, callback, state)); }
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); }