Example #1
0
        private void ThreadMain()
        {
            bytesReceived = 0;
            // this callback will kill the socket when the
            // token was canceled, which is the only way to get out
            // of the blocking udpClient.Receive()
            token.Register(() => receiverClient.Close());
            for (;;)
            {
                if (token.IsCancellationRequested)
                {
                    // ScanSyncReceiverThread got cancellation notice. Exiting cleanly.
                    break;
                }

                try
                {
                    // raw scansync packet
                    var rsp  = receiverClient.Receive(ref groupEP);
                    var pckt = new ScanSyncPacket(rsp);
                    Packets.Enqueue(pckt);
                    goodPackets++;
                    bytesReceived += rsp.Length;
                    if (counter++ == EventUpdateFrequency)
                    {
                        ScanSyncUpdate.Raise(this, new ScanSyncUpdateEvent(pckt.ScanSyncData));
                        counter = 0;
                    }
                }
                catch (ArgumentException)
                {
                    badPackets++;
                }

                catch (OperationCanceledException)
                {
                    // perfectly normal, nothing to see here
                    break;
                }

                catch (SocketException)
                {
                    // we get here if we call Close() on the UdpClient
                    // while it is in the Receive(0 call. Apparently
                    // the only way to abort a Receive call is to
                    // close the underlying Socket.
                    break;
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    // Receive failed.
                    break;
                }
            }
        }
        private void ThreadMain()
        {
            bytesReceived = 0;
            // this callback will kill the socket when the
            // token was canceled, which is the only way to get out
            // of the blocking udpClient.Receive()
            token.Register(() => receiverClient.Close());
            for (; ;)
            {
                try
                {
                    token.ThrowIfCancellationRequested();

                    // raw scansync packet
                    byte[] rsp = receiverClient.Receive(ref groupEP);
                    goodPackets++;
                    bytesReceived += rsp.Length;
                    if (counter++ == EventUpdateFrequency)
                    {
                        var pckt = new ScanSyncPacket(rsp);
                        ScanSyncUpdate.Raise(this, new ScanSyncUpdateEvent(pckt.ScanSyncData));
                        counter = 0;
                    }
                }
                catch (ArgumentException)
                {
                    badPackets++;
                }
                catch (OperationCanceledException)
                {
                    // perfectly normal, nothing to see here
                    break;
                }
                catch (SocketException)
                {
                    // we get here if we call Close() on the UdpClient
                    // while it is in the Receive(0 call. Apparently
                    // the only way to abort a Receive call is to
                    // close the underlying Socket.
                    break;
                }
                catch (Exception)
                {
                    // Receive failed.
                    break;
                }
            }
        }