Send() public method

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.

public Send ( short msgType, MessageBase msg ) : bool
msgType short The ID of the message to send.
msg MessageBase The message to send.
return bool
        internal void AssignAuthorityCallback(NetworkConnection conn, NetworkIdentity uv, bool authorityState)
        {
            PeerAuthorityMessage msg = new PeerAuthorityMessage {
                connectionId   = conn.connectionId,
                netId          = uv.netId,
                authorityState = authorityState
            };

            if (LogFilter.logDebug)
            {
                Debug.Log("AssignAuthorityCallback send for netId" + uv.netId);
            }
            for (int i = 0; i < NetworkServer.connections.Count; i++)
            {
                NetworkConnection connection = NetworkServer.connections[i];
                if (connection != null)
                {
                    connection.Send(0x12, msg);
                }
            }
        }
        public void SetTrigger(int hash)
        {
            AnimationTriggerMessage animationTriggerMessage = new AnimationTriggerMessage();

            animationTriggerMessage.netId = base.netId;
            animationTriggerMessage.hash  = hash;
            if (base.hasAuthority && base.localPlayerAuthority)
            {
                if (NetworkClient.allClients.Count > 0)
                {
                    NetworkConnection readyConnection = ClientScene.readyConnection;
                    if (readyConnection != null)
                    {
                        readyConnection.Send(42, animationTriggerMessage);
                    }
                }
            }
            else if (base.isServer && !base.localPlayerAuthority)
            {
                NetworkServer.SendToReady(base.gameObject, 42, animationTriggerMessage);
            }
        }
Esempio n. 3
0
        public static bool Ready(NetworkConnection conn)
        {
            bool result;

            if (ClientScene.s_IsReady)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("A connection has already been set as ready. There can only be one.");
                }
                result = false;
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log("ClientScene::Ready() called with connection [" + conn + "]");
                }
                if (conn != null)
                {
                    ReadyMessage msg = new ReadyMessage();
                    conn.Send(35, msg);
                    ClientScene.s_IsReady                 = true;
                    ClientScene.s_ReadyConnection         = conn;
                    ClientScene.s_ReadyConnection.isReady = true;
                    result = true;
                }
                else
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Ready() called with invalid connection object: conn=null");
                    }
                    result = false;
                }
            }
            return(result);
        }
Esempio n. 4
0
 internal static void HideForConnection(NetworkIdentity uv, NetworkConnection conn)
 {
     ObjectDestroyMessage msg = new ObjectDestroyMessage {
         netId = uv.netId
     };
     conn.Send(13, msg);
 }
Esempio n. 5
0
 public void KickPlayer(NetworkConnection conn)
 {
     conn.Send(MsgKicked, new KickMsg());
 }
Esempio n. 6
0
        public void SendPeerInfo()
        {
            if (!m_HostMigration)
            {
                return;
            }
            PeerListMessage        peerListMessage = new PeerListMessage();
            List <PeerInfoMessage> list            = new List <PeerInfoMessage>();
            PeerInfoPlayer         item            = default(PeerInfoPlayer);
            PeerInfoPlayer         item2           = default(PeerInfoPlayer);

            for (int i = 0; i < NetworkServer.connections.Count; i++)
            {
                NetworkConnection networkConnection = NetworkServer.connections[i];
                if (networkConnection == null)
                {
                    continue;
                }
                PeerInfoMessage peerInfoMessage = new PeerInfoMessage();
                NetworkTransport.GetConnectionInfo(NetworkServer.serverHostId, networkConnection.connectionId, out string address, out int port, out NetworkID _, out NodeID _, out byte _);
                peerInfoMessage.connectionId = networkConnection.connectionId;
                peerInfoMessage.port         = port;
                if (i == 0)
                {
                    peerInfoMessage.port    = NetworkServer.listenPort;
                    peerInfoMessage.isHost  = true;
                    peerInfoMessage.address = "<host>";
                }
                else
                {
                    peerInfoMessage.address = address;
                    peerInfoMessage.isHost  = false;
                }
                List <PeerInfoPlayer> list2 = new List <PeerInfoPlayer>();
                for (int j = 0; j < networkConnection.playerControllers.Count; j++)
                {
                    PlayerController playerController = networkConnection.playerControllers[j];
                    if (playerController != null && playerController.unetView != null)
                    {
                        item.netId = playerController.unetView.netId;
                        item.playerControllerId = playerController.unetView.playerControllerId;
                        list2.Add(item);
                    }
                }
                if (networkConnection.clientOwnedObjects != null)
                {
                    foreach (NetworkInstanceId clientOwnedObject in networkConnection.clientOwnedObjects)
                    {
                        GameObject gameObject = NetworkServer.FindLocalObject(clientOwnedObject);
                        if (!(gameObject == null))
                        {
                            NetworkIdentity component = gameObject.GetComponent <NetworkIdentity>();
                            if (component.playerControllerId == -1)
                            {
                                item2.netId = clientOwnedObject;
                                item2.playerControllerId = -1;
                                list2.Add(item2);
                            }
                        }
                    }
                }
                if (list2.Count > 0)
                {
                    peerInfoMessage.playerIds = list2.ToArray();
                }
                list.Add(peerInfoMessage);
            }
            peerListMessage.peers = list.ToArray();
            for (int k = 0; k < NetworkServer.connections.Count; k++)
            {
                NetworkConnection networkConnection2 = NetworkServer.connections[k];
                if (networkConnection2 != null)
                {
                    peerListMessage.oldServerConnectionId = networkConnection2.connectionId;
                    networkConnection2.Send(11, peerListMessage);
                }
            }
        }
        /// <summary>
        ///   <para>This sends the set of peers in the game to all the peers in the game.</para>
        /// </summary>
        public void SendPeerInfo()
        {
            if (!this.m_HostMigration)
            {
                return;
            }
            PeerListMessage        peerListMessage     = new PeerListMessage();
            List <PeerInfoMessage> peerInfoMessageList = new List <PeerInfoMessage>();

            for (int index = 0; index < NetworkServer.connections.Count; ++index)
            {
                NetworkConnection connection = NetworkServer.connections[index];
                if (connection != null)
                {
                    PeerInfoMessage peerInfoMessage = new PeerInfoMessage();
                    string          address;
                    int             port;
                    NetworkID       network;
                    NodeID          dstNode;
                    byte            error;
                    NetworkTransport.GetConnectionInfo(NetworkServer.serverHostId, connection.connectionId, out address, out port, out network, out dstNode, out error);
                    peerInfoMessage.connectionId = connection.connectionId;
                    peerInfoMessage.port         = port;
                    if (index == 0)
                    {
                        peerInfoMessage.port    = NetworkServer.listenPort;
                        peerInfoMessage.isHost  = true;
                        peerInfoMessage.address = "<host>";
                    }
                    else
                    {
                        peerInfoMessage.address = address;
                        peerInfoMessage.isHost  = false;
                    }
                    List <PeerInfoPlayer> peerInfoPlayerList = new List <PeerInfoPlayer>();
                    using (List <PlayerController> .Enumerator enumerator = connection.playerControllers.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            PlayerController current = enumerator.Current;
                            if (current != null && (Object)current.unetView != (Object)null)
                            {
                                PeerInfoPlayer peerInfoPlayer;
                                peerInfoPlayer.netId = current.unetView.netId;
                                peerInfoPlayer.playerControllerId = current.unetView.playerControllerId;
                                peerInfoPlayerList.Add(peerInfoPlayer);
                            }
                        }
                    }
                    if (connection.clientOwnedObjects != null)
                    {
                        using (HashSet <NetworkInstanceId> .Enumerator enumerator = connection.clientOwnedObjects.GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                NetworkInstanceId current     = enumerator.Current;
                                GameObject        localObject = NetworkServer.FindLocalObject(current);
                                if (!((Object)localObject == (Object)null) && (int)localObject.GetComponent <NetworkIdentity>().playerControllerId == -1)
                                {
                                    PeerInfoPlayer peerInfoPlayer;
                                    peerInfoPlayer.netId = current;
                                    peerInfoPlayer.playerControllerId = (short)-1;
                                    peerInfoPlayerList.Add(peerInfoPlayer);
                                }
                            }
                        }
                    }
                    if (peerInfoPlayerList.Count > 0)
                    {
                        peerInfoMessage.playerIds = peerInfoPlayerList.ToArray();
                    }
                    peerInfoMessageList.Add(peerInfoMessage);
                }
            }
            peerListMessage.peers = peerInfoMessageList.ToArray();
            for (int index = 0; index < NetworkServer.connections.Count; ++index)
            {
                NetworkConnection connection = NetworkServer.connections[index];
                if (connection != null)
                {
                    peerListMessage.oldServerConnectionId = connection.connectionId;
                    connection.Send((short)11, (MessageBase)peerListMessage);
                }
            }
        }
Esempio n. 8
0
 public bool RemoveClientAuthority(NetworkConnection conn)
 {
     if (!this.isServer)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RemoveClientAuthority can only be call on the server for spawned objects.");
         }
         return false;
     }
     if (this.connectionToClient != null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RemoveClientAuthority cannot remove authority for a player object");
         }
         return false;
     }
     if (this.m_ClientAuthorityOwner == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RemoveClientAuthority for " + base.gameObject + " has no clientAuthority owner.");
         }
         return false;
     }
     if (this.m_ClientAuthorityOwner != conn)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RemoveClientAuthority for " + base.gameObject + " has different owner.");
         }
         return false;
     }
     this.m_ClientAuthorityOwner.RemoveOwnedObject(this);
     this.m_ClientAuthorityOwner = null;
     ClientAuthorityMessage msg = new ClientAuthorityMessage {
         netId = this.netId,
         authority = false
     };
     conn.Send(15, msg);
     return true;
 }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
 /// <summary>
 ///   <para>Removes ownership for an object for a client by its conneciton.</para>
 /// </summary>
 /// <param name="conn">The connection of the client to remove authority for.</param>
 /// <returns>
 ///   <para>True if authority is removed.</para>
 /// </returns>
 public bool RemoveClientAuthority(NetworkConnection conn)
 {
   if (!this.isServer)
   {
     if (LogFilter.logError)
       Debug.LogError((object) "RemoveClientAuthority can only be call on the server for spawned objects.");
     return false;
   }
   if (this.connectionToClient != null)
   {
     if (LogFilter.logError)
       Debug.LogError((object) "RemoveClientAuthority cannot remove authority for a player object");
     return false;
   }
   if (this.m_ClientAuthorityOwner == null)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("RemoveClientAuthority for " + (object) this.gameObject + " has no clientAuthority owner."));
     return false;
   }
   if (this.m_ClientAuthorityOwner != conn)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("RemoveClientAuthority for " + (object) this.gameObject + " has different owner."));
     return false;
   }
   this.m_ClientAuthorityOwner.RemoveOwnedObject(this);
   this.m_ClientAuthorityOwner = (NetworkConnection) null;
   this.ForceAuthority(true);
   conn.Send((short) 15, (MessageBase) new ClientAuthorityMessage()
   {
     netId = this.netId,
     authority = false
   });
   if (NetworkIdentity.clientAuthorityCallback != null)
     NetworkIdentity.clientAuthorityCallback(conn, this, false);
   return true;
 }
 /// <summary>
 ///   <para>This assigns control of an object to a client via the client's NetworkConnection.</para>
 /// </summary>
 /// <param name="conn">The connection of the client to assign authority to.</param>
 /// <returns>
 ///   <para>True if authority was assigned.</para>
 /// </returns>
 public bool AssignClientAuthority(NetworkConnection conn)
 {
   if (!this.isServer)
   {
     if (LogFilter.logError)
       Debug.LogError((object) "AssignClientAuthority can only be call on the server for spawned objects.");
     return false;
   }
   if (!this.localPlayerAuthority)
   {
     if (LogFilter.logError)
       Debug.LogError((object) "AssignClientAuthority can only be used for NetworkIdentity component with LocalPlayerAuthority set.");
     return false;
   }
   if (this.m_ClientAuthorityOwner != null && conn != this.m_ClientAuthorityOwner)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("AssignClientAuthority for " + (object) this.gameObject + " already has an owner. Use RemoveClientAuthority() first."));
     return false;
   }
   if (conn == null)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("AssignClientAuthority for " + (object) this.gameObject + " owner cannot be null. Use RemoveClientAuthority() instead."));
     return false;
   }
   this.m_ClientAuthorityOwner = conn;
   this.m_ClientAuthorityOwner.AddOwnedObject(this);
   this.ForceAuthority(false);
   conn.Send((short) 15, (MessageBase) new ClientAuthorityMessage()
   {
     netId = this.netId,
     authority = true
   });
   if (NetworkIdentity.clientAuthorityCallback != null)
     NetworkIdentity.clientAuthorityCallback(conn, this, true);
   return true;
 }
 /// <summary>
 /// <para>This sends the set of peers in the game to all the peers in the game.</para>
 /// </summary>
 public void SendPeerInfo()
 {
     if (this.m_HostMigration)
     {
         PeerListMessage        msg  = new PeerListMessage();
         List <PeerInfoMessage> list = new List <PeerInfoMessage>();
         for (int i = 0; i < NetworkServer.connections.Count; i++)
         {
             NetworkConnection connection = NetworkServer.connections[i];
             if (connection != null)
             {
                 string          str;
                 int             num2;
                 NetworkID       kid;
                 NodeID          eid;
                 byte            num3;
                 PeerInfoMessage item = new PeerInfoMessage();
                 NetworkTransport.GetConnectionInfo(NetworkServer.serverHostId, connection.connectionId, out str, out num2, out kid, out eid, out num3);
                 item.connectionId = connection.connectionId;
                 item.port         = num2;
                 if (i == 0)
                 {
                     item.port    = NetworkServer.listenPort;
                     item.isHost  = true;
                     item.address = "<host>";
                 }
                 else
                 {
                     item.address = str;
                     item.isHost  = false;
                 }
                 List <PeerInfoPlayer> list2 = new List <PeerInfoPlayer>();
                 for (int k = 0; k < connection.playerControllers.Count; k++)
                 {
                     PlayerController controller = connection.playerControllers[k];
                     if ((controller != null) && (controller.unetView != null))
                     {
                         PeerInfoPlayer player;
                         player.netId = controller.unetView.netId;
                         player.playerControllerId = controller.unetView.playerControllerId;
                         list2.Add(player);
                     }
                 }
                 if (connection.clientOwnedObjects != null)
                 {
                     foreach (NetworkInstanceId id in connection.clientOwnedObjects)
                     {
                         GameObject obj2 = NetworkServer.FindLocalObject(id);
                         if ((obj2 != null) && (obj2.GetComponent <NetworkIdentity>().playerControllerId == -1))
                         {
                             PeerInfoPlayer player2;
                             player2.netId = id;
                             player2.playerControllerId = -1;
                             list2.Add(player2);
                         }
                     }
                 }
                 if (list2.Count > 0)
                 {
                     item.playerIds = list2.ToArray();
                 }
                 list.Add(item);
             }
         }
         msg.peers = list.ToArray();
         for (int j = 0; j < NetworkServer.connections.Count; j++)
         {
             NetworkConnection connection2 = NetworkServer.connections[j];
             if (connection2 != null)
             {
                 msg.oldServerConnectionId = connection2.connectionId;
                 connection2.Send(11, msg);
             }
         }
     }
 }
 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
 {
   if (SceneManager.GetSceneAt(0).name != this.m_LobbyScene)
     return;
   int num = 0;
   using (List<PlayerController>.Enumerator enumerator = conn.playerControllers.GetEnumerator())
   {
     while (enumerator.MoveNext())
     {
       if (enumerator.Current.IsValid)
         ++num;
     }
   }
   if (num >= this.maxPlayersPerConnection)
   {
     if (LogFilter.logWarn)
       Debug.LogWarning((object) "NetworkLobbyManager no more players for this connection.");
     EmptyMessage emptyMessage = new EmptyMessage();
     conn.Send((short) 45, (MessageBase) emptyMessage);
   }
   else
   {
     byte slot = this.FindSlot();
     if ((int) slot == (int) byte.MaxValue)
     {
       if (LogFilter.logWarn)
         Debug.LogWarning((object) "NetworkLobbyManager no space for more players");
       EmptyMessage emptyMessage = new EmptyMessage();
       conn.Send((short) 45, (MessageBase) emptyMessage);
     }
     else
     {
       GameObject player = this.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
       if ((Object) player == (Object) null)
         player = (GameObject) Object.Instantiate((Object) this.lobbyPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
       NetworkLobbyPlayer component = player.GetComponent<NetworkLobbyPlayer>();
       component.slot = slot;
       this.lobbySlots[(int) slot] = component;
       NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
     }
   }
 }
Esempio n. 15
0
 internal void InternalSetClientNotReady(NetworkConnection conn)
 {
     if (conn.isReady)
     {
         if (LogFilter.logDebug)
         {
             Debug.Log("PlayerNotReady " + conn);
         }
         conn.isReady = false;
         conn.RemoveObservers();
         NotReadyMessage msg = new NotReadyMessage();
         conn.Send(0x24, msg);
     }
 }
Esempio n. 16
0
 private void SendCRC(NetworkConnection targetConnection)
 {
     if (NetworkCRC.singleton == null)
     return;
       CRCMessage crcMessage = new CRCMessage();
       List<CRCMessageEntry> list = new List<CRCMessageEntry>();
       using (Dictionary<string, int>.KeyCollection.Enumerator enumerator = NetworkCRC.singleton.scripts.Keys.GetEnumerator())
       {
     while (enumerator.MoveNext())
     {
       string current = enumerator.Current;
       list.Add(new CRCMessageEntry()
       {
     name = current,
     channel = (byte) NetworkCRC.singleton.scripts[current]
       });
     }
       }
       crcMessage.scripts = list.ToArray();
       targetConnection.Send((short) 14, (MessageBase) crcMessage);
 }
Esempio n. 17
0
 /// <summary>
 ///   <para>Signal that the client connection is ready to enter the game.</para>
 /// </summary>
 /// <param name="conn">The client connection which is ready.</param>
 public static bool Ready(NetworkConnection conn)
 {
   if (ClientScene.s_IsReady)
   {
     if (LogFilter.logError)
       Debug.LogError((object) "A connection has already been set as ready. There can only be one.");
     return false;
   }
   if (LogFilter.logDebug)
     Debug.Log((object) ("ClientScene::Ready() called with connection [" + (object) conn + "]"));
   if (conn != null)
   {
     ReadyMessage readyMessage = new ReadyMessage();
     conn.Send((short) 35, (MessageBase) readyMessage);
     ClientScene.s_IsReady = true;
     ClientScene.s_ReadyConnection = conn;
     ClientScene.s_ReadyConnection.isReady = true;
     return true;
   }
   if (LogFilter.logError)
     Debug.LogError((object) "Ready() called with invalid connection object: conn=null");
   return false;
 }
Esempio n. 18
0
 /// <summary>
 /// <para>Signal that the client connection is ready to enter the game.</para>
 /// </summary>
 /// <param name="conn">The client connection which is ready.</param>
 public static bool Ready(NetworkConnection conn)
 {
     if (s_IsReady)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("A connection has already been set as ready. There can only be one.");
         }
         return false;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log("ClientScene::Ready() called with connection [" + conn + "]");
     }
     if (conn != null)
     {
         ReadyMessage msg = new ReadyMessage();
         conn.Send(0x23, msg);
         s_IsReady = true;
         s_ReadyConnection = conn;
         s_ReadyConnection.isReady = true;
         return true;
     }
     if (LogFilter.logError)
     {
         Debug.LogError("Ready() called with invalid connection object: conn=null");
     }
     return false;
 }
Esempio 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);
        }
Esempio n. 20
0
 internal static void HideForConnection(NetworkIdentity uv, NetworkConnection conn)
 {
     conn.Send((short) 13, (MessageBase) new ObjectDestroyMessage()
       {
     netId = uv.netId
       });
 }
Esempio n. 21
0
        private void OnServerAddPlayer(NetworkConnection conn, short player_controller_id)
        {
            PlayerData pd = new PlayerData();
            pd.ip = conn.address;
            pd.ping = 0;
            pd.ready = false;
            pd.network_connection = conn;
            pd.player_controller_id = player_controller_id;

            _player_data.Add(conn.connectionId, pd);

            conn.Send(MessageIDs.PlayerInfoRequest, new IntegerMessage(conn.connectionId));
        }
Esempio n. 22
0
 internal void InternalSetClientNotReady(NetworkConnection conn)
 {
     if (!conn.isReady)
     return;
       if (LogFilter.logDebug)
     Debug.Log((object) ("PlayerNotReady " + (object) conn));
       conn.isReady = false;
       conn.RemoveObservers();
       NotReadyMessage notReadyMessage = new NotReadyMessage();
       conn.Send((short) 36, (MessageBase) notReadyMessage);
 }
Esempio n. 23
0
 public bool AssignClientAuthority(NetworkConnection conn)
 {
     if (!this.isServer)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("AssignClientAuthority can only be call on the server for spawned objects.");
         }
         return false;
     }
     if (!this.localPlayerAuthority)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("AssignClientAuthority can only be used for NetworkIdentity component with LocalPlayerAuthority set.");
         }
         return false;
     }
     if ((this.m_ClientAuthorityOwner != null) && (conn != this.m_ClientAuthorityOwner))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("AssignClientAuthority for " + base.gameObject + " already has an owner. Use RemoveClientAuthority() first.");
         }
         return false;
     }
     if (conn == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("AssignClientAuthority for " + base.gameObject + " owner cannot be null. Use RemoveClientAuthority() instead.");
         }
         return false;
     }
     this.m_ClientAuthorityOwner = conn;
     this.m_ClientAuthorityOwner.AddOwnedObject(this);
     ClientAuthorityMessage msg = new ClientAuthorityMessage {
         netId = this.netId,
         authority = true
     };
     conn.Send(15, msg);
     return true;
 }
Esempio n. 24
0
 internal void SendSpawnMessage(NetworkIdentity uv, NetworkConnection conn)
 {
     if (uv.serverOnly)
     return;
       if (uv.sceneId.IsEmpty())
       {
     ObjectSpawnMessage objectSpawnMessage = new ObjectSpawnMessage();
     objectSpawnMessage.netId = uv.netId;
     objectSpawnMessage.assetId = uv.assetId;
     objectSpawnMessage.position = uv.transform.position;
     NetworkWriter writer = new NetworkWriter();
     uv.UNetSerializeAllVars(writer);
     if ((int) writer.Position > 0)
       objectSpawnMessage.payload = writer.ToArray();
     if (conn != null)
       conn.Send((short) 3, (MessageBase) objectSpawnMessage);
     else
       NetworkServer.SendToReady(uv.gameObject, (short) 3, (MessageBase) objectSpawnMessage);
     NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 3, uv.assetId.ToString(), 1);
       }
       else
       {
     ObjectSpawnSceneMessage spawnSceneMessage = new ObjectSpawnSceneMessage();
     spawnSceneMessage.netId = uv.netId;
     spawnSceneMessage.sceneId = uv.sceneId;
     spawnSceneMessage.position = uv.transform.position;
     NetworkWriter writer = new NetworkWriter();
     uv.UNetSerializeAllVars(writer);
     if ((int) writer.Position > 0)
       spawnSceneMessage.payload = writer.ToArray();
     if (conn != null)
       conn.Send((short) 10, (MessageBase) spawnSceneMessage);
     else
       NetworkServer.SendToReady(uv.gameObject, (short) 3, (MessageBase) spawnSceneMessage);
     NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 10, "sceneId", 1);
       }
 }
        // 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);
        }
Esempio n. 26
0
 internal void SetClientReadyInternal(NetworkConnection conn)
 {
     if (conn.isReady)
       {
     if (!LogFilter.logDebug)
       return;
     Debug.Log((object) ("SetClientReady conn " + (object) conn.connectionId + " already ready"));
       }
       else
       {
     if (conn.playerControllers.Count == 0 && LogFilter.logDebug)
       Debug.LogWarning((object) "Ready with no player object");
     conn.isReady = true;
     if (conn is ULocalConnectionToClient)
     {
       if (LogFilter.logDev)
     Debug.Log((object) "NetworkServer Ready handling ULocalConnectionToClient");
       using (Dictionary<NetworkInstanceId, NetworkIdentity>.ValueCollection.Enumerator enumerator = NetworkServer.objects.Values.GetEnumerator())
       {
     while (enumerator.MoveNext())
     {
       NetworkIdentity current = enumerator.Current;
       if ((UnityEngine.Object) current != (UnityEngine.Object) null && (UnityEngine.Object) current.gameObject != (UnityEngine.Object) null && !current.isClient)
       {
         if (LogFilter.logDev)
           Debug.Log((object) "LocalClient.SetSpawnObject calling OnStartClient");
         current.OnStartClient();
       }
     }
       }
     }
     else
     {
       ObjectSpawnFinishedMessage spawnFinishedMessage = new ObjectSpawnFinishedMessage();
       spawnFinishedMessage.state = 0U;
       conn.Send((short) 12, (MessageBase) spawnFinishedMessage);
       using (Dictionary<NetworkInstanceId, NetworkIdentity>.ValueCollection.Enumerator enumerator = NetworkServer.objects.Values.GetEnumerator())
       {
     while (enumerator.MoveNext())
     {
       NetworkIdentity current = enumerator.Current;
       if ((UnityEngine.Object) current == (UnityEngine.Object) null)
       {
         if (LogFilter.logWarn)
           Debug.LogWarning((object) "Invalid object found in server local object list (null NetworkIdentity).");
       }
       else
       {
         if (LogFilter.logDebug)
         {
           object[] objArray = new object[4];
           int index1 = 0;
           string str1 = "Sending spawn message for current server objects name='";
           objArray[index1] = (object) str1;
           int index2 = 1;
           string name = current.gameObject.name;
           objArray[index2] = (object) name;
           int index3 = 2;
           string str2 = "' netId=";
           objArray[index3] = (object) str2;
           int index4 = 3;
           // ISSUE: variable of a boxed type
           __Boxed<NetworkInstanceId> local = (ValueType) current.netId;
           objArray[index4] = (object) local;
           Debug.Log((object) string.Concat(objArray));
         }
         if (current.OnCheckObserver(conn))
           current.AddObserver(conn);
       }
     }
       }
       spawnFinishedMessage.state = 1U;
       conn.Send((short) 12, (MessageBase) spawnFinishedMessage);
     }
       }
 }
Esempio n. 27
0
 public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
 {
     if (Application.loadedLevelName == this.m_LobbyScene)
     {
         int num = 0;
         foreach (PlayerController controller in conn.playerControllers)
         {
             if (controller.IsValid)
             {
                 num++;
             }
         }
         if (num >= this.maxPlayersPerConnection)
         {
             if (LogFilter.logWarn)
             {
                 Debug.LogWarning("NetworkLobbyManager no more players for this connection.");
             }
             EmptyMessage msg = new EmptyMessage();
             conn.Send(0x2d, msg);
         }
         else
         {
             byte index = this.FindSlot();
             if (index == 0xff)
             {
                 if (LogFilter.logWarn)
                 {
                     Debug.LogWarning("NetworkLobbyManager no space for more players");
                 }
                 EmptyMessage message2 = new EmptyMessage();
                 conn.Send(0x2d, message2);
             }
             else
             {
                 GameObject obj2 = this.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
                 if (obj2 == null)
                 {
                     obj2 = (GameObject) UnityEngine.Object.Instantiate(this.lobbyPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
                 }
                 NetworkLobbyPlayer component = obj2.GetComponent<NetworkLobbyPlayer>();
                 component.slot = index;
                 this.lobbySlots[index] = component;
                 NetworkServer.AddPlayerForConnection(conn, obj2, playerControllerId);
             }
         }
     }
 }
Esempio n. 28
0
 private void FinishPlayerForConnection(NetworkConnection conn, NetworkIdentity uv, GameObject playerGameObject)
 {
     if (uv.netId.IsEmpty())
     NetworkServer.Spawn(playerGameObject);
       conn.Send((short) 4, (MessageBase) new OwnerMessage()
       {
     netId = uv.netId,
     playerControllerId = uv.playerControllerId
       });
 }
        public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
        {
            if (Application.loadedLevelName != this.m_LobbyScene)
            return;

            // see how many players are playing from a connection
              int num = 0;
              using (List<PlayerController>.Enumerator enumerator = conn.playerControllers.GetEnumerator())
              {
            while (enumerator.MoveNext())
            {
              if (enumerator.Current.IsValid)
            ++num;
            }
              }

            // default value is one player per connection
              if (num >= this.maxPlayersPerConnection)
              {
            if (LogFilter.logWarn)
              Debug.LogWarning((object) "NetworkLobbyManager no more players for this connection.");
            EmptyMessage emptyMessage = new EmptyMessage();
            conn.Send((short) 45, (MessageBase) emptyMessage);
              }
              else // if the connection meets the value set for maxPlayersPerConnection then attempt to fill a lobby slot with that player
              {
            byte slot = this.FindSlot();
            if ((int) slot == (int) byte.MaxValue) // then all lobby slots are full, no more room in game
            {
              if (LogFilter.logWarn)
            Debug.LogWarning((object) "NetworkLobbyManager no space for more players");
              EmptyMessage emptyMessage = new EmptyMessage();
              conn.Send((short) 45, (MessageBase) emptyMessage);
            }
            else // set up the lobby player to be spawned into the lobby scene
            {
              GameObject player = this.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);

              if ((UnityEngine.Object) player == (UnityEngine.Object) null)
            player = (GameObject) UnityEngine.Object.Instantiate((UnityEngine.Object) this.lobbyPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);

              NetworkLobbyPlayer component = player.GetComponent<NetworkLobbyPlayer>();
              component.slot = slot;
              this.lobbySlots[(int) slot] = component;
              NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
            }
              }
        }
Esempio n. 30
0
 private static void FinishPlayerForConnection(NetworkConnection conn, NetworkIdentity uv, GameObject playerGameObject)
 {
     if (uv.netId.IsEmpty())
     {
         Spawn(playerGameObject);
     }
     OwnerMessage msg = new OwnerMessage {
         netId = uv.netId,
         playerControllerId = uv.playerControllerId
     };
     conn.Send(4, msg);
 }