Example #1
0
        protected InjectorHostBase(bool forceCanInject = false)
        {
            _forceCanInject = forceCanInject;

            var dtoAssembly = typeof(NewAssemblyMessage).GetTypeInfo().Assembly;

            _messageHub.AdditionalTypeResolutionAssemblies.Add(dtoAssembly);

            // set up the discovery responder
            _servicePublisher = new InjectorServiceDefinition(async() =>
            {
                // get the current interfaces
                var ifs    = await CommsInterface.GetAllInterfacesAsync();
                var usable = ifs.Where(iface => iface.IsUsable && !iface.IsLoopback)
                             .Where(iface => iface.ConnectionStatus == CommsInterfaceStatus.Connected).ToList();

                // if none (how :O), we won't respond. how could we event
                if (!usable.Any())
                {
                    return(null);
                }

                return(new InjectorHostServiceResponse
                {
                    ServiceGuid = ServiceGuid,
                    ServiceName = GetType().FullName,
                    Port = ServicePort,
                    NumConnectedClients = ConnectedClients.Count,
                    RunningAt = ifs.Select(iface => String.Join(":", iface.IpAddress, ServicePort.ToString())).ToList()
                });
            }).CreateServicePublisher();
        }
Example #2
0
        public static async Task <string> GetLocalIp()
        {
            var interfaces = await CommsInterface.GetAllInterfacesAsync();

            //TODO: check if any
            return(interfaces.Last(i => !i.IsLoopback && i.IsUsable).IpAddress + ":" + Port);
        }
        public async void LoadNetworkInterfaces()
        {
            var interfaces = await CommsInterface.GetAllInterfacesAsync();

            foreach (var ni in interfaces)
            {
                _netInterfaces.Add(ni);
            }
        }
        public async Task CommsInterface_CanAccessInterfaceProperties()
        {
            var ifs =
                (await CommsInterface.GetAllInterfacesAsync())
                .Select(i => String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}",
                                           i.Name, i.IpAddress, i.ConnectionStatus,
                                           i.NativeInterfaceId, i.IsLoopback, i.IsUsable,
                                           i.GatewayAddress, i.BroadcastAddress));

            Debug.WriteLine(String.Join(Environment.NewLine, ifs));
        }
Example #5
0
        public static async Task <string> GetLocalIPAddress2()
        {
            List <CommsInterface> interfaces = await CommsInterface.GetAllInterfacesAsync();

            foreach (CommsInterface comms in interfaces)
            {
                if (comms.ConnectionStatus == Sockets.Plugin.Abstractions.CommsInterfaceStatus.Connected)
                {
                    return(comms.IpAddress);
                }
            }

            //raise exception
            throw (new NoNetworkInterface("Unable to find an active network interface connection. Is this device connected to wifi?"));
        }
        public async Task CommsInterface_CanEnumerateNetworkInterfaces()
        {
            var timeout =
                Task.Delay(TimeSpan.FromSeconds(3))
                .ContinueWith(_ => { throw new TimeoutException("Couldn't enumerate network interfaces."); });
            var ifs = CommsInterface.GetAllInterfacesAsync();

            var result = await Task.WhenAny(timeout, ifs);

            if (result.Exception != null)
            {
                throw result.Exception;
            }

            Debug.WriteLine("{0} interfaces returned", ifs.Result.Count);
        }
        public async Task <bool> StartAsync()
        {
            var interfaces = await CommsInterface.GetAllInterfacesAsync().ConfigureAwait(false);

            var nicInterface = interfaces.FirstOrDefault(i => i.IpAddress == _nicAddress.ToString());

            if (nicInterface == null)
            {
                _log.LogError($"Could not locate network interface with IP '{_nicAddress}'");
                return(false);
            }

            await _listener.StartListeningAsync(0, nicInterface).ConfigureAwait(false);

            return(true);
        }
Example #8
0
        public async Task <bool> StartAsync()
        {
            var interfaces = await CommsInterface.GetAllInterfacesAsync().ConfigureAwait(false);

            var nicInterface = interfaces.FirstOrDefault(i => i.IpAddress == _nicIp.ToString());

            if (nicInterface == null)
            {
                _log.LogError($"Could not locate network interface with IP '{_nicIp}'");
                return(false);
            }

            await _client.JoinMulticastGroupAsync(_useOriginalMulticastIp
                                                  ?CitpMulticastOriginalIp.ToString()
                                                      : CitpMulticastIp.ToString(), CitpUdpPort, nicInterface).ConfigureAwait(false);

            return(true);
        }
        public NetworkSettings()
        {
            TTL              = 10;
            MulticastPort    = 30307;
            ListenPort       = 30303;
            MulticastAddress = "239.0.0.222";
            Adapters         = null;
            var interfaces = CommsInterface.GetAllInterfacesAsync().Result;

            foreach (var i in interfaces)
            {
                if (i.IsUsable && !i.IsLoopback)
                {
                    //Adapters = i;
                    break;
                }
            }
        }
Example #10
0
        public async Task StartAsync()
        {
            List <CommsInterface> interfaces = await CommsInterface.GetAllInterfacesAsync();

            this.receiver = new UdpSocketReceiver();

            receiver.MessageReceived += this.ReceiverOnMessageReceived;

            try {
                // listen for udp traffic on listenPort
                await receiver.StartListeningAsync(this.listenPort);

                this.log($"listening on port {this.listenPort}");
            }
            catch (Exception e) {
                this.log($"failed to listen on port {this.listenPort}.{Environment.NewLine}{e}");
            }
        }
        public async Task Discover()
        {
            var msg = _serviceDefinition.DiscoveryRequest();

            if (!_listening)
            {
                await _backingReceiver.StartListeningAsync(_serviceDefinition.ResponsePort);

                _listening = true;
            }

            // TODO: Investigate the network conditions that allow/prevent broadcast traffic
            // Typically sending to 255.255.255.255 suffices. However, on some corporate networks the traffic does not
            // appear to be routed even within the same subnet. Currently, a packet is sent to the broadcast address
            // of each interface and the global broadcast address. This can result in multiple responses if a publisher
            // is bound across several interfaces. Multiple responses are easily consolidated if the payload format
            // contains a service guid; ideally we do not need to require users to incorporate that.
            //
            //  Options:
            //     - Detect when broadcast wont work, only send out on individual interfaces in these cases
            //     - Consolidation responses within ServiceDiscoverer internally, only OnNext for unique services
            //       This could require TPayloadFormat to be constrained to an interface that can carry the consolidated
            //       information - for example, List of the interface addresses that responded to the request.
            if (SendOnAllInterfaces)
            {
                var ifs =
                    (await CommsInterface.GetAllInterfacesAsync()).Where(ci => ci.IsUsable && !ci.IsLoopback).ToList();
                foreach (var if0 in ifs)
                {
                    await _backingReceiver.SendToAsync(msg, if0.BroadcastAddress, _serviceDefinition.DiscoveryPort);
                }
            }
            else
            {
                await _backingReceiver.SendToAsync(msg, "255.255.255.255", _serviceDefinition.DiscoveryPort);
            }
        }
Example #12
0
 private async void Button_Click(object sender, RoutedEventArgs e)
 {
     var allInterfaces = await CommsInterface.GetAllInterfacesAsync();
 }