Create() public static method

public static Create ( System.Int64 count, System.Boolean isComplete, System.Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags ) : WebSocketFrameHeader
count System.Int64
isComplete System.Boolean
headerSent System.Boolean
option WebSocketFrameOption
extensionFlags WebSocketExtensionFlags
return WebSocketFrameHeader
        internal void WriteInternal(ArraySegment <Byte> buffer, Int32 count, Boolean isCompleted, Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags)
        {
            try
            {
                var header = WebSocketFrameHeader.Create(count, isCompleted, headerSent, option, extensionFlags);
                header.ToBytes(buffer.Array, buffer.Offset - header.HeaderLength);

                if (!_writeSemaphore.Wait(_options.WebSocketSendTimeout))
                {
                    throw new WebSocketException("Write timeout");
                }
                _clientStream.Write(buffer.Array, buffer.Offset - header.HeaderLength, count + header.HeaderLength);
            }
            catch (InvalidOperationException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (IOException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (Exception ex)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
                throw new WebSocketException("Cannot write on WebSocket", ex);
            }
            finally
            {
                if (_isClosed == 0)
                {
                    SafeEnd.ReleaseSemaphore(_writeSemaphore);
                }
            }
        }
示例#2
0
        public ArraySegment <byte> PrepareFrame(ArraySegment <byte> payload, int length, bool isCompleted, bool headerSent, WebSocketMessageType type, WebSocketExtensionFlags extensionFlags)
        {
            var mask = 0U;

            if (this.maskData)
            {
                mask = unchecked ((uint)ThreadStaticRandom.NextNotZero());
            }

            var header = WebSocketFrameHeader.Create(length, isCompleted, headerSent, mask, (WebSocketFrameOption)type, extensionFlags);

            if (header.WriteTo(payload.Array, payload.Offset - header.HeaderLength) != header.HeaderLength)
            {
                throw new WebSocketException("Wrong frame header written.");
            }

            if (this.log.IsDebugEnabled)
            {
                this.log.Debug($"({this.GetHashCode():X}) [FRAME->] {header}");
            }

            header.EncodeBytes(payload.Array, payload.Offset, length);

            return(new ArraySegment <byte>(payload.Array, payload.Offset - header.HeaderLength, length + header.HeaderLength));
        }
示例#3
0
        private async Task WriteInternalAsync(ArraySegment <Byte> buffer, Int32 count, Boolean isCompleted, Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags, CancellationToken cancellation)
        {
            CancellationTokenRegistration reg = cancellation.Register(this.Close, false);

            try
            {
                var header = WebSocketFrameHeader.Create(count, isCompleted, headerSent, option, extensionFlags);
                header.ToBytes(buffer.Array, buffer.Offset - header.HeaderLength);

                if (!_writeSemaphore.Wait(_options.WebSocketSendTimeout))
                {
                    throw new WebSocketException("Write timeout");
                }
                await _clientStream.WriteAsync(buffer.Array, buffer.Offset - header.HeaderLength, count + header.HeaderLength, cancellation).ConfigureAwait(false);
            }
            catch (InvalidOperationException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (IOException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (Exception ex)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
                throw new WebSocketException("Cannot write on WebSocket", ex);
            }
            finally
            {
                reg.Dispose();
                _writeSemaphore.Release();
            }
        }
        internal async Task WriteInternalAsync(ArraySegment <byte> buffer, int count, bool isCompleted, bool headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags, CancellationToken cancel)
        {
            try
            {
                var header = WebSocketFrameHeader.Create(count, isCompleted, headerSent, option, extensionFlags);
                header.ToBytes(buffer.Array, buffer.Offset - header.HeaderLength);

                if (!await _writeSemaphore.WaitAsync(_options.WebSocketSendTimeout, cancel).ConfigureAwait(false))
                {
                    throw new WebSocketException("Write timeout");
                }
                await _clientStream.WriteAsync(buffer.Array, buffer.Offset - header.HeaderLength, count + header.HeaderLength, cancel).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                Close(WebSocketCloseReasons.GoingAway);
            }
            catch (InvalidOperationException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (IOException)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
            }
            catch (Exception ex)
            {
                Close(WebSocketCloseReasons.UnexpectedCondition);
                throw new WebSocketException("Cannot write on WebSocket", ex);
            }
            finally
            {
                SafeEnd.ReleaseSemaphore(_writeSemaphore);
            }
        }