Example #1
0
 public static async void StartListening()
 {
     if (DatagramSocket != null)
     {
         await DatagramSocket.CancelIOAsync();
     }
     DatagramSocket = new DatagramSocket();
     DatagramSocket.MessageReceived += async(sender, args) =>
     {
         await OnReceived(args.GetDataStream(), args.RemoteAddress);
     };
     await DatagramSocket.BindServiceNameAsync(DeviceUnicastPort.ToString());
 }
    async void udpMessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
    {
        if (!targetIPReady && !connected && !trashUDP)
        {
            trashUDP = true;
            DataReader reader     = args.GetDataReader();
            uint       len        = reader.UnconsumedBufferLength;
            string     msg        = reader.ReadString(len);
            string     remoteHost = args.RemoteAddress.DisplayName;
            targetIP      = msg;
            targetIPReady = true;
            textOut("" + msg);
            await listenerSocket.CancelIOAsync();

            listenerSocket.MessageReceived -= udpMessageReceived;
            listenerSocket.Dispose();
            listenerSocket = null;//new since working



            //socket.Dispose();

            /* //exception smashing test
             * reader.Dispose();
             *
             * await Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
             * {
             *  targetIP = msg;
             *  targetIPReady = true;
             *  textOut("UDP Set up. "+targetIP+" "+targetIPReady);
             * });
             */
        }
    }
Example #3
0
        public async Task StopListener()
        {
            datagramSocket.MessageReceived -= DatagramSocket_MessageReceived;
            await datagramSocket.CancelIOAsync();

            datagramSocket = null;
            await BackgroundTaskService.Unregister <BroadcastListenerBackgroundTask>();
        }
Example #4
0
        public async Task TransferOwnership()
        {
            if (_datagramSocket != null)
            {
                await _datagramSocket.CancelIOAsync();

                _datagramSocket.TransferOwnership("UserDatagramSocket");
            }
        }
Example #5
0
        public async Task TransferOwnership()
        {
            socket.MessageReceived -= OnMessageReceived;
            await socket.CancelIOAsync();

            IBuffer data = information.Serialize();
            SocketActivityContext context = new SocketActivityContext(data);

            socket.TransferOwnership(nameof(MulticastServer), context);
            socket = null;
        }
Example #6
0
    public async void StopListening()
    {
        IsListening = false;

        if (socket != null)
        {
            await socket.CancelIOAsync();

            socket.Dispose();
        }

        socket = null;
    }
Example #7
0
        async void ShutDownClient()
        {
            try
            {
                await _udpClient.CancelIOAsync();

                _udpClient.Dispose();
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.Log(ex);
            }
        }
        private async void Search(string st, TimeSpan?timeout = null)
        {
            Log("Search - " + st);

            var ssdp_data = new StringBuilder()
                            .Append("M-SEARCH * HTTP/1.1").Append("\r\n")
                            .Append("HOST: ").Append(MULTICAST_ADDRESS).Append(":").Append(SSDP_PORT.ToString()).Append("\r\n")
                            .Append("MAN: ").Append("\"ssdp:discover\"").Append("\r\n")
                            .Append("MX: ").Append(MX.ToString()).Append("\r\n")
                            .Append("ST: ").Append(st).Append("\r\n")
                            .Append("\r\n")
                            .ToString();

            var adapters = TargetNetworkAdapters ?? await GetActiveAdaptersAsync().ConfigureAwait(false);

            await Task.WhenAll(adapters.Select(async adapter =>
            {
                using (var socket = new DatagramSocket())
                {
                    socket.Control.DontFragment = true;
                    socket.MessageReceived     += OnDatagramSocketMessageReceived;

                    try
                    {
                        await socket.BindServiceNameAsync("", adapter);

                        using (var output = await socket.GetOutputStreamAsync(MULTICAST_HOST, SSDP_PORT.ToString()))
                        {
                            using (var writer = new DataWriter(output))
                            {
                                writer.WriteString(ssdp_data);
                                await writer.StoreAsync();
                            }
                        }

                        await Task.Delay(timeout ?? DEFAULT_TIMEOUT).ConfigureAwait(false);
                        Log("Search Timeout");
                        await socket.CancelIOAsync();
                    }
                    catch (Exception e)
                    {
                        Log("Failed to send multicast: " + e.StackTrace);
                    }

                    socket.MessageReceived -= OnDatagramSocketMessageReceived;
                }
            })).ConfigureAwait(false);

            Finished?.Invoke(this, new EventArgs());
        }
Example #9
0
            public override void Close()
            {
                if (udp != null)
                {
#if XRUWP && !UNITY_EDITOR
                    await udp.CancelIOAsync();

                    udp.Dispose();
#else
                    receive.Abort();
                    udp.Close();
#endif
                    udp = null;
                    base.Close();
                }
            }
Example #10
0
    async void udpMessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
    {
        if (!targetIPReady && !connected && !trashUDP)
        {
            trashUDP = true;
            DataReader reader     = args.GetDataReader();
            uint       len        = reader.UnconsumedBufferLength;
            string     msg        = reader.ReadString(len);
            string     remoteHost = args.RemoteAddress.DisplayName;
            targetIP      = msg;
            targetIPReady = true;
            await listenerSocket.CancelIOAsync();

            listenerSocket.MessageReceived -= udpMessageReceived;
            listenerSocket.Dispose();
            listenerSocket = null;
        }
    }
Example #11
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            if (_container == null)
            {
                _container = new UnityContainer();
                _container.RegisterType <IStorageFactory, StorageFactory>();
                _container.RegisterFactory <IStorage>((c) => c.Resolve <IStorageFactory>().Create(), FactoryLifetime.Singleton);
                _container.RegisterType <IChecklistsFactory, ChecklistsFactory>();
                _container.RegisterFactory <IChecklists>((c) => c.Resolve <IChecklistsFactory>().Create(), FactoryLifetime.Singleton);
                _container.RegisterType <ILiveTileFactory, LiveTileFactory>();
                _container.RegisterFactory <ILiveTile>((c) => c.Resolve <ILiveTileFactory>().Create(), FactoryLifetime.Singleton);
            }
            var checklists         = _container.Resolve <IChecklists>();
            var liveTile           = _container.Resolve <ILiveTile>();
            var backgroundWorkCost = BackgroundWorkCost.CurrentBackgroundWorkCost;

            if (backgroundWorkCost == BackgroundWorkCostValue.High)
            {
                return;
            }
            else
            {
                var deferral = taskInstance.GetDeferral();
                try
                {
                    var details           = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
                    var socketInformation = details.SocketInformation;
                    switch (details.Reason)
                    {
                    case SocketActivityTriggerReason.SocketActivity:
                        var datagramSocket = socketInformation.DatagramSocket;
                        datagramSocket.MessageReceived += async(s, e) =>
                        {
                            var dataReader   = e.GetDataReader();
                            var defconStatus = dataReader.ReadString(dataReader.UnconsumedBufferLength);
                            int.TryParse(defconStatus, out int parsedDefconStatus);
                            if (parsedDefconStatus > 0 && parsedDefconStatus < 6)
                            {
                                ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;
                                int savedDefconStatus = 0;
                                if (roamingSettings.Values.ContainsKey("defconStatus"))
                                {
                                    savedDefconStatus = Convert.ToInt16(roamingSettings.Values["defconStatus"].ToString());
                                }
                                if (parsedDefconStatus != savedDefconStatus)
                                {
                                    roamingSettings.Values["defconStatus"] = defconStatus;
                                    ShowToast("DEFCON " + defconStatus);
                                    liveTile.DefconTile.SetTile(int.Parse(defconStatus));
                                }
                            }
                            if (parsedDefconStatus == 0)
                            {
                                try
                                {
                                    using (var streamSocket = new StreamSocket())
                                    {
                                        await streamSocket.ConnectAsync(e.RemoteAddress, "4537");

                                        string response = String.Empty;
                                        using (Stream inputStream = streamSocket.InputStream.AsStreamForRead())
                                        {
                                            using (StreamReader streamReader = new StreamReader(inputStream))
                                            {
                                                response = await streamReader.ReadLineAsync();

                                                var checkListItems        = JsonConvert.DeserializeObject <List <CheckListItem> >(response);
                                                var defcon1CheckListItems = checklists.Collection.Defcon1Checklist;
                                                var defcon2CheckListItems = checklists.Collection.Defcon2Checklist;
                                                var defcon3CheckListItems = checklists.Collection.Defcon3Checklist;
                                                var defcon4CheckListItems = checklists.Collection.Defcon4Checklist;
                                                var defcon5CheckListItems = checklists.Collection.Defcon5Checklist;
                                                foreach (var item in checkListItems)
                                                {
                                                    bool itemFound = false;
                                                    if (item.DefconStatus == 1)
                                                    {
                                                        for (int i = 0; i < defcon1CheckListItems.Count; i++)
                                                        {
                                                            if (defcon1CheckListItems[i].UnixTimeStampCreated == item.UnixTimeStampCreated)
                                                            {
                                                                itemFound = true;
                                                                if (defcon1CheckListItems[i].Deleted != item.Deleted)
                                                                {
                                                                    defcon1CheckListItems[i].Deleted    = true;
                                                                    defcon1CheckListItems[i].Visibility = Visibility.Collapsed;
                                                                }
                                                                else if (defcon1CheckListItems[i].UnixTimeStampUpdated < item.UnixTimeStampUpdated)
                                                                {
                                                                    defcon1CheckListItems[i].Item    = item.Item;
                                                                    defcon1CheckListItems[i].Checked = item.Checked;
                                                                }
                                                            }
                                                        }
                                                        if (!itemFound)
                                                        {
                                                            defcon1CheckListItems.Add(item);
                                                        }
                                                    }
                                                    else if (item.DefconStatus == 2)
                                                    {
                                                        for (int i = 0; i < defcon2CheckListItems.Count; i++)
                                                        {
                                                            if (defcon2CheckListItems[i].UnixTimeStampCreated == item.UnixTimeStampCreated)
                                                            {
                                                                itemFound = true;
                                                                if (defcon2CheckListItems[i].Deleted != item.Deleted)
                                                                {
                                                                    defcon2CheckListItems[i].Deleted    = true;
                                                                    defcon2CheckListItems[i].Visibility = Visibility.Collapsed;
                                                                }
                                                                else if (defcon2CheckListItems[i].UnixTimeStampUpdated < item.UnixTimeStampUpdated)
                                                                {
                                                                    defcon2CheckListItems[i].Item    = item.Item;
                                                                    defcon2CheckListItems[i].Checked = item.Checked;
                                                                }
                                                            }
                                                        }
                                                        if (!itemFound)
                                                        {
                                                            defcon2CheckListItems.Add(item);
                                                        }
                                                    }
                                                    else if (item.DefconStatus == 3)
                                                    {
                                                        for (int i = 0; i < defcon3CheckListItems.Count; i++)
                                                        {
                                                            if (defcon3CheckListItems[i].UnixTimeStampCreated == item.UnixTimeStampCreated)
                                                            {
                                                                itemFound = true;
                                                                if (defcon3CheckListItems[i].Deleted != item.Deleted)
                                                                {
                                                                    defcon3CheckListItems[i].Deleted    = true;
                                                                    defcon3CheckListItems[i].Visibility = Visibility.Collapsed;
                                                                }
                                                                else if (defcon3CheckListItems[i].UnixTimeStampUpdated < item.UnixTimeStampUpdated)
                                                                {
                                                                    defcon3CheckListItems[i].Item    = item.Item;
                                                                    defcon3CheckListItems[i].Checked = item.Checked;
                                                                }
                                                            }
                                                        }
                                                        if (!itemFound)
                                                        {
                                                            defcon3CheckListItems.Add(item);
                                                        }
                                                    }
                                                    else if (item.DefconStatus == 4)
                                                    {
                                                        for (int i = 0; i < defcon4CheckListItems.Count; i++)
                                                        {
                                                            if (defcon4CheckListItems[i].UnixTimeStampCreated == item.UnixTimeStampCreated)
                                                            {
                                                                itemFound = true;
                                                                if (defcon4CheckListItems[i].Deleted != item.Deleted)
                                                                {
                                                                    defcon4CheckListItems[i].Deleted    = true;
                                                                    defcon4CheckListItems[i].Visibility = Visibility.Collapsed;
                                                                }
                                                                else if (defcon4CheckListItems[i].UnixTimeStampUpdated < item.UnixTimeStampUpdated)
                                                                {
                                                                    defcon4CheckListItems[i].Item    = item.Item;
                                                                    defcon4CheckListItems[i].Checked = item.Checked;
                                                                }
                                                            }
                                                        }
                                                        if (!itemFound)
                                                        {
                                                            defcon4CheckListItems.Add(item);
                                                        }
                                                    }
                                                    else if (item.DefconStatus == 5)
                                                    {
                                                        for (int i = 0; i < defcon5CheckListItems.Count; i++)
                                                        {
                                                            if (defcon5CheckListItems[i].UnixTimeStampCreated == item.UnixTimeStampCreated)
                                                            {
                                                                itemFound = true;
                                                                if (defcon5CheckListItems[i].Deleted != item.Deleted)
                                                                {
                                                                    defcon5CheckListItems[i].Deleted    = true;
                                                                    defcon5CheckListItems[i].Visibility = Visibility.Collapsed;
                                                                }
                                                                else if (defcon5CheckListItems[i].UnixTimeStampUpdated < item.UnixTimeStampUpdated)
                                                                {
                                                                    defcon5CheckListItems[i].Item    = item.Item;
                                                                    defcon5CheckListItems[i].Checked = item.Checked;
                                                                }
                                                            }
                                                        }
                                                        if (!itemFound)
                                                        {
                                                            defcon5CheckListItems.Add(item);
                                                        }
                                                    }
                                                }
                                                await checklists.Operations.SaveCheckList(defcon1CheckListItems, 1);

                                                await checklists.Operations.SaveCheckList(defcon2CheckListItems, 2);

                                                await checklists.Operations.SaveCheckList(defcon3CheckListItems, 3);

                                                await checklists.Operations.SaveCheckList(defcon4CheckListItems, 4);

                                                await checklists.Operations.SaveCheckList(defcon5CheckListItems, 5);
                                            }
                                        }
                                        await streamSocket.CancelIOAsync();

                                        streamSocket.Dispose();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    SocketErrorStatus webErrorStatus = SocketError.GetStatus(ex.GetBaseException().HResult);
                                }
                            }
                        };
                        await datagramSocket.CancelIOAsync();

                        datagramSocket.TransferOwnership("myDefconDatagramSocket");
                        break;

                    case SocketActivityTriggerReason.SocketClosed:
                        DatagramSocket newDatagramSocket = new DatagramSocket();
                        newDatagramSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await newDatagramSocket.BindServiceNameAsync("4536");

                        await newDatagramSocket.CancelIOAsync();

                        newDatagramSocket.TransferOwnership("myDefconDatagramSocket");
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        DatagramSocket newDatagramSocket = new DatagramSocket();
                        newDatagramSocket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
                        await newDatagramSocket.BindServiceNameAsync("4536");

                        await newDatagramSocket.CancelIOAsync();

                        newDatagramSocket.TransferOwnership("myDefconDatagramSocket");
                    }
                    catch (Exception) { }
                }
                deferral.Complete();
            }
        }
Example #12
0
        public async Task TransferOwnership()
        {
            await datagramSocket?.CancelIOAsync();

            datagramSocket?.TransferOwnership("myDefconDatagramSocket");
        }