diff -r 5a8b3a153621 src/trinityrealm/Main.cpp
--- a/src/trinityrealm/Main.cpp Fri Mar 19 22:28:00 2010 +0100
+++ b/src/trinityrealm/Main.cpp Fri Mar 19 23:53:13 2010 +0100
@@ -34,6 +34,8 @@
#include <ace/Dev_Poll_Reactor.h>
#include <ace/ACE.h>
+#include <ace/Event_Handler.h>
+#include <ace/Sig_Handler.h>
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
@@ -57,14 +59,41 @@
#endif
bool StartDB();
-void UnhookSignals();
-void HookSignals();
bool stopEvent = false; ///< Setting it to true stops the server
RealmList m_realmList; ///< Holds the list of realms for this server
DatabaseType loginDatabase; ///< Accessor to the realm server database
+/// Handle termination signals
+/** Put the global variable stopEvent to 'true' if a termination signal is caught **/
+class SignalHandler : public ACE_Event_Handler
+{
+ public:
+ SignalHandler(int Signal) : m_Signal(Signal) {}
+
+ virtual int handle_signal(int SigNum, siginfo_t* = 0, ucontext_t* = 0)
+ {
+ switch(SigNum)
+ {
+ case SIGINT:
+ case SIGTERM:
+ stopEvent = true;
+ break;
+ #ifdef _WIN32
+ case SIGBREAK:
+ if (m_ServiceStatus != 1)
+ stopEvent = true;
+ break;
+ #endif /* _WIN32 */
+ }
+ return 0;
+ }
+
+ private:
+ int m_Signal;
+};
+
/// Print out the usage string for this program on the console.
void usage(const char *prog)
{
@@ -223,8 +252,19 @@
return 1;
}
- ///- Catch termination signals
- HookSignals();
+ // Set the signal handlers
+ SignalHandler SignalINT(SIGINT), SignalTERM(SIGTERM);
+ #ifdef _WIN32
+ SignalHandler SignalBREAK(SIGBREAK);
+ #endif /* _WIN32 */
+
+ // Register realmd's signal handlers and catch it if the correct signal occured
+ ACE_Sig_Handler Handler;
+ Handler.register_handler(SIGINT, &SignalINT);
+ Handler.register_handler(SIGTERM, &SignalTERM);
+ #ifdef _WIN32
+ Handler.register_handler(SIGBREAK, &SignalBREAK);
+ #endif /* _WIN32 */
///- Handle affinity for multiple processors and process priority on Windows
#ifdef WIN32
@@ -312,34 +352,10 @@
loginDatabase.ThreadEnd();
loginDatabase.HaltDelayThread();
- ///- Remove signal handling before leaving
- UnhookSignals();
-
sLog.outString("Halting process...");
return 0;
}
-/// Handle termination signals
-/** Put the global variable stopEvent to 'true' if a termination signal is caught **/
-void OnSignal(int s)
-{
- switch (s)
- {
- case SIGINT:
- case SIGTERM:
- stopEvent = true;
- break;
- #ifdef _WIN32
- case SIGBREAK:
- if (m_ServiceStatus != 1)
- stopEvent = true;
- break;
- #endif
- }
-
- signal(s, OnSignal);
-}
-
/// Initialize connection to the database
bool StartDB()
{
@@ -360,26 +376,4 @@
return true;
}
-/// Define hook 'OnSignal' for all termination signals
-void HookSignals()
-{
- signal(SIGINT, OnSignal);
- signal(SIGTERM, OnSignal);
- #ifdef _WIN32
- if (m_ServiceStatus != 1)
- signal(SIGBREAK, OnSignal);
- #endif
-}
-
-/// Unhook the signals before leaving
-void UnhookSignals()
-{
- signal(SIGINT, 0);
- signal(SIGTERM, 0);
- #ifdef _WIN32
- if (m_ServiceStatus != 1)
- signal(SIGBREAK, 0);
- #endif
-}
-
/// @}