Ejemplo n.º 1
0
        /// <summary>
        /// Writes data to the stream.
        /// </summary>
        /// <param name="buffer">Buffer to write from.</param>
        /// <param name="offset">Offset into the buffer from where we'll start writing.</param>
        /// <param name="count">Number of bytes to write.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            try
            {
                var remainingBytes = count;
                while (0 < remainingBytes)
                {
                    var sendBuffer = bufPool.Allocate();
                    var sendCount  = Math.Min(sendBuffer.WritableBytes, remainingBytes);
                    sendBuffer.WriteBytes(buffer, offset, sendCount);
                    streamSocket.Send(sendBuffer);
                    remainingBytes -= sendCount;
                }
            }
            catch (Exception e)
            {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }

                // some sort of error occurred on the socked call,
                // set the SocketException as InnerException and throw
                throw new IOException(string.Format("Unable to write data to the transport connection: {0}.", e.Message), e);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Flushes data in send cache to the stream.
        /// </summary>
        public override void Flush()
        {
            if (sendDataCache != null && sendDataCache.IsReadable())
            {
                try
                {
                    streamSocket.Send(sendDataCache);
                    sendDataCache = null;
                }
                catch (Exception e)
                {
                    if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                    {
                        throw;
                    }

                    // some sort of error occurred on the socked call,
                    // set the SocketException as InnerException and throw
                    throw new IOException(string.Format("Unable to write data to the transport connection: {0}.", e.Message), e);
                }
            }
        }