/// <summary>
        ///   Stops the renewal process and deletes the public endpoint.
        /// </summary>
        /// <seealso cref="NatClient.DeletePublicEndpointAsync(Lease)"/>
        public void Dispose()
        {
            if (renewalCancellation != null)
            {
                var cancel = renewalCancellation;
                renewalCancellation = null;
                cancel.Cancel();
                cancel.Dispose();
            }

            if (Lease != null)
            {
                var lease = Lease;
                Lease = null;
                if (lease.Nat != null)
                {
                    lease.Nat.DeletePublicEndpointAsync(lease); // do not wait for task to complete!
                }
            }
        }
Example #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);
                }
            }
        }
Example #3
0
 /// <summary>
 ///   Cancel the lease.
 /// </summary>
 /// <param name="lease">
 ///   The lease to break.
 /// </param>
 /// <returns>
 ///   A task that represents the asynchronous operation.
 /// </returns>
 /// <remarks>
 ///   Tells that NAT that the public endpoint can be removed.
 /// </remarks>
 public abstract Task DeletePublicEndpointAsync(Lease lease);
Example #4
0
 /// <summary>
 ///   Renew the lease.
 /// </summary>
 /// <param name="lease">
 ///   The lease to renew.
 /// </param>
 /// <returns>
 ///   A task that represents the asynchronous operation. The task's result is
 ///   a <see cref="Lease"/> that defines an endpoint that is connectable by
 ///   devices not behind the NAT.
 /// </returns>
 /// <remarks>
 ///   The returned <see cref="Lease"/> may have different external address
 ///   and/or port than the original <paramref name="lease"/>.
 /// </remarks>
 public abstract Task <Lease> RenewPublicEndpointAsync(Lease lease);