public bool StartWrite( IList <ArraySegment <byte> > buffer, int offset, AsyncCallback cb, object state, out bool completed) { if (!_isConnected) { return(_delegate.StartWrite(buffer, offset, cb, state, out completed)); } else if (SslStream == null) { throw new ConnectionLostException(); } Debug.Assert(SslStream != null); if (!_authenticated) { completed = false; return(StartAuthenticate(cb, state)); } // // We limit the packet size for beingWrite to ensure connection timeouts are based // on a fixed packet size. // int remaining = buffer.GetByteCount() - offset; int packetSize = GetSendPacketSize(remaining); try { _writeCallback = cb; ArraySegment <byte> data = buffer.GetSegment(offset, packetSize); _writeResult = SslStream.BeginWrite(data.Array, 0, data.Count, WriteCompleted, state); completed = packetSize == remaining; return(_writeResult.CompletedSynchronously); } catch (IOException ex) { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } if (Network.Timeout(ex)) { throw new ConnectionTimeoutException(); } throw new TransportException(ex); } catch (ObjectDisposedException ex) { throw new ConnectionLostException(ex); } }
public void FinishWrite(IList <ArraySegment <byte> > buffer, ref int offset) { if (!_isConnected) { _delegate.FinishWrite(buffer, ref offset); return; } else if (_sslStream == null) // Transceiver was closed { int remaining = buffer.GetByteCount() - offset; if (GetSendPacketSize(remaining) == remaining) // Sent last packet { offset = remaining; // Assume all the data was sent for at-most-once semantics. } _writeResult = null; return; } else if (!_authenticated) { FinishAuthenticate(); return; } Debug.Assert(_writeResult != null); int bytesTransferred = GetSendPacketSize(buffer.GetByteCount() - offset); try { _sslStream.EndWrite(_writeResult); offset += bytesTransferred; } catch (IOException ex) { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } if (Network.Timeout(ex)) { throw new ConnectionTimeoutException(); } throw new TransportException(ex); } catch (ObjectDisposedException ex) { throw new ConnectionLostException(ex); } }
public void FinishRead(ref ArraySegment <byte> buffer, ref int offset) { if (!_isConnected) { _delegate.FinishRead(ref buffer, ref offset); return; } else if (_sslStream == null) // Transceiver was closed { _readResult = null; return; } Debug.Assert(_readResult != null); try { int ret = _sslStream.EndRead(_readResult); _readResult = null; if (ret == 0) { throw new ConnectionLostException(); } Debug.Assert(ret > 0); offset += ret; } catch (IOException ex) { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } if (Network.Timeout(ex)) { throw new ConnectionTimeoutException(); } throw new TransportException(ex); } catch (ObjectDisposedException ex) { throw new ConnectionLostException(ex); } }
public bool StartRead(ref ArraySegment <byte> buffer, ref int offset, AsyncCallback callback, object state) { if (!_isConnected) { return(_delegate.StartRead(ref buffer, ref offset, callback, state)); } else if (_sslStream == null) { throw new ConnectionLostException(); } Debug.Assert(_sslStream.IsAuthenticated); int packetSize = GetRecvPacketSize(buffer.Count - offset); try { _readCallback = callback; _readResult = _sslStream.BeginRead(buffer.Array, buffer.Offset + offset, packetSize, ReadCompleted, state); return(_readResult.CompletedSynchronously); } catch (IOException ex) { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } if (Network.Timeout(ex)) { throw new ConnectionTimeoutException(); } throw new TransportException(ex); } catch (ObjectDisposedException ex) { throw new ConnectionLostException(ex); } }