Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
292 lines (210 loc) · 4.31 KB
/
Copy pathmain.cpp
File metadata and controls
292 lines (210 loc) · 4.31 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "base/arg_helper.h"
#include "net/net_factory.h"
#include "base/strtool.h"
#include "net/codec.h"
#include "rpc/zzrpc_broker.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
using namespace zz;
void pause()
{
char ch;
cin>>ch;
}
void SayHello()
{
cout<<"This is a test from task binder !"<<endl;
}
int SayHello_ret()
{
cout<<"This is a test from task binder with ret value !"<<endl;
return 1;
}
int PrintStr_ret(string & str)
{
cout<<"[ret]str is"<<str<<endl;
return 1;
}
void PrintStr(string & str)
{
cout<<"str is"<<str<<endl;
}
class phone_t
{
public:
void call(string& phonenum)
{
cout<<"call:"<<phonenum<<endl;
phone_t * pThis = this;
task_binder::bind(&phone_t::send_sm,this);
}
virtual void send_sm()
{
cout<<"phone send message to:"<<"10086"<<endl;
string str = "dfce";
}
int m;
};
class android_phone_t : public phone_t
{
public:
virtual void send_sm()
{
cout<<"android_phone send message to:"<<"10086"<<endl;
string str = "dfce";
}
};
void test()
{
task_t t_ = task_binder::bind(SayHello);
t_.exe();
string str = "zk";
task_t t1_ = task_binder::bind(PrintStr,str);
t1_.exe();
task_t t2_(t1_);
t2_.exe();
t2_ = t_;
t2_.exe();
phone_t ph;
string strnum = "13437265183";
task_t t3_ = task_binder::bind(&phone_t::send_sm,&ph);
t3_.exe();
char ch;
cin>>ch;
}
class msg_handler_impl : public msg_handler_i
{
public:
virtual int handle_msg(msg_t& msg_, socket_ptr_t sock_)
{
cout<<"client handle_msg:"<<endl;
cout<<"cmd_id "<<msg_.cmd_id()<<endl;
cout<<"msg body "<<msg_.body.c_str()<<endl;
cout<<endl;
return 0;
}
virtual int handle_broken(socket_ptr_t sock_)
{
sock_connection_info& addr = sock_->connection_info();
cout<<"client broken:"<<endl;
cout<<"local:"<<addr.local_addr<<":"<<addr.local_port<<endl;
cout<<"peer:"<<addr.remote_addr<<":"<<addr.remote_port<<endl;
cout<<endl;
return 0;
}
virtual int handle_open(socket_ptr_t sock_)
{
sock_connection_info& addr = sock_->connection_info();
cout<<"client connect:"<<endl;
cout<<"local:"<<addr.local_addr<<":"<<addr.local_port<<endl;
cout<<"peer:"<<addr.remote_addr<<":"<<addr.remote_port<<endl;
cout<<endl;
return 0;
}
};
void test2()
{
msg_handler_impl handler;
std::string listen_host = "tcp://127.0.0.1:12345";
net_factory_t::listen(listen_host, &handler);
char ch;
cin>>ch;
net_factory_t::global_data.stop();
}
void test3()
{
void (phone_t::*func_ptr)() = &phone_t::send_sm;
int (phone_t::*varible_ptr) = &phone_t::m;
phone_t* phone = new android_phone_t;
(phone->*func_ptr)();
}
void test4(int argc, char** argv)
{
string host = "tcp://127.0.0.1:8001";
vector<string> vec_strs;
strtool_t::split(host, ":", vec_strs);
arg_helper_t arg_helper(argc, argv);
if (arg_helper.is_enable_option("-listen"))
{
string str = arg_helper.get_option_value("-listen");
cout<<str.c_str()<<endl;
}
if (arg_helper.is_enable_option("-broker"))
{
string str = arg_helper.get_option_value("-listen");
cout<<str.c_str()<<endl;
}
else
{
cout<<"-broker arg not exist"<<endl;
}
}
void test5()
{
bin_encoder_t encoder_;
char v1 = '1';
unsigned char v2 = 0xaa;
unsigned short v3 = 65523;
short zz = 12324;
int m = 12 ;
unsigned int um = 1245778;
string str = "123456789";
double c = 123e+12;
encoder_<<m<<zz<<str;
bin_decoder_t decoder_;
decoder_.init_decoder(encoder_.get_encoded_string());
string str_r;
int n;
short cc;
try
{
decoder_>>n>>cc>>str_r;
}
catch (exception& e_)
{
cout<<e_.what()<<endl;
}
pause();
}
void test6()
{
bin_encoder_t encoder_;
bin_decoder_t decoder_;
vector<int> vec_src;
vector<int> vec_dest;
list<int> list_src;
list<int> list_dest;
for (int i = 0; i<10; i++)
{
vec_src.push_back(i);
list_src.push_back(i+10);
}
encoder_<<vec_src<<list_src;
decoder_.init_decoder(encoder_.get_encoded_string());
decoder_>>vec_dest>>list_dest;
pause();
}
void test7()
{
msg_handler_impl handler;
std::string listen_host = "tcp://127.0.0.1:12345";
zzrpc_master_broker_t broker(listen_host, &handler);
if ( broker.start() == -1 )
{
cout<<"broker.start() fail"<<endl;
}
else
{
cout<<"broker.start() ok."<<endl;
cout<<"press any key to exit."<<endl;
}
char ch;
cin>>ch;
net_factory_t::global_data.stop();
}
int main(int argc, char* argv[])
{
test7();
return 0;
}