-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathichunkserver.h
185 lines (164 loc) · 5.17 KB
/
ichunkserver.h
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
#ifndef ITRUNKSERVER_H
#define ITRUNKSERVER_H
//chunk header
#include "ichunkmanager.h"
//#include "srcimgmanager.h"
#include "ichunkworker.h"
#include "chunk_conf.h"
//#include "global_var.h"
#include "rtmpclient.h"
#include "cutmachine.h"
//common header
#include "common.h"
#include <string>
extern void SigQuitFunc(void);
extern void SigPipeFunc(void);
extern void SigIgn(void);
template <class N>
class ITrunkServer
{
public:
// start logserver listenserver config managerthread workerthraed
static int StartServer()
{
int32_t totalCount = getCPUCount();
int8_t *cpuUse = new int8_t[totalCount];
int32_t cpuUseLen = totalCount;
memset(cpuUse, 0, cpuUseLen);
//daemon
doDaemon();
//config init
if(!ChunkConf::GetChunkConf()->Initialize("./chunk.conf"))
{
FATAL("[%s:%d ] chunk.conf init error", __func__, __LINE__);
exit(0);
}
//sys set
//set max limit of file
if ( !setLimitNoFile(/*RLIM_INFINITY*/1024*1024) )
{
FATAL("Unable to start daemonize. setLimitNoFile() failed");
return 0;
}
//signal process
#define I_SIG_IGN (void(*)()) 1
//installSignal(SIGINT, SigQuitFunc);
installSignal(SIGHUP, SigIgn);
installSignal(SIGPIPE, SigIgn);
installSignal(SIGTERM, SigIgn);
installSignal(SIGPIPE, SigPipeFunc);
installSignal(SIGQUIT, SigQuitFunc);
//log init
//loglevel logpath
int logLevel = _INFO_;
bool logCorlor = false;
logLevel = ChunkConf::GetChunkConf()->GetInt("loglevel", logLevel);
logCorlor = ChunkConf::GetChunkConf()->GetBool("logcorlor", logCorlor);
std::string log_path = ChunkConf::GetChunkConf()->GetString("log_path", "./");
INITIALIZE_LOG(logLevel, logCorlor);
if ( !Logger::LogSetDir(log_path.c_str()) )
{
FATAL("Logger::LogSetDir(%s) failed", log_path.c_str());
return 0;
}
//g_srcimg_manager = new SrcimgManager();
//if(!g_srcimg_manager->Initialize())
//{
// FATAL("srmimage manager init ok");
//}
//flv buffer init
g_flvbuffer = new FlvMicrotome();
g_flvbuffer->Initialize();
//rtmp client init
g_rtmpclient = new RtmpClient();
if(g_rtmpclient->Initialize())
{
FATAL(" rtmp initlize error ");
delete g_rtmpclient;
return 0;
}
//chunk_manager init
g_chunkmanager = new ChunkManager();
if( !g_chunkmanager->Initialize() )
{
FATAL("chunk manager init error");
return 0;
}
//woker init
int32_t netCount = 1;
netCount = ChunkConf::GetChunkConf()->GetInt("workthreadnum", netCount);
//if(netCount> totalCount)
//{
// FATAL("worker thread num is too bigger");
// return 0;
//}
for (int i = 0; i < netCount; i++)
{
ChunkWorker *worker = new N();
if ( !worker->Initialize(i, g_chunkmanager) )
{
FATAL("IChunkWorker initialize failed");
return 0;
}
// for (int j = 0; j < cpuUseLen; j++)
// {
// if ( 0 == cpuUse[j] )
// {
// cpuUse[j] = 1;
// //worker->cpuID_ = j;
// break;
// }
// }
g_chunkmanager->AddWorker(worker);
}
std::string hostIP = "0.0.0.0";
uint16_t hostPort = 5020;
hostIP = ChunkConf::GetChunkConf()->GetString("hostip", hostIP);
hostPort = ChunkConf::GetChunkConf()->GetInt("hostport", hostPort);
if ( !g_chunkmanager->SetupAccpet(hostIP, hostPort) )
{
FATAL("Listen %s %d falied", hostIP.c_str(), hostPort);
return 0;
}
//rtmpclient thread
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if( pthread_create(&thread, &attr, RtmpClient::StartThread, g_rtmpclient) < 0)
{
FATAL("RtmpClient::StartThread failed");
return 0;
}
//cutmachine thread
sleep(2);
if( pthread_create(&thread, &attr, FlvMicrotome::RunCutMachine, g_flvbuffer) < 0)
{
FATAL("FlvMicrotome::RunCutMachine failed");
return 0;
}
#if 0
//cutter thread init
/*vector<int32_t> cpu_ids;
cpu_ids.clear();
int flvbuffer_cpu_id = 2;
cpu_ids.push_back(flvbuffer_cpu_id);
if ( !setaffinityNp(cpu_ids, thread) )
{
FATAL("CpuID:%d setaffinityNp failed.", run_cpu_id);
exit(0);
}*/
#endif
g_chunkmanager->Start();
g_chunkmanager->Clear();
delete g_chunkmanager;
g_chunkmanager = NULL;
g_rtmpclient->Close();
delete g_rtmpclient;
delete g_flvbuffer;
delete[] cpuUse;
Logger::LogClose();
return 0;
};
};
#endif