Exemple #1
0
 public void Init()
 {
     TestRuntime.AssertXcodeVersion(11, 0);
     // connect so that we can later when the report and test with it
     connectedEvent = new AutoResetEvent(false);
     reportEvent    = new AutoResetEvent(false);
     host           = NetworkResources.MicrosoftUri.Host;
     // we create a connection which we are going to use to get the availabe
     // interfaces, that way we can later test protperties of the NWParameters class.
     using (var parameters = NWParameters.CreateUdp())
         using (var endpoint = NWEndpoint.Create(host, "80"))
         {
             using (var protocolStack = parameters.ProtocolStack) {
                 var ipOptions = protocolStack.InternetProtocol;
                 ipOptions.IPSetVersion(NWIPVersion.Version4);
             }
             connection = new NWConnection(endpoint, parameters);
             connection.SetQueue(DispatchQueue.DefaultGlobalQueue);              // important, else we will get blocked
             connection.SetStateChangeHandler(ConnectionStateHandler);
             connection.Start();
             Assert.True(connectedEvent.WaitOne(20000), "Connection timed out.");
             connection.GetEstablishmentReport(DispatchQueue.DefaultGlobalQueue, (r) => {
                 report = r;
                 reportEvent.Set();
             });
             Assert.True(reportEvent.WaitOne(20000), "Connection timed out.");
         }
 }
Exemple #2
0
        public void Init()
        {
            TestRuntime.AssertXcodeVersion(10, 0);
            // we want to use a single connection, since it is expensive
            connectedEvent = new AutoResetEvent(false);
            interfaces     = new List <NWInterface> ();
            host           = NetworkResources.MicrosoftUri.Host;
            // we create a connection which we are going to use to get the availabe
            // interfaces, that way we can later test protperties of the NWParameters class.
            using (var parameters = NWParameters.CreateUdp())
                using (var endpoint = NWEndpoint.Create(host, "80"))
                {
                    using (var protocolStack = parameters.ProtocolStack) {
                        var ipOptions = protocolStack.InternetProtocol;
#if NET
                        ipOptions.SetVersion(NWIPVersion.Version4);
#else
                        ipOptions.IPSetVersion(NWIPVersion.Version4);
#endif
                    }
                    connection = new NWConnection(endpoint, parameters);
                    connection.SetQueue(DispatchQueue.DefaultGlobalQueue);              // important, else we will get blocked
                    connection.SetStateChangeHandler(ConnectionStateHandler);
                    connection.Start();
                    Assert.True(connectedEvent.WaitOne(20000), "Connection timed out.");
                }
        }
    static NWConnection CreateOutboundConnection(string name, string port)
    {
        NWEndpoint endpoint;

        if (bonjour)
        {
            endpoint = NWEndpoint.CreateBonjourService(name, GetServiceType(), "local");
        }
        else
        {
            endpoint = NWEndpoint.Create(name, port);
        }

        Action <NWProtocolOptions> configureTls = SetupTls();
        NWParameters parameters;

        if (useUdp)
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null);
            }
            else
            {
                parameters = NWParameters.CreateUdp(configureUdp: null);
            }
        }
        else
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null);
            }
            else
            {
                parameters = NWParameters.CreateTcp(configureTcp: null);
            }
        }

        using (NWProtocolStack protocolStack = parameters.ProtocolStack){
            if (ipv4 || ipv6)
            {
                NWProtocolOptions ipOptions = protocolStack.InternetProtocol;
                ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6);
            }
        }

        if (localAddr != null || localPort != null)
        {
            using (NWEndpoint localEndpoint = NWEndpoint.Create(localAddr != null ? localAddr : "::", port == null ? "0" : port))
                parameters.LocalEndpoint = localEndpoint;
        }

        var connection = new NWConnection(endpoint, parameters);

        endpoint.Dispose();
        parameters.Dispose();
        return(connection);
    }
 public void SetUp()
 {
     TestRuntime.AssertXcodeVersion(12, TestRuntime.MinorXcode12APIMismatch);
     endpoint        = NWEndpoint.Create("224.0.0.251", "5353");
     parameters      = NWParameters.CreateUdp();
     descriptor      = new NWMulticastGroup(endpoint);
     connectionGroup = new NWConnectionGroup(descriptor, parameters);
 }
 public void LocalEndpointPropertyTest()
 {
     Assert.Ignore("nw_parameters_copy_local_endpoint always return null. Rdar filled 44095278.");
     using (var parameters = NWParameters.CreateUdp())
         using (var endpoint = NWEndpoint.Create(NetworkResources.MicrosoftUri.Host, "80"))
         {
             var defaultValue = parameters.LocalEndpoint;
             Assert.IsNull(defaultValue, "Default value changed.");
             parameters.LocalEndpoint = endpoint;
             Assert.IsNotNull(parameters.LocalEndpoint, "New value was not stored.");
         }
 }
 public void Init()
 {
     TestRuntime.AssertXcodeVersion(10, 0);
     // we want to use a single connection, since it is expensive
     connectedEvent = new AutoResetEvent(false);
     host           = NetworkResources.MicrosoftUri.Host;
     interfaces     = new List <NWInterface> ();
     using (var parameters = NWParameters.CreateUdp())
         using (var endpoint = NWEndpoint.Create(host, "80")) {
             connection = new NWConnection(endpoint, parameters);
             connection.SetQueue(DispatchQueue.DefaultGlobalQueue);              // important, else we will get blocked
             connection.SetStateChangeHandler(ConnectionStateHandler);
             connection.Start();
             Assert.True(connectedEvent.WaitOne(20000), "Connection timed out.");
             using (var path = connection.CurrentPath)
             {
                 path.EnumerateInterfaces(EnumerateInterfacesHandler);
             }
         }
 }
 public void Init()
 {
     TestRuntime.AssertXcodeVersion(10, 0);
     // we want to use a single connection, since it is expensive
     connectedEvent = new AutoResetEvent(false);
     host           = "www.google.com";
     using (var parameters = NWParameters.CreateUdp())
         using (var endpoint = NWEndpoint.Create(host, "80")) {
             connection = new NWConnection(endpoint, parameters);
             connection.SetQueue(DispatchQueue.DefaultGlobalQueue);              // important, else we will get blocked
             connection.SetStateChangeHandler(ConnectionStateHandler);
             connection.Start();
             Assert.True(connectedEvent.WaitOne(20000), "Connection timed out.");
             stack = parameters.ProtocolStack;
             using (var ipOptions = stack.InternetProtocol) {
                 ipOptions.IPSetVersion(NWIPVersion.Version4);
                 stack.PrependApplicationProtocol(ipOptions);
             }
         }
 }
 public void SetUp()
 {
     // connect and once the connection is done, deal with the diff tests
     connectedEvent = new AutoResetEvent(false);
     host           = NetworkResources.MicrosoftUri.Host;
     // we create a connection which we are going to use to get the availabe
     // interfaces, that way we can later test protperties of the NWParameters class.
     using (var parameters = NWParameters.CreateUdp())
         using (var endpoint = NWEndpoint.Create(host, "80"))
         {
             using (var protocolStack = parameters.ProtocolStack) {
                 var ipOptions = protocolStack.InternetProtocol;
                 ipOptions.IPSetVersion(NWIPVersion.Version4);
             }
             connection = new NWConnection(endpoint, parameters);
             connection.SetQueue(DispatchQueue.DefaultGlobalQueue);              // important, else we will get blocked
             connection.SetStateChangeHandler(ConnectionStateHandler);
             connection.Start();
             Assert.True(connectedEvent.WaitOne(20000), "Connection timed out.");
         }
 }
    static NWListener CreateAndStartListener(string host, string port)
    {
        Action <NWProtocolOptions> configureTls = SetupTls();
        NWParameters parameters;

        // Create the parameters, either TLS or no TLS, and with UDP or no UDP
        if (useUdp)
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null);
            }
            else
            {
                parameters = NWParameters.CreateUdp(configureUdp: null);
            }
        }
        else
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null);
            }
            else
            {
                parameters = NWParameters.CreateTcp(configureTcp: null);
            }
        }

        // If specified, set the IP version
        using (NWProtocolStack protocolStack = parameters.ProtocolStack){
            if (ipv4 || ipv6)
            {
                NWProtocolOptions ipOptions = protocolStack.InternetProtocol;
                ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6);
            }
        }

        // Bind to local address and port
        string address = bonjour ? null : host;

        if (address != null || port != null)
        {
            NWEndpoint localEndpoint = NWEndpoint.Create(address != null ? address : "::", port != null ? port : "0");
            Console.WriteLine("Getting {0} and {1}", address != null ? address : "::", port != null ? port : "0");
            parameters.LocalEndpoint = localEndpoint;
            Console.WriteLine("With port: " + localEndpoint.Port);
        }

        var listener = NWListener.Create(parameters);

        if (bonjour && host != null)
        {
            listener.SetAdvertiseDescriptor(NWAdvertiseDescriptor.CreateBonjourService(host, GetServiceType(), "local"));
            listener.SetAdvertisedEndpointChangedHandler((NWEndpoint advertisedEndpoint, bool added) => {
                if (verbose)
                {
                    var astr = added ? "added" : "removed";
                    warn($"Listener {astr} on {advertisedEndpoint.BonjourServiceName} on ({advertisedEndpoint.BonjourServiceName}.{GetServiceType()}.local");
                }
            });
        }

        listener.SetQueue(DispatchQueue.MainQueue);
        listener.SetStateChangedHandler((listenerState, error) => {
            var errno = (SslStatus)(error == null ? 0 : error.ErrorCode);
            switch (listenerState)
            {
            case NWListenerState.Waiting:
                if (verbose)
                {
                    warn($"Listener on port {listener.Port} udp={useUdp} waiting");
                }
                break;

            case NWListenerState.Failed:
                warn($"Listener on port {listener.Port} udp={useUdp} failed");
                break;

            case NWListenerState.Ready:
                if (verbose)
                {
                    warn($"Listener on port {listener.Port} udp={useUdp} ready");
                }
                break;

            case NWListenerState.Cancelled:
                listener = null;
                break;
            }
        });

        listener.SetNewConnectionHandler((connection) => {
            if (inboundConnection != null)
            {
                // We only support one connection at a time, so if we already
                // have one, reject the incoming connection.
                connection.Cancel();
            }
            else
            {
                if (verbose)
                {
                    warn($"New Connection on {connection.Handle} with {connection.Endpoint}");
                }
                // Accept the incoming connection and start sending  and receiving on it
                inboundConnection = connection;
                StartConnection(inboundConnection);
                StartSendReceiveLoop(inboundConnection);
            }
        });
        listener.Start();

        return(listener);
    }