Exemple #1
0
        private async Task StartListener()
        {
            var comm        = new CommunicationsInterface();
            var allComms    = comm.GetAllInterfaces();
            var networkComm = allComms.FirstOrDefault(x => x.GatewayAddress != null);

            var httpListener = new HttpListener(timeout: TimeSpan.FromSeconds(3));
            await httpListener.StartTcpRequestListener(port : 8000, communicationInterface : networkComm);

            await httpListener.StartTcpResponseListener(port : 8001, communicationInterface : networkComm);

            await httpListener.StartUdpMulticastListener(ipAddr : "239.255.255.250", port : 1900, communicationInterface : networkComm);


            var observeHttpRequests = httpListener
                                      .HttpRequestObservable
                                      // Must observe on Dispatcher for XAML to work
                                      .ObserveOnDispatcher().Subscribe(async
                                                                       request =>
            {
                if (!request.IsUnableToParseHttp)
                {
                    Method.Text = request?.Method ?? "N/A";
                    Path.Text   = request?.Path ?? "N/A";
                    if (request.RequestType == RequestType.TCP)
                    {
                        var response = new HttpReponse
                        {
                            StatusCode     = (int)HttpStatusCode.OK,
                            ResponseReason = HttpStatusCode.OK.ToString(),
                            Headers        = new Dictionary <string, string>
                            {
                                { "Date", DateTime.UtcNow.ToString("r") },
                                { "Content-Type", "text/html; charset=UTF-8" },
                            },
                            Body = new MemoryStream(Encoding.UTF8.GetBytes($"<html>\r\n<body>\r\n<h1>Hello, World! {DateTime.Now}</h1>\r\n</body>\r\n</html>"))
                        };

                        await httpListener.HttpReponse(request, response).ConfigureAwait(false);
                    }
                }
                else
                {
                    Method.Text = "Unable to parse request";
                }
            },
                                                                       // Exception
                                                                       ex =>
            {
            });

            // Remember to dispose of subscriber when done
            //observeHttpRequests.Dispose();
        }
Exemple #2
0
        private static async void InitializeHttpListenerAsync()
        {
            var communicationInterface = new CommunicationsInterface();
            var allInterfaces          = communicationInterface.GetAllInterfaces();

            var firstUsableInterface = allInterfaces.FirstOrDefault(x => x.IpAddress == "10.10.13.204");

            HttpListener.StartTcpRequestListener(1900, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);
            HttpListener.StartTcpResponseListener(1901, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);
            HttpListener.StartUdpMulticastListener("239.255.255.250", 1900, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);
            HttpListener.StartUdpListener(1900, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);
        }
        protected void CheckCommunicationInterface(ICommunicationInterface communicationsInterface)
        {
            if (communicationsInterface != null && !communicationsInterface.IsUsable)
            {
                throw new InvalidOperationException("Cannot listen on an unusable communication interface.");
            }

            if (communicationsInterface == null) //Try and find best possible Network interface.
            {
                var newCommunicationsInterface = new CommunicationsInterface();
                var allInterfaces = newCommunicationsInterface.GetAllInterfaces();
                if (allInterfaces != null && allInterfaces.Any())
                {
                    var firstUsableCommunicationInterface = allInterfaces.FirstOrDefault(x => x.IsUsable && !x.IsLoopback && x.GatewayAddress != null);
                    if (firstUsableCommunicationInterface == null)
                    {
                        communicationsInterface = allInterfaces.FirstOrDefault(x => x.IsUsable);
                    }
                }
            }
        }
Exemple #4
0
        public static async Task <IHttpListener> GetHttpListener(
            string ipAddress,
            TimeSpan timeout = default(TimeSpan))
        {
            if (timeout == default(TimeSpan))
            {
                timeout = TimeSpan.FromSeconds(30);
            }

            var communicationInterface = new CommunicationsInterface();
            var allInterfaces          = communicationInterface.GetAllInterfaces();

            var firstUsableInterface = allInterfaces.FirstOrDefault(x => x.IpAddress == ipAddress);

            if (firstUsableInterface == null)
            {
                throw new ArgumentException($"Unable to locate any network communication interface with the ip address: {ipAddress}");
            }

            return(await GetHttpListener(firstUsableInterface));
        }
        private async void StartTest()
        {
            var comm = new CommunicationsInterface();
            var all  = comm.GetAllInterfaces();
            var one  = all.FirstOrDefault(x => x.GatewayAddress != null);

            var tcpListener          = new SocketLite.Services.TcpSocketListener();
            var udpMulticastListener = new SocketLite.Services.UdpSocketMulticastClient();



            var udpListenerSubscribe = udpMulticastListener.ObservableMessages
                                       .ObserveOnDispatcher().Subscribe(
                msg =>
            {
                RemoteUdpClient.Text = msg.RemoteAddress + ":" + msg.RemoteAddress;
                Data.Text            = System.Text.Encoding.UTF8.GetString(msg.ByteData);
            });


            var tcpListenerSubscribe = StartTcpListener(tcpListener);

            await tcpListener.StartListeningAsync(8000, allowMultipleBindToSamePort : true);



            await udpMulticastListener.JoinMulticastGroupAsync("239.255.255.250", 1900, allowMultipleBindToSamePort : true);

            //await Task.Delay(TimeSpan.FromSeconds(1));

            //// Testing that subscription can "survive" a disconnect and connect again.
            //tcpListener.StopListening();
            //tcpListenerSubscribe.Dispose();
            //udpMulticastListener.Disconnect();

            //await tcpListener.StartListeningAsync(8000, allowMultipleBindToSamePort: true);
            //tcpListenerSubscribe = StartTcpListener(tcpListener);

            //await udpMulticastListener.JoinMulticastGroupAsync("239.255.255.250", 1900, allowMultipleBindToSamePort: true);
        }
        static async void Start()
        {
            var communicationInterface = new CommunicationsInterface();
            var allInterfaces          = communicationInterface.GetAllInterfaces();

            var networkInterface = allInterfaces.FirstOrDefault(x => x.IpAddress == "192.168.0.36");

            //var tcpListener = new TcpSocketListener();

            //var observerTcpListner = await tcpListener.CreateObservableListener(
            //    port:8000,
            //    communicationInterface: networkInterface,
            //    allowMultipleBindToSamePort:true);

            //var subscriberTcpListener = observerTcpListner.Subscribe(
            //    tcpClient =>
            //    {
            //        //Insert your code here
            //    },
            //    ex =>
            //    {
            //        // Insert your exception code here
            //    },
            //    () =>
            //    {
            //        // Insert your completed code here
            //    });

            //var udpReceiver = new UdpSocketReceiver();

            //var observerUdpReceiver = await udpReceiver.ObservableUnicastListener(
            //    port: 1900,
            //    communicationInterface: networkInterface,
            //    allowMultipleBindToSamePort: true);

            //var subscriberUpdReceiver = observerUdpReceiver.Subscribe(
            //    udpMsg =>
            //    {
            //        System.Console.WriteLine($"Udp package received: {udpMsg.RemoteAddress}:{udpMsg.RemotePort}");
            //    },
            //    ex =>
            //    {
            //        //Inset your exception code here
            //    },
            //    () =>
            //    {
            //        //Insert your completion code here
            //    });

            var udpMulticast = new UdpSocketMulticastClient();

            var observerUdpMulticast = await udpMulticast.ObservableMulticastListener(
                "239.255.255.250",
                1900,
                networkInterface,
                allowMultipleBindToSamePort : false);

            var subscriberUdpMilticast = observerUdpMulticast.Subscribe(
                udpMsg =>
            {
                System.Console.WriteLine($"Udp package received: {udpMsg.RemoteAddress}:{udpMsg.RemotePort}");
            },
                ex =>
            {
                //Insert your exception code here
            },
                () =>
            {
                //Insert your completion code here
            });

            //await udpMulti.JoinMulticastGroupAsync(
            //    "239.255.255.250",
            //    1900,
            //    firstUsableInterface,
            //    allowMultipleBindToSamePort: true,
            //    mcastIpv6AddressList:ipv6MultiCastAddressList);

            //var udpListener = new UdpSocketReceiver();

            //await udpListener.StartListeningAsync(1900, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);

            Console.WriteLine("Listening...");
        }