forked from p308945/MyNetWorkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBaseServer.cpp
More file actions
88 lines (81 loc) · 1.96 KB
/
MyBaseServer.cpp
File metadata and controls
88 lines (81 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*************************************************************************
> File Name: MyBaseServer.cpp
> Author: huangyun
> Mail: 895175589@qq.com
> Created Time: Sat 08 Aug 2015 02:41:36 PM
************************************************************************/
#include "MyBaseServer.h"
#include <signal.h>
#include <iostream>
#include <unistd.h>
namespace MyNameSpace
{
namespace
{
void handlerHup(int sig)
{
std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): get hup signal"<<std::endl;
MyBaseServer::Container_IT iter = MyBaseServer::mServerContainer.begin();
for (; iter != MyBaseServer::mServerContainer.end(); ++iter)
{
(*iter)->reload();
}
}
void handlerCtrlC(int sig)
{
std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): get ctrl c signal"<<std::endl;
MyBaseServer::Container_IT iter = MyBaseServer::mServerContainer.begin();
for (; iter != MyBaseServer::mServerContainer.end(); ++iter)
{
(*iter)->fini();
}
}
}
MyBaseServer::Container MyBaseServer::mServerContainer;
bool MyBaseServer::reload()
{
return true;
}
void MyBaseServer::regInnerCallBack(uint32_t cmdId, CallBackFunT fun)
{
mInnerDispatcher.regCallback(cmdId, fun);
}
void MyBaseServer::regOutterCallBack(uint32_t cmdId, CallBackFunT fun)
{
mOutterDispatcher.regCallback(cmdId, fun);
}
bool MyBaseServer::init(int port)
{
mServerContainer.push_back(this);
bool ret = mTcpServer.bindPort(port);
if (!ret)
{
std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): bind fail"<<std::endl;
return false;
}
struct sigaction sigact;
sigact.sa_handler = handlerHup;
sigaction(SIGHUP, &sigact, NULL);
sigact.sa_handler = handlerCtrlC;
sigaction(SIGINT, &sigact, NULL);
signal(SIGPIPE,SIG_IGN);
return ret;
}
int MyBaseServer::serverProcess()
{
int ret = mTcpServer.acceptCallBack();
if (ret >= 0)
{
newTask(ret);
}
return ret;
}
void MyBaseServer::mainLoop()
{
while(!isFini())
{
serverProcess();
usleep(3*1000);
}
}
}