예제 #1
0
        public static void OnDataReadCompletion(uint bytesRead, DataReader readPacket)
        {
            try
            {
                if (readPacket == null)
                {
                    Debug.WriteLine("DataReader is null");
                    return;
                }

                uint buffLen = readPacket.UnconsumedBufferLength;

                if (buffLen == 0)
                {
                    Debug.WriteLine("Buffer is empty");
                    return;
                }

                Debug.WriteLine($"Network Received (b={bytesRead},l={buffLen})");

                List <byte> bytes = new List <byte>();
                while (buffLen > 0)
                {
                    byte b = readPacket.ReadByte();
                    bytes.Add(b);
                    buffLen--;
                }

                lastCmdReceived = MainPage.stopwatch.ElapsedMilliseconds;

                if (App.isRPi)
                {
                    MultiWii.sendRequestMSP(bytes);
                    PostSocketRead(readBuff);
                }
                else
                {
                    MultiWii.evaluateResponseMSP(bytes);
                    PostSocketRead(readBuff);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnDataReadCompletion() - " + ex.Message);
            }
        }
예제 #2
0
        private static async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            try
            {
                if (App.isRPi && peer == null)
                {
                    IOutputStream outputStream = await sender.GetOutputStreamAsync(args.RemoteAddress, args.RemotePort);

                    // It might happen that the OnMessage was invoked more than once before the GetOutputStreamAsync call
                    // completed. In this case we will end up with multiple streams - just keep one of them.
                    object syncRoot = new object();
                    lock (syncRoot)
                    {
                        peer = new RemotePeer(outputStream, args.RemoteAddress, args.RemotePort);
                    }

                    socketIsConnected = true;
                }

                var readPacket = args.GetDataReader();

                if (readPacket == null)
                {
                    Debug.WriteLine("DataReader is null");
                    return;
                }

                uint buffLen = readPacket.UnconsumedBufferLength;

                if (buffLen == 0)
                {
                    Debug.WriteLine("Buffer is empty");
                    return;
                }

                List <byte> bytes = new List <byte>();
                while (buffLen > 0)
                {
                    byte b = readPacket.ReadByte();
                    bytes.Add(b);
                    buffLen--;
                }

                lastCmdReceived = MainPage.stopwatch.ElapsedMilliseconds;

                if (App.isRPi)
                {
                    if (bytes[0] == '#')
                    {
                        MultiWii.evaluateCustomCommand(bytes);
                    }
                    else
                    {
                        MultiWii.sendRequestMSP(bytes);
                    }
                }
                else
                {
                    if (bytes[0] == '#')
                    {
                        MultiWii.evaluateCustomCommand(bytes);
                    }
                    else
                    {
                        MultiWii.evaluateResponseMSP(bytes);
                    }

                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        var currentPage     = ((ContentControl)Window.Current.Content).Content as Page;
                        var droneStatus     = currentPage.FindName("droneStatus") as Image;
                        droneStatus.Opacity = 1;
                    });
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnConnection() - " + ex.Message);
            }
        }
예제 #3
0
        private static async void MessageWebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            try
            {
                var readPacket = args.GetDataReader();

                if (readPacket == null)
                {
                    Debug.WriteLine("DataReader is null");
                    return;
                }

                uint buffLen = readPacket.UnconsumedBufferLength;

                if (buffLen == 0)
                {
                    Debug.WriteLine("Buffer is empty");
                    return;
                }

                List <byte> bytes = new List <byte>();
                while (buffLen > 0)
                {
                    byte b = readPacket.ReadByte();
                    bytes.Add(b);
                    buffLen--;
                }

                lastCmdReceived = MainPage.stopwatch.ElapsedMilliseconds;

                if (App.isRPi)
                {
                    if (bytes[0] == '#')
                    {
                        MultiWii.evaluateCustomCommand(bytes);
                    }
                    else
                    {
                        MultiWii.sendRequestMSP(bytes);
                    }
                }
                else
                {
                    if (bytes[0] == '#')
                    {
                        MultiWii.evaluateCustomCommand(bytes);
                    }
                    else
                    {
                        MultiWii.evaluateResponseMSP(bytes);
                    }

                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        var currentPage     = ((ContentControl)Window.Current.Content).Content as Page;
                        var droneStatus     = currentPage.FindName("droneStatus") as Image;
                        droneStatus.Opacity = 1;
                    });
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("MessageWebSocket_MessageReceived() - " + ex.Message);
                HandleException(ex.Message);
            }
        }