Exemple #1
0
 public override void Dispose()
 {
     try
     {
         udpReceiver.Dispose();
         subscriberUpdReceiver.Dispose();
     }
     catch (Exception)
     {
     }
 }
        private async Task <IObservable <IHttpRequestReponse> > GetUdpRequestResponseObservable(
            int port,
            ICommunicationInterface communicationInterface = null,
            bool allowMultipleBindToSamePort = false)
        {
            IUdpSocketReceiver udpListener = new UdpSocketReceiver();

            var observeUdpRequest = await udpListener.ObservableUnicastListener(
                port,
                communicationInterface,
                allowMultipleBindToSamePort);

            var observable = Observable.Create <IHttpRequestReponse>(
                obs =>
            {
                var disp = observeUdpRequest.Subscribe(
                    udpSocket =>
                {
                    var stream         = new MemoryStream(udpSocket.ByteData);
                    var requestHandler = new HttpParserDelegate
                    {
                        HttpRequestReponse =
                        {
                            RemoteAddress = udpSocket.RemoteAddress,
                            RemotePort    = int.Parse(udpSocket.RemotePort),
                            RequestType   = RequestType.UDP
                        }
                    };

                    var result = _httpStreamParser.Parse(requestHandler, stream, Timeout);
                    obs.OnNext(result);
                },
                    ex =>
                {
                    Cleanup();
                    obs.OnError(ex);
                },
                    () =>
                {
                    Cleanup();
                    obs.OnCompleted();
                });

                return(disp);

                void Cleanup()
                {
                    _udpReceiverPortToObservable.Remove(port);
                    udpListener.Dispose();
                }
            });

            return(observable);
        }
Exemple #3
0
 //destory connection
 public void Dispose()
 {
     listenerUDP?.Dispose();
     listenerTCP.Dispose();
 }
Exemple #4
0
        async public static Task <ServerList> BrowseForNoahServers(string friendlyName, string ipAddress)
        {
            var client   = new UdpSocketClient();
            var receiver = new UdpSocketReceiver();

            try
            {
                var address = GetIpAddressForBroadcast(ipAddress);
                //// convert our greeting message into a byte array
                string clientGUID        = "{B0BE0E7D-F70B-40BE-91AB-14125863B0B7}";
                System.Text.Encoding enc = System.Text.Encoding.UTF8;
                byte[] sendBuffer        = enc.GetBytes(clientGUID);

                receiver.MessageReceived += (sender, args) =>
                {
                    try
                    {
                        var from   = String.Format("{0}:{1}", args.RemoteAddress, args.RemotePort);
                        var fromIp = args.RemoteAddress;
                        var data   = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);

                        try
                        {
                            LocalRemoteHostInfo payload =
                                Helpers.Deserialize <LocalRemoteHostInfo>(Encoding.UTF8.GetString(args.ByteData, 0,
                                                                                                  args.ByteData.Length));
                            string guid = payload.LocalNoahServerGuid;
                            if (guid.Equals("{5FE140D5-1D3F-4E46-8892-15FA89DAE9F4}"))
                            {
                                bool duplicate = false;
                                foreach (ServerData servData in ServerCollection)
                                {
                                    if (servData.ServerIp.Equals(fromIp))
                                    {
                                        duplicate = true; //The adress is allready in the list
                                        break;
                                    }
                                }
                                if (duplicate == false) //No need to list Gatteway IP
                                {
                                    if (string.IsNullOrEmpty(friendlyName))
                                    {
                                        ServerCollection.Add(new ServerData
                                        {
                                            ServerName   = payload.HostName,
                                            FriendlyName = payload.FriendlyName,
                                            ServerIp     = payload.LocalHostIpAddress,
                                            RemoteHostId = payload.RemoteHostId
                                        });
                                    }
                                    else
                                    {
                                        if (friendlyName == payload.FriendlyName)
                                        {
                                            receiver.StopListeningAsync();
                                            ServerCollection.Add(new ServerData
                                            {
                                                ServerName   = payload.HostName,
                                                FriendlyName = payload.FriendlyName,
                                                ServerIp     = payload.LocalHostIpAddress,
                                                RemoteHostId = payload.RemoteHostId
                                            });

                                            //client.Dispose();
                                            //receiver.Dispose();
                                            waitTime = false;
                                        }
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                    catch { }
                };
                try
                {
                    receiver.StartListeningAsync(listenPort);
                }
                catch (Exception e)
                {
                }
                client.ConnectAsync(address, port);
                client.SendAsync(sendBuffer);

                DateTime now  = DateTime.Now;
                DateTime stop = now + new TimeSpan(0, 0, 0, 5);


                while (waitTime)
                {
                    if (DateTime.Now > stop)
                    {
                        waitTime = false;
                    }
                }
            }
            catch (Exception e)
            {
                Log.ErrorFormat("Himsa.Noah.MobileAccessLayer.NoahServerUdpBrowser::BrowseForNoahServers(): {0}", e);
                throw;
            }
            finally
            {
                client.DisconnectAsync();
                receiver.StopListeningAsync();
                receiver.Dispose();
            }

            return(new ServerList
            {
                Servers = ServerCollection.ToArray()
            });
        }