Example #1
0
        private async Task StartAsyncCore()
        {
            if (Features.Get <IConnectionInherentKeepAliveFeature>() == null)
            {
                Debug.Assert(ProtocolReaderWriter != null, "Expected the ProtocolReaderWriter to be set before StartAsync is called");
                _pingMessage = ProtocolReaderWriter.WriteMessage(PingMessage.Instance);
                _connectionContext.Features.Get <IConnectionHeartbeatFeature>()?.OnHeartbeat(state => ((HubConnectionContext)state).KeepAliveTick(), this);
            }

            try
            {
                while (await Output.Reader.WaitToReadAsync())
                {
                    while (Output.Reader.TryRead(out var hubMessage))
                    {
                        var buffer = ProtocolReaderWriter.WriteMessage(hubMessage);
                        while (await _connectionContext.Transport.Writer.WaitToWriteAsync())
                        {
                            if (_connectionContext.Transport.Writer.TryWrite(buffer))
                            {
                                Interlocked.Exchange(ref _lastSendTimestamp, Stopwatch.GetTimestamp());
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Abort(ex);
            }
        }
Example #2
0
        public override async Task WriteAsync(HubMessage message)
        {
            try
            {
                await _writeLock.WaitAsync();

                var buffer = ProtocolReaderWriter.WriteMessage(message);

                await _connection.SendAsync(buffer, CancellationToken.None);
            }
            finally
            {
                _writeLock.Release();
            }
        }
Example #3
0
        public virtual async Task WriteAsync(HubMessage message)
        {
            try
            {
                await _writeLock.WaitAsync();

                var buffer = ProtocolReaderWriter.WriteMessage(message);

                _connectionContext.Transport.Output.Write(buffer);

                Interlocked.Exchange(ref _lastSendTimestamp, Stopwatch.GetTimestamp());

                await _connectionContext.Transport.Output.FlushAsync(CancellationToken.None);
            }
            finally
            {
                _writeLock.Release();
            }
        }