Пример #1
0
        public async Task SendsRenewal()
        {
            int nRequests = 0;

            using (var server = new MockNat())
            {
                server.RequestReceived += (s, req) => ++ nRequests;
                var nat   = new Pcp.Client(server.Address);
                var lease = new Lease
                {
                    Nat           = nat,
                    Nonce         = new byte[12],
                    PublicAddress = IPAddress.Loopback,
                    PublicPort    = 1234,
                    InternalPort  = 1234,
                    Lifetime      = TimeSpan.FromSeconds(1)
                };
                using (var endpoint = new LeasedEndpoint(lease))
                {
                    await Task.Delay(lease.Lifetime);

                    Assert.AreNotEqual(0, nRequests);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///   Gets the NATs.
        /// </summary>
        /// <returns>
        ///   A sequence of NAT clients that can be talked to,
        /// </returns>
        /// <remarks>
        ///   Asks each <see cref="GetGateways">gateways</see> if it supports
        ///   PCP or PMP.  If true, then a <see cref="NatClient"/> is retuned.
        /// </remarks>
        public static IEnumerable <NatClient> GetNats()
        {
            foreach (var gateway in GetGateways())
            {
                NatClient nat = new Pcp.Client(gateway);
                if (nat.IsAvailableAsync().Result)
                {
                    yield return(nat);

                    continue;
                }

                nat = new Pmp.Client(gateway);
                if (nat.IsAvailableAsync().Result)
                {
                    yield return(nat);

                    continue;
                }
            }
        }
Пример #3
0
        public async Task RaisesChanged_OnPortChange()
        {
            using (var server = new MockNat())
            {
                server.RequestReceived += (s, req) =>
                {
                    var map      = Message.Create <MapRequest>(req.Buffer);
                    var response = new MapResponse
                    {
                        AssignedExternalAdddress = map.SuggestedExternalAdddress,
                        AssignedExternalPort     = 4321,
                        EpochTime    = TimeSpan.FromSeconds(1),
                        Lifetime     = TimeSpan.FromSeconds(1),
                        Opcode       = Opcode.Map,
                        Nonce        = map.Nonce,
                        InternalPort = map.InternalPort,
                        Protocol     = map.Protocol
                    }.ToByteArray();
                    server.udp.Send(response, response.Length, req.RemoteEndPoint);
                };
                var nat   = new Pcp.Client(server.Address);
                var lease = new Lease
                {
                    Nat           = nat,
                    Nonce         = new byte[12],
                    PublicAddress = IPAddress.Loopback,
                    PublicPort    = 1234,
                    InternalPort  = 1234,
                    Lifetime      = TimeSpan.FromSeconds(1)
                };
                using (var endpoint = new LeasedEndpoint(lease))
                {
                    int nChanges = 0;
                    endpoint.Changed += (s, e) => ++ nChanges;
                    await Task.Delay(lease.Lifetime);

                    Assert.AreNotEqual(0, nChanges);
                }
            }
        }