Network message classes should be derived from this class. These message classes can then be sent using the various Send functions of NetworkConnection, NetworkClient and NetworkServer.

Beispiel #1
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);
 }
 public static bool SendUnreliableToReady(GameObject contextObj, short msgType, MessageBase msg)
 {
     if (LogFilter.logDev)
     Debug.Log((object) ("Server.SendUnreliableToReady id:" + (object) msgType));
       if ((UnityEngine.Object) contextObj == (UnityEngine.Object) null)
       {
     for (int localIndex = NetworkServer.s_Instance.m_Connections.LocalIndex; localIndex < NetworkServer.s_Instance.m_Connections.Count; ++localIndex)
     {
       NetworkConnection networkConnection = NetworkServer.s_Instance.m_Connections.Get(localIndex);
       if (networkConnection != null && networkConnection.isReady)
     networkConnection.SendUnreliable(msgType, msg);
     }
     return true;
       }
       bool flag = true;
       NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
       int count = component.observers.Count;
       for (int index = 0; index < count; ++index)
       {
     NetworkConnection networkConnection = component.observers[index];
     if (networkConnection.isReady)
       flag &= networkConnection.SendUnreliable(msgType, msg);
       }
       return flag;
 }
 public static void SendToClientOfPlayer(GameObject player, short msgType, MessageBase msg)
 {
     NetworkConnection conn;
       if (NetworkServer.instance.m_Connections.ContainsPlayer(player, out conn))
       {
     conn.Send(msgType, msg);
       }
       else
       {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("Failed to send message to player object '" + player.name + ", not found in connection list"));
       }
 }
 private static bool SendToObservers(GameObject contextObj, short msgType, MessageBase msg)
 {
     if (LogFilter.logDev)
     Debug.Log((object) ("Server.SendToObservers id:" + (object) msgType));
       bool flag = true;
       NetworkIdentity component = contextObj.GetComponent<NetworkIdentity>();
       if ((UnityEngine.Object) component == (UnityEngine.Object) null || component.observers == null)
     return false;
       int count = component.observers.Count;
       for (int index = 0; index < count; ++index)
       {
     NetworkConnection networkConnection = component.observers[index];
     flag &= networkConnection.Send(msgType, msg);
       }
       return flag;
 }
 public bool SendUnreliable(short msgType, MessageBase msg)
 {
     if (this.m_Connection == null)
     return false;
       NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 0, msgType.ToString() + ":" + msg.GetType().Name, 1);
       return this.m_Connection.SendUnreliable(msgType, msg);
 }
 void HandleConnectMessage(UnityEngine.Networking.MessageBase msg)
 {
     SceneManager.LoadScene("ClientController");
 }
 /// <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);
 }
 public virtual bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     if (this.CheckChannel(channelId))
     return this.m_Channels[channelId].Send(msgType, msg);
       return false;
 }
 public override bool SendUnreliable(short msgType, MessageBase msg)
 {
     return(this.m_LocalServer.InvokeHandlerOnServer(this, msgType, msg, 1));
 }
 public override bool SendUnreliable(short msgType, MessageBase msg)
 {
     return(m_LocalServer.InvokeHandlerOnServer(this, msgType, msg, Channels.DefaultUnreliable));
 }
 public override bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     m_LocalClient.InvokeHandlerOnClient(msgType, msg, channelId);
     return(true);
 }
 public override bool SendUnreliable(short msgType, MessageBase msg)
 {
     m_LocalClient.InvokeHandlerOnClient(msgType, msg, Channels.DefaultUnreliable);
     return(true);
 }
Beispiel #13
0
		void ServerSendMessage(Message.ID id, MessageBase msg, byte channel, List<ServerPlayer> players)
		{
			foreach(var p in players)
			{
				p.connection.SendByChannel((short)id, msg, channel);
			}
		}
Beispiel #14
0
		void ServerSendMessage(Message.ID id, MessageBase msg, byte channel, ServerPlayer sp)
		{
			if(sp.connection.isConnected)
				sp.connection.SendByChannel((short)id, msg, channel);
			else
				common.printConsole (tag, "Player is not connected on client " + sp.connection.address, true);
		}
 public override bool SendUnreliable(short msgType, MessageBase msg)
 {
     this.m_LocalClient.InvokeHandlerOnClient(msgType, msg, 1);
     return true;
 }
 /// <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;
 }
 public override bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     return(this.m_LocalServer.InvokeHandlerOnServer(this, msgType, msg, channelId));
 }
 public override bool SendUnreliable(short msgType, MessageBase msg)
 {
     return this.m_LocalServer.InvokeHandlerOnServer(this, msgType, msg, 1);
 }
Beispiel #19
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);
        }
Beispiel #20
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);
        }
Beispiel #21
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);
 }
Beispiel #22
0
		void ServerSendMessage(Message.ID id, MessageBase msg, byte channel, List<NetworkConnection> playerConnectionList)
		{
			foreach(NetworkConnection nc in playerConnectionList)
			{
				nc.SendByChannel((short)id, msg, channel);
			}
		}
Beispiel #23
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
            foreach (var peer in s_Peers)
            {
                if (peer.playerIds == null)
                {
                    // this coluld be empty if this peer had no players
                    continue;
                }
                if (peer.connectionId == s_ReconnectId)
                {
                    foreach (var playerInfo in peer.playerIds)
                    {
                        var msg = new ReconnectMessage();
                        msg.oldConnectionId    = s_ReconnectId;
                        msg.netId              = playerInfo.netId;
                        msg.playerControllerId = playerInfo.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);
        }
 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;
 }
Beispiel #25
0
 public virtual bool Send(short msgType, MessageBase msg)
 {
     return(SendByChannel(msgType, msg, Channels.DefaultReliable));
 }
 public static void SendToClient(int connectionId, short msgType, MessageBase msg)
 {
     NetworkConnection networkConnection = NetworkServer.instance.m_Connections.Get(connectionId);
       if (networkConnection != null)
       {
     networkConnection.Send(msgType, msg);
       }
       else
       {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("Failed to send message to connection ID '" + (object) connectionId + ", not found in connection list"));
       }
 }
Beispiel #27
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;
 }
 public static bool SendUnreliableToAll(short msgType, MessageBase msg)
 {
     if (LogFilter.logDev)
     Debug.Log((object) ("Server.SendUnreliableToAll msgType:" + (object) msgType));
       ConnectionArray connectionArray = NetworkServer.instance.m_Connections;
       bool flag = true;
       for (int localIndex = connectionArray.LocalIndex; localIndex < connectionArray.Count; ++localIndex)
       {
     NetworkConnection networkConnection = connectionArray.Get(localIndex);
     if (networkConnection != null)
       flag &= networkConnection.SendUnreliable(msgType, msg);
       }
       return flag;
 }
Beispiel #29
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 (!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);
        }
 public override bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     this.m_LocalClient.InvokeHandlerOnClient(msgType, msg, channelId);
     return true;
 }
 public void Write(MessageBase msg)
 {
     msg.Serialize(this);
 }
Beispiel #32
0
		// ===========================================================================
		// ========================= SEND FUNCTIONS =========================
		// ===========================================================================

		void ClientSendMessage(Message.ID id, MessageBase msg, byte channel)
		{
			if(isConnectedAndAuthenticated())
				myClient.connection.SendByChannel((short)id, msg, channel);
			else
				common.printConsole(tag, "Is not connected to any server", true);
		}
 public virtual bool Send(short msgType, MessageBase msg)
 {
     return(this.SendByChannel(msgType, msg, 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);
 }
 public virtual bool SendUnreliable(short msgType, MessageBase msg)
 {
     return(this.SendByChannel(msgType, msg, 1));
 }
 public override bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     return this.m_LocalServer.InvokeHandlerOnServer(this, msgType, msg, channelId);
 }
Beispiel #37
0
 public bool SendUnreliable(short msgType, MessageBase msg)
 {
     return(SendByChannel(msgType, msg, Channels.DefaultUnreliable));
 }
 /// <summary>
 /// <para>This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.</para>
 /// </summary>
 /// <param name="msgType">The ID of the message to send.</param>
 /// <param name="msg">The message to send.</param>
 /// <returns>
 /// <para>True if the message was sent.</para>
 /// </returns>
 public virtual bool Send(short msgType, MessageBase msg)
 {
     return this.SendByChannel(msgType, msg, 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;
 }
 /// <summary>
 /// <para>This sends a network message with a message ID on the connection. This message is sent on channel one, which by default is the unreliable channel.</para>
 /// </summary>
 /// <param name="msgType">The message ID to send.</param>
 /// <param name="msg">The message to send.</param>
 /// <returns>
 /// <para>True if the message was sent.</para>
 /// </returns>
 public virtual bool SendUnreliable(short msgType, MessageBase msg)
 {
     return this.SendByChannel(msgType, msg, 1);
 }
 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);
 }
 public void Write(MessageBase msg)
 {
   msg.Serialize(this);
 }
 public bool SendByChannel(short msgType, MessageBase msg, int channelId)
 {
     NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 0, msgType.ToString() + ":" + msg.GetType().Name, 1);
       if (this.m_Connection != null)
     return this.m_Connection.SendByChannel(msgType, msg, channelId);
       return false;
 }
 private void BroadCast(short msgType, MessageBase msg, int channelId)
 {
     for(int i = 0; i < NetworkServer.connections.Count; ++i)
     {
         NetworkConnection conn = NetworkServer.connections[i];
         if (conn != null && conn.isReady)
         {
             conn.SendByChannel(msgType, msg, channelId);
         }
     }
 }
Beispiel #45
0
		// ===========================================================================
		// ========================= SEND FUNCTIONS =========================
		// ===========================================================================

		void ServerSendMessage(Message.ID id, MessageBase msg, byte channel, NetworkConnection conn)
		{
			if(conn != null)
				conn.SendByChannel((short)id, msg, channel);
			else
				common.printConsole (tag, "Is not connected to client " + conn.address, true);
		}