Exemplo n.º 1
0
        public static bool SendReconnectMessage(MessageBase extraMessage)
        {
            bool result;

            if (!ClientScene.hasMigrationPending())
            {
                result = false;
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log("ClientScene::AddPlayer reconnect " + ClientScene.s_ReconnectId);
                }
                if (ClientScene.s_Peers == null)
                {
                    ClientScene.SetReconnectId(-1, null);
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
                    }
                    result = false;
                }
                else
                {
                    for (int i = 0; i < ClientScene.s_Peers.Length; i++)
                    {
                        PeerInfoMessage peerInfoMessage = ClientScene.s_Peers[i];
                        if (peerInfoMessage.playerIds != null)
                        {
                            if (peerInfoMessage.connectionId == ClientScene.s_ReconnectId)
                            {
                                for (int j = 0; j < peerInfoMessage.playerIds.Length; j++)
                                {
                                    ReconnectMessage reconnectMessage = new ReconnectMessage();
                                    reconnectMessage.oldConnectionId    = ClientScene.s_ReconnectId;
                                    reconnectMessage.netId              = peerInfoMessage.playerIds[j].netId;
                                    reconnectMessage.playerControllerId = peerInfoMessage.playerIds[j].playerControllerId;
                                    if (extraMessage != null)
                                    {
                                        NetworkWriter networkWriter = new NetworkWriter();
                                        extraMessage.Serialize(networkWriter);
                                        reconnectMessage.msgData = networkWriter.ToArray();
                                        reconnectMessage.msgSize = (int)networkWriter.Position;
                                    }
                                    ClientScene.s_ReadyConnection.Send(47, reconnectMessage);
                                }
                            }
                        }
                    }
                    ClientScene.SetReconnectId(-1, null);
                    result = true;
                }
            }
            return(result);
        }
        public static bool SendReconnectMessage(MessageBase extraMessage)
        {
            if (!hasMigrationPending())
            {
                return(false);
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
            }

            if (s_Peers == null)
            {
                SetReconnectId(ReconnectIdInvalid, null);
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
                }
                return(false);
            }

            // reconnect all the players
            for (int i = 0; i < s_Peers.Length; i++)
            {
                var peer = s_Peers[i];
                if (peer.playerIds == null)
                {
                    // this could be empty if this peer had no players
                    continue;
                }
                if (peer.connectionId == s_ReconnectId)
                {
                    for (int pid = 0; pid < peer.playerIds.Length; pid++)
                    {
                        var msg = new ReconnectMessage();
                        msg.oldConnectionId    = s_ReconnectId;
                        msg.netId              = peer.playerIds[pid].netId;
                        msg.playerControllerId = peer.playerIds[pid].playerControllerId;
                        if (extraMessage != null)
                        {
                            var writer = new NetworkWriter();
                            extraMessage.Serialize(writer);
                            msg.msgData = writer.ToArray();
                            msg.msgSize = writer.Position;
                        }

                        s_ReadyConnection.Send(MsgType.ReconnectPlayer, msg);
                    }
                }
            }
            // this should only be done once.
            SetReconnectId(ReconnectIdInvalid, null);
            return(true);
        }
        private void OnServerReconnectPlayerMessage(NetworkMessage netMsg)
        {
            ReconnectMessage reconnectMessage = netMsg.ReadMessage <ReconnectMessage>();

            if (LogFilter.logDev)
            {
                Debug.Log((object)("OnReconnectMessage: connId=" + (object)reconnectMessage.oldConnectionId + " playerControllerId:" + (object)reconnectMessage.playerControllerId + " netId:" + (object)reconnectMessage.netId));
            }
            GameObject pendingPlayer = this.FindPendingPlayer(reconnectMessage.oldConnectionId, reconnectMessage.netId, reconnectMessage.playerControllerId);

            if ((Object)pendingPlayer == (Object)null)
            {
                if (!LogFilter.logError)
                {
                    return;
                }
                Debug.LogError((object)("OnReconnectMessage connId=" + (object)reconnectMessage.oldConnectionId + " player null for netId:" + (object)reconnectMessage.netId + " msg.playerControllerId:" + (object)reconnectMessage.playerControllerId));
            }
            else if (pendingPlayer.activeSelf)
            {
                if (!LogFilter.logError)
                {
                    return;
                }
                Debug.LogError((object)("OnReconnectMessage connId=" + (object)reconnectMessage.oldConnectionId + " player already active?"));
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log((object)("OnReconnectMessage: player=" + (object)pendingPlayer));
                }
                NetworkReader extraMessageReader = (NetworkReader)null;
                if (reconnectMessage.msgSize != 0)
                {
                    extraMessageReader = new NetworkReader(reconnectMessage.msgData);
                }
                if ((int)reconnectMessage.playerControllerId != -1)
                {
                    if (extraMessageReader == null)
                    {
                        this.OnServerReconnectPlayer(netMsg.conn, pendingPlayer, reconnectMessage.oldConnectionId, reconnectMessage.playerControllerId);
                    }
                    else
                    {
                        this.OnServerReconnectPlayer(netMsg.conn, pendingPlayer, reconnectMessage.oldConnectionId, reconnectMessage.playerControllerId, extraMessageReader);
                    }
                }
                else
                {
                    this.OnServerReconnectObject(netMsg.conn, pendingPlayer, reconnectMessage.oldConnectionId);
                }
            }
        }
Exemplo n.º 4
0
        private void OnServerReconnectPlayerMessage(NetworkMessage netMsg)
        {
            ReconnectMessage reconnectMessage = netMsg.ReadMessage <ReconnectMessage>();

            if (LogFilter.logDev)
            {
                Debug.Log("OnReconnectMessage: connId=" + reconnectMessage.oldConnectionId + " playerControllerId:" + reconnectMessage.playerControllerId + " netId:" + reconnectMessage.netId);
            }
            GameObject gameObject = FindPendingPlayer(reconnectMessage.oldConnectionId, reconnectMessage.netId, reconnectMessage.playerControllerId);

            if (gameObject == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("OnReconnectMessage connId=" + reconnectMessage.oldConnectionId + " player null for netId:" + reconnectMessage.netId + " msg.playerControllerId:" + reconnectMessage.playerControllerId);
                }
                return;
            }
            if (gameObject.activeSelf)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("OnReconnectMessage connId=" + reconnectMessage.oldConnectionId + " player already active?");
                }
                return;
            }
            if (LogFilter.logDebug)
            {
                Debug.Log("OnReconnectMessage: player=" + gameObject);
            }
            NetworkReader networkReader = null;

            if (reconnectMessage.msgSize != 0)
            {
                networkReader = new NetworkReader(reconnectMessage.msgData);
            }
            if (reconnectMessage.playerControllerId != -1)
            {
                if (networkReader == null)
                {
                    OnServerReconnectPlayer(netMsg.conn, gameObject, reconnectMessage.oldConnectionId, reconnectMessage.playerControllerId);
                }
                else
                {
                    OnServerReconnectPlayer(netMsg.conn, gameObject, reconnectMessage.oldConnectionId, reconnectMessage.playerControllerId, networkReader);
                }
            }
            else
            {
                OnServerReconnectObject(netMsg.conn, gameObject, reconnectMessage.oldConnectionId);
            }
        }
        private void OnServerReconnectPlayerMessage(NetworkMessage netMsg)
        {
            ReconnectMessage message = netMsg.ReadMessage <ReconnectMessage>();

            if (LogFilter.logDev)
            {
                Debug.Log(string.Concat(new object[] { "OnReconnectMessage: connId=", message.oldConnectionId, " playerControllerId:", message.playerControllerId, " netId:", message.netId }));
            }
            GameObject oldPlayer = this.FindPendingPlayer(message.oldConnectionId, message.netId, message.playerControllerId);

            if (oldPlayer == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError(string.Concat(new object[] { "OnReconnectMessage connId=", message.oldConnectionId, " player null for netId:", message.netId, " msg.playerControllerId:", message.playerControllerId }));
                }
            }
            else if (oldPlayer.activeSelf)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("OnReconnectMessage connId=" + message.oldConnectionId + " player already active?");
                }
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log("OnReconnectMessage: player=" + oldPlayer);
                }
                NetworkReader extraMessageReader = null;
                if (message.msgSize != 0)
                {
                    extraMessageReader = new NetworkReader(message.msgData);
                }
                if (message.playerControllerId != -1)
                {
                    if (extraMessageReader == null)
                    {
                        this.OnServerReconnectPlayer(netMsg.conn, oldPlayer, message.oldConnectionId, message.playerControllerId);
                    }
                    else
                    {
                        this.OnServerReconnectPlayer(netMsg.conn, oldPlayer, message.oldConnectionId, message.playerControllerId, extraMessageReader);
                    }
                }
                else
                {
                    this.OnServerReconnectObject(netMsg.conn, oldPlayer, message.oldConnectionId);
                }
            }
        }
Exemplo n.º 6
0
    protected void TryAddToWaitingReconnectList(IPEndPoint ipEndPoint, ServerType serverType, Action onConnectSuccessCallBack, Action onConnectFailedCallBack)
    {
        Debug.Log("TryAddToWaitingReconnectList");
        for (int i = 0; i < this.waitingReconnectList.get_Count(); i++)
        {
            if (this.waitingReconnectList.get_Item(i).ipEndPoint == ipEndPoint && this.waitingReconnectList.get_Item(i).serverType == serverType)
            {
                return;
            }
        }
        ReconnectMessage reconnectMessage = default(ReconnectMessage);

        reconnectMessage.ipEndPoint = ipEndPoint;
        reconnectMessage.serverType = serverType;
        reconnectMessage.onConnectSuccessCallBack = onConnectSuccessCallBack;
        reconnectMessage.onConnectFailedCallBack  = onConnectFailedCallBack;
        this.waitingReconnectList.Add(reconnectMessage);
        Debug.Log("After AddToWaitingReconnectList: " + this.waitingReconnectList.get_Count());
    }
Exemplo n.º 7
0
        // use this to implicitly become ready
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + PlayerController.MaxPlayersPerClient);
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient / 2)
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                }
            }

            // fill out local players array
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }

            // ensure valid ready connection
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            PlayerController existingPlayerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out existingPlayerController))
            {
                if (existingPlayerController.IsValid && existingPlayerController.gameObject != null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                    }
                    return(false);
                }
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }

#if ENABLE_UNET_HOST_MIGRATION
            if (s_ReconnectId == ReconnectIdInvalid)
            {
#endif
            var msg = new AddPlayerMessage();
            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
                msg.msgSize = writer.Position;
            }
            s_ReadyConnection.Send(MsgType.AddPlayer, msg);
#if ENABLE_UNET_HOST_MIGRATION
        }

        else
        {
            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
            }

            if (s_Peers == null)
            {
                SetReconnectId(ReconnectIdInvalid, null);
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
                }
                return(false);
            }

            // reconnect all the players
            for (int i = 0; i < s_Peers.Length; i++)
            {
                var peer = s_Peers[i];
                if (peer.playerIds == null)
                {
                    // this coluld be empty if this peer had no players
                    continue;
                }
                if (peer.connectionId == s_ReconnectId)
                {
                    for (int pid = 0; pid < peer.playerIds.Length; pid++)
                    {
                        var msg = new ReconnectMessage();
                        msg.oldConnectionId    = s_ReconnectId;
                        msg.netId              = peer.playerIds[pid].netId;
                        msg.playerControllerId = peer.playerIds[pid].playerControllerId;
                        if (extraMessage != null)
                        {
                            var writer = new NetworkWriter();
                            extraMessage.Serialize(writer);
                            msg.msgData = writer.ToArray();
                            msg.msgSize = writer.Position;
                        }

                        s_ReadyConnection.Send(MsgType.ReconnectPlayer, msg);
                    }
                }
            }
            // this should only be done once.
            SetReconnectId(ReconnectIdInvalid, null);
        }
#endif
            return(true);
        }
Exemplo n.º 8
0
        /// <summary>
        ///   <para>This adds a player object for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer will be called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
        /// </summary>
        /// <param name="readyConn">The connection to become ready for this client.</param>
        /// <param name="playerControllerId">The local player ID number.</param>
        /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
        /// <returns>
        ///   <para>True if player was added.</para>
        /// </returns>
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if ((int)playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is negative"));
                }
                return(false);
            }
            if ((int)playerControllerId > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is too high, max is " + (object)32));
                }
                return(false);
            }
            if ((int)playerControllerId > 16 && LogFilter.logWarn)
            {
                Debug.LogWarning((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is unusually high"));
            }
            while ((int)playerControllerId >= ClientScene.s_LocalPlayers.Count)
            {
                ClientScene.s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!ClientScene.s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError((object)"Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                ClientScene.s_IsReady         = true;
                ClientScene.s_ReadyConnection = readyConn;
            }
            PlayerController playerController;

            if (ClientScene.s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && (Object)playerController.gameObject != (Object)null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " already in use."));
                }
                return(false);
            }
            if (LogFilter.logDebug)
            {
                Debug.Log((object)("ClientScene::AddPlayer() for ID " + (object)playerControllerId + " called with connection [" + (object)ClientScene.s_ReadyConnection + "]"));
            }
            if (ClientScene.s_ReconnectId == -1)
            {
                AddPlayerMessage addPlayerMessage = new AddPlayerMessage();
                addPlayerMessage.playerControllerId = playerControllerId;
                if (extraMessage != null)
                {
                    NetworkWriter writer = new NetworkWriter();
                    extraMessage.Serialize(writer);
                    addPlayerMessage.msgData = writer.ToArray();
                    addPlayerMessage.msgSize = (int)writer.Position;
                }
                ClientScene.s_ReadyConnection.Send((short)37, (MessageBase)addPlayerMessage);
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log((object)("ClientScene::AddPlayer reconnect " + (object)ClientScene.s_ReconnectId));
                }
                if (ClientScene.s_Peers == null)
                {
                    ClientScene.SetReconnectId(-1, (PeerInfoMessage[])null);
                    if (LogFilter.logError)
                    {
                        Debug.LogError((object)"ClientScene::AddPlayer: reconnecting, but no peers.");
                    }
                    return(false);
                }
                foreach (PeerInfoMessage peer in ClientScene.s_Peers)
                {
                    if (peer.playerIds != null && peer.connectionId == ClientScene.s_ReconnectId)
                    {
                        foreach (PeerInfoPlayer playerId in peer.playerIds)
                        {
                            ReconnectMessage reconnectMessage = new ReconnectMessage();
                            reconnectMessage.oldConnectionId    = ClientScene.s_ReconnectId;
                            reconnectMessage.netId              = playerId.netId;
                            reconnectMessage.playerControllerId = playerId.playerControllerId;
                            if (extraMessage != null)
                            {
                                NetworkWriter writer = new NetworkWriter();
                                extraMessage.Serialize(writer);
                                reconnectMessage.msgData = writer.ToArray();
                                reconnectMessage.msgSize = (int)writer.Position;
                            }
                            ClientScene.s_ReadyConnection.Send((short)47, (MessageBase)reconnectMessage);
                        }
                    }
                }
                ClientScene.SetReconnectId(-1, (PeerInfoMessage[])null);
            }
            return(true);
        }