Révision | dbe0282763fbef5ad89e4c72bbfd00386075ca64 (tree) |
---|---|
l'heure | 2012-09-15 03:51:00 |
Auteur | shibacow <shibacow@user...> |
Commiter | shibacow |
シグナルを受けて終了するように変更
@@ -75,7 +75,14 @@ namespace network { | ||
75 | 75 | void Server::Stop() |
76 | 76 | { |
77 | 77 | io_service_.stop(); |
78 | + Logger::Info("stop server"); | |
78 | 79 | } |
80 | + void Server::Stop(int innterrupt_type) | |
81 | + { | |
82 | + io_service_.stop(); | |
83 | + Logger::Info(_T("stop server innterrupt_type=%d"),innterrupt_type); | |
84 | + } | |
85 | + | |
79 | 86 | |
80 | 87 | bool Server::Empty() const |
81 | 88 | { |
@@ -28,6 +28,7 @@ class Server { | ||
28 | 28 | Server(uint16_t port); |
29 | 29 | void Start(CallbackFuncPtr callback); |
30 | 30 | void Stop(); |
31 | + void Stop(int interrupt_type); | |
31 | 32 | void SendAll(const Command&); |
32 | 33 | void SendOthers(const Command&, SessionWeakPtr); |
33 | 34 |
@@ -0,0 +1,62 @@ | ||
1 | +// | |
2 | +// 割り込みハンドラーを用いてgracefull delete | |
3 | +// | |
4 | + | |
5 | +#pragma once | |
6 | +#include <csignal> | |
7 | +#include <boost/thread.hpp> | |
8 | +//#include "../common/network/Session.hpp" | |
9 | +#include "Server.hpp" | |
10 | + | |
11 | +namespace network { | |
12 | + | |
13 | +class ServerSigHandler { | |
14 | +public: | |
15 | + ServerSigHandler(int sig,Server *_server) : sig_to_wait(sig){ | |
16 | + sigset_t ss; | |
17 | + sigemptyset( &ss ); | |
18 | + sigaddset( &ss, sig_to_wait ); | |
19 | + // マスクをセットして現在のマスクのバックアップも行う | |
20 | + sigprocmask( SIG_BLOCK, &ss, &backup ); | |
21 | + // シグナルを待機して、補足、メンバー関数の実行をするスレッドを実行 | |
22 | + signal_thread = new boost::thread( boost::bind(&ServerSigHandler::wait_and_exec, this) ); | |
23 | + signal_thread->detach(); | |
24 | + server=_server; | |
25 | + } | |
26 | + | |
27 | + ~ServerSigHandler() { | |
28 | + sigprocmask( SIG_SETMASK, &backup, NULL ); | |
29 | + //signal_thread->join(); いらない | |
30 | + delete signal_thread; | |
31 | + } | |
32 | + | |
33 | +private: | |
34 | + // トラップするシグナル | |
35 | + int sig_to_wait; | |
36 | + // マスクのバックアップ | |
37 | + sigset_t backup; | |
38 | + boost::thread *signal_thread; | |
39 | + Server *server; | |
40 | + bool wait_and_exec() { | |
41 | + sigset_t ss; | |
42 | + sigemptyset(&ss); | |
43 | + int ret = sigaddset(&ss, sig_to_wait); | |
44 | + if (ret != 0) | |
45 | + return false; | |
46 | + // シグナルをブロック | |
47 | + ret = pthread_sigmask(SIG_BLOCK, &ss, NULL); | |
48 | + if (ret != 0) | |
49 | + return false; | |
50 | + while(1) { | |
51 | + int signo; | |
52 | + if (sigwait(&ss, &signo) == 0) { | |
53 | + //Serverをstopする. | |
54 | + (server->Stop)( signo ); | |
55 | + break; | |
56 | + } | |
57 | + } | |
58 | + return true; | |
59 | + } | |
60 | +}; | |
61 | + | |
62 | +} |
@@ -16,6 +16,8 @@ | ||
16 | 16 | #include "../common/Logger.hpp" |
17 | 17 | #include "Config.hpp" |
18 | 18 | #include "Account.hpp" |
19 | +#include "ServerSigHandler.hpp" | |
20 | +#include <csignal> | |
19 | 21 | |
20 | 22 | #ifdef _WIN32 |
21 | 23 | #include <boost/interprocess/windows_shared_memory.hpp> |
@@ -316,11 +318,12 @@ int main(int argc, char* argv[]) | ||
316 | 318 | }); |
317 | 319 | } |
318 | 320 | #endif |
319 | - | |
321 | + network::ServerSigHandler handler(SIGINT,&server); | |
320 | 322 | server.Start(callback); |
321 | 323 | |
322 | 324 | } catch (std::exception& e) { |
323 | 325 | Logger::Error(e.what()); |
326 | + | |
324 | 327 | Logger::Info("Stop Server"); |
325 | 328 | } |
326 | 329 |