WriteInternal() private method

private WriteInternal ( ArraySegment buffer, Int32 count, System.Boolean isCompleted, System.Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags ) : void
buffer ArraySegment
count System.Int32
isCompleted System.Boolean
headerSent System.Boolean
option WebSocketFrameOption
extensionFlags WebSocketExtensionFlags
return void
        internal override async Task StartPing()
        {
            _lastPong     = DateTime.Now.Add(_pingTimeout);
            _pingInterval = TimeSpan.FromMilliseconds(Math.Max(500, _pingTimeout.TotalMilliseconds / 2));

            while (_connection.IsConnected)
            {
                await Task.Delay(_pingInterval).ConfigureAwait(false);

                try
                {
                    var now = DateTime.Now;

                    if (_lastPong.Add(_pingTimeout) < now)
                    {
                        _connection.Close(WebSocketCloseReasons.GoingAway);
                    }
                    else
                    {
                        ((UInt64)now.Ticks).ToBytes(_pingBuffer.Array, _pingBuffer.Offset);
                        _connection.WriteInternal(_pingBuffer, 8, true, false, WebSocketFrameOption.Ping, WebSocketExtensionFlags.None);
                    }
                }
                catch
                {
                    _connection.Close(WebSocketCloseReasons.ProtocolError);
                }
            }
        }
Ejemplo n.º 2
0
        internal override async Task StartPing()
        {
            _lastActivity = DateTime.Now.Add(_pingTimeout);
            _pingInterval = TimeSpan.FromMilliseconds(Math.Max(500, _pingTimeout.TotalMilliseconds / 2));

            while (_connection.IsConnected)
            {
                await Task.Delay(_pingInterval).ConfigureAwait(false);

                try
                {
                    var now = DateTime.Now;

                    if (_lastActivity.Add(_pingTimeout) < now)
                    {
                        _connection.Close(WebSocketCloseReasons.GoingAway);
                    }
                    else if (_lastActivity.Add(_pingInterval) < now)
                    {
                        _connection.WriteInternal(_pingBuffer, 0, true, false, WebSocketFrameOption.Ping, WebSocketExtensionFlags.None);
                    }
                }
                catch (Exception ex)
                {
                    DebugLog.Fail("BandwidthSavingPing.StartPing", ex);
                    _connection.Close(WebSocketCloseReasons.ProtocolError);
                }
            }
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_isFinished)
            {
                throw new WebSocketException("The write stream has been already flushed or disposed.");
            }

            RemoveUTF8BOM(buffer, ref offset, ref count);

            while (count > 0)
            {
                BufferData(buffer, ref offset, ref count);

                if (_internalUsedBufferLength == _webSocket.SendBuffer.Count && count > 0)
                {
                    _webSocket.WriteInternal(_webSocket.SendBuffer, _internalUsedBufferLength, false, _isHeaderSent, _messageType, ExtensionFlags);
                    _internalUsedBufferLength = 0;
                    _isHeaderSent             = true;
                }
            }
        }