-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
165 lines (144 loc) · 3.23 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
#include "include/fxnet_interface.h"
#include "include/message_queue.h"
#include "text_session.h"
#ifdef GPERF
#include "gperftools/tcmalloc.h"
#include "gperftools/profiler.h"
#include "gperftools/heap-profiler.h"
#include "gperftools/malloc_extension.h"
#endif //!GPERF
#ifndef _WIN32
#include <unistd.h>
#endif
#include <signal.h>
#include <vector>
bool g_bProFileState = false;
void OnSig45(int sig)
{
g_bProFileState = !g_bProFileState;
#ifdef GPERF
if (g_bProFileState)
{
ProfilerStart("prefix");
}
else
{
ProfilerStop();
}
#endif //!GPERF
std::cout <<__FUNCTION__ << "\n";
}
bool g_bHeapProFileState = false;
void OnSig46(int n)
{
g_bHeapProFileState = !g_bHeapProFileState;
#ifdef GPERF
if (g_bHeapProFileState)
{
HeapProfilerStart("heap_prefix");
}
else
{
HeapProfilerStop();
}
#endif //!GPERF
std::cout <<__FUNCTION__ << "\n";
}
void OnSig47(int sig)
{
#ifdef GPERF
MallocExtension::instance()->ReleaseFreeMemory();
#endif //!GPERF
}
int main()
{
signal(45, OnSig45);
signal(46, OnSig46);
signal(47, OnSig47);
FXNET::StartLogModule();
#ifdef _WIN32
Sleep(1);
#else
usleep(1000);
#endif // _WIN32
FXNET::MessageEventQueue oQueue;
FXNET::StartIOModule(&oQueue);
for (int i = 0; i < 100; ++i)
{
#ifdef _WIN32
Sleep(1);
#else
usleep(1000);
#endif // _WIN32
}
const char* szTargetIp = "81.70.54.105";
// const char* szTargetIp = "192.168.10.104";
std::vector<CTextSession*> vecSession;
#define SERVER_TEST 1
#if SERVER_TEST
#define TCP_CONNECT(ip, tcp_port) {}
#define UDP_CONNECT(ip, udp_port) {}
#else
#define TCP_CONNECT(ip, tcp_port) \
{\
vecSession.push_back(new CTextSession());\
int dwIndex1 = FXNET::GetFxIoModuleIndex();\
FXNET::PostEvent(dwIndex1, new FXNET::TCPConnect(ip, tcp_port, dwIndex1, vecSession.back()));\
}\
#define UDP_CONNECT(ip, udp_port) \
{\
vecSession.push_back(new CTextSession());\
int dwIndex1 = FXNET::GetFxIoModuleIndex();\
FXNET::PostEvent(dwIndex1, new FXNET::UDPConnect(ip, udp_port, dwIndex1, vecSession.back()));\
}\
#endif
#if SERVER_TEST
#define TCP_LISTEN(tcp_port) \
{\
int dwIndex1 = FXNET::GetFxIoModuleIndex();\
FXNET::PostEvent(dwIndex1, new FXNET::TCPListen("0.0.0.0", tcp_port, dwIndex1, new TextSessionMaker));\
}\
#define UDP_LISTEN(udp_port) \
{\
int dwIndex1 = FXNET::GetFxIoModuleIndex();\
FXNET::PostEvent(dwIndex1, new FXNET::UDPListen("0.0.0.0", udp_port, dwIndex1, new TextSessionMaker));\
}\
#else
#define TCP_LISTEN(tcp_port){}
#define UDP_LISTEN(udp_port){}
#endif
TCP_LISTEN(10085);
UDP_LISTEN(10086);
std::stringstream* pStrstream = FXNET::GetStream();
pStrstream->flags(std::cout.fixed);
for (int i = 0; ; ++i)
{
if (i >= 10 && i < 30)
{
TCP_CONNECT(szTargetIp, 10085);
UDP_CONNECT(szTargetIp, 10086);
}
#ifdef __SINGLE_THREAD__
FXNET::ProcSignelThread(pStrstream);
#endif //!__SINGLE_THREAD__
std::deque<MessageEventBase*> dequeMessage;
oQueue.SwapEvent(dequeMessage);
if(0 == dequeMessage.size())
{
#ifdef _WIN32
Sleep(1);
#else
usleep(1000);
#endif // _WIN32
continue;
}
for (std::deque<MessageEventBase*>::iterator it = dequeMessage.begin()
; it != dequeMessage.end(); ++it)
{
(**it)(pStrstream);
}
FXNET::PushLog(pStrstream);
pStrstream = FXNET::GetStream();
pStrstream->flags(std::cout.fixed);
}
}