internal static void HandleConnectionApproved(uint clientId, BitReader reader, int channelId)
        {
            netManager.LocalClientId = reader.ReadUInt();
            uint sceneIndex = 0;

            if (netManager.NetworkConfig.EnableSceneSwitching)
            {
                sceneIndex = reader.ReadUInt();
            }

#if !DISABLE_CRYPTOGRAPHY
            if (netManager.NetworkConfig.EnableEncryption)
            {
                byte[] serverPublicKey = reader.ReadByteArray();
                netManager.clientAesKey = netManager.clientDiffieHellman.GetSharedSecret(serverPublicKey);
                if (netManager.NetworkConfig.SignKeyExchange)
                {
                    byte[] publicKeySignature = reader.ReadByteArray();
                    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                    {
                        rsa.PersistKeyInCsp = false;
                        rsa.FromXmlString(netManager.NetworkConfig.RSAPublicKey);
                        if (!rsa.VerifyData(serverPublicKey, new SHA512CryptoServiceProvider(), publicKeySignature))
                        {
                            //Man in the middle.
                            if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
                            {
                                if (LogHelper.CurrentLogLevel <= LogLevel.Normal)
                                {
                                    LogHelper.LogWarning("Signature doesnt match for the key exchange public part. Disconnecting");
                                }
                            }
                            netManager.StopClient();
                            return;
                        }
                    }
                }
            }
#endif

            float netTime     = reader.ReadFloat();
            int   remoteStamp = reader.ReadInt();
            int   msDelay     = NetworkingManager.singleton.NetworkConfig.NetworkTransport.GetRemoteDelayTimeMS(clientId, remoteStamp, out byte error);
            netManager.NetworkTime = netTime + (msDelay / 1000f);

            netManager.ConnectedClients.Add(netManager.LocalClientId, new NetworkedClient()
            {
                ClientId = netManager.LocalClientId
            });
            int clientCount = reader.ReadInt();
            for (int i = 0; i < clientCount; i++)
            {
                uint _clientId = reader.ReadUInt();
                netManager.ConnectedClients.Add(_clientId, new NetworkedClient()
                {
                    ClientId = _clientId
                });
                netManager.ConnectedClientsList.Add(netManager.ConnectedClients[_clientId]);
            }
            if (netManager.NetworkConfig.HandleObjectSpawning)
            {
                SpawnManager.DestroySceneObjects();
                int objectCount = reader.ReadInt();
                for (int i = 0; i < objectCount; i++)
                {
                    bool isPlayerObject = reader.ReadBool();
                    uint networkId      = reader.ReadUInt();
                    uint ownerId        = reader.ReadUInt();
                    int  prefabId       = reader.ReadInt();
                    bool isActive       = reader.ReadBool();
                    bool sceneObject    = reader.ReadBool();
                    bool visible        = reader.ReadBool();

                    float xPos = reader.ReadFloat();
                    float yPos = reader.ReadFloat();
                    float zPos = reader.ReadFloat();

                    float xRot = reader.ReadFloat();
                    float yRot = reader.ReadFloat();
                    float zRot = reader.ReadFloat();

                    NetworkedObject netObject = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
                                                                                 new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), reader, visible, false);
                    netObject.SetLocalVisibility(visible);
                    netObject.sceneObject = sceneObject;
                    netObject.gameObject.SetActive(isActive);
                }
            }

            if (netManager.NetworkConfig.EnableSceneSwitching)
            {
                NetworkSceneManager.OnSceneSwitch(sceneIndex);
            }

            netManager.isConnectedClients = true;
            if (netManager.OnClientConnectedCallback != null)
            {
                netManager.OnClientConnectedCallback.Invoke(netManager.LocalClientId);
            }
        }
Example #2
0
        internal static void HandleConnectionApproved(uint clientId, byte[] incommingData, int channelId)
        {
            BitReader reader = new BitReader(incommingData);

            netManager.myClientId = reader.ReadUInt();
            uint sceneIndex = 0;

            if (netManager.NetworkConfig.EnableSceneSwitching)
            {
                sceneIndex = reader.ReadUInt();
            }

            if (netManager.NetworkConfig.EnableEncryption)
            {
                byte[] serverPublicKey = reader.ReadByteArray();
                netManager.clientAesKey = netManager.clientDiffieHellman.GetSharedSecret(serverPublicKey);
                if (netManager.NetworkConfig.SignKeyExchange)
                {
                    byte[] publicKeySignature = reader.ReadByteArray();
                    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                    {
                        rsa.PersistKeyInCsp = false;
                        rsa.FromXmlString(netManager.NetworkConfig.RSAPublicKey);
                        if (!rsa.VerifyData(serverPublicKey, new SHA512CryptoServiceProvider(), publicKeySignature))
                        {
                            //Man in the middle.
                            Debug.LogWarning("MLAPI: Signature doesnt match for the key exchange public part. Disconnecting");
                            netManager.StopClient();
                            return;
                        }
                    }
                }
            }

            float netTime     = reader.ReadFloat();
            int   remoteStamp = reader.ReadInt();
            byte  error;
            int   msDelay = NetworkingManager.singleton.NetworkConfig.NetworkTransport.GetRemoteDelayTimeMS(clientId, remoteStamp, out error);

            netManager.networkTime = netTime + (msDelay / 1000f);

            netManager.connectedClients.Add(netManager.MyClientId, new NetworkedClient()
            {
                ClientId = netManager.MyClientId
            });
            int clientCount = reader.ReadInt();

            for (int i = 0; i < clientCount; i++)
            {
                uint _clientId = reader.ReadUInt();
                netManager.connectedClients.Add(_clientId, new NetworkedClient()
                {
                    ClientId = _clientId
                });
            }
            if (netManager.NetworkConfig.HandleObjectSpawning)
            {
                SpawnManager.DestroySceneObjects();
                int objectCount = reader.ReadInt();
                for (int i = 0; i < objectCount; i++)
                {
                    bool isPlayerObject = reader.ReadBool();
                    uint networkId      = reader.ReadUInt();
                    uint ownerId        = reader.ReadUInt();
                    int  prefabId       = reader.ReadInt();
                    bool isActive       = reader.ReadBool();
                    bool sceneObject    = reader.ReadBool();
                    bool visible        = reader.ReadBool();

                    float xPos = reader.ReadFloat();
                    float yPos = reader.ReadFloat();
                    float zPos = reader.ReadFloat();

                    float xRot = reader.ReadFloat();
                    float yRot = reader.ReadFloat();
                    float zRot = reader.ReadFloat();

                    if (isPlayerObject)
                    {
                        GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
                        go.GetComponent <NetworkedObject>().SetLocalVisibility(visible);
                    }
                    else
                    {
                        GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
                                                                            new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));

                        go.GetComponent <NetworkedObject>().SetLocalVisibility(visible);
                        go.GetComponent <NetworkedObject>().sceneObject = sceneObject;
                        go.SetActive(isActive);
                    }
                }
            }

            if (netManager.NetworkConfig.EnableSceneSwitching)
            {
                NetworkSceneManager.OnSceneSwitch(sceneIndex);
            }

            netManager._isClientConnected = true;
            if (netManager.OnClientConnectedCallback != null)
            {
                netManager.OnClientConnectedCallback.Invoke(netManager.MyClientId);
            }
        }
 internal static void HandleSwitchScene(uint clientId, BitReader reader, int channelId)
 {
     NetworkSceneManager.OnSceneSwitch(reader.ReadUInt());
 }
Example #4
0
        internal static void HandleSwitchScene(uint clientId, byte[] incommingData, int channelId)
        {
            BitReader reader = new BitReader(incommingData);

            NetworkSceneManager.OnSceneSwitch(reader.ReadUInt());
        }