Exemplo n.º 1
0
        private async Task WatchForOutboundMessages(INetworkClient currentClient)
        {
            var token       = currentClient.CancellationTokenSource.Token;
            var taskStarted = false;
            var task        = Task.Run(async() =>
            {
                try
                {
                    token.ThrowIfCancellationRequested();
                    var stream  = currentClient.GetCommunicationStream();
                    taskStarted = true;
                    while (true)
                    {
                        while (currentClient.OutboundMessages.Count == 0)
                        {
                            if (token.IsCancellationRequested)
                            {
                                token.ThrowIfCancellationRequested();
                            }

                            await Task.Delay(1, token);
                        }

                        INetworkMessage newMessage;
                        if (currentClient.OutboundMessages.TryDequeue(out newMessage))
                        {
                            var messageBytes = newMessage.GetMessageBytes();
                            stream.Write(messageBytes, 0, messageBytes.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }, currentClient.CancellationTokenSource.Token);

            currentClient.WatchForOutboundMessagesTask = task;

            while (!taskStarted)
            {
                await Task.Delay(1, token);
            }
        }
Exemplo n.º 2
0
        private async Task WatchForInboundNetworkTraffic(INetworkClient currentClient)
        {
            var token       = currentClient.CancellationTokenSource.Token;
            var taskStarted = false;
            var task        = Task.Run(async() =>
            {
                try
                {
                    token.ThrowIfCancellationRequested();
                    var stream  = currentClient.GetCommunicationStream();
                    taskStarted = true;
                    while (true)
                    {
                        while (!stream.DataAvailable)
                        {
                            if (token.IsCancellationRequested)
                            {
                                token.ThrowIfCancellationRequested();
                            }

                            await Task.Delay(1, token);
                        }

                        var message = MessageFactory.ReadMessage(stream);
                        currentClient.InboundMessages.Enqueue(message);
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }, currentClient.CancellationTokenSource.Token);

            currentClient.WatchForInboundNetworkTrafficTask = task;

            while (!taskStarted)
            {
                await Task.Delay(1, token);
            }
        }