Ejemplo n.º 1
0
        /// <summary>
        /// is called before performing a registration.
        /// </summary>
        /// <param name="Registration">Registration to be performed.</param>
        /// <param name="TcpPortMapped">What TCP Ports are already mapped.</param>
        /// <param name="UdpPortMapped">What UDP Ports are already mapped.</param>
        protected override void BeforeRegistration(InternetGatewayRegistration Registration,
                                                   Dictionary <ushort, bool> TcpPortMapped, Dictionary <ushort, bool> UdpPortMapped)
        {
            try
            {
                do
                {
                    this.tcpListener = new TcpListener(this.localAddress, Registration.LocalPort);
                    this.tcpListener.Start(this.backlog);

                    int i = ((IPEndPoint)this.tcpListener.LocalEndpoint).Port;

                    if (i < 0 || i > ushort.MaxValue ||
                        TcpPortMapped.ContainsKey((ushort)i) ||
                        UdpPortMapped.ContainsKey((ushort)i))
                    {
                        this.tcpListener.Stop();
                        this.tcpListener = null;
                    }
                    else
                    {
                        try
                        {
                            this.udpClient = new UdpClient(this.tcpListener.LocalEndpoint.AddressFamily);
                            this.udpClient.Client.Bind((IPEndPoint)this.tcpListener.LocalEndpoint);

                            Registration.LocalPort = (ushort)i;
                            if (Registration.ExternalPort == 0 ||
                                TcpPortMapped.ContainsKey((ushort)Registration.ExternalPort) ||
                                UdpPortMapped.ContainsKey((ushort)Registration.ExternalPort))
                            {
                                Registration.ExternalPort = Registration.LocalPort;
                            }
                        }
                        catch (Exception)
                        {
                            this.tcpListener.Stop();
                            this.tcpListener = null;
                        }
                    }
                }while (this.tcpListener is null);

                this.localEndpoint    = new IPEndPoint(this.localAddress, Registration.LocalPort);
                this.externalEndpoint = new IPEndPoint(this.externalAddress, Registration.ExternalPort);

                this.AcceptTcpClients();
                this.BeginReceiveUdp();
            }
            catch (Exception ex)
            {
                this.exception = ex;
                this.State     = PeerToPeerNetworkState.Error;
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// is called before performing a registration.
 /// </summary>
 /// <param name="Registration">Registration to be performed.</param>
 /// <param name="TcpPortMapped">What TCP ports have already been mapped.</param>
 /// <param name="UdpPortMapped">What UDP ports have already been mapped.</param>
 protected virtual void BeforeRegistration(InternetGatewayRegistration Registration,
                                           Dictionary <ushort, bool> TcpPortMapped, Dictionary <ushort, bool> UdpPortMapped)
 {
     // Do nothing by default.
 }