Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 19th, 2010 | Syntax: Diff | Size: 3.71 KB | Hits: 61 | Expires: Never
Copy text to clipboard
  1. diff -r 5a8b3a153621 src/trinityrealm/Main.cpp
  2. --- a/src/trinityrealm/Main.cpp Fri Mar 19 22:28:00 2010 +0100
  3. +++ b/src/trinityrealm/Main.cpp Fri Mar 19 23:53:13 2010 +0100
  4. @@ -34,6 +34,8 @@
  5.  
  6.  #include <ace/Dev_Poll_Reactor.h>
  7.  #include <ace/ACE.h>
  8. +#include <ace/Event_Handler.h>
  9. +#include <ace/Sig_Handler.h>
  10.  
  11.  #include <openssl/opensslv.h>
  12.  #include <openssl/crypto.h>
  13. @@ -57,14 +59,41 @@
  14.  #endif
  15.  
  16.  bool StartDB();
  17. -void UnhookSignals();
  18. -void HookSignals();
  19.  
  20.  bool stopEvent = false;                                     ///< Setting it to true stops the server
  21.  RealmList m_realmList;                                      ///< Holds the list of realms for this server
  22.  
  23.  DatabaseType loginDatabase;                                 ///< Accessor to the realm server database
  24.  
  25. +/// Handle termination signals
  26. +/** Put the global variable stopEvent to 'true' if a termination signal is caught **/
  27. +class SignalHandler : public ACE_Event_Handler
  28. +{
  29. +    public:
  30. +        SignalHandler(int Signal) : m_Signal(Signal) {}
  31. +
  32. +        virtual int handle_signal(int SigNum, siginfo_t* = 0, ucontext_t* = 0)
  33. +        {
  34. +            switch(SigNum)
  35. +            {
  36. +                case SIGINT:
  37. +                case SIGTERM:
  38. +                    stopEvent = true;
  39. +                    break;
  40. +                #ifdef _WIN32
  41. +                case SIGBREAK:
  42. +                    if (m_ServiceStatus != 1)
  43. +                        stopEvent = true;
  44. +                    break;
  45. +                #endif /* _WIN32 */
  46. +            }
  47. +            return 0;
  48. +        }
  49. +
  50. +    private:
  51. +        int m_Signal;
  52. +};
  53. +
  54.  /// Print out the usage string for this program on the console.
  55.  void usage(const char *prog)
  56.  {
  57. @@ -223,8 +252,19 @@
  58.          return 1;
  59.      }
  60.  
  61. -    ///- Catch termination signals
  62. -    HookSignals();
  63. +    // Set the signal handlers
  64. +    SignalHandler SignalINT(SIGINT), SignalTERM(SIGTERM);
  65. +    #ifdef _WIN32
  66. +    SignalHandler SignalBREAK(SIGBREAK);
  67. +    #endif /* _WIN32 */
  68. +
  69. +    // Register realmd's signal handlers and catch it if the correct signal occured
  70. +    ACE_Sig_Handler Handler;
  71. +    Handler.register_handler(SIGINT, &SignalINT);
  72. +    Handler.register_handler(SIGTERM, &SignalTERM);
  73. +    #ifdef _WIN32
  74. +    Handler.register_handler(SIGBREAK, &SignalBREAK);
  75. +    #endif /* _WIN32 */
  76.  
  77.      ///- Handle affinity for multiple processors and process priority on Windows
  78.      #ifdef WIN32
  79. @@ -312,34 +352,10 @@
  80.      loginDatabase.ThreadEnd();
  81.      loginDatabase.HaltDelayThread();
  82.  
  83. -    ///- Remove signal handling before leaving
  84. -    UnhookSignals();
  85. -
  86.      sLog.outString("Halting process...");
  87.      return 0;
  88.  }
  89.  
  90. -/// Handle termination signals
  91. -/** Put the global variable stopEvent to 'true' if a termination signal is caught **/
  92. -void OnSignal(int s)
  93. -{
  94. -    switch (s)
  95. -    {
  96. -        case SIGINT:
  97. -        case SIGTERM:
  98. -            stopEvent = true;
  99. -            break;
  100. -        #ifdef _WIN32
  101. -        case SIGBREAK:
  102. -            if (m_ServiceStatus != 1)
  103. -                stopEvent = true;
  104. -            break;
  105. -        #endif
  106. -    }
  107. -
  108. -    signal(s, OnSignal);
  109. -}
  110. -
  111.  /// Initialize connection to the database
  112.  bool StartDB()
  113.  {
  114. @@ -360,26 +376,4 @@
  115.      return true;
  116.  }
  117.  
  118. -/// Define hook 'OnSignal' for all termination signals
  119. -void HookSignals()
  120. -{
  121. -    signal(SIGINT, OnSignal);
  122. -    signal(SIGTERM, OnSignal);
  123. -    #ifdef _WIN32
  124. -    if (m_ServiceStatus != 1)
  125. -        signal(SIGBREAK, OnSignal);
  126. -    #endif
  127. -}
  128. -
  129. -/// Unhook the signals before leaving
  130. -void UnhookSignals()
  131. -{
  132. -    signal(SIGINT, 0);
  133. -    signal(SIGTERM, 0);
  134. -    #ifdef _WIN32
  135. -    if (m_ServiceStatus != 1)
  136. -        signal(SIGBREAK, 0);
  137. -    #endif
  138. -}
  139. -
  140.  /// @}