-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathmain.cpp
194 lines (167 loc) · 4.95 KB
/
main.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*================================================================
* Copyright (C) 2014 All rights reserved.
*
* @ File : main.cpp
* @ Author : Zhang Yuanhao
* @ Email : [email protected]
* @ Date : 2014年7月29日
* @ Version : 1.0
* @ Description :
*
================================================================*/
#include <iostream>
#include <signal.h>
#include "netlib.h"
#include "ConfigFileReader.h"
#include "HttpConn.h"
#include "FileManager.h"
#include "ThreadPool.h"
using namespace std;
using namespace msfs;
FileManager* FileManager::m_instance = NULL;
FileManager* g_fileManager = NULL;
CConfigFileReader config_file("msfs.conf");
CThreadPool g_PostThreadPool;
CThreadPool g_GetThreadPool;
void closeall(int fd)
{
int fdlimit = sysconf(_SC_OPEN_MAX);
while (fd < fdlimit)
close(fd++);
}
int daemon(int nochdir, int noclose, int asroot)
{
switch (fork())
{
case 0: break;
case -1: return -1;
default: _exit(0); /* exit the original process */
}
if (setsid() < 0) /* shoudn't fail */
return -1;
if ( !asroot && (setuid(1) < 0) ) /* shoudn't fail */
return -1;
/* dyke out this switch if you want to acquire a control tty in */
/* the future -- not normally advisable for daemons */
switch (fork())
{
case 0: break;
case -1: return -1;
default: _exit(0);
}
if (!nochdir)
chdir("/");
if (!noclose)
{
closeall(0);
dup(0); dup(0);
}
return 0;
}
// for client connect in
void http_callback(void* callback_data, uint8_t msg, uint32_t handle,
void* pParam)
{
if (msg == NETLIB_MSG_CONNECT)
{
CHttpConn* pConn = new CHttpConn();
// CHttpTask* pTask = new CHttpTask(handle, pConn);
// g_ThreadPool.AddTask(pTask);
pConn->OnConnect(handle);
} else
{
log("!!!error msg: %d\n", msg);
}
}
void doQuitJob()
{
char fileCntBuf[20] = {0};
snprintf(fileCntBuf, 20, "%llu", g_fileManager->getFileCntCurr());
config_file.SetConfigValue("FileCnt", fileCntBuf);
FileManager::destroyInstance();
netlib_destroy();
log("I'm ready quit...\n");
}
void Stop(int signo)
{
switch(signo)
{
case SIGINT:
case SIGTERM:
case SIGHUP:
case SIGQUIT:
doQuitJob();
_exit(0);
break;
default:
cout<< "unknown signal"<<endl;
_exit(0);
}
}
int main(int argc, char* argv[])
{
for(int i=0; i < argc; ++i)
{
if(strncmp(argv[i], "-d", 2) == 0)
{
if(daemon(1, 0, 1) < 0)
{
cout<<"daemon error"<<endl;
return -1;
}
break;
}
}
log("MsgServer max files can open: %d\n", getdtablesize());
char* listen_ip = config_file.GetConfigName("ListenIP");
char* str_listen_port = config_file.GetConfigName("ListenPort");
char* base_dir = config_file.GetConfigName("BaseDir");
char* str_file_cnt = config_file.GetConfigName("FileCnt");
char* str_files_per_dir = config_file.GetConfigName("FilesPerDir");
char* str_post_thread_count = config_file.GetConfigName("PostThreadCount");
char* str_get_thread_count = config_file.GetConfigName("GetThreadCount");
if (!listen_ip || !str_listen_port || !base_dir || !str_file_cnt || !str_files_per_dir || !str_post_thread_count || !str_get_thread_count)
{
log("config file miss, exit...\n");
return -1;
}
log("%s,%s\n",listen_ip, str_listen_port);
uint16_t listen_port = atoi(str_listen_port);
long long int fileCnt = atoll(str_file_cnt);
int filesPerDir = atoi(str_files_per_dir);
int nPostThreadCount = atoi(str_post_thread_count);
int nGetThreadCount = atoi(str_get_thread_count);
if(nPostThreadCount <= 0 || nGetThreadCount <= 0)
{
log("thread count is invalied");
return -1;
}
g_PostThreadPool.Init(nPostThreadCount);
g_GetThreadPool.Init(nGetThreadCount);
g_fileManager = FileManager::getInstance(listen_ip, base_dir, fileCnt, filesPerDir);
int ret = g_fileManager->initDir();
if (ret) {
printf("The BaseDir is set incorrectly :%s\n",base_dir);
return ret;
}
ret = netlib_init();
if (ret == NETLIB_ERROR)
return ret;
CStrExplode listen_ip_list(listen_ip, ';');
for (uint32_t i = 0; i < listen_ip_list.GetItemCnt(); i++)
{
ret = netlib_listen(listen_ip_list.GetItem(i), listen_port,
http_callback, NULL);
if (ret == NETLIB_ERROR)
return ret;
}
signal(SIGINT, Stop);
signal (SIGTERM, Stop);
signal (SIGHUP, Stop);
signal (SIGQUIT, Stop);
printf("server start listen on: %s:%d\n", listen_ip, listen_port);
init_http_conn();
printf("now enter the event loop...\n");
netlib_eventloop();
return 0;
}