-
Notifications
You must be signed in to change notification settings - Fork 370
/
main.cpp
177 lines (147 loc) · 4.9 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
#include <rest_rpc.hpp>
using namespace rest_rpc;
using namespace rpc_service;
#include <fstream>
#include "qps.h"
struct dummy {
int add(rpc_conn conn, int a, int b) {
auto shared_conn = conn.lock();
if (shared_conn) {
shared_conn->set_user_data(std::string("aa"));
auto s = conn.lock()->get_user_data<std::string>();
std::cout << s << '\n'; // aa
}
return a + b;
}
};
std::string translate(rpc_conn conn, const std::string &orignal) {
std::string temp = orignal;
for (auto &c : temp) {
c = std::toupper(c);
}
return temp;
}
void hello(rpc_conn conn, const std::string &str) {
std::cout << "hello " << str << std::endl;
}
struct person {
int id;
std::string name;
int age;
MSGPACK_DEFINE(id, name, age);
};
std::string get_person_name(rpc_conn conn, const person &p) { return p.name; }
person get_person(rpc_conn conn) { return {1, "tom", 20}; }
void upload(rpc_conn conn, const std::string &filename,
const std::string &content) {
std::cout << content.size() << std::endl;
std::ofstream file(filename, std::ios::binary);
file.write(content.data(), content.size());
}
std::string download(rpc_conn conn, const std::string &filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
return "";
}
file.seekg(0, std::ios::end);
size_t file_len = file.tellg();
file.seekg(0, std::ios::beg);
std::string content;
content.resize(file_len);
file.read(&content[0], file_len);
std::cout << file_len << std::endl;
return content;
}
qps g_qps;
std::string get_name(rpc_conn conn, const person &p) {
g_qps.increase();
return p.name;
}
// if you want to response later, you can use async model, you can control when
// to response
void delay_echo(rpc_conn conn, const std::string &src) {
auto sp = conn.lock();
sp->set_delay(true);
auto req_id = sp->request_id(); // note: you need keep the request id at that
// time, and pass it into the async thread
std::thread thd([conn, req_id, src] {
std::this_thread::sleep_for(std::chrono::seconds(1));
auto conn_sp = conn.lock();
if (conn_sp) {
conn_sp->pack_and_response(req_id, std::move(src));
}
});
thd.detach();
}
std::string echo(rpc_conn conn, const std::string &src) {
g_qps.increase();
return src;
}
int get_int(rpc_conn conn, int val) { return val; }
void test_ssl() {
rpc_server server(9000, std::thread::hardware_concurrency(),
{"server.crt", "server.key"});
server.register_handler("hello", hello);
server.register_handler("echo", echo);
server.run();
}
void benchmark_test() {
rpc_server server(9000, std::thread::hardware_concurrency());
server.register_handler("echo", echo);
server.run();
}
struct dummy1 {
size_t id;
std::wstring str;
MSGPACK_DEFINE(id, str);
};
dummy1 get_dummy(rpc_conn conn, dummy1 d) { return d; }
int main() {
// benchmark_test();
rpc_server server(9000, std::thread::hardware_concurrency(), 3600);
dummy d;
server.register_handler("add", &dummy::add, &d);
server.register_handler("get_dummy", get_dummy);
server.register_handler("translate", translate);
server.register_handler("hello", hello);
server.register_handler("get_person_name", get_person_name);
server.register_handler("get_person", get_person);
server.register_handler("upload", upload);
server.register_handler("download", download);
server.register_handler("get_name", get_name);
server.register_handler("delay_echo", delay_echo);
server.register_handler("echo", echo);
server.register_handler("get_int", get_int);
server.register_handler("publish_by_token", [&server](rpc_conn conn,
std::string key,
std::string token,
std::string val) {
server.publish_by_token(std::move(key), std::move(token), std::move(val));
});
server.register_handler("publish",
[&server](rpc_conn conn, std::string key,
std::string token, std::string val) {
server.publish(std::move(key), std::move(val));
});
server.set_network_err_callback(
[](std::shared_ptr<connection> conn, std::string reason) {
std::cout << "remote client address: " << conn->remote_address()
<< " networking error, reason: " << reason << "\n";
});
bool stop = false;
std::thread thd([&server, &stop] {
person p{1, "tom", 20};
while (!stop) {
server.publish("key", "hello subscriber");
auto list = server.get_token_list();
for (auto &token : list) {
server.publish_by_token("key", token, p);
server.publish_by_token("key1", token, "hello subscriber1");
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
server.run();
stop = true;
thd.join();
}