Files
rest_rpc/examples/server/main.cpp
T

161 lines
4.4 KiB
C++
Raw Normal View History

2020-08-26 15:59:14 +08:00
#include <rest_rpc.hpp>
2019-03-21 16:03:24 +08:00
using namespace rest_rpc;
using namespace rpc_service;
2019-03-28 12:58:18 +08:00
#include <fstream>
2019-04-04 16:55:48 +08:00
#include "qps.h"
2021-08-20 20:16:05 +08:00
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;
}
2019-03-21 16:03:24 +08:00
};
2021-08-20 20:16:05 +08:00
std::string translate(rpc_conn conn, const std::string &orignal) {
std::string temp = orignal;
for (auto &c : temp) {
c = std::toupper(c);
}
return temp;
2019-03-21 16:03:24 +08:00
}
2021-08-20 20:16:05 +08:00
void hello(rpc_conn conn, const std::string &str) {
std::cout << "hello " << str << std::endl;
2019-03-21 16:03:24 +08:00
}
struct person {
2021-08-20 20:16:05 +08:00
int id;
std::string name;
int age;
2019-03-21 16:03:24 +08:00
2021-08-20 20:16:05 +08:00
MSGPACK_DEFINE(id, name, age);
2019-03-21 16:03:24 +08:00
};
2021-08-20 20:16:05 +08:00
std::string get_person_name(rpc_conn conn, const person &p) { return p.name; }
2019-03-21 16:03:24 +08:00
2021-08-20 20:16:05 +08:00
person get_person(rpc_conn conn) { return {1, "tom", 20}; }
2019-03-21 16:03:24 +08:00
2021-08-20 20:16:05 +08:00
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());
2019-03-28 12:58:18 +08:00
}
2021-08-20 20:16:05 +08:00
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;
2019-03-28 12:58:18 +08:00
}
2019-04-04 16:55:48 +08:00
qps g_qps;
2021-08-20 20:16:05 +08:00
std::string get_name(rpc_conn conn, const person &p) {
g_qps.increase();
return p.name;
2019-04-04 16:55:48 +08:00
}
2021-08-20 20:16:05 +08:00
// if you want to response later, you can use async model, you can control when
// to response
void async_echo(rpc_conn conn, const std::string &src) {
auto req_id =
conn.lock()->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();
2019-05-17 14:56:44 +08:00
}
2021-08-20 20:16:05 +08:00
std::string echo(rpc_conn conn, const std::string &src) {
2020-09-21 15:38:29 +08:00
g_qps.increase();
2021-08-20 20:16:05 +08:00
return src;
2019-05-09 10:25:58 +08:00
}
2021-08-20 20:16:05 +08:00
int get_int(rpc_conn conn, int val) { return val; }
2019-06-04 08:56:41 +08:00
2020-06-08 09:23:51 +08:00
void test_ssl() {
2021-08-20 20:16:05 +08:00
rpc_server server(9000, std::thread::hardware_concurrency(),
{"server.crt", "server.key"});
server.register_handler("hello", hello);
server.register_handler("echo", echo);
server.run();
2020-06-08 09:23:51 +08:00
}
2021-08-20 20:16:05 +08:00
void benchmark_test() {
2020-09-21 15:38:29 +08:00
rpc_server server(9000, std::thread::hardware_concurrency());
server.register_handler("echo", echo);
server.run();
}
2019-03-21 16:03:24 +08:00
int main() {
2021-08-20 20:16:05 +08:00
// benchmark_test();
rpc_server server(9000, std::thread::hardware_concurrency());
dummy d;
server.register_handler("add", &dummy::add, &d);
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<Async>("async_echo", async_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));
});
std::thread thd([&server] {
person p{1, "tom", 20};
while (true) {
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();
std::string str;
std::cin >> str;
2019-03-21 16:03:24 +08:00
}