WriteInternalAsync() private method

private WriteInternalAsync ( ArraySegment buffer, Int32 count, System.Boolean isCompleted, System.Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags, CancellationToken cancellation ) : Task
buffer ArraySegment
count System.Int32
isCompleted System.Boolean
headerSent System.Boolean
option WebSocketFrameOption
extensionFlags WebSocketExtensionFlags
cancellation System.Threading.CancellationToken
return Task
        internal override async Task StartPing()
        {
            _lastActivity = DateTime.Now.Add(_pingTimeout);
            var pingInterval = TimeSpan.FromMilliseconds(Math.Max(500, _pingTimeout.TotalMilliseconds / 3));
            var skipInterval = TimeSpan.FromMilliseconds(Math.Max(250, _pingTimeout.TotalMilliseconds / 2));

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

                try
                {
                    var now = DateTime.Now;

                    if (_lastActivity < now.Subtract(_pingTimeout))
                    {
                        _connection.Close(WebSocketCloseReasons.GoingAway);
                    }
                    else if (_lastActivity < now.Subtract(skipInterval))
                    {
                        await _connection.WriteInternalAsync(_pingBuffer, 0, true, false, WebSocketFrameOption.Ping, WebSocketExtensionFlags.None, CancellationToken.None).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Fail("BandwidthSavingPing.StartPing " + ex.Message);
                    _connection.Close(WebSocketCloseReasons.ProtocolError);
                }
            }
        }
        internal override async Task StartPing()
        {
            _lastPong     = _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
                    {
                        ((UInt64)now.Ticks).ToBytes(_pingBuffer.Array, _pingBuffer.Offset);
                        await _connection.WriteInternalAsync(_pingBuffer, 8, true, false, WebSocketFrameOption.Ping, WebSocketExtensionFlags.None, CancellationToken.None).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    Debug.Fail("LatencyControlPing.StartPing " + ex.Message);
                    _connection.Close(WebSocketCloseReasons.ProtocolError);
                }
            }
        }
        public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancel)
        {
            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)
                {
                    await _webSocket.WriteInternalAsync(_webSocket.SendBuffer, _internalUsedBufferLength, false, _isHeaderSent, _messageType, ExtensionFlags, cancel).ConfigureAwait(false);

                    _internalUsedBufferLength = 0;
                    _isHeaderSent             = true;
                }
            }
        }