Serialize() public method

The method is used to populate a NetworkWriter stream from a message object.

public Serialize ( NetworkWriter writer ) : void
writer NetworkWriter Stream to write to.
return void
Ejemplo n.º 1
0
 public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     m_Writer.StartMessage(msgType);
     msg.Serialize(m_Writer);
     m_Writer.FinishMessage();
     return(SendWriter(m_Writer, channelId));
 }
Ejemplo n.º 2
0
 internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
 {
     NetworkWriter writer = new NetworkWriter();
     writer.StartMessage(msgType);
     msg.Serialize(writer);
     writer.FinishMessage();
     this.InvokeBytesOnClient(writer.AsArray(), channelId);
 }
Ejemplo n.º 3
0
 public bool Send(short msgType, MessageBase msg)
 {
     ChannelBuffer.s_SendWriter.StartMessage(msgType);
     msg.Serialize(ChannelBuffer.s_SendWriter);
     ChannelBuffer.s_SendWriter.FinishMessage();
     ++this.numMsgsOut;
     return(this.SendWriter(ChannelBuffer.s_SendWriter));
 }
Ejemplo n.º 4
0
 public bool Send(short msgType, MessageBase msg)
 {
     s_SendWriter.StartMessage(msgType);
     msg.Serialize(s_SendWriter);
     s_SendWriter.FinishMessage();
     numMsgsOut++;
     return(SendWriter(s_SendWriter));
 }
Ejemplo n.º 5
0
        internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
        {
            NetworkWriter writer = new NetworkWriter();

            writer.StartMessage(msgType);
            msg.Serialize(writer);
            writer.FinishMessage();
            this.InvokeBytesOnClient(writer.AsArray(), channelId);
        }
Ejemplo n.º 6
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 bool Send(short msgType, MessageBase msg)
        {
            // build the stream
            s_SendWriter.StartMessage(msgType);
            msg.Serialize(s_SendWriter);
            s_SendWriter.FinishMessage();

            numMsgsOut += 1;
            return SendWriter(s_SendWriter);
        }
Ejemplo n.º 8
0
        public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
        {
            NetworkWriter writer = new NetworkWriter();

            msg.Serialize(writer);

            // pack message and send
            byte[] message = Protocol.PackMessage((ushort)msgType, writer.ToArray());
            return(SendBytes(message, channelId));
        }
        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);
        }
Ejemplo n.º 10
0
 public bool Send(short msgType, MessageBase msg)
 {
     s_SendWriter.StartMessage(msgType);
     msg.Serialize(s_SendWriter);
     s_SendWriter.FinishMessage();
     this.numMsgsOut++;
     return this.SendWriter(s_SendWriter);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// <para>This sends a network message on the connection using a specific transport layer channel.</para>
 /// </summary>
 /// <param name="msgType">The message ID to send.</param>
 /// <param name="msg">The message to send.</param>
 /// <param name="channelId">The transport layer channel to send on.</param>
 /// <returns>
 /// <para>True if the message was sent.</para>
 /// </returns>
 public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     this.m_Writer.StartMessage(msgType);
     msg.Serialize(this.m_Writer);
     this.m_Writer.FinishMessage();
     return this.SendWriter(this.m_Writer, channelId);
 }
        // 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 (!hasMigrationPending())
            {
#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
        {
            return(SendReconnectMessage(extraMessage));
        }
#endif
            return(true);
        }
Ejemplo n.º 13
0
 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, "]" }));
     }
     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);
     return true;
 }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
 public void Write(MessageBase msg)
 {
     msg.Serialize(this);
 }
Ejemplo n.º 16
0
 public virtual bool Send(short msgType, MessageBase msg)
 {
     this.m_writer.StartMessage(msgType);
       msg.Serialize(this.m_writer);
       this.m_writer.FinishMessage();
       return this.SendWriter(this.m_writer, 0);
 }
Ejemplo n.º 17
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;
 }
Ejemplo n.º 18
0
 internal bool InvokeHandlerOnServer(ULocalConnectionToServer conn, short msgType, MessageBase msg, int channelId)
 {
     if (this.m_MessageHandlers.GetHandler(msgType) != null)
       {
     NetworkConnection networkConnection = this.m_Connections.Get(conn.connectionId);
     if (networkConnection != null)
     {
       ULocalConnectionToClient connectionToClient = (ULocalConnectionToClient) networkConnection;
       NetworkWriter writer = new NetworkWriter();
       msg.Serialize(writer);
       NetworkReader reader = new NetworkReader(writer);
       this.m_MessageHandlers.InvokeHandler(msgType, (NetworkConnection) connectionToClient, reader, channelId);
       return true;
     }
     if (LogFilter.logError)
       Debug.LogError((object) ("Local invoke: Failed to find local connection to invoke handler on [connectionId=" + (object) conn.connectionId + "]"));
     return false;
       }
       if (LogFilter.logError)
     Debug.LogError((object) ("Local invoke: Failed to find message handler for message ID " + (object) msgType));
       return false;
 }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
 public bool Send(short msgType, MessageBase msg)
 {
   ChannelBuffer.s_SendWriter.StartMessage(msgType);
   msg.Serialize(ChannelBuffer.s_SendWriter);
   ChannelBuffer.s_SendWriter.FinishMessage();
   ++this.numMsgsOut;
   return this.SendWriter(ChannelBuffer.s_SendWriter);
 }
Ejemplo n.º 21
0
 public void Write(MessageBase msg)
 {
   msg.Serialize(this);
 }
Ejemplo n.º 22
0
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            bool result;

            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                result = false;
            }
            else if (playerControllerId > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError(string.Concat(new object[]
                    {
                        "ClientScene::AddPlayer: playerControllerId of ",
                        playerControllerId,
                        " is too high, max is ",
                        32
                    }));
                }
                result = false;
            }
            else
            {
                if (playerControllerId > 16)
                {
                    if (LogFilter.logWarn)
                    {
                        Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + 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("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))
                {
                    if (playerController.IsValid && playerController.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 [",
                        ClientScene.s_ReadyConnection,
                        "]"
                    }));
                }
                if (!ClientScene.hasMigrationPending())
                {
                    AddPlayerMessage addPlayerMessage = new AddPlayerMessage();
                    addPlayerMessage.playerControllerId = playerControllerId;
                    if (extraMessage != null)
                    {
                        NetworkWriter networkWriter = new NetworkWriter();
                        extraMessage.Serialize(networkWriter);
                        addPlayerMessage.msgData = networkWriter.ToArray();
                        addPlayerMessage.msgSize = (int)networkWriter.Position;
                    }
                    ClientScene.s_ReadyConnection.Send(37, addPlayerMessage);
                    result = true;
                }
                else
                {
                    result = ClientScene.SendReconnectMessage(extraMessage);
                }
            }
            return(result);
        }
Ejemplo n.º 23
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 (!hasMigrationPending())
            {
                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
            {
                return(SendReconnectMessage(extraMessage));
            }
            return(true);
        }
Ejemplo n.º 24
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;
 }
Ejemplo n.º 25
0
        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 > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + 32);
                }
                return(false);
            }
            if (playerControllerId > 16 && 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;
            }
            PlayerController playerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && playerController.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 + "]");
            }
            AddPlayerMessage addPlayerMessage = new AddPlayerMessage();

            addPlayerMessage.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                NetworkWriter networkWriter = new NetworkWriter();
                extraMessage.Serialize(networkWriter);
                addPlayerMessage.msgData = networkWriter.ToArray();
                addPlayerMessage.msgSize = networkWriter.Position;
            }
            s_ReadyConnection.Send(37, addPlayerMessage);
            return(true);
        }
Ejemplo n.º 26
0
 internal bool InvokeHandlerOnServer(ULocalConnectionToServer conn, short msgType, MessageBase msg, int channelId)
 {
     if (handlers.ContainsKey(msgType) && (this.m_LocalConnection != null))
     {
         NetworkWriter writer = new NetworkWriter();
         msg.Serialize(writer);
         NetworkReader reader = new NetworkReader(writer);
         this.m_LocalConnection.InvokeHandler(msgType, reader, channelId);
         return true;
     }
     if (LogFilter.logError)
     {
         Debug.LogError(string.Concat(new object[] { "Local invoke: Failed to find local connection to invoke handler on [connectionId=", conn.connectionId, "] for MsgId:", msgType }));
     }
     return false;
 }