示例#1
0
 /// <summary>
 /// 关闭socket
 /// </summary>
 protected void CloseWifi()
 {
     if (this.m_ClientSocket != null)
     {
         this.m_ClientSocket.Disconnect();
         this.m_ClientSocket.ReceiveData -= new MessaegeHandler(this.ReceiveWeightData);
         this.m_ClientSocket              = (WifiClient)null;
     }
 }
示例#2
0
 /// <summary>
 /// 打开socket
 /// </summary>
 /// <param name="ipAddress"></param>
 private void OpenWifi(string ipAddress)
 {
     if (this.m_ClientSocket != null && this.m_ClientSocket.IsConnected())
     {
         return;
     }
     this.m_ClientSocket = new WifiClient(ipAddress, "60000");
     this.m_ClientSocket.Connect();
     this.m_ClientSocket.ReceiveData += new MessaegeHandler(this.ReceiveWeightData);
 }
示例#3
0
        public void Listen(CancellationToken cancellationToken)
        {
            Task.Run(async() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    UdpReceiveResult receiveResult = await this._udpClient.ReceiveAsync().ConfigureAwait(false);

                    IPEndPoint remoteEndPoint = receiveResult.RemoteEndPoint;

                    var buffer = receiveResult.Buffer.ToList();

                    if (buffer[0] == 13)
                    {
                        buffer.RemoveAt(0);
                    }

                    if (buffer.SequenceEqual(WifiConstants.CMSG_DISCOVERY_DATAGRAM))
                    {
                        Debug.WriteLine("<<<CMSG_DISCOVERY_DATAGRAM---");

                        Debug.WriteLine("Response");

                        var responseMessage = BitConverter.ToString(WifiConstants.SMSG_DISCOVERY_DATAGRAM);

                        Debug.WriteLine(responseMessage);

                        await this._udpClient.SendAsync(WifiConstants.SMSG_DISCOVERY_DATAGRAM, WifiConstants.SMSG_DISCOVERY_DATAGRAM.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                        Debug.WriteLine(">>>SMSG_DISCOVERY_DATAGRAM---");
                    }
                    else if (buffer.SequenceEqual(WifiConstants.CMSG_DISCOVERY_DATAGRAM2))
                    {
                        Debug.WriteLine("<<<CMSG_DISCOVERY_DATAGRAM2---");

                        Debug.WriteLine("Response");

                        var responseMessage = BitConverter.ToString(WifiConstants.SMSG_DISCOVERY_DATAGRAM2);

                        Debug.WriteLine(responseMessage);

                        await this._udpClient.SendAsync(WifiConstants.SMSG_DISCOVERY_DATAGRAM2, WifiConstants.SMSG_DISCOVERY_DATAGRAM2.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                        Debug.WriteLine(">>>SMSG_DISCOVERY_DATAGRAM2---");
                    }
                    else if (buffer.SequenceEqual(WifiConstants.CMSG_DISCOVERY_DATAGRAM3))
                    {
                        Debug.WriteLine("<<<CMSG_DISCOVERY_DATAGRAM3---");

                        Debug.WriteLine("Response");

                        var responseMessage = BitConverter.ToString(WifiConstants.SMSG_DISCOVERY_DATAGRAM3);

                        Debug.WriteLine(responseMessage);

                        await this._udpClient.SendAsync(WifiConstants.SMSG_DISCOVERY_DATAGRAM3, WifiConstants.SMSG_DISCOVERY_DATAGRAM3.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                        Debug.WriteLine(">>>SMSG_DISCOVERY_DATAGRAM3---");
                    }
                    else if (buffer.SequenceEqual(WifiConstants.CMSG_DISCOVERY_DATAGRAM4))
                    {
                        Debug.WriteLine("<<<CMSG_DISCOVERY_DATAGRAM4---");

                        Debug.WriteLine("Response");

                        var responseMessage = BitConverter.ToString(WifiConstants.SMSG_DISCOVERY_DATAGRAM4);

                        Debug.WriteLine(responseMessage);

                        await this._udpClient.SendAsync(WifiConstants.SMSG_DISCOVERY_DATAGRAM4, WifiConstants.SMSG_DISCOVERY_DATAGRAM4.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                        Debug.WriteLine(">>>SMSG_DISCOVERY_DATAGRAM4---");
                    }
                    else if (buffer.SequenceEqual(WifiConstants.CMSG_START_DATAGRAM))
                    {
                        Debug.WriteLine("<<<CMSG_START_DATAGRAM---");

                        //Debug.WriteLine("Response");

                        //var responseMessage = BitConverter.ToString(WifiConstants.SMSG_FINSHED_DATAGRAM);

                        //Debug.WriteLine(responseMessage);

                        //await this._udpClient.SendAsync(WifiConstants.SMSG_FINSHED_DATAGRAM, WifiConstants.SMSG_FINSHED_DATAGRAM.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                        //Debug.WriteLine(">>>SMSG_FINSHED_DATAGRAM---");
                    }
                    else if (buffer.SequenceEqual(WifiConstants.CMSG_DISCOVERY_DATAGRAM5))
                    {
                        Debug.WriteLine("<<<CMSG_DISCOVERY_DATAGRAM5---");

                        //Debug.WriteLine("Response");

                        //var responseMessage = BitConverter.ToString(WifiConstants.SMSG_FINSHED_DATAGRAM);

                        //Debug.WriteLine(responseMessage);

                        //await this._udpClient.SendAsync(WifiConstants.SMSG_FINSHED_DATAGRAM, WifiConstants.SMSG_FINSHED_DATAGRAM.Length, receiveResult.RemoteEndPoint).ConfigureAwait(false);
                    }
                    else
                    {
                        Debug.WriteLine("!!!NEW PACKET!!!");
                    }

                    Debug.WriteLine(remoteEndPoint);

                    var receivedMessage = BitConverter.ToString(receiveResult.Buffer);

                    Debug.WriteLine(receivedMessage);

                    Array.ForEach(buffer.ToArray(), b => Debug.Write($"{b}, "));

                    Debug.WriteLine("");

                    WifiClient wifiClient = WifiClient.ToWifiClient(remoteEndPoint);

                    if (this.DeviceIpEndPoints.FirstOrDefault(d => d.Address == wifiClient.Address) == null)
                    {
                        this.DeviceIpEndPoints.Add(wifiClient);
                    }
                }
            }, cancellationToken);
        }