Пример #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
        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);
                }
            }
        }