Example #1
0
        /// <summary>
        /// Sends all written data that has not been sent over the socket. If nothing has been written to the stream
        /// or everything has already been flushed, this method does not throw and does not send anything through the socket.
        /// If this stream is in read mode or has been closed or disposed, this method throws.
        /// </summary>
        public override void Flush()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("SocketStream");
            }
            if (IsReadMode)
            {
                throw new InvalidOperationException("Can't flush a SocketStream that is in Read Mode");
            }

            IEnumerator <RecordContentsStream> it = UnderlyingStreams.GetEnumerator();

            try
            {
                // Move until the last flushed stream
                if (LastFlushedStream != null)
                {
                    while (it.MoveNext())
                    {
                        if (it.Current == LastFlushedStream)
                        {
                            break;
                        }
                    }
                }

                // Now flush the ones that haven't been flushed for real!
                while (it.MoveNext())
                {
                    // We have to make sure that the current stream is not empty,
                    // because that would mean us sending an empty record here, when it should only happen in Close()
                    if (it.Current.Length == 0)
                    {
                        continue;
                    }

                    using (var record = (StreamRecordBase)RecordFactory.CreateRecord(RequestId, RecordType))
                    {
                        record.Contents = it.Current;
                        Send(record);
                    }
                }
            }
            catch
            {
                it.Dispose();
                throw;
            }

            // Internal bookkeeping
            LastFlushedStream = LastUnfilledStream;
            var newLastStream = new RecordContentsStream();

            underlyingStreams.AddLast(newLastStream);
            LastUnfilledStream = newLastStream;
        }
Example #2
0
        /// <summary>
        /// This method is called internally when enough data has been written to this stream that a new <seealso cref="RecordContentsStream" /> must
        /// be created and appended to the internal list of underlying streams.
        /// </summary>
        public virtual void AppendStream(RecordContentsStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (stream.Length == 0 && IsReadMode && underlyingStreams.Count > 0)
            {
                IsComplete = true;
            }

            LastUnfilledStream = stream;
            underlyingStreams.AddLast(stream);
        }
Example #3
0
        public SocketStream(Socket sock, RecordType streamType, bool readMode)
            : base(readMode)
        {
            if (sock == null)
            {
                throw new ArgumentNullException("sock");
            }
            else if (!streamType.IsStreamType())
            {
                throw new ArgumentException("streamType must be a stream record type");
            }

            Socket            = sock;
            RecordType        = streamType;
            IsDisposed        = false;
            LastFlushedStream = null;
        }