Esempio n. 1
0
            public void Receive()
            {
#if XRUWP && !UNITY_EDITOR
                async void receive()
                {
                    DatagramSocket udp = new DatagramSocket();
                    await udp.BindServiceNameAsync(ip.Port.ToString());

                    udp.MessageReceived += async(s, r) =>
                    {
                        using (DataReader dr = r.GetDataReader())
                        {
                            byte[] d = new byte[dr.UnconsumedBufferLength];
                            dr.ReadBytes(d);
                            if (Net.LogEnable)
                            {
                                Debug.Log("收到" + _ip + ",udp数据长度:" + d.Length);
                            }
                            buffer.Write(d);
                            if (buffer.IsRead())
                            {
                                NetData n = XRMaker.NetData.ReadBuffer(buffer.Read());
                                n.RemoteIP = _ip;
                                Net.NetData.Add(n);
                            }
                        }
                    };
                }

                receive();
#else
                receive = new Thread(() =>
                {
                    EndPoint _ip = new IPEndPoint(IPAddress.Any, 0);
                    byte[] d     = new byte[4096];
                    while (true)
                    {
                        try
                        {
                            int l = udp.ReceiveFrom(d, ref _ip);
                            if (LogEnable)
                            {
                                Debug.Log("收到" + _ip + ",udp数据长度:" + l);
                            }
                            if (l < d.Length)
                            {
                                byte[] _d = new byte[l];
                                Array.Copy(d, 0, _d, 0, l);
                                buffer.Write(_d);
                            }
                            else
                            {
                                buffer.Write(d);
                            }
                            if (buffer.IsRead)
                            {
                                NetData n  = NetData.ReadBuffer(buffer.Read());
                                n.RemoteIP = (IPEndPoint)_ip;
                                Net.NetDatas.Add(n);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.LogError("udpInfo:" + e.Message.ToString());
                        }
                    }
                });
                receive.Start();
#endif
            }