private IEnumerator <WaitCommand> RegisterPlayerInternal(
            ConnectionId connectionId,
            ApprovalSecret approvalSecret,
            Action <ApprovalSecret> approve,
            Action <ApprovalSecret> deny)
        {
            while (!_natFacilitatorConnection.ExternalEndpoint.IsResultAvailable)
            {
                yield return(WaitCommand.WaitForNextFrame);
            }

            var externalEndpoint = _natFacilitatorConnection.ExternalEndpoint.Result;

            var playerSessionResult = new AsyncResult <Maybe <PlayerSessionInfo> >();
            var clientSessionId     = new ClientSessionId(approvalSecret.Value);

            _masterServerClient.Client.GetPlayerInfo(externalEndpoint, clientSessionId,
                                                     (statusCode, p) => {
                if (statusCode == HttpStatusCode.OK)
                {
                    playerSessionResult.SetResult(Maybe.Just(p));
                }
                else
                {
                    playerSessionResult.SetResult(Maybe.Nothing <PlayerSessionInfo>());
                }
            });

            while (!playerSessionResult.IsResultAvailable)
            {
                yield return(WaitCommand.WaitForNextFrame);
            }

            var playerSessionInfo = playerSessionResult.Result;

            if (playerSessionInfo.IsJust && !_clientSessions.Values.Contains(clientSessionId))
            {
                var playerInfo = playerSessionInfo.Value.PlayerInfo;
                Debug.Log("'" + playerInfo.Name + "' connected");
                // TODO:
                // Store player info and use it to enhance the replicated pilot so
                // that players can see who's flying that pilot
                _connectedClients[connectionId] = playerInfo;
                _clientSessions[connectionId]   = clientSessionId;
                approve(approvalSecret);
            }
            else
            {
                Debug.Log("Player never registered at the master server, or player was already registered. Denying connection");
                deny(approvalSecret);
            }
        }
 private void RegisterPlayer(ConnectionId connectionId, ApprovalSecret approvalSecret, Action <ApprovalSecret> approve, Action <ApprovalSecret> deny)
 {
     _coroutineScheduler.Run(RegisterPlayerInternal(connectionId, approvalSecret, approve, deny));
 }
Esempio n. 3
0
        public IEnumerator <WaitCommand> Join(IPEndPoint endPoint,
                                              int clientPort = -1,
                                              OnConnectionEstablished onEstablished = null,
                                              OnConnectionFailure onFailure         = null,
                                              OnDisconnected onDisconnected         = null)
        {
            _outstandingPings.Clear();
            _frameId          = 0;
            _isJoinInProgress = true;

            var    sessionIdResult = new AsyncResult <JoinResponse>();
            string password        = null;

            _masterServerClient.Client.Join(endPoint, password, (statusCode, s) => {
                sessionIdResult.SetResult(s);
            });

            while (!sessionIdResult.IsResultAvailable)
            {
                yield return(WaitCommand.WaitForNextFrame);
            }

            if (sessionIdResult.Result != null)
            {
                _sessionId = sessionIdResult.Result.SessionId;

                var netConfig = new NetPeerConfiguration(NetworkConfig.GameId);
                // Kind of arbitrary, we need to have enough room for connection attempts
                // and cancellation running simultaneously but we don't need to have an unlimited number
                netConfig.MaximumConnections = 16;
                if (clientPort > -1)
                {
                    netConfig.Port = clientPort;
                }
                _transporter.Open(netConfig);

                var approvalSecret = new ApprovalSecret(_sessionId.Value.Value);
                _hostConnectionId = _networkSystems.ConnectionManager.Connect(endPoint,
                                                                              approvalSecret,
                                                                              (connId, endpoint) => {
                    _isJoinInProgress = false;
                    OnConnectionEstablished(connId, endpoint);
                    if (onEstablished != null)
                    {
                        onEstablished(connId, endpoint);
                    }
                },
                                                                              (endpoint, exception) => {
                    _isJoinInProgress = false;
                    OnConnectionFailure(endpoint, exception);
                    if (onFailure != null)
                    {
                        onFailure(endpoint, exception);
                    }
                },
                                                                              connId => {
                    _isJoinInProgress = false;
                    OnDisconnected(connId);
                    if (onDisconnected != null)
                    {
                        onDisconnected(connId);
                    }
                });
            }
            else
            {
                OnConnectionFailure(endPoint, new Exception("Master server did not allow join on " + endPoint));
            }
        }