Пример #1
0
        private void udpCatchingJob_DoWork(object sender, DoWorkEventArgs e)
        {
            UdpClient        bcstUDPreader    = new UdpClient(portBcstRecvng, AddressFamily.InterNetwork);
            BackgroundWorker SelfWorker       = sender as BackgroundWorker;
            IPEndPoint       RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            UdpState         s = new UdpState();

            s.ipEndPoint = RemoteIpEndPoint;
            s.UDPclient  = bcstUDPreader;

            while (true)
            {
                if (SelfWorker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                if (recievingUDPmessage)
                {
                    Application.DoEvents();
                    Thread.Sleep(0);
                    continue;
                }

                bcstMessageReceived = false;
                recievingUDPmessage = true;
                bcstUDPreader.BeginReceive(bcstReceiveCallback, s);
            }

            bcstUDPreader.Close();
        }
Пример #2
0
        public static void bcstReceiveCallback(IAsyncResult ar)
        {
            string     bcstMessage = "";
            UdpState   udpSt       = (UdpState)(ar.AsyncState);
            UdpClient  udpClt      = (UdpClient)(udpSt.UDPclient);
            IPEndPoint ipEP        = (IPEndPoint)(udpSt.ipEndPoint);

            remoteSktAddr    = PropertyHelper.GetPrivatePropertyValue <SocketAddress>((object)ar, "SocketAddress");
            udpSt.sktAddress = remoteSktAddr;

            if (udpClt.Client != null)
            {
                try
                {
                    Byte[] receiveBytes = udpClt.EndReceive(ar, ref ipEP);
                    recievingUDPmessage = false;

                    bcstMessage = Encoding.ASCII.GetString(receiveBytes);

                    if (bcstMessage != "")
                    {
                        cquArduinoUDPCatchedMessages.Enqueue(new IncomingUDPmessageBoundle(remoteSktAddr, bcstMessage));

                        //bcstMessageReceived = true;
                    }
                }
                catch (Exception exc)
                {
                    bcstMessageReceived = false;
                    recievingUDPmessage = false;
                    bcstMessage         = exc.Message;
                }
                udpSt.udpMessage = bcstMessage;
            }
            else
            {
                bcstMessageReceived = false;
                recievingUDPmessage = false;
            }
        }