Beispiel #1
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            CheckDisposed();
            if (!CanWrite)
            {
                throw new InvalidOperationException("Stream not open for writing");
            }

            if (count == 0)
            {
                return;
            }

            EnsureDataMessage();

            if (count <= _buf.WriteSpaceLeft)
            {
                _buf.WriteBytesSimple(buffer, offset, count);
                return;
            }

            // Buffer is too big. Write whatever will fit and flush.
            var written = _buf.WriteSpaceLeft;

            _buf.WriteBytesSimple(buffer, offset, _buf.WriteSpaceLeft);
            Flush();

            offset += written;
            count  -= written;

            // If the remainder fits in a single buffer, no problem.
            if (count <= _buf.WriteSpaceLeft)
            {
                EnsureDataMessage();
                _buf.WriteBytesSimple(buffer, offset, count);
                return;
            }

            // Otherwise, write the CopyData header via our buffer and the remaining data directly to the socket
            _buf.WriteByte((byte)BackendMessageCode.CopyData);
            _buf.WriteInt32(count);
            _buf.Flush();
            _buf.Underlying.Write(buffer, offset, count);
        }
Beispiel #2
0
        void EnsureDataMessage()
        {
            if (_writingDataMsg)
            {
                return;
            }

            Contract.Assert(_buf.WritePosition == 0);
            _buf.WriteByte((byte)BackendMessageCode.CopyData);
            // Leave space for the message length
            _buf.WriteInt32(0);
            _writingDataMsg = true;
        }
Beispiel #3
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            CheckDisposed();
            if (!CanWrite)
            {
                throw new InvalidOperationException("Stream not open for writing");
            }

            if (count == 0)
            {
                return;
            }

            EnsureDataMessage();

            if (count <= _buf.WriteSpaceLeft)
            {
                _buf.WriteBytes(buffer, offset, count);
                return;
            }

            try {
                // Value is too big. Flush whatever is in the buffer, then write a new CopyData
                // directly with the buffer.
                Flush();

                _buf.WriteByte((byte)BackendMessageCode.CopyData);
                _buf.WriteInt32(count + 4);
                _buf.Flush();
                _buf.Underlying.Write(buffer, offset, count);
                EnsureDataMessage();
            } catch {
                _connector.Break();
                Cleanup();
                throw;
            }
        }