Ejemplo n.º 1
0
        public void ProducingComplete(MemoryPoolIterator end)
        {
            var block = _producingBlock;

            while (block != end.Block)
            {
                // If we don't handle an exception from _outputStream.Write() here, we'll leak memory blocks.
                if (_canWrite)
                {
                    try
                    {
                        _outputStream.Write(block.Data.Array, block.Data.Offset, block.Data.Count);
                    }
                    catch (Exception ex)
                    {
                        _canWrite = false;
                        _logger.ConnectionError(_connectionId, ex);
                    }
                }

                var returnBlock = block;
                block = block.Next;
                returnBlock.Pool.Return(returnBlock);
            }

            if (_canWrite)
            {
                try
                {
                    _outputStream.Write(end.Block.Array, end.Block.Data.Offset, end.Index - end.Block.Data.Offset);
                }
                catch (Exception ex)
                {
                    _canWrite = false;
                    _logger.ConnectionError(_connectionId, ex);
                }
            }

            end.Block.Pool.Return(end.Block);
        }
        public void Write(ArraySegment <byte> buffer, bool chunk)
        {
            lock (_writeLock)
            {
                if (buffer.Count == 0)
                {
                    return;
                }

                try
                {
                    if (!_canWrite)
                    {
                        return;
                    }

                    if (chunk && buffer.Array != null)
                    {
                        var beginChunkBytes = ChunkWriter.BeginChunkBytes(buffer.Count);
                        _outputStream.Write(beginChunkBytes.Array, beginChunkBytes.Offset, beginChunkBytes.Count);
                    }

                    _outputStream.Write(buffer.Array ?? _nullBuffer, buffer.Offset, buffer.Count);

                    if (chunk && buffer.Array != null)
                    {
                        _outputStream.Write(_endChunkBytes, 0, _endChunkBytes.Length);
                    }
                }
                catch (Exception ex)
                {
                    _canWrite = false;
                    _logger.ConnectionError(_connectionId, ex);
                }
            }
        }