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); } }
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); } }