Beispiel #1
0
        public Task DiscoverAsync(CancellationToken cancellationToken)
        {
            this.ConnectedMounts.Clear();

            Task.Run(async() =>
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    UdpReceiveResult receiveResult = await this._udpClient.ReceiveAsync().ConfigureAwait(false);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    IPEndPoint remoteEndPoint = receiveResult.RemoteEndPoint;

                    Debug.WriteLine(remoteEndPoint.Address);

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

                    var buffer = receiveResult.Buffer;

                    Debug.WriteLine("GOT MESSAGE");

                    Debug.WriteLine(receivedMessage);

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

                    WifiMount wifiMount = WifiMount.ToWifiMount(remoteEndPoint);

                    if (!this._discoveredMounts.Contains(wifiMount.Address))
                    {
                        this._discoveredMounts.Add(wifiMount.Address);

                        MountInfo mountInfo = new MountInfo()
                        {
                            WifiMount = wifiMount
                        };

                        Task.Run(async() => { await this.Handshake(mountInfo).ConfigureAwait(false); });
                    }

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

                    Debug.WriteLine("");
                }
            }, cancellationToken);

            return(Task.CompletedTask);
        }
        public async Task <byte[]> SendRecieveCommand(WifiMount mount, byte[] command)
        {
            int port = this._rand.Next(50101, 50199);

            Debug.WriteLine($"UDP on port {port}");

            // TODO: Implement connection pooling
            UdpClient udpClient;

            try
            {
                udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, port));
            }
            catch (SocketException)
            {
                port      = this._rand.Next(50101, 50199);
                udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, port));
            }

            await udpClient.SendAsync(command, command.Length, new IPEndPoint(IPAddress.Parse(mount.Address), mount.Port)).ConfigureAwait(false);

            UdpReceiveResult receiveResult = await udpClient.ReceiveAsync().ConfigureAwait(false);

            udpClient.Close();

            IPEndPoint remoteEndPoint = receiveResult.RemoteEndPoint;

            Debug.WriteLine(remoteEndPoint.Address);

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

            byte[] buffer = receiveResult.Buffer;

            Debug.WriteLine("GOT MESSAGE");

            Debug.WriteLine(receivedMessage);

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

            return(buffer);
        }
        public async Task SendCommand(WifiMount mount, byte[] command)
        {
            await this._udpClient.SendAsync(command, command.Length, new IPEndPoint(IPAddress.Parse(mount.Address), mount.Port)).ConfigureAwait(false);

            await Task.Delay(2000).ConfigureAwait(false);
        }