This network message is used when a client reconnect to the new host of a game.

Inheritance: UnityEngine.Networking.MessageBase
 /// <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;
 }
Esempio n. 2
0
 /// <summary>
 /// <para>This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is 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)
 {
     PlayerController controller;
     if (playerControllerId < 0)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
         }
         return false;
     }
     if (playerControllerId > 0x20)
     {
         if (LogFilter.logError)
         {
             Debug.LogError(string.Concat(new object[] { "ClientScene::AddPlayer: playerControllerId of ", playerControllerId, " is too high, max is ", 0x20 }));
         }
         return false;
     }
     if ((playerControllerId > 0x10) && LogFilter.logWarn)
     {
         Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
     }
     while (playerControllerId >= s_LocalPlayers.Count)
     {
         s_LocalPlayers.Add(new PlayerController());
     }
     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;
     }
     if (s_ReadyConnection.GetPlayerController(playerControllerId, out controller) && (controller.IsValid && (controller.gameObject != null)))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
         }
         return false;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log(string.Concat(new object[] { "ClientScene::AddPlayer() for ID ", playerControllerId, " called with connection [", s_ReadyConnection, "]" }));
     }
     if (s_ReconnectId == -1)
     {
         AddPlayerMessage msg = new AddPlayerMessage {
             playerControllerId = playerControllerId
         };
         if (extraMessage != null)
         {
             NetworkWriter writer = new NetworkWriter();
             extraMessage.Serialize(writer);
             msg.msgData = writer.ToArray();
             msg.msgSize = writer.Position;
         }
         s_ReadyConnection.Send(0x25, msg);
     }
     else
     {
         if (LogFilter.logDebug)
         {
             Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
         }
         if (s_Peers == null)
         {
             SetReconnectId(-1, null);
             if (LogFilter.logError)
             {
                 Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
             }
             return false;
         }
         for (int i = 0; i < s_Peers.Length; i++)
         {
             PeerInfoMessage message2 = s_Peers[i];
             if ((message2.playerIds != null) && (message2.connectionId == s_ReconnectId))
             {
                 for (int j = 0; j < message2.playerIds.Length; j++)
                 {
                     ReconnectMessage message3 = new ReconnectMessage {
                         oldConnectionId = s_ReconnectId,
                         netId = message2.playerIds[j].netId,
                         playerControllerId = message2.playerIds[j].playerControllerId
                     };
                     if (extraMessage != null)
                     {
                         NetworkWriter writer2 = new NetworkWriter();
                         extraMessage.Serialize(writer2);
                         message3.msgData = writer2.ToArray();
                         message3.msgSize = writer2.Position;
                     }
                     s_ReadyConnection.Send(0x2f, message3);
                 }
             }
         }
         SetReconnectId(-1, null);
     }
     return true;
 }