internal static void CompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat)
        {
            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                                   (Stream) new GZipStream(bufferedOutStream, CompressionMode.Compress, true) :
                                   (Stream) new DeflateStream(bufferedOutStream, CompressionMode.Compress, true))
                {
                    ds.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                byte[] compressedBytes = bufferedOutStream.ToArray(out int length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(compressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
        public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager)
        {
            if (size <= 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.ValueMustBePositive));
            }

            ThrowPendingWriteException();

            if (immediate || flushTimeout == 0)
            {
                WriteNow(buffer, offset, size, timeout, bufferManager);
            }
            else
            {
                WriteLater(buffer, offset, size, timeout);
                bufferManager.ReturnBuffer(buffer);
            }
        }
        void WriteNow(byte[] buffer, int offset, int size, TimeSpan timeout, BufferManager bufferManager)
        {
            lock (ThisLock)
            {
                if (pendingWriteSize > 0)
                {
                    int remainingSize = writeBufferSize - pendingWriteSize;
                    CancelFlushTimer();
                    if (size <= remainingSize)
                    {
                        Buffer.BlockCopy(buffer, offset, writeBuffer, pendingWriteSize, size);
                        if (bufferManager != null)
                        {
                            bufferManager.ReturnBuffer(buffer);
                        }
                        pendingWriteSize += size;
                        FlushCore(timeout);
                        return;
                    }
                    else
                    {
                        TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                        FlushCore(timeoutHelper.RemainingTime());
                        timeout = timeoutHelper.RemainingTime();
                    }
                }

                if (bufferManager == null)
                {
                    Connection.Write(buffer, offset, size, true, timeout);
                }
                else
                {
                    Connection.Write(buffer, offset, size, true, timeout, bufferManager);
                }
            }
        }
示例#4
0
 public override void ReturnBuffer(byte[] buffer)
 {
     _innerBufferManager.ReturnBuffer(buffer);
 }
        protected override async Task OnSendCoreAsync(Message message, CancellationToken token)
        {
            Fx.Assert(message != null, "message should not be null.");

            WebSocketMessageType outgoingMessageType = GetWebSocketMessageType(message);

            if (IsStreamedOutput)
            {
                WebSocketStream webSocketStream = new WebSocketStream(WebSocket, outgoingMessageType, token);
                TimeoutStream   timeoutStream   = new TimeoutStream(webSocketStream, token);
                await MessageEncoder.WriteMessageAsync(message, timeoutStream);

                await webSocketStream.WriteEndOfMessageAsync(token);
            }
            else
            {
                ArraySegment <byte> messageData = EncodeMessage(message);
                bool success = false;
                try
                {
                    //if (TD.WebSocketAsyncWriteStartIsEnabled())
                    //{
                    //    TD.WebSocketAsyncWriteStart(
                    //        this.WebSocket.GetHashCode(),
                    //        messageData.Count,
                    //        this.RemoteAddress != null ? this.RemoteAddress.ToString() : string.Empty);
                    //}

                    try
                    {
                        await WebSocket.SendAsync(messageData, outgoingMessageType, true, token);
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex))
                        {
                            throw;
                        }

                        WebSocketHelper.ThrowCorrectException(ex, TimeoutHelper.GetOriginalTimeout(token), WebSocketHelper.SendOperation);
                    }

                    //if (TD.WebSocketAsyncWriteStopIsEnabled())
                    //{
                    //    TD.WebSocketAsyncWriteStop(this.webSocket.GetHashCode());
                    //}

                    success = true;
                }
                finally
                {
                    try
                    {
                        BufferManager.ReturnBuffer(messageData.Array);
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex) || success)
                        {
                            throw;
                        }

                        Fx.Exception.TraceUnhandledException(ex);
                    }
                }
            }
        }
示例#6
0
 public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager)
 {
     Write(buffer, offset, size, immediate, timeout);
     bufferManager.ReturnBuffer(buffer);
 }