Ejemplo n.º 1
0
        public void doWork()
        {
            while (_KeepRunning)
            {
                //  D("UDP receive loop");
                if (!_HasInit)
                {
                    doInitUdp();
                }
                if (!_HasError)
                {
                    try
                    {
                        if (_InitOkIndex++ == 0)
                        {
                            IPEndPoint tIPendPoint = new IPEndPoint(IPAddress.Any, 0);
                            BeatHeatStatusChangeHandler?.Invoke(CONNECT_STATUS.DISCOVERY_INIT_OK, tIPendPoint);
                        }
                        // Blocks until a message returns on this socket from a remote host.
                        // if (_UdpClient.Available > 0)
                        //  {
                        _RecevBytes = _UdpClient.Receive(ref _RemoteIpEndPoint);
                        // }
                    }
                    catch (Exception e)
                    {
                        D("receive exception : " + e.ToString());
                        _HasError = true;
                    }
                }
                //D("" + mRemoteIpEndPoint.Address.ToString()  + ":" + mRemoteIpEndPoint.Port.ToString());
                if ((!_HasInit || _HasError) && _KeepRunning)
                {
                    Thread.Sleep(3000);//wait a moment.

                    try
                    {
                        _UdpClient?.Close();
                    }
                    catch { }

                    _HasInit = false;
                }

                if (!_HasError)
                {
                    ParseUdpPkg(_RecevBytes, _RemoteIpEndPoint);
                }
            }
        }
Ejemplo n.º 2
0
 private void doInitUdp()
 {
     try
     {
         _UdpClient        = new UdpClient(_UdpListenPort);
         _RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
         _HasInit          = true;
         _HasError         = false;
     }
     catch (Exception e)
     {
         _HasError = true;
         _UdpClient.Close();
         D("exceptio e " + e.ToString());
         BeatHeatStatusChangeHandler?.Invoke(CONNECT_STATUS.DISCOVERY_INIT_ERROR, new IPEndPoint(IPAddress.Any, 0));
     }
 }
Ejemplo n.º 3
0
        //        {
        //    "msgtype": "broadcast",
        //    "promt": "testpcb",
        //    "index": 1,
        //    "ip": "192.168.31.128",
        //    "udpport": 28000,
        //    "tcpport": 28001
        //}
        // {"msgtype":"Test", "gogo":"98"}
        private void ParseUdpPkg(byte[] msg, IPEndPoint iep)
        {
            if (msg == null || msg.Length < 2)
            {
                return;
            }
            string msgstring = Encoding.Default.GetString(msg);

            //       D("udp receiv: " + msgstring);
            //         D("udp endpoint " + iep.Address + " port:" + iep.Port);
            try
            {
                JObject obj     = JObject.Parse(msgstring);
                string  msgtype = obj["msgtype"].ToString();
                //D("the msgtype is " + msgtype);

                switch (msgtype)
                {
                case "broadcast":
                    int        port   = ProgramInfo.PCB_TCP_SERVER_LISTEN_PORT;
                    string     ipaddr = iep.Address.ToString();
                    IPEndPoint pcbtcp = new IPEndPoint(IPAddress.Parse(ipaddr), port);
                    if (_PcbTcpServerEndPoint == null || (pcbtcp.Address != _PcbTcpServerEndPoint.Address && pcbtcp.Port != _PcbTcpServerEndPoint.Port))
                    {
                        D("get broadcast : pcbTcpEndPoint is " + pcbtcp.Address + ":" + pcbtcp.Port);
                        _PcbTcpServerEndPoint = pcbtcp;
                        D("set mPcbTcp ");
                        ProgramInfo.PCBIPAddrEndPoint = _PcbTcpServerEndPoint;
                        BeatHeatStatusChangeHandler?.Invoke(CONNECT_STATUS.DISCOVERY_GET_PCB, _PcbTcpServerEndPoint);
                    }
                    break;

                default:
                    break;
                }
            }
            catch (JsonException je)
            {
                D("jsonexception " + je.ToString());
            }
        }