/// <summary> /// Prepare new connection. /// Preparing process includes all data that connection need to work with server and base map loading. /// After preparing, connection will receive all QNetObjects that are currently created. /// </summary> public static void PrepareNewConnection(QNetConnection connection) { var qNetPlayer = QNetPlayer.GetQNetPlayer(connection.ConnectionIdentity); if (qNetPlayer == null) { JEMLogger.LogError("Newly received connection don't have his QNetPlayer instance. Disconnecting!"); QNetManager.Server.CloseConnection(connection, "InternalQNetPlayerError"); } else { JEMLogger.Log($"Preparing newly received connection called {qNetPlayer.Nickname}."); var writer = QNetManager.Server.GenerateOutgoingMessage((ushort)QNetUnityLocalHeader.LEVEL_LOADING); writer.WriteString(QNetLevelLoader.LevelName); QNetManager.Server.Send(connection, QNetLocalChannel.DEFAULT, QNetMessageMethod.ReliableOrdered, writer); } }
/// <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(); }