From 8e778cd9ab74d821deb06e0b22344927d2c0276b Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Fri, 13 Oct 2017 02:25:50 +0200 Subject: [PATCH] Address pull request comments --- src/base/system.c | 5 ++-- src/base/system.h | 5 ++-- src/engine/server.h | 2 ++ src/engine/server/server.cpp | 49 +++++++++++++++++++++++++-------- src/engine/server/server.h | 5 ++++ src/game/server/gamecontext.cpp | 22 +++++++-------- src/game/server/gamecontext.h | 4 +++ 7 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index 9b29e2f6c..6b1d91977 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -85,7 +85,7 @@ IOHANDLE io_stderr() { return (IOHANDLE)stderr; } typedef struct { DBG_LOGGER logger; - void (*finish)(void *user); + DBG_LOGGER_FINISH finish; void *user; } DBG_LOGGER_DATA; @@ -194,7 +194,7 @@ static void dbg_logger_finish(void) } } -void dbg_logger(DBG_LOGGER logger, void (*finish)(void *user), void *user) +void dbg_logger(DBG_LOGGER logger, DBG_LOGGER_FINISH finish, void *user) { DBG_LOGGER_DATA data; if(num_loggers == 0) @@ -737,7 +737,6 @@ void async_wait(ASYNCIO *aio) thread_wait(thread); } - void *thread_init(void (*threadfunc)(void *), void *u) { #if defined(CONF_FAMILY_UNIX) diff --git a/src/base/system.h b/src/base/system.h index 57a6af887..c09062c43 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1358,14 +1358,13 @@ void swap_endian(void *data, unsigned elem_size, unsigned num); typedef void (*DBG_LOGGER)(const char *line, void *user); -void dbg_logger(DBG_LOGGER logger, void (*finish)(void *user), void *user); +typedef void (*DBG_LOGGER_FINISH)(void *user); +void dbg_logger(DBG_LOGGER logger, DBG_LOGGER_FINISH finish, void *user); void dbg_logger_stdout(); void dbg_logger_debugger(); void dbg_logger_file(const char *filename); -void dbg_log_finish(); - typedef struct { int allocated; diff --git a/src/engine/server.h b/src/engine/server.h index 87bb5ae3c..4bdc3041f 100644 --- a/src/engine/server.h +++ b/src/engine/server.h @@ -180,6 +180,8 @@ public: virtual void ResetNetErrorString(int ClientID) = 0; virtual bool SetTimedOut(int ClientID, int OrigID) = 0; virtual void SetTimeoutProtected(int ClientID) = 0; + + virtual void SetErrorShutdown(const char *pReason) = 0; }; class IGameServer : public IInterface diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 08f2a2d78..86ca758c4 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -334,6 +334,8 @@ CServer::CServer() CSqlConnector::SetWriteServers(m_apSqlWriteServers); #endif + m_aErrorShutdownReason[0] = 0; + Init(); } @@ -1746,6 +1748,10 @@ int CServer::Run() Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); GameServer()->OnInit(); + if(ErrorShutdown()) + { + return 1; + } str_format(aBuf, sizeof(aBuf), "version %s", GameServer()->NetVersion()); Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf); @@ -1808,6 +1814,10 @@ int CServer::Run() m_ServerInfoFirstRequest = 0; Kernel()->ReregisterInterface(GameServer()); GameServer()->OnInit(); + if(ErrorShutdown()) + { + break; + } UpdateServerInfo(); } else @@ -1885,6 +1895,10 @@ int CServer::Run() } GameServer()->OnTick(); + if(ErrorShutdown()) + { + break; + } } // snap game @@ -1942,11 +1956,16 @@ int CServer::Run() } } } + const char *pDisconnectReason = "Server shutdown"; + if(ErrorShutdown()) + { + pDisconnectReason = m_aErrorShutdownReason; + } // disconnect all clients on shutdown for(int i = 0; i < MAX_CLIENTS; ++i) { if(m_aClients[i].m_State != CClient::STATE_EMPTY) - m_NetServer.Drop(i, "Server shutdown"); + m_NetServer.Drop(i, pDisconnectReason); } m_Econ.Shutdown(); @@ -1962,17 +1981,17 @@ int CServer::Run() mem_free(m_pCurrentMapData); #if defined (CONF_SQL) - for (int i = 0; i < MAX_SQLSERVERS; i++) - { - if (m_apSqlReadServers[i]) - delete m_apSqlReadServers[i]; + for (int i = 0; i < MAX_SQLSERVERS; i++) + { + if (m_apSqlReadServers[i]) + delete m_apSqlReadServers[i]; - if (m_apSqlWriteServers[i]) - delete m_apSqlWriteServers[i]; - } + if (m_apSqlWriteServers[i]) + delete m_apSqlWriteServers[i]; + } #endif - return 0; + return ErrorShutdown(); } void CServer::ConTestingCommands(CConsole::IResult *pResult, void *pUser) @@ -2812,12 +2831,13 @@ const char *CServer::GetAnnouncementLine(char const *pFileName) return v[m_AnnouncementLastLine]; } -int* CServer::GetIdMap(int ClientID) +int *CServer::GetIdMap(int ClientID) { - return (int*)(IdMap + VANILLA_MAX_CLIENTS * ClientID); + return (int *)(IdMap + VANILLA_MAX_CLIENTS * ClientID); } -bool CServer::SetTimedOut(int ClientID, int OrigID) { +bool CServer::SetTimedOut(int ClientID, int OrigID) +{ if (!m_NetServer.SetTimedOut(ClientID, OrigID)) { return false; @@ -2826,3 +2846,8 @@ bool CServer::SetTimedOut(int ClientID, int OrigID) { m_aClients[ClientID].m_Authed = IServer::AUTHED_NO; return true; } + +void CServer::SetErrorShutdown(const char *pReason) +{ + str_copy(m_aErrorShutdownReason, pReason, sizeof(m_aErrorShutdownReason)); +} diff --git a/src/engine/server/server.h b/src/engine/server/server.h index 291d8e59c..6d40b062a 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -209,6 +209,8 @@ public: int64 m_ServerInfoFirstRequest; int m_ServerInfoNumRequests; + char m_aErrorShutdownReason[128]; + CServer(); int TrySetClientName(int ClientID, const char *pName); @@ -353,6 +355,9 @@ public: void ResetNetErrorString(int ClientID) { m_NetServer.ResetErrorString(ClientID); }; bool SetTimedOut(int ClientID, int OrigID); void SetTimeoutProtected(int ClientID) { m_NetServer.SetTimeoutProtected(ClientID); }; + + bool ErrorShutdown() const { return m_aErrorShutdownReason[0] != 0; } + void SetErrorShutdown(const char *pReason); }; #endif diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 4615c16e7..25892c7ed 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -608,7 +608,7 @@ void CGameContext::OnTick() if(Error) { dbg_msg("teehistorian", "error writing to file, err=%d", Error); - exit(1); + Server()->SetErrorShutdown("teehistorian io error"); } if(!m_TeeHistorian.Starting()) @@ -2454,6 +2454,7 @@ void CGameContext::OnConsoleInit() { m_pServer = Kernel()->RequestInterface(); m_pConsole = Kernel()->RequestInterface(); + m_pStorage = Kernel()->RequestInterface(); m_ChatPrintCBIndex = Console()->RegisterPrintCallback(0, SendChatResponse, this); @@ -2494,6 +2495,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/) { m_pServer = Kernel()->RequestInterface(); m_pConsole = Kernel()->RequestInterface(); + m_pStorage = Kernel()->RequestInterface(); m_World.SetGameServer(this); m_Events.SetGameServer(this); @@ -2576,11 +2578,12 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/) char aFilename[64]; str_format(aFilename, sizeof(aFilename), "teehistorian/%s.teehistorian", aGameUuid); - IOHANDLE File = Kernel()->RequestInterface()->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); + IOHANDLE File = Storage()->OpenFile(aFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE); if(!File) { dbg_msg("teehistorian", "failed to open '%s'", aFilename); - exit(1); + Server()->SetErrorShutdown("teehistorian open error"); + return; } else { @@ -2770,22 +2773,19 @@ void CGameContext::DeleteTempfile() { if(m_aDeleteTempfile[0] != 0) { - IStorage *pStorage = Kernel()->RequestInterface(); - pStorage->RemoveFile(m_aDeleteTempfile, IStorage::TYPE_SAVE); + Storage()->RemoveFile(m_aDeleteTempfile, IStorage::TYPE_SAVE); m_aDeleteTempfile[0] = 0; } } void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) { - IStorage *pStorage = Kernel()->RequestInterface(); - char aConfig[128]; char aTemp[128]; str_format(aConfig, sizeof(aConfig), "maps/%s.cfg", g_Config.m_SvMap); str_format(aTemp, sizeof(aTemp), "%s.temp.%d", pNewMapName, pid()); - IOHANDLE File = pStorage->OpenFile(aConfig, IOFLAG_READ, IStorage::TYPE_ALL); + IOHANDLE File = Storage()->OpenFile(aConfig, IOFLAG_READ, IStorage::TYPE_ALL); if(!File) { // No map-specific config, just return. @@ -2818,7 +2818,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) } CDataFileReader Reader; - Reader.Open(pStorage, pNewMapName, IStorage::TYPE_ALL); + Reader.Open(Storage(), pNewMapName, IStorage::TYPE_ALL); CDataFileWriter Writer; Writer.Init(); @@ -2896,7 +2896,7 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) dbg_msg("mapchange", "imported settings"); Reader.Close(); - Writer.OpenFile(pStorage, aTemp); + Writer.OpenFile(Storage(), aTemp); Writer.Finish(); str_copy(pNewMapName, aTemp, MapNameSize); @@ -2917,7 +2917,7 @@ void CGameContext::OnShutdown(bool FullShutdown) if(Error) { dbg_msg("teehistorian", "error closing file, err=%d", Error); - exit(1); + Server()->SetErrorShutdown("teehistorian close error"); } async_free(m_pTeeHistorianFile); } diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index a1eb2bd22..aa1ab3785 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -52,10 +52,13 @@ enum NUM_TUNEZONES = 256 }; +class IStorage; + class CGameContext : public IGameServer { IServer *m_pServer; class IConsole *m_pConsole; + IStorage *m_pStorage; CLayers m_Layers; CCollision m_Collision; CNetObjHandler m_NetObjHandler; @@ -107,6 +110,7 @@ class CGameContext : public IGameServer public: IServer *Server() const { return m_pServer; } class IConsole *Console() { return m_pConsole; } + IStorage *Storage() { return m_pStorage; } CCollision *Collision() { return &m_Collision; } CTuningParams *Tuning() { return &m_Tuning; } CTuningParams *TuningList() { return &m_aTuningList[0]; }