Ejemplo n.º 1
0
        public Property TcpSocketServerChannel_can_bind_on_any_valid_EndPoint(EndPoint ep)
        {
            IChannel c      = null;
            var      family = ep.BestAddressFamily();

            // TODO: remove this code once https://bugzilla.xamarin.com/show_bug.cgi?id=35536 is fixed
            if (IsMono && family == AddressFamily.InterNetworkV6 && ep is DnsEndPoint)
            {
                family = AddressFamily.InterNetwork;
            }

            try
            {
                var sb = new ServerBootstrap()
                         .ChannelFactory(() => new TcpServerSocketChannel(family))
                         .ChildHandler(new ActionChannelInitializer <TcpSocketChannel>(channel => { }))
                         .PreferredDnsResolutionFamily(family)
                         .Group(_serverGroup);

                c = sb.BindAsync(ep).Result;

                return(c.IsOpen.Label("Channel should be open").And(c.IsActive).Label("Channel should be active"));
            }
            finally
            {
                c?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
            }
        }
Ejemplo n.º 2
0
        public Property TcpServerSocketChannel_can_accept_connection_on_any_valid_Endpoint(EndPoint ep)
        {
            var ip     = ep as IPEndPoint;
            var family = ep.BestAddressFamily();

            // TODO: remove this code once https://bugzilla.xamarin.com/show_bug.cgi?id=35536 is fixed

            if (IsMono && family == AddressFamily.InterNetworkV6 && ep is DnsEndPoint)
            {
                family = AddressFamily.InterNetwork;
            }

            IChannel s = null;
            IChannel c = null;

            try
            {
                var sb = new ServerBootstrap()
                         .ChannelFactory(() => new TcpServerSocketChannel(family))
                         .ChildHandler(new ActionChannelInitializer <TcpSocketChannel>(channel => { }))
                         .PreferredDnsResolutionFamily(family)
                         .Group(_serverGroup);

                s = sb.BindAsync(ep).Result;

                var cb = new ClientBootstrap()
                         .ChannelFactory(() => new TcpSocketChannel(family))
                         .Option(ChannelOption.TcpNodelay, true)
                         .Option(ChannelOption.ConnectTimeout, TimeSpan.FromMilliseconds(100))
                         .PreferredDnsResolutionFamily(family)
                         .Handler(new ActionChannelInitializer <TcpSocketChannel>(channel => { }))
                         .Group(_clientGroup);

                var clientEp = s.LocalAddress;
                if (ip != null) // handle special case of 0.0.0.0, which clients can't connect to directly.
                {
                    if (ip.Address.Equals(IPAddress.Any))
                    {
                        clientEp = new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)s.LocalAddress).Port);
                    }
                    if (ip.Address.Equals(IPAddress.IPv6Any))
                    {
                        clientEp = new IPEndPoint(IPAddress.IPv6Loopback, ((IPEndPoint)s.LocalAddress).Port);
                    }
                }

                c = cb.ConnectAsync(clientEp).Result;
                c.WriteAndFlushAsync(Unpooled.Buffer(4).WriteInt(2)).Wait(20);

                return(c.IsOpen.Label("Channel should be open")
                       .And(c.IsActive).Label("Channel should be active")
                       .And(c.IsWritable).Label("Channel should be writable"));
            }
            finally
            {
                try
                {
                    c?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
                    s?.CloseAsync().Wait(TimeSpan.FromMilliseconds(200));
                }
                catch
                {
                }
            }
        }