public Task <bool> SendAsync(NetworkMessageSlice slice)
 {
     if (!slice.IsEmpty)
     {
         _queue.Enqueue(slice);
         return(Task.FromResult(true));
     }
     return(Task.FromResult(false));
 }
Esempio n. 2
0
 public static async Task <bool> WriteResponseAsync(this PipeWriter writer, NetworkMessageSlice response, CancellationToken token)
 {
     if (response.IsEmpty)
     {
         return(true);
     }
     foreach (var segment in response.DataList)
     {
         if ((await writer.WriteAsync(segment, token)).IsCanceled)
         {
             return(false);
         }
     }
     return(!(await writer.WriteAsync(response.Channel.ILIntEncode(), token)).IsCanceled);
 }
        private async Task SendingPipeDequeueAsync(PipeWriter writer)
        {
            var senderWriterLock = new AsyncLock();

            while (_active)
            {
                try {
                    if (_stopping)
                    {
                        break;
                    }
                    using (await senderWriterLock.LockAsync()) {
                        var response = await DequeueAsync(_linkedToken);

                        if (!await writer.WriteResponseAsync(response, _linkedToken))
                        {
                            break;
                        }
                    }
                } catch (OperationCanceledException oce) {
                    writer.Complete(oce);
                    return;
                } catch (SocketException se) when(se.ErrorCode.In(10054, 104))
                {
                    _localSource.Cancel(false);
                    writer.Complete(se);
                    return;
                } catch (Exception e) {
                    _logger.LogError(e, "While dequeueing");
                    writer.Complete(e);
                    return;
                }
            }
            writer.Complete();

            async Task <NetworkMessageSlice> DequeueAsync(CancellationToken token)
            {
                NetworkMessageSlice slice = default;

                while (!_stopping && !token.IsCancellationRequested && !_queue.TryDequeue(out slice))
                {
                    await Task.Delay(1, token);
                }
                return(slice);
            }
        }
Esempio n. 4
0
 static Task <Success> messageProcessor(NetworkMessageSlice channelBytes) => Task.FromResult(Success.Exit);
 internal Task <Success> SinkAsync(NetworkMessageSlice slice) => DoAsync(() => InnerSinkAsync(slice));
 internal Task <bool> SendAsync(NetworkMessageSlice slice) => DoAsync(() => InnerSendAsync(slice));