Skip to content

Commit 561dacb

Browse files
Synchronize changes from 1.6 master branch [ci skip]
b5060de Print local server output to console on disconnect/shutdown 86945b4 Improve graceful server shutdown
2 parents 1a468ee + b5060de commit 561dacb

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

Client/mods/deathmatch/logic/CServer.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ void CServer::Stop(bool graceful)
225225
}
226226
}
227227

228+
// Print out the remaining server output (but not everything to avoid blocking here).
229+
for (int i = 0; i < 5; ++i)
230+
{
231+
if (!PrintServerOutputToConsole())
232+
break;
233+
}
234+
228235
if (DWORD exitCode{}; GetExitCodeProcess(m_process, &exitCode))
229236
{
230237
const char* errorText = "Unknown exit code";
@@ -256,16 +263,7 @@ void CServer::Pulse()
256263
if (!m_isRunning)
257264
return;
258265

259-
// Try to read the process standard output and then write it to the console window.
260-
if (DWORD numBytes{}; PeekNamedPipe(m_stdout, nullptr, 0, nullptr, &numBytes, nullptr) && numBytes > 0)
261-
{
262-
std::array<char, 4096> buffer{};
263-
264-
if (ReadFile(m_stdout, buffer.data(), std::min<DWORD>(buffer.size(), numBytes), &numBytes, nullptr) && numBytes > 0)
265-
{
266-
g_pCore->GetConsole()->Printf("%.*s", numBytes, buffer.data());
267-
}
268-
}
266+
PrintServerOutputToConsole();
269267

270268
// Check if the server process was terminated externally.
271269
if (!IsProcessRunning(m_process))
@@ -285,3 +283,20 @@ void CServer::Pulse()
285283
}
286284
}
287285
}
286+
287+
bool CServer::PrintServerOutputToConsole()
288+
{
289+
// Try to read the process standard output and then write it to the console window.
290+
if (DWORD numBytes{}; PeekNamedPipe(m_stdout, nullptr, 0, nullptr, &numBytes, nullptr) && numBytes > 0)
291+
{
292+
std::array<char, 4096> buffer{};
293+
294+
if (ReadFile(m_stdout, buffer.data(), std::min<DWORD>(buffer.size(), numBytes), &numBytes, nullptr) && numBytes > 0)
295+
{
296+
g_pCore->GetConsole()->Printf("%.*s", numBytes, buffer.data());
297+
return true;
298+
}
299+
}
300+
301+
return false;
302+
}

Client/mods/deathmatch/logic/CServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class CServer final
3030

3131
const std::string& GetPassword() const noexcept { return m_password; }
3232

33+
public:
34+
bool PrintServerOutputToConsole();
35+
3336
private:
3437
bool m_isRunning{};
3538
bool m_isAcceptingConnections{};

Server/core/CModManagerImpl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ void CModManagerImpl::Unload(bool bKeyPressBeforeTerm)
130130
Print("Press Q to shut down the server!\n");
131131
WaitForKey('q');
132132
}
133-
TerminateProcess(GetCurrentProcess(), GetExitCode());
134133
}
135134
#endif
136135
// Unload the library

Server/core/CServerImpl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,26 @@ int CServerImpl::Run(int iArgumentCount, char* szArguments[])
351351

352352
if (m_XMLLibrary.Load(PathJoin(m_strServerPath, SERVER_BIN_PATH, szXMLLibName)))
353353
{
354-
// Grab the network interface
355-
InitNetServerInterface pfnInitNetServerInterface = (InitNetServerInterface)(m_NetworkLibrary.GetProcedureAddress("InitNetServerInterface"));
356-
InitXMLInterface pfnInitXMLInterface = (InitXMLInterface)(m_XMLLibrary.GetProcedureAddress("InitXMLInterface"));
354+
auto pfnInitNetServerInterface = (InitNetServerInterface)(m_NetworkLibrary.GetProcedureAddress("InitNetServerInterface"));
355+
auto pfnReleaseNetServerInterface = (ReleaseNetServerInterface)(m_NetworkLibrary.GetProcedureAddress("ReleaseNetServerInterface"));
356+
auto pfnInitXMLInterface = (InitXMLInterface)(m_XMLLibrary.GetProcedureAddress("InitXMLInterface"));
357+
357358
if (pfnInitNetServerInterface && pfnInitXMLInterface)
358359
{
359360
// Call it to grab the network interface class
360361
m_pNetwork = pfnInitNetServerInterface();
361362
m_pXML = pfnInitXMLInterface(*m_strServerModPath);
363+
362364
if (m_pNetwork && m_pXML)
363365
{
364366
// Make the modmanager load our mod
365367
if (m_pModManager->Load("deathmatch", iArgumentCount, szArguments)) // Hardcoded for now
366368
{
367369
// Enter our mainloop
368370
MainLoop();
371+
372+
if (pfnReleaseNetServerInterface)
373+
pfnReleaseNetServerInterface();
369374
}
370375
else
371376
{

Server/core/CServerImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CServerImpl;
2828

2929
typedef CXML* (*InitXMLInterface)(const char* szSaveFlagDirectory);
3030
typedef CNetServer* (*InitNetServerInterface)();
31+
typedef void (*ReleaseNetServerInterface)();
3132

3233
#ifdef WIN32
3334
constexpr SHORT SCREEN_BUFFER_SIZE = 256;

0 commit comments

Comments
 (0)