/// <summary>
        /// Listens for incoming punch requests.
        /// </summary>
        /// <param name="listenEndpoint">The endpoint where new players should join.</param>
        public void ListenForPunches(IPEndPoint listenEndpoint)
        {
            // Bind the socket
            Transport.Bind(listenEndpoint);

            _isRunning = true;

            RunListenerJob(false, false);

            _isRunning = false;
        }
        /// <summary>
        /// Listens for a single punch and returns when the punch is successful.
        /// </summary>
        /// <returns>The address of the connector that punched through our NAT.</returns>
        /// <param name="listenEndpoint">The endpoint where new players should join.</param>
        public IPEndPoint ListenForSinglePunch(IPEndPoint listenEndpoint)
        {
            // Bind the socket
            Transport.Bind(listenEndpoint);

            _isRunning = true;

            IPEndPoint endpoint = RunListenerJob(true, false);

            _isRunning = false;

            return(endpoint);
        }
        /// <summary>
        /// Starts punching the requested peer.
        /// </summary>
        /// <returns>The remote peer connectable address.</returns>
        /// <param name="connectAddress">The peer connect address.</param>
        public bool TryPunch(IPAddress connectAddress, out IPEndPoint punchResult)
        {
            if (connectAddress.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Only IPv4 addresses can be punched. IPv6 addresses does not have to be punched as they dont use NAT.");
            }

            // Bind the socket
            Transport.Bind(new IPEndPoint(IPAddress.Any, 0));

            // Set running state
            _isRunning = true;

            // Generate random token
            byte[] token = new byte[32];
            Random rnd   = new Random();

            rnd.NextBytes(token);

            // Default punch result
            punchResult = null;

            // Register with NAT server
            SendRegisterRequest(connectAddress, token);

            // Waits for response from the puncher server.
            if (TryWaitForConnectorRegisterResponse(token, out IPEndPoint punchEndPoint, false))
            {
                if (DropUnknownAddresses && !punchEndPoint.Address.Equals(connectAddress))
                {
                    // The address we were asked to punch was not the same as the one we connected to.
                    // This might mean either a proxy, or a malicious interception.
                    return(false);
                }

                // Sends punches
                SendPunches(punchEndPoint, new ArraySegment <byte>(token, 0, token.Length));

                // Waits for PunchSuccess
                if (TryCompleteConnectorPunch(punchEndPoint, token, out punchResult) && punchResult != null)
                {
                    _isRunning = false;

                    return(true);
                }
            }


            _isRunning = false;
            return(false);
        }
Exemple #4
0
        /// <summary>
        /// Listens for incoming punch requests.
        /// </summary>
        /// <param name="listenEndpoint">The endpoint where new players should join.</param>
        public void ListenForPunches(IPEndPoint listenEndpoint)
        {
            // Bind the socket
            Transport.Bind(listenEndpoint);

            _isRunning = true;

            // Register with NAT server
            SendRegister(null, null);

            // Punch
            ExecutePunch(false, false, null);

            _isRunning = false;
        }
Exemple #5
0
        /// <summary>
        /// Listens for a single punch and returns when the punch is successful.
        /// </summary>
        /// <returns>The address of the connector that punched through our NAT.</returns>
        /// <param name="listenEndpoint">The endpoint where new players should join.</param>
        public IPEndPoint ListenForSinglePunch(IPEndPoint listenEndpoint)
        {
            // Bind the socket
            Transport.Bind(listenEndpoint);

            _isRunning = true;

            // Register with NAT server
            SendRegister(null, null);

            // Punch
            IPEndPoint endpoint = ExecutePunch(false, true, null);

            _isRunning = false;

            return(endpoint);
        }
Exemple #6
0
        /// <summary>
        /// Starts punching the requested peer.
        /// </summary>
        /// <returns>The remote peer connectable address.</returns>
        /// <param name="connectAddress">The peer connect address.</param>
        public IPEndPoint Punch(IPAddress connectAddress)
        {
            // Bind the socket
            Transport.Bind(new IPEndPoint(IPAddress.Any, 0));

            _isRunning = true;

            byte[] token = new byte[32];
            Random rnd   = new Random();

            rnd.NextBytes(token);

            // Register with NAT server
            SendRegister(connectAddress, token);

            // Punch
            IPEndPoint endpoint = ExecutePunch(true, true, token);

            _isRunning = false;

            return(endpoint);
        }