示例#1
0
 /// <summary>
 /// Logs the stream message.
 /// </summary>
 /// <param name="sessionId">The session id.</param>
 /// <param name="isReceive">if set to <c>true</c> [is receive].</param>
 /// <param name="messageData">The message data.</param>
 public void LogStreamMessage(int sessionId, bool isReceive, byte[] messageData, bool encodeBase64)
 {
     if (!queueWriter.TryWrite(new StreamLogItem(sessionId, isReceive, messageData, encodeBase64)))
     {
         log.Warn("Could not write data stream item to queue!");
     }
 }
示例#2
0
        public async Task TestBaseClassReadAsync()
        {
            WrapperChannel <int> channel = new WrapperChannel <int>(10);
            ChannelReader <int>  reader  = channel.Reader;
            ChannelWriter <int>  writer  = channel.Writer;

            // 1- do it through synchronous TryRead()
            writer.TryWrite(50);
            Assert.Equal(50, await reader.ReadAsync());

            // 2- do it through async
            ValueTask <int> readTask = reader.ReadAsync();

            writer.TryWrite(100);
            Assert.Equal(100, await readTask);

            // 3- use cancellation token
            CancellationToken ct = new CancellationToken(true); // cancelled token
            await Assert.ThrowsAsync <TaskCanceledException>(() => reader.ReadAsync(ct).AsTask());

            // 4- throw during reading
            readTask = reader.ReadAsync();
            ((WrapperChannelReader <int>)reader).ForceThrowing = true;
            writer.TryWrite(200);
            await Assert.ThrowsAsync <InvalidOperationException>(() => readTask.AsTask());

            // 5- close the channel while waiting reading
            ((WrapperChannelReader <int>)reader).ForceThrowing = false;
            Assert.Equal(200, await reader.ReadAsync());
            readTask = reader.ReadAsync();
            channel.Writer.TryComplete();
            await Assert.ThrowsAsync <ChannelClosedException>(() => readTask.AsTask());
        }
示例#3
0
        public void Return(T pooledObject)
        {
            Interlocked.Increment(ref _idleCount);
            var written = _idleObjectWriter.TryWrite(pooledObject);

            Debug.Assert(written);
        }
示例#4
0
        public override void Return(T item)
        {
            if (_firstItem is null && Interlocked.CompareExchange(ref _firstItem, item, null) == null)
            {
                return;
            }

            _writer.TryWrite(item);
        }
示例#5
0
            public void Run()
            {
                // Ack tick message 1 is implied as "init done"
                _channelWriter.TryWrite(new AckTicksMessage(1));
                Running = true;

                Tick += (a, b) => Console.WriteLine("tick: {0}", _gameTiming.CurTick);

                _gameTiming.InSimulation = true;

                while (Running)
                {
                    var message = _channelReader.ReadAsync().AsTask().Result;

                    switch (message)
                    {
                    case RunTicksMessage msg:
                        _gameTiming.InSimulation = true;
                        var simFrameEvent = new FrameEventArgs(msg.Delta);
                        for (var i = 0; i < msg.Ticks && Running; i++)
                        {
                            Input?.Invoke(this, simFrameEvent);
                            Tick?.Invoke(this, simFrameEvent);
                            _gameTiming.CurTick = new GameTick(_gameTiming.CurTick.Value + 1);
                            Update?.Invoke(this, simFrameEvent);
                        }

                        _channelWriter.TryWrite(new AckTicksMessage(msg.MessageId));
                        break;

                    case StopMessage _:
                        Running = false;
                        break;

                    case PostMessage postMessage:
                        postMessage.Post();
                        _channelWriter.TryWrite(new AckTicksMessage(postMessage.MessageId));
                        break;

                    case AssertMessage assertMessage:
                        try
                        {
                            assertMessage.Assertion();
                        }
                        catch (Exception e)
                        {
                            _channelWriter.TryWrite(new AssertFailMessage(e));
                        }

                        _channelWriter.TryWrite(new AckTicksMessage(assertMessage.MessageId));
                        break;
                    }
                }
            }
示例#6
0
 private void OnWriteAvailable(bool available)
 {
     if (available && _writer.TryWrite(_awaitingElement))
     {
         Pull(_inlet);
     }
     else
     {
         TryComplete();
     }
 }
 public bool Send(string message)
 {
     if (ValidationUtils.ValidateInput(message))
     {
         return(_sentMessageQueueWriter.TryWrite(new SentMessage(new ArraySegment <byte>(_messageEncoding.GetBytes(message)), WebSocketMessageType.Text)));
     }
     else
     {
         throw new WebSocketBadInputException($"Input message (string) of the Send function is null or empty. Please correct it.");
     }
 }
示例#8
0
 public IActionResult Write(int id)
 {
     for (int i = 0; i < id; i++)
     {
         var mes = $"{nameof(Write)}_{i}";
         _channelWriter.TryWrite(mes);
         Console.WriteLine(mes);
     }
     //_channelWriter.Complete();
     return(Ok());
 }
示例#9
0
 public bool Send(byte[] message)
 {
     if (ValidationUtils.ValidateInput(message))
     {
         return(_binaryMessageQueueWriter.TryWrite(new ArraySegment <byte>(message)));
     }
     else
     {
         throw new WebSocketBadInputException($"Input message (byte[]) of the Send function is null or 0 Length. Please correct it.");
     }
 }
示例#10
0
        public void TestChannelCreateBoundedWaitsUntilItHasRoom()
        {
            Channel <int>       queue  = Channel.CreateBounded <int>(1);
            ChannelWriter <int> writer = queue.Writer;

            var written = writer.TryWrite(8);

            Assert.True(written);

            written = writer.TryWrite(11);
            Assert.False(written);
        }
        public bool ReplayNotification(int index)
        {
            if (index == -1 && lastFinishedRequest != null)
            {
                index = lastFinishedRequest.Id;
            }

            if (!activityDict.ContainsKey(index))
            {
                return(false);
            }

            return(activityWriter.TryWrite(activityDict[index]));
        }
示例#12
0
            public void Run()
            {
                // Main run method is only used when running from asynchronous thread.

                // Ack tick message 1 is implied as "init done"
                _channelWriter.TryWrite(new AckTicksMessage(1));

                while (Running)
                {
                    _channelReader.WaitToReadAsync().AsTask().Wait();

                    SingleThreadRunUntilEmpty();
                }
            }
示例#13
0
 private async Task OutboundMessageProcessor()
 {
     try
     {
         while (await _outboundChannelReader.WaitToReadAsync())
         {
             while (_outboundChannelReader.TryRead(out byte[] data))
             {
                 try
                 {
                     _outboundQueueChannelWritter.TryWrite(data);
                     _outboundFileChannelWritter.TryWrite(data);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex);
                     //TODO
                     throw;
                 }
             }
         }
         _outboundQueueChannelWritter.Complete();
         _outboundFileChannelWritter.Complete();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         //TODO
         throw;
     }
 }
示例#14
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var    response = new StreamingMessage();
            string message  = formatter(state, exception);
            var    rpcLog   = new RpcLog
            {
                EventId     = eventId.ToString(),
                Exception   = exception.ToRpcException(),
                Category    = _category,
                LogCategory = WorkerMessage.IsSystemLog ? RpcLogCategory.System : RpcLogCategory.User,
                Level       = ToRpcLogLevel(logLevel),
                Message     = message
            };

            // Grab the invocation id from the current scope, if present.
            _scopeProvider.ForEachScope((scope, log) =>
            {
                if (scope is IEnumerable <KeyValuePair <string, object> > properties)
                {
                    foreach (KeyValuePair <string, object> pair in properties)
                    {
                        if (pair.Key == FunctionInvocationScope.FunctionInvocationIdKey)
                        {
                            log.InvocationId = pair.Value?.ToString();
                            break;
                        }
                    }
                }
            },
                                        rpcLog);

            response.RpcLog = rpcLog;

            _channelWriter.TryWrite(response);
        }
示例#15
0
        private void OnMessage(byte[] body)
        {
            var    type = Serializer.GetMessageType(body);
            object obj  = null;

            switch (type)
            {
            case MessageType.NewOrderRequest:
                obj = OrderSerializer.Deserialize(body);
                break;

            case MessageType.CancelRequest:
                obj = CancelRequestSerializer.Deserialize(body);
                break;

            case MessageType.BookRequest:
                obj = BookRequestSerializer.Deserialize(body);
                break;

            default:
                //TODO
                break;
            }
            var message = new Message(type.Value, body, obj);

            _inboundChannelWritter.TryWrite(message);
        }
        private static async Task RunAsync()
        {
            var manejadorZip      = new ManejadorZip();
            var manejadorImagenes = new ManejadorImagenes();
            IEnumerable <string> listaDeFotografias = manejadorImagenes.GetListadoFotografias();

            Channel <byte[]> m_channel = Channel.CreateUnbounded <byte[]>();

            ChannelReader <byte[]> reader = m_channel.Reader;
            await Task.Factory.StartNew(async() => {
                while (await reader.WaitToReadAsync())
                {
                    while (reader.TryRead(out byte[] miniatura))
                    {
                        await manejadorZip.AgregarAZipAsync(miniatura);
                    }
                }
            });

            ChannelWriter <byte[]> writer = m_channel.Writer;

            foreach (var foto in listaDeFotografias)
            {
                // Generar miniatura
                byte[] miniatura = await manejadorImagenes.GenerarMiniaturaAsync(foto);

                writer.TryWrite(miniatura);
            }
            writer.Complete();
        }
示例#17
0
        private void SendMessageWithPrefix(string message, string prefix)
        {
            if (prefix == null)
            {
                prefix = "";
            }
            else if (prefix.Length > 0 && !prefix.EndsWith(' '))
            {
                prefix += ' ';
            }

            if (message.Length + prefix.Length <= 500)
            {
                outgoingChatWriter.TryWrite($"PRIVMSG #{channel} :{prefix}{message}");
            }
            else
            {
                while (message.Length + prefix.Length > 500)
                {
                    //Split message
                    int endMessage = 500 - prefix.Length;

                    //Find last space in first message to split
                    while (message[endMessage] != ' ' && endMessage > 0)
                    {
                        --endMessage;
                    }

                    //Fallback for space-less messages
                    if (endMessage == 0)
                    {
                        endMessage = 500 - prefix.Length;
                    }

                    outgoingChatWriter.TryWrite($"PRIVMSG #{channel} :{prefix}{message[0..endMessage]}");
示例#18
0
 public static void MustWrite <T>(this ChannelWriter <T> writer, T value)
 {
     if (!writer.TryWrite(value))
     {
         throw new InvalidOperationException("cannot write");
     }
 }
 public void Send(ArraySegment <byte> msg)
 {
     if (!writer.TryWrite(msg))
     {
         throw new Exception();
     }
 }
示例#20
0
        public static void PostToChannel <T>(this IAsyncEnumerable <T> asyncEnumerable, ChannelWriter <T> channelWriter, CancellationToken cancellationToken = default)
        {
            async Task RunPostToChannel()
            {
                try
                {
                    await foreach (var item in asyncEnumerable.WithCancellation(cancellationToken))
                    {
                        if (!channelWriter.TryWrite(item))
                        {
                            await channelWriter.WriteAsync(item, cancellationToken);
                        }
                    }
                }
                catch (Exception ex)
                {
                    channelWriter.TryComplete(ex);
                }
                finally
                {
                    channelWriter.TryComplete();
                }
            }

            _ = RunPostToChannel();
        }
示例#21
0
        private Task RunProducer(TimeSpan interval, ChannelWriter <int> outCh, ChannelWriter <string> logCh)
        {
            // チャネル にデータを書き込む際の基本形
            //   - WaitToWriteAsync() で書き込みできるか確認
            //   - TryWrite() で値を書き込み
            return(Task.Run(async() =>
            {
                var count = 0;
                while (await outCh.WaitToWriteAsync())
                {
                    if (!outCh.TryWrite(count))
                    {
                        continue;
                    }

                    if (await logCh.WaitToWriteAsync())
                    {
#if LOGGING_PRODUCER
                        logCh.TryWrite($"[producer] {count}");
#endif
                    }

                    count++;
                    await Task.Delay(interval);
                }

                Console.WriteLine($"[producer] total {count - 1} items");
            }));
        }
示例#22
0
        private IEnumerable <Task> RunConsumers(int numConsumers, ChannelReader <int> inCh, ChannelWriter <string> logCh)
        {
            var tasks = new List <Task>();

            for (var i = 0; i < numConsumers; i++)
            {
                var index = i;
                tasks.Add(Task.Run(async() =>
                {
                    // チャネル からデータを読み取る際の基本形
                    //   - WaitToReadAsync() で読み取りできるか確認
                    //   - TryRead() で値を読み取り
                    while (await inCh.WaitToReadAsync())
                    {
                        while (inCh.TryRead(out var v))
                        {
                            if (await logCh.WaitToWriteAsync())
                            {
                                logCh.TryWrite($"[consumer{index + 1}] {v}");
                            }
                        }
                    }
                }));
            }

            return(tasks.ToArray());
        }
示例#23
0
        private void FetchPagesInParallel(int pageCount, ChannelWriter <IEnumerable <Person> > channelWriter, CancellationToken ct)
        {
            Task[] tasks = new Task[pageCount];
            for (int page = 0; page < pageCount; ++page)
            {
                int localPage = page;
                tasks[page] = _repo.GetPersonPage(page, _pageSize, ct)
                              .ContinueWith(task =>
                {
                    if (task.IsCompletedSuccessfully)
                    {
                        Console.WriteLine("Page {0} fetched successfully.", localPage);
                        channelWriter.TryWrite(task.Result);
                    }
                    else
                    {
                        Console.WriteLine("Fetching page {0} failed: {1}.", localPage, task.Exception);
                    }
                }, ct);
            }
            Task.WaitAll(tasks, ct);

            // complete the channel so that the reader stops after reading all data
            channelWriter.Complete();
        }
        private async void SortLines(ChannelReader <ILine> reader, ChannelWriter <ILine> writer)
        {
            var lines = new Dictionary <int, ILine>();

            while (await reader.WaitToReadAsync())
            {
                while (reader.TryRead(out var line))
                {
                    if (_parserCount == 1)
                    {
                        writer.TryWrite(line);
                    }
                    else
                    {
                        lines.Add(line.LogLine.LineNumber, line);

                        if (lines.Count >= SortBatchSize * 2)
                        {
                            SortBatch(lines, writer);
                        }
                    }
                }
            }

            // Do the remainder of the lines
            if (_parserCount != 1)
            {
                SortBatch(lines, writer, true);
            }

            writer.Complete();
        }
示例#25
0
        /// <summary>
        ///     Read items from reader and write to writer
        /// </summary>
        /// <typeparam name="T">Type of the item</typeparam>
        /// <param name="reader">Read channel</param>
        /// <param name="writer">Write channel</param>
        /// <returns></returns>
        public static async Task WriteTo <T>(
            this ChannelReader <T> reader,
            ChannelWriter <T> writer)
        {
            try
            {
                while (await reader.WaitToReadAsync())
                {
                    while (reader.TryRead(out var evnt))
                    {
                        while (!writer.TryWrite(evnt))
                        {
                            if (!await writer.WaitToWriteAsync())
                            {
                                return;
                            }
                        }
                    }
                }

                // Manifest any errors in the completion task
                await reader.Completion;
            }
            catch (Exception ex)
            {
                writer.TryComplete(ex);
            }
            finally
            {
                // This will safely no-op if the catch block above ran.
                writer.TryComplete();
            }
        }
        private static async Task ProcessStep2(ChannelReader <MyTask> reader, ChannelWriter <MyTask> writer)
        {
            var tasks = new List <Task>();

            while (await reader.WaitToReadAsync())
            {
                while (reader.TryRead(out MyTask mytask))
                {
                    var task = mytask;
                    var t    = Task.Run(async() =>
                    {
                        task.DoStepN(2);
                        while (await writer.WaitToWriteAsync())
                        {
                            if (writer.TryWrite(task))
                            {
                                break;
                            }
                        }
                    });
                    tasks.Add(t);
                }

                if (reader.Completion.IsCompleted)
                {
                    break;
                }
            }

            Task.WaitAll(tasks.ToArray());
            writer.TryComplete();
        }
示例#27
0
        public static async Task CheckPort(string target, ChannelReader <int> portChannel, ChannelWriter <int> outputWriter)
        {
            TcpClient client = new TcpClient();

            //wait while channel is open
            while (await portChannel.WaitToReadAsync())
            {
                while (portChannel.TryRead(out var message))
                {
                    try
                    {
                        client = new TcpClient(target, message);
                    }
                    catch
                    {
                        continue;
                    }
                    finally
                    {
                        try
                        {
                            client.Close();
                        }
                        catch { }
                    }
                    outputWriter.TryWrite(message);
                }
            }
            client.Close();
            client.Dispose();
            outputWriter.TryComplete();
        }
示例#28
0
        public async Task ExecuteAsync_InsertAllMessagesAfter50ms_Ok()
        {
            var sqlServerMock = new Mock <ISqlServerBulk>();

            sqlServerMock.Setup(
                v => v.ExecuteAsync(It.IsAny <ICollection <YadlMessage> >(), It.IsAny <CancellationToken>()))
            .Returns(() => Task.CompletedTask);

            var options = new YadlLoggerOptions
            {
                BatchSize   = 100,
                BatchPeriod = 50
            };

            var yadlProcessor = new YadlProcessor(options);
            var hostedService = new TimedHostedService(yadlProcessor, options, sqlServerMock.Object);

            await hostedService.StartAsync(CancellationToken.None);

            for (int i = 1; i <= 15; i++)
            {
                _ = yadlProcessor !.ChannelWriter.TryWrite(new YadlMessage
                {
                    Message          = $"MSG: {i}",
                    Level            = 1,
                    LevelDescription = "Debug",
                    TimeStamp        = DateTimeOffset.Now
                });
            }

            await hostedService.StopAsync(CancellationToken.None);

            yadlProcessor.Messages.Should().BeEmpty();
        }
示例#29
0
        private async Task <int> ReadCurrentSetOfFileLinesAsync(StreamReader sr, ChannelWriter <LogDatum> writer)
        {
            string line;
            int    sessionCount = 0;
            var    count        = 0;

            while ((line = await sr.ReadLineAsync()) != null && !CancelSource.IsCancellationRequested)
            {
                _rawLineCount++;
                sessionCount++;

                if (line == String.Empty)
                {
                    continue;
                }

                count++;

                var logLine = new LogDatum(line, _rawLineCount);
                writer.TryWrite(logLine);

                // Track so we can print debug message in other channels
                _lastLineNumber = logLine.LineNumber;
            }

            if (sessionCount > 0)
            {
                WriteMessage($"Lines read: {count:N0} / {sessionCount:N0}, {_sw.Elapsed} elapsed");
            }

            return(count);
        }
示例#30
0
 public static void TrySendOrClose <T>(this ChannelWriter <T> writer, T data)
 {
     if (!writer.TryWrite(data))
     {
         writer.TryComplete();
     }
 }