Example #1
0
        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);
            }
        }