Exemplo n.º 1
0
        private void ListenToSocket(IUdpSocket socket)
        {
            // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings.
            var t = Task.Run(async() =>
            {
                var cancelled = false;
                while (!cancelled)
                {
                    try
                    {
                        var result = await socket.ReceiveAsync().ConfigureAwait(false);

                        if (result.ReceivedBytes > 0)
                        {
                            // Strange cannot convert compiler error here if I don't explicitly
                            // assign or cast to Action first. Assignment is easier to read,
                            // so went with that.
                            ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.RemoteEndPoint, result.LocalIPAddress);
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        cancelled = true;
                    }
                    catch (TaskCanceledException)
                    {
                        cancelled = true;
                    }
                }
            });
        }
Exemplo n.º 2
0
        private async void ListenToSocket(IUdpSocket socket)
        {
            // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings.
            try
            {
                await TaskEx.Run(async() =>
                {
                    var cancelled = false;
                    while (!cancelled)
                    {
                        try
                        {
                            var result = await socket.ReceiveAsync().ConfigureAwait(false);

                            if (result.ReceivedBytes > 0)
                            {
                                // Strange cannot convert compiler error here if I don't explicitly
                                // assign or cast to Action first. Assignment is easier to read,
                                // so went with that.
                                Action processWork = () => ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.ReceivedFrom);
                                var processTask    = TaskEx.Run(processWork);
                            }
                        }
                        catch (SocketClosedException)
                        {
                            if (this.IsDisposed)
                            {
                                return;                                              //No error or reconnect if we're shutdown.
                            }
                            await ReconnectBroadcastListeningSocket();
                            cancelled = true;
                            break;
                        }
                        catch (ObjectDisposedException)
                        {
                            cancelled = true;
                        }
                        catch (TaskCanceledException)
                        {
                            cancelled = true;
                        }
                    }
                });
            }
            catch
            {
                if (this.IsDisposed)
                {
                    return;
                }

                await ReconnectBroadcastListeningSocket();
            }
        }
Exemplo n.º 3
0
        private async void StartListening()
        {
            while (!_isDisposed)
            {
                try
                {
                    var result = await _udpClient.ReceiveAsync().ConfigureAwait(false);

                    OnMessageReceived(result);
                }
                catch (ObjectDisposedException)
                {
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error receiving udp message", ex);
                }
            }
        }
Exemplo n.º 4
0
        private void ListenToSocket(IUdpSocket socket)
        {
            // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings.
            var t = TaskEx.Run(async () =>
            {

                var cancelled = false;
                while (!cancelled)
                {
                    try
                    {
                        var result = await socket.ReceiveAsync();

                        if (result.ReceivedBytes > 0)
                        {
                            // Strange cannot convert compiler error here if I don't explicitly
                            // assign or cast to Action first. Assignment is easier to read,
                            // so went with that.
                            Action processWork = () => ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.ReceivedFrom);
                            var processTask = TaskEx.Run(processWork);
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        cancelled = true;
                    }
                    catch (TaskCanceledException)
                    {
                        cancelled = true;
                    }
                }
            });
        }