internal static void OnPlayerCreate(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            JEMLogger.Log("QNet is receiving player create event.");

            // de-serialized message
            var serializedPlayer = reader.ReadMessage <QNetPlayerSerialized>();

            // resolve qNetPlayer
            var qNetPlayer = QNetPlayer.GetQNetPlayer(serializedPlayer.ConnectionIdentity);

            if (qNetPlayer == null)
            {
                if (QNetManager.IsHostActive)
                {
                    throw new InvalidOperationException(
                              $"Host is unable to GetQNetPlayer by identity {serializedPlayer.ConnectionIdentity}.");
                }
                qNetPlayer =
                    QNetPlayer.CreateQNetPlayer(serializedPlayer.ConnectionIdentity, serializedPlayer.NickName, 0);
            }

            if (QNetManager.Client.ConnectionIdentity == serializedPlayer.ConnectionIdentity)
            {
                // do something with local player
                // we can't TagAsReady because WORLD_SERIALIZED message already do this
            }
            else
            {
                // tag player as ready
                qNetPlayer.TagAsReady();
            }
        }
Пример #2
0
        internal static void OnServerWorldSerialization(QNetMessage message, QNetMessageReader reader,
                                                        ref bool disallowRecycle)
        {
            // CLIENT: server qnet object send ends
            // server ends sending of serialized objects
            // now client can starts it's de-serialization but if client is also host, this operation is completely ignored
            // because all object was serialized at server startup

            if (QNetManager.IsServerActive)
            {
                // ignoring all operations and continuing by sending WORLD_SERIALIZED message
                JEMLogger.Log("Ignoring QNetObject serialization. (Host)");
                QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                        QNetUnityLocalHeader.WORLD_SERIALIZED);
            }
            else
            {
                // run late client world serializer to load all serialized objects from memory
                JEMLogger.Log("Client received all QNetObjects. Starting serialization.");
                QNetGameInitializer.RunLateClientWorldSerializer(() =>
                {
                    // send WORLD_SERIALIZED message
                    JEMLogger.Log("QNetObject serialized.");
                    QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                            QNetUnityLocalHeader.WORLD_SERIALIZED);
                });
            }
        }
Пример #3
0
        internal static void OnClientLevelLoaded(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            // SERVER: player loads level
            if (QNetManager.IsHostActive)
            {
                if (QNetManager.Client.ConnectionIdentity == reader.Connection.ConnectionIdentity)
                {
                    // loaded level message has been send from host client, send only WORLD_SERIALIZATION message
                    QNetManager.Server.Send(reader.Connection, QNetLocalChannel.DEFAULT,
                                            QNetMessageMethod.ReliableOrdered, QNetUnityLocalHeader.WORLD_SERIALIZATION);
                    return;
                }
            }

            JEMLogger.Log($"Connection {reader.Connection} loads level. Sending world objects.");

            // send WORLD_SERIALIZING message to prepare client
            QNetManager.Server.Send(reader.Connection, QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                    QNetUnityLocalHeader.WORLD_SERIALIZING);

            // send all objects to client
            QNetServerObjects.SendAllObjectsToConnection(reader.Connection);

            // send WORLD_SERIALIZATION message to start object loading on client
            QNetManager.Server.Send(reader.Connection, QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                    QNetUnityLocalHeader.WORLD_SERIALIZATION);
        }
Пример #4
0
        internal static void OnServerObjectDelete(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            // read object identity to remove
            var objectIdentity = reader.ReadInt16();

            // and do it!
            QNetWorldSerializer.RemoveSerializedObjectFromMemory(objectIdentity);
        }
Пример #5
0
        internal static void OnServerWorldSerializing(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            // CLIENT: server starts to send qnet objects
            if (QNetManager.IsServerActive)
            {
                throw new InvalidOperationException(
                          "OnWorldSerializing message has been received while server(host) is active.");
            }

            // we can activate now some loading algorithms to view progress
        }
Пример #6
0
        public static void OnClientLevelLoadedOnFly(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            var player = QNetPlayer.GetQNetPlayer(reader.Connection);

            if (player == null)
            {
                throw new NullReferenceException("player");
            }
            if (player.Loaded)
            {
                return;
            }
        }
        internal static void OnPlayerDelete(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            JEMLogger.Log("QNet is receiving player delete event.");
            var connectionIdentity = reader.ReadInt16();
            var player             = QNetPlayer.GetQNetPlayer(connectionIdentity);

            if (player == null)
            {
                throw new InvalidOperationException("InvalidConnectionIdentityError");
            }

            player.TagAsNotReady();
            QNetPlayer.DestroyQNetPlayer(player);
        }
Пример #8
0
        internal static void OnServerObjectCreate(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            // write new object in to serializer
            var obj = reader.ReadMessage <QNetObjectSerialized>();

            //JEMLogger.Log($"Local QNet received object create message. PrefabIdentity -> {obj.PrefabIdentity}, ObjectIdentity -> {obj.ObjectIdentity}, OwnerIdentity -> {obj.OwnerIdentity} at position -> {obj.Position}");
            QNetWorldSerializer.WriteSerializedObjectToMemory(new QNetWorldSerializerObject
            {
                Object = obj,
                SerializedServerState = reader
            });

            // disallow to recycle this message
            disallowRecycle = true;
        }
Пример #9
0
        internal static void OnClientWorldSerialized(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            // SERVER: player loads all sent qnet objects
            // prepare this client's controller and send final message so the player can play
            // we will tag this connection as ready, so the system should send this player to all connections on server including owner
            // if owner receive it's player instance from server, they will initialize local systems including camera and movement controller
            var player = QNetPlayer.GetQNetPlayer(reader.Connection);

            if (player == null)
            {
                JEMLogger.LogError("QNet encounter an unexcepted error. Connection QNetPlayer not exists.");
                QNetManager.Server.CloseConnection(reader.Connection, "QNetUnexceptedError");
            }
            else
            {
                player.TagAsReady();
            }
        }
Пример #10
0
        internal static void OnServerObjectState(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            if (!QNetWorldSerializer.WorldIsSerialized)
            {
                return; // system can't receive query data while initializing,
            }
            var objectIdentity = reader.ReadInt16();
            var qNetObject     = QNetObjectBehaviour.GetObject(objectIdentity);

            if (qNetObject == null)
            {
                if (QNetManager.PrintNetworkWarnings)
                {
                    JEMLogger.LogWarning($"Local machine received QNetEntity state update message but object of identity {objectIdentity} not exists in local world.");
                }
                return;
            }

            qNetObject.DeSerializeServerState(reader);
        }
Пример #11
0
 internal static void OnServerLevelLoading(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
 {
     // write new object in to serializer
     if (QNetManager.IsServerActive)
     {
         JEMLogger.Log(
             $"We are on server. Client initialization from {nameof(OnServerLevelLoading)} will be ignored.");
         // send level loaded message instantly
         QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                 QNetUnityLocalHeader.LEVEL_LOADED);
     }
     else
     {
         QNetGameInitializer.RunClient(new QNetGameInitializerData {
             LevelName = reader.ReadString()
         },
                                       () =>
         {
             QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
                                     QNetUnityLocalHeader.LEVEL_LOADED);
         });
     }
 }
Пример #12
0
 public static void OnLevelLoadingOnFly(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
 {
     /*
      * // write new object in to serializer
      * if (QNetManager.IsServerActive)
      * {
      *  JEMLogger.Log(
      *      $"We are on server. Client initialization from {nameof(OnServerLevelLoading)} will be ignored.");
      *  // send level loaded message instantly
      *  QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
      *      QNetUnityLocalHeader.LEVEL_LOADED_ON_FLY);
      * }
      * else
      * {
      *  var levelName = reader.ReadString();
      *  QNetGameInitializer.LoadLevelOnFly(levelName, () =>
      *  {
      *      QNetManager.Client.Send(QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered,
      *          QNetUnityLocalHeader.LEVEL_LOADED_ON_FLY);
      *  });
      * }
      */
 }
Пример #13
0
        public static void OnClientEntityQuery(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
        {
            if (!QNetWorldSerializer.WorldIsSerialized)
            {
                return; // system can't receive entity query data while initializing,
            }
            var objectIdentity = reader.ReadInt16();
            var qNetObject     = QNetObjectBehaviour.GetObject(objectIdentity);

            if (qNetObject == null)
            {
                if (QNetManager.PrintNetworkWarnings)
                {
                    JEMLogger.LogWarning(
                        $"Local machine received QNetEntity query message but object of identity {objectIdentity} not exists in local world.");
                }
                return;
            }

            var entity = qNetObject.GetComponent <QNetEntity>();

            if (entity == null)
            {
                throw new NullReferenceException(
                          $"QNetEntity query target exists but does not have {nameof(QNetEntity)} based script.");
            }

            if (message.IsClientMessage)
            {
                QNetSimulation.ReceivedServerFrame = reader.ReadUInt32();
                QNetSimulation.AdjustServerFrames  = QNetSimulation.ReceivedServerFrame > QNetTime.ServerFrame;
            }

            var index = reader.ReadByte();

            entity.InvokeNetworkMessage(index, reader);
        }
Пример #14
0
 public static void OnServerLevelLoadingOnFlyRun(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
 {
 }
 internal static void OnPlayerQuery(QNetMessage message, QNetMessageReader reader, ref bool disallowRecycle)
 {
     JEMLogger.Log("QNet is receiving player query event.");
 }