Example #1
0
        private void OnUnconnectedDataReceived(IPEndPoint endpoint, NetBuffer incomingData)
        {
            if (endpoint.Equals(_natFacilitatorEndpoint))
            {
                var messageType = (NatFacilitatorMessageType)incomingData.ReadByte();
                switch (messageType)
                {
                case NatFacilitatorMessageType.HostNotRegistered:
                    // Handle host not registered response
                    if (OnNatPunchFailure != null)
                    {
                        var punchId = new NatPunchId(incomingData.ReadString());
                        OnNatPunchFailure(punchId, endpoint);
                    }
                    break;

                case NatFacilitatorMessageType.PeerRegistrationSuccess:
                    if (_isRegistrationRunning)
                    {
                        _externalEndpoint.SetResult(incomingData.ReadIPEndPoint());
                    }
                    break;

                default:
                    // Skip this message
                    break;
                }
            }
        }
        private void OnNatPunchFailure(NatPunchId punchId, IPEndPoint actualEndpoint)
        {
            IPooledObject <PunchAttempt> attempt;

            if (_natPunchAttempts.TryGetValue(punchId, out attempt))
            {
                attempt.Instance.OnFailure(punchId);
                RemoveNatPunchAttempt(attempt);
            }
        }
Example #3
0
        private void OnNatPunchFailure(NatPunchId punchId, IPEndPoint actualEndpoint)
        {
            PunchAttempt attempt;

            if (_natPunchAttempts.TryGetValue(punchId, out attempt))
            {
                attempt.OnFailure(punchId);
                RemoveNatPunchAttempt(attempt);
            }
        }
Example #4
0
 public void SendIntroduction(IPEndPoint remoteEndpoint, NatPunchId punchId)
 {
     _outgoingMessage.Reset();
     _outgoingMessage.Write((byte)NatFacilitatorRequestType.RequestIntroduction);
     _outgoingMessage.Write(_transporter.InternalEndpoint);
     _outgoingMessage.Write(remoteEndpoint);
     _outgoingMessage.Write(punchId.Value);
     _transporter.SendUnconnected(_natFacilitatorEndpoint, _outgoingMessage);
     _transporter.Flush();
 }
        private void OnPunchFailure(NatPunchId punchId)
        {
            ConnectionRegistration registration;

            if (_punchAttempts.TryGetValue(punchId, out registration))
            {
                _punchAttempts.Remove(punchId);
                // TODO Add punch exception
                registration.OnConnectionFailure(registration.PublicEndpoint, exception: null);
            }
        }
        private void OnNatPunchSuccess(NatPunchId punchId, IPEndPoint actualEndpoint)
        {
            IPooledObject <PunchAttempt> attempt;

            if (_natPunchAttempts.TryGetValue(punchId, out attempt))
            {
                Debug.Log("Received NAT punch success to endpoint: " + actualEndpoint);

                attempt.Instance.OnSuccess(punchId, actualEndpoint);
                RemoveNatPunchAttempt(attempt);
            }
        }
        private void OnPunchFailure(NatPunchId punchId)
        {
            IPooledObject <ConnectionRegistration> registration;

            if (_punchAttempts.TryGetValue(punchId, out registration))
            {
                if (!IsCancelled(registration.Instance))
                {
                    // TODO Add punch exception
                    registration.Instance.OnConnectionFailure(registration.Instance.PublicEndpoint, exception: null);
                }
                CancelPunchAttempt(punchId, registration.Instance);
            }
        }
        private void OnPunchSuccess(NatPunchId punchId, IPEndPoint endPoint)
        {
            ConnectionRegistration registration;

            if (_punchAttempts.TryGetValue(punchId, out registration))
            {
                //Debug.Log("NAT introduction succeeded to " + endPoint);

                _punchAttempts.Remove(punchId);

                registration.ConnectionEndpoint = endPoint;
                var connectionId = _transporter.Connect(endPoint);
                AddConnectionAttempt(connectionId, registration);
            }
        }
Example #9
0
        private void OnNatPunchSuccess(NatPunchId punchId, IPEndPoint actualEndpoint)
        {
            PunchAttempt attempt;

            if (_natPunchAttempts.TryGetValue(punchId, out attempt))
            {
                Debug.Log("Received NAT punch success to endpoint: " + actualEndpoint);

                attempt.OnSuccess(punchId, actualEndpoint);
                RemoveNatPunchAttempt(attempt);

//                _natPunchSessionCache.Insert(desiredEndpoint, desiredEndpoint);
//                _natPunchSessionCache.Insert(actualEndpoint, desiredEndpoint);
            }
            else
            {
//                _natPunchSessionCache.Insert(actualEndpoint, actualEndpoint);
            }
        }
        public LidgrenNatPunchClient(ICoroutineScheduler coroutineScheduler, LidgrenNatFacilitatorConnection facilitatorConnection)
        {
            _facilitatorConnection = facilitatorConnection;

            _punchAttemptPool = new ObjectPool <PunchAttempt>(() => new PunchAttempt());

            {
                const int natpunchIdCount = 256;
                var       punchIds        = new NatPunchId[natpunchIdCount];
                for (int i = 0; i < natpunchIdCount; i++)
                {
                    punchIds[i] = new NatPunchId(Guid.NewGuid().ToString());
                }
                _natPunchIds = CollectionUtil.LoopingEnumerator(punchIds);
            }

            _natPunchAttempts      = new Dictionary <NatPunchId, IPooledObject <PunchAttempt> >();
            _natPunchRegistrations = new List <IPooledObject <PunchAttempt> >();
            _facilitatorConnection.OnNatPunchSuccess += OnNatPunchSuccess;
            _facilitatorConnection.OnNatPunchFailure += OnNatPunchFailure;
            _cleanupRoutine = coroutineScheduler.Run(ConnectionTimeoutCleanup());
        }
        private void OnPunchSuccess(NatPunchId punchId, IPEndPoint endPoint)
        {
            IPooledObject <ConnectionRegistration> registration;

            if (_punchAttempts.TryGetValue(punchId, out registration))
            {
                //Debug.Log("NAT introduction succeeded to " + endPoint);

                if (IsCancelled(registration.Instance))
                {
                    CancelPunchAttempt(punchId, registration.Instance);
                }
                else
                {
                    RemovePunchAttempt(punchId);
                    registration.Instance.ConnectionEndpoint = endPoint;
                    var connectionId = registration.Instance.ConnectionId;
                    _transporter.Connect(connectionId, registration.Instance.ApprovalSecret, endPoint);
                    AddConnectionAttempt(registration);
                }
            }
        }
 private void CancelPunchAttempt(NatPunchId punchId, ConnectionRegistration registration)
 {
     _punchAttempts.Remove(punchId);
     _cancelledAttempts[registration.ConnectionId.Value] = ConnectionId.NoConnection;
     _connectionIdPool.Put(registration.ConnectionId);
 }
 private void RemovePunchAttempt(NatPunchId punchId)
 {
     _punchAttempts.Remove(punchId);
 }