-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_protobuf_example.cpp
157 lines (124 loc) · 3.51 KB
/
server_protobuf_example.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
#include "stdio.h"
#include "signal.h"
#include "pthread.h"
#include <unistd.h>
#include "include/server.h"
#include "include/log.h"
#include "include/net_timer.h"
#include "logic/protobuf_client.h"
#include "logic/protobuf_server.h"
#include <memory>
#include <string>
#include <vector>
#include "net_msg/login.pb.h"
#include "net_msg/msg_num.pb.h"
#include <gtest/gtest.h>
std::atomic<int> is_running(true);
static void ctrl_handler(int sig) {
LOG("ctrl+c");
is_running = false;
}
static void* sock_thread_handler(void* ser) {
if (NULL == ser) {
LOG("thread error");
return NULL;
}
server* pser = (server*)ser;
int res = pser->init_ae();
if (res < 0) {
LOG("init_ae error");
return NULL;
}
res = pser->create_server_sock("0.0.0.0", 9999);
if (res < 0) {
LOG("create error");
return NULL;
}
pser->ae_poll();
return NULL;
}
static void* client_handler(void* ser) {
std::unique_ptr<protobuf_client> pc(new protobuf_client(0, 0));
int res = pc->sync_connect("0.0.0.0", 9999);
if (res != 0) {
LOG("connect error");
return NULL;
}
//pc->set_noblock();
pc->set_nodelay();
game::ReqLogin login_msg;
login_msg.set_msg_id(game::eMsg_ReqLogin);
login_msg.set_account_id("allen");
login_msg.set_device_id(111);
int msg_id = login_msg.msg_id();
pc->send_pb_msg(&login_msg, msg_id);
pc->send_pb_msg(&login_msg, msg_id);
sleep(9);
pc->send_pb_msg(&login_msg, msg_id);
pc->send_pb_msg(&login_msg, msg_id);
sleep(10);
//pc->read_data();
LOG("thread finish!");
return NULL;
}
void naive_client_for_test(int thread_num) {
std::vector< pthread_t*> pvec;
for (size_t i = 0; i < thread_num; i++){
pthread_t* pt = new pthread_t;
pthread_create(pt, NULL, client_handler, NULL);
pvec.push_back(pt);
}
for (size_t i = 0; i < thread_num; i++) {
pthread_t* pt = pvec[i];
pthread_join(*pt, NULL);
delete pt;
}
}
enum program_type_enum {
program_type_server = 0,
program_type_client = 1,
program_type_test = 2
};
int main(int argc, char* argv[]) {
std::unique_ptr<net_timer> ptimer(net_timer::get_instance());
signal(SIGINT, ctrl_handler);
int program_type = 0;
if (argc > 1) {
program_type = atoi(argv[1]);
LOG("program_type %d", program_type);
}
if (program_type_test == program_type) {
int argc = 0;
char* argv[] = {"test"};
testing::GTEST_FLAG(output) = "xml:";
LOG("test1");
testing::InitGoogleTest(&argc, argv);
LOG("test2");
RUN_ALL_TESTS();
LOG("test3");
return 0;
}
if (program_type_client == program_type) {
int thread_num = 0;
if (argc > 2) {
thread_num = atoi(argv[2]);
}
naive_client_for_test(thread_num);
return 0;
}
std::unique_ptr<server> pser(new protobuf_server);
if (NULL == pser) {
LOG("create server error");
return -1;
}
pthread_t t;
pthread_create(&t, NULL, sock_thread_handler, (void*)pser.get());
while (is_running) {
sleep(1);
}
LOG("try to stop server");
pser->set_running_flag(false);
pthread_join(t, NULL);
LOG("finish main");
return 0;
}