Esempio n. 1
0
        protected void ReadPacket(long connectionId, NetDataReader reader)
        {
            var msgType = reader.GetPackedUShort();
            MessageHandlerDelegate handlerDelegate;

            if (messageHandlers.TryGetValue(msgType, out handlerDelegate))
            {
                var messageHandler = new LiteNetLibMessageHandler(msgType, this, connectionId, reader);
                handlerDelegate.Invoke(messageHandler);
            }
        }
Esempio n. 2
0
 protected void HandleServerPing(LiteNetLibMessageHandler messageHandler)
 {
     isPinging = false;
     Rtt       = Timestamp - pingTime;
     // Time offset = server time - current timestamp - rtt
     ServerUnixTimeOffset = messageHandler.reader.GetPackedLong() - Timestamp - Rtt;
     if (LogDev)
     {
         Logging.Log(LogTag, "Rtt: " + Rtt + ", ServerUnixTimeOffset: " + ServerUnixTimeOffset);
     }
 }
        protected virtual void HandleServerCallFunction(LiteNetLibMessageHandler messageHandler)
        {
            NetDataReader         reader = messageHandler.reader;
            LiteNetLibElementInfo info   = LiteNetLibElementInfo.DeserializeInfo(reader);
            LiteNetLibIdentity    identity;

            if (Assets.TryGetSpawnedObject(info.objectId, out identity))
            {
                identity.ProcessNetFunction(info, reader, true);
            }
        }
Esempio n. 4
0
        protected virtual void HandleServerTime(LiteNetLibMessageHandler messageHandler)
        {
            // Server time updated at server, if this is host (client and server) then skip it.
            if (IsServer)
            {
                return;
            }
            ServerTimeMessage message = messageHandler.ReadMessage <ServerTimeMessage>();

            ServerUnixTimeOffset = message.serverUnixTime - Timestamp;
            ServerTimeOffset     = message.serverTime - Time.unscaledTime;
        }
Esempio n. 5
0
        protected virtual void HandleClientCallFunction(LiteNetLibMessageHandler messageHandler)
        {
            NetDataReader     reader    = messageHandler.reader;
            FunctionReceivers receivers = (FunctionReceivers)reader.GetByte();
            long connectionId           = -1;

            if (receivers == FunctionReceivers.Target)
            {
                connectionId = reader.GetPackedLong();
            }
            LiteNetLibElementInfo info = LiteNetLibElementInfo.DeserializeInfo(reader);
            LiteNetLibIdentity    identity;

            if (Assets.TryGetSpawnedObject(info.objectId, out identity))
            {
                LiteNetLibFunction netFunction = identity.GetNetFunction(info);
                if (netFunction == null)
                {
                    // There is no net function that player try to call (player may try to hack)
                    return;
                }
                if (!netFunction.CanCallByEveryone && messageHandler.connectionId != identity.ConnectionId)
                {
                    // The function not allowed anyone except owner client to call this net function
                    // And the client is also not the owner client
                    return;
                }
                if (receivers == FunctionReceivers.Server)
                {
                    // Request from client to server, so hook callback at server immediately
                    identity.ProcessNetFunction(netFunction, reader, true);
                }
                else
                {
                    // Request from client to other clients, so hook callback later
                    identity.ProcessNetFunction(netFunction, reader, false);
                    // Use call with out parameters set because parameters already set while process net function
                    if (receivers == FunctionReceivers.Target)
                    {
                        netFunction.CallWithoutParametersSet(connectionId);
                    }
                    else
                    {
                        netFunction.CallWithoutParametersSet(DeliveryMethod.ReliableOrdered, receivers);
                    }
                }
            }
        }
Esempio n. 6
0
        protected virtual void HandleServerDestroyObject(LiteNetLibMessageHandler messageHandler)
        {
            ServerDestroyObjectMessage message = messageHandler.ReadMessage <ServerDestroyObjectMessage>();

            if (!IsServer)
            {
                Assets.NetworkDestroy(message.objectId, message.reasons);
            }
            // If this is host and reasons is removed from subscribing so hide it, don't destroy it
            LiteNetLibIdentity identity;

            if (IsServer && message.reasons == DestroyObjectReasons.RemovedFromSubscribing && Assets.TryGetSpawnedObject(message.objectId, out identity))
            {
                identity.OnServerSubscribingRemoved();
            }
        }
Esempio n. 7
0
        protected virtual void HandleServerSpawnSceneObject(LiteNetLibMessageHandler messageHandler)
        {
            var message = messageHandler.ReadMessage <ServerSpawnSceneObjectMessage>();

            if (!IsServer)
            {
                Assets.NetworkSpawnScene(message.objectId, message.position, message.rotation);
            }
            LiteNetLibIdentity identity;

            // If it is host, it may hidden so show it
            if (IsServer && Assets.TryGetSpawnedObject(message.objectId, out identity))
            {
                identity.OnServerSubscribingAdded();
            }
        }
Esempio n. 8
0
        protected virtual void HandleServerUpdateSyncList(LiteNetLibMessageHandler messageHandler)
        {
            // List updated at server, if this is host (client and server) then skip it.
            if (IsServer)
            {
                return;
            }
            NetDataReader         reader = messageHandler.reader;
            LiteNetLibElementInfo info   = LiteNetLibElementInfo.DeserializeInfo(reader);
            LiteNetLibIdentity    identity;

            if (Assets.TryGetSpawnedObject(info.objectId, out identity))
            {
                identity.ProcessSyncList(info, reader);
            }
        }
Esempio n. 9
0
        protected virtual void HandleClientSendTransform(LiteNetLibMessageHandler messageHandler)
        {
            NetDataReader      reader         = messageHandler.reader;
            uint               objectId       = reader.GetPackedUInt();
            byte               behaviourIndex = reader.GetByte();
            LiteNetLibIdentity identity;

            if (Assets.TryGetSpawnedObject(objectId, out identity))
            {
                LiteNetLibTransform netTransform;
                if (identity.TryGetBehaviour(behaviourIndex, out netTransform))
                {
                    netTransform.HandleClientSendTransform(reader);
                }
            }
        }
Esempio n. 10
0
        protected virtual void HandleServerSyncBehaviour(LiteNetLibMessageHandler messageHandler)
        {
            // Behaviour sync from server, if this is host (client and server) then skip it.
            if (IsServer)
            {
                return;
            }
            NetDataReader      reader         = messageHandler.reader;
            uint               objectId       = reader.GetPackedUInt();
            byte               behaviourIndex = reader.GetByte();
            LiteNetLibIdentity identity;

            if (Assets.TryGetSpawnedObject(objectId, out identity))
            {
                identity.ProcessSyncBehaviour(behaviourIndex, reader);
            }
        }
Esempio n. 11
0
        protected virtual void HandleServerSpawnObject(LiteNetLibMessageHandler messageHandler)
        {
            var message = messageHandler.ReadMessage <ServerSpawnObjectMessage>();

            if (!IsServer)
            {
                Assets.NetworkSpawn(message.hashAssetId, message.position, message.rotation, message.objectId, 0);
            }
            // Setup owner client
            LiteNetLibIdentity identity;

            if (Assets.TryGetSpawnedObject(message.objectId, out identity))
            {
                identity.SetOwnerClient(message.isOwner);
                // If it is host, it may hidden so show it
                if (IsServer)
                {
                    identity.OnServerSubscribingAdded();
                }
            }
        }
Esempio n. 12
0
        protected virtual void HandleClientUpdateSyncField(LiteNetLibMessageHandler messageHandler)
        {
            // Field updated at owner-client, if this is server then multicast message to other clients
            if (!IsServer)
            {
                return;
            }
            NetDataReader         reader = messageHandler.reader;
            LiteNetLibElementInfo info   = LiteNetLibElementInfo.DeserializeInfo(reader);
            LiteNetLibIdentity    identity;

            if (Assets.TryGetSpawnedObject(info.objectId, out identity))
            {
                LiteNetLibSyncField syncField = identity.GetSyncField(info);
                // Sync field at server also have to be client multicast to allow it to multicast to other clients
                if (syncField != null && syncField.syncMode == LiteNetLibSyncField.SyncMode.ClientMulticast)
                {
                    // If this is server but it is not host, set data (deserialize) then pass to other clients
                    // If this is host don't set data because it's already did (in LiteNetLibSyncField class)
                    if (!identity.IsOwnerClient)
                    {
                        syncField.Deserialize(reader, false);
                    }
                    // Send to other clients
                    foreach (long connectionId in GetConnectionIds())
                    {
                        // Don't send the update to owner client because it was updated before send update to server
                        if (connectionId == messageHandler.connectionId)
                        {
                            continue;
                        }
                        // Send update to clients except owner client
                        if (identity.IsSubscribedOrOwning(connectionId))
                        {
                            syncField.SendUpdate(false, connectionId);
                        }
                    }
                }
            }
        }
Esempio n. 13
0
        protected virtual void HandleServerSceneChange(LiteNetLibMessageHandler messageHandler)
        {
            // Server scene changes made from server, if this is host (client and server) then skip it.
            if (IsServer)
            {
                return;
            }
            // Scene name sent from server
            ServerSceneChangeMessage message = messageHandler.ReadMessage <ServerSceneChangeMessage>();
            string serverSceneName           = message.serverSceneName;

            if (string.IsNullOrEmpty(serverSceneName) || serverSceneName.Equals(SceneManager.GetActiveScene().name))
            {
                Assets.Initialize();
                OnClientOnlineSceneLoaded();
                SendClientReady();
            }
            else
            {
                // If scene is difference, load changing scene
                StartCoroutine(LoadSceneRoutine(serverSceneName, true));
            }
        }
Esempio n. 14
0
        protected virtual void HandleServerSpawnSceneObject(LiteNetLibMessageHandler messageHandler)
        {
            ServerSpawnSceneObjectMessage message = messageHandler.ReadMessage <ServerSpawnSceneObjectMessage>();

            if (!IsServer)
            {
                Assets.NetworkSpawnScene(message.objectId, message.position, message.rotation);
            }
            LiteNetLibIdentity identity;

            if (Assets.TryGetSpawnedObject(message.objectId, out identity))
            {
                // If it is not server, read its initial data
                if (!IsServer)
                {
                    identity.ReadInitialSyncFields(messageHandler.reader);
                }
                // If it is host, it may hidden so show it
                if (IsServer)
                {
                    identity.OnServerSubscribingAdded();
                }
            }
        }
Esempio n. 15
0
 protected virtual void HandleServerEnterGame(LiteNetLibMessageHandler messageHandler)
 {
     ClientConnectionId = messageHandler.reader.GetLong();
 }
Esempio n. 16
0
 protected virtual void HandleClientEnterGame(LiteNetLibMessageHandler messageHandler)
 {
     SendServerSceneChange(messageHandler.connectionId, ServerSceneName);
 }
Esempio n. 17
0
 protected virtual void HandleClientNotReady(LiteNetLibMessageHandler messageHandler)
 {
     SetPlayerNotReady(messageHandler.connectionId, messageHandler.reader);
 }