コード例 #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;
                }
            }
        }
コード例 #2
0
        private void OnNatPunchFailure(NatPunchId punchId, IPEndPoint actualEndpoint)
        {
            IPooledObject <PunchAttempt> attempt;

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

            if (_natPunchAttempts.TryGetValue(punchId, out attempt))
            {
                attempt.OnFailure(punchId);
                RemoveNatPunchAttempt(attempt);
            }
        }
コード例 #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();
 }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #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);
            }
        }
コード例 #10
0
        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());
        }
コード例 #11
0
        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);
                }
            }
        }
コード例 #12
0
 private void CancelPunchAttempt(NatPunchId punchId, ConnectionRegistration registration)
 {
     _punchAttempts.Remove(punchId);
     _cancelledAttempts[registration.ConnectionId.Value] = ConnectionId.NoConnection;
     _connectionIdPool.Put(registration.ConnectionId);
 }
コード例 #13
0
 private void RemovePunchAttempt(NatPunchId punchId)
 {
     _punchAttempts.Remove(punchId);
 }