Ejemplo n.º 1
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);
                });
            }
        }
Ejemplo n.º 2
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);
         });
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Starts local server based on given or local configuration.
        /// </summary>
        public static void StartServer(QNetConfiguration configuration = null)
        {
            if (IsServerActive)
            {
                throw new InvalidOperationException(
                          "QNet is unable to start server while there is already active instance of server.");
            }
            if (configuration == null)
            {
                configuration = new QNetConfiguration();
            }

            var hostIsActive = IsHostActive;

            Server = new QNetServer();
            Server = InternalStartPeer(Server, configuration, () =>
            {
                QNetHandlerStack.RegisterServerHandlers(Server);
                OnServerRegisterHeaders?.Invoke();
            });
            OnServerPrepare?.Invoke();

            Server.OnBeforeMessage += message =>
            {
                // server should always send the server frame to the client!
                message.Write(QNetTime.ServerFrame);
            };

            Server.OnConnectionAuthorizing += (QNetConnection connection, QNetMessageWriter writer, ref bool refuse) =>
            {
                writer.WriteInt32(QNetTime.TickRate);
                writer.WriteUInt32(QNetTime.ServerFrame);

                OnServerAuthorizePlayer?.Invoke(connection, writer, ref refuse);
            };

            Server.OnConnectionReady += reader =>
            {
                // read player nickname and create its QNetPlayer instance
                var nickname = reader.ReadString();
                var token    = reader.ReadUInt32();
                var version  = reader.ReadString();
                if (JEMBuild.BuildVersion != version)
                {
                    JEMLogger.LogError(
                        $"Newly received connection don't have right version of the game -> {version} ( is {JEMBuild.BuildVersion} )");
                    Server.CloseConnection(reader.Connection, "InvalidBuildVersion");
                }
                else
                {
                    if (IsHostActive && (reader.Connection.ConnectionIdentity == 0 ||
                                         reader.Connection.ConnectionIdentity == Client.ConnectionIdentity))
                    {
                        HostClientConnection = reader.Connection;
                        JEMLogger.Log("QNetUnity received host client connection.");
                    }

                    if (QNetPlayer.GetQNetPlayerByToken(token) != null)
                    {
                        JEMLogger.LogError("Newly received connection is using already used token. Disconnecting!");
                        Server.CloseConnection(reader.Connection, "TokenAlreadyInUse");
                        return;
                    }

                    var qNetPlayer = QNetPlayer.CreateQNetPlayer(reader.Connection.ConnectionIdentity, nickname, token);
                    if (qNetPlayer == null)
                    {
                        JEMLogger.LogError(
                            "Newly received connection don't have his QNetPlayer instance. Disconnecting!");
                        Server.CloseConnection(reader.Connection, "InternalQNetPlayerError");
                        return;
                    }

                    OnServerNewPlayer?.Invoke(qNetPlayer, reader);
                    QNetServerConnectionInit.PrepareNewConnection(reader.Connection);
                }
            };

            Server.OnConnectionLost += (connection, reason) =>
            {
                var qNetPlayer = QNetPlayer.GetQNetPlayer(connection);
                if (qNetPlayer != null) // if QNetPlayer of this connection not exists, just ignore
                {
                    OnServerPlayerLost?.Invoke(qNetPlayer, reason);

                    // the only thing to do here is to tag player as not ready (if ready)
                    if (qNetPlayer.Ready)
                    {
                        qNetPlayer.TagAsNotReady();
                    }

                    // and remove QNetPlayer from local machine
                    QNetPlayer.DestroyQNetPlayer(qNetPlayer);
                }
            };

            Server.OnServerStop += reason =>
            {
                // server has been stopped, try to de-initialize game
                if (!ShuttingDownByApplicationQuit)
                {
                    QNetGameInitializer.DeInitialize(() => { OnServerShutdown?.Invoke(hostIsActive); });
                }
                else
                {
                    OnServerShutdown?.Invoke(hostIsActive);
                }

                IsServerActive = false;
                IsHostActive   = false;
            };

            IsServerActive = true;
            OnServerStarted?.Invoke();
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Starts client connection.
        /// </summary>
        public static void StartClient([NotNull] string ipAddress, ushort port, string password)
        {
            if (IsClientActive)
            {
                throw new InvalidOperationException(
                          "QNet is unable to start client while there is already active instance of client.");
            }
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }

            var configuration = new QNetConfiguration
            {
                IpAddress      = ipAddress,
                Port           = port,
                MaxConnections = 2
            };

            if (OnClientPrepare == null)
            {
                throw new NullReferenceException("QNet is unable to start client. OnClientPrepare event is not set.");
            }

            Client = new QNetClient();
            Client = InternalStartPeer(Client, configuration, () =>
            {
                QNetHandlerStack.RegisterClientHandlers(Client);
                OnClientRegisterHeaders?.Invoke();
            });
            OnClientPrepare.Invoke(out var nickname, out var token);

            Client.OnMessagePoll += reader =>
            {
                // as the server always send the server frame
                // we need to read that right here
                QNetSimulation.ReceivedServerFrame = reader.ReadUInt32();
                QNetSimulation.AdjustServerFrames  = QNetSimulation.ReceivedServerFrame > QNetTime.ServerFrame;
            };

            Client.OnConnectionReady += (reader, writer) =>
            {
                var tickRate    = reader.ReadInt32();
                var frameNumber = reader.ReadUInt32();

                // set TickRate
                QNetTime.TickRate = tickRate;

                // initialize server frame count
                QNetSimulation.ReceivedServerFrame  = frameNumber;
                QNetSimulation.EstimatedServerFrame = frameNumber;

                // write player data
                writer.WriteString(nickname);
                writer.WriteUInt32(token);
                writer.WriteString(JEMBuild.BuildVersion);
                OnClientReady?.Invoke(reader, writer);
            };

            Client.OnDisconnection += (lostConnection, reason) =>
            {
                // stop peer
                if (InternalRunningPeers.Contains(Client))
                {
                    InternalStopPeer(Client, reason);
                }

                // update active state
                IsClientActive = false;

                // and de-initialize game
                if (!IsHostActive && QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsDeInitializing
                    ) // ignore if host, server will de-initialize it anyway
                {
                    QNetGameInitializer.DeInitialize(() =>
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    });
                }
                else
                {
                    if (!QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsInitializing)
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    }
                }
            };

            IsClientActive = true;
        }
        private IEnumerator InternalDeSerializeObjectsInMemory(Action onDone, Action <string> onChange)
        {
            JEMLogger.Log($"QNetUnity is de-serializing {InternalSerializedObjectsInMemory.Count} objects in memory.");
            for (var index = 0; index < InternalSerializedObjectsInMemory.Count; index++)
            {
                var obj = InternalSerializedObjectsInMemory[index];
                if (obj.Equals(default(QNetWorldSerializerObject)))
                {
                    continue;
                }

                onChange?.Invoke($"fromSerializedObject '{obj.Object.ObjectIdentity}/{obj.Object.PrefabIdentity}'");

                var time      = DateTime.Now;
                var isWorking = true;
                StartCoroutine(InternalFromSerializedObject(obj, () => { isWorking = false; }));
                while (isWorking && QNetGameInitializer.DeserializingOnceTimeout >= (DateTime.Now - time).Seconds)
                {
                    yield return(new WaitForEndOfFrame());
                }

                if (isWorking)
                {
                    if (obj.SerializedServerState?.GetMessage() != null)
                    {
                        QNetManager.Client.OriginalClient.Recycle(obj.SerializedServerState.GetMessage());
                    }

                    var str =
                        $"QNetUnity failed fromSerializedObject on '{obj.Object.ObjectIdentity}/{obj.Object.PrefabIdentity}' (timeout)";
                    if (QNetManager.ShutdownOnSerializationTimeout)
                    {
                        QNetGameInitializer.ShutdownInitializing(str);
                        yield break;
                    }

                    JEMLogger.LogError(str);
                }
            }

            JEMLogger.Log($"{InternalSerializedAndInstancedObjects.Count} objects loaded.");
            JEMLogger.Log($"QNetUnity is loading {QNetObjectBehaviour.PredefinedBehaviours.Length} predefined objects.");
            for (var index = 0; index < QNetObjectBehaviour.PredefinedBehaviours.Length; index++)
            {
                var obj = QNetObjectBehaviour.PredefinedBehaviours[index];
                onChange?.Invoke($"predefinedUpdated '{obj.ObjectIdentity}'");
                // just set set active
                obj.gameObject.SetActive(true);
                // and call spawned event
                obj.OnInternalSpawned();
                yield return(new WaitForEndOfFrame());
            }

            IsSerializing     = false;
            WorldIsSerialized = true;
            ClearSerializedObjectsFromMemory();
            onChange?.Invoke("finishing");

            JEMLogger.Log("All objects de-serialized. QNetUnity's world serialization is done.");
            onDone?.Invoke();
        }