Files
rest_rpc/examples/server/main.cpp
T

105 lines
2.4 KiB
C++
Raw Normal View History

2019-03-21 16:03:24 +08:00
#include <rpc_server.h>
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"
2019-03-21 16:03:24 +08:00
struct dummy{
2019-05-17 14:56:44 +08:00
int add(rpc_conn conn, int a, int b) { return a + b; }
2019-03-21 16:03:24 +08:00
};
2019-05-17 14:56:44 +08:00
std::string translate(rpc_conn conn, const std::string& orignal) {
2019-03-21 16:03:24 +08:00
std::string temp = orignal;
for (auto& c : temp) {
c = std::toupper(c);
}
return temp;
}
2019-05-17 14:56:44 +08:00
void hello(rpc_conn conn, const std::string& str) {
2019-03-21 16:03:24 +08:00
std::cout << "hello " << str << std::endl;
}
struct person {
int id;
std::string name;
int age;
MSGPACK_DEFINE(id, name, age);
};
2019-05-17 14:56:44 +08:00
std::string get_person_name(rpc_conn conn, const person& p) {
2019-03-21 16:03:24 +08:00
return p.name;
}
2019-05-17 14:56:44 +08:00
person get_person(rpc_conn conn) {
2019-03-21 16:03:24 +08:00
return { 1, "tom", 20 };
}
2019-05-17 14:56:44 +08:00
void upload(rpc_conn conn, const std::string& filename, const std::string& content) {
2019-03-28 12:58:18 +08:00
std::cout << content.size() << std::endl;
std::ofstream file(filename, std::ios::binary);
file.write(content.data(), content.size());
}
2019-05-17 14:56:44 +08:00
std::string download(rpc_conn conn, const std::string& filename) {
2019-03-28 12:58:18 +08:00
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-04-04 16:55:48 +08:00
qps g_qps;
2019-05-17 14:56:44 +08:00
std::string get_name(rpc_conn conn, const person& p) {
2019-04-04 16:55:48 +08:00
g_qps.increase();
return p.name;
}
2019-05-17 14:56:44 +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) {
std::thread thd([conn, src] {
std::this_thread::sleep_for(std::chrono::seconds(1));
auto conn_sp = conn.lock();
if (conn_sp) {
conn_sp->pack_and_response(std::move(src));
}
});
thd.detach();
}
std::string echo(rpc_conn conn, const std::string& src) {
return src;
2019-05-09 10:25:58 +08:00
}
2019-03-21 16:03:24 +08:00
int main() {
2019-04-10 13:48:35 +08:00
rpc_server server(9000, std::thread::hardware_concurrency());
2019-03-21 16:03:24 +08:00
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);
2019-03-28 12:58:18 +08:00
server.register_handler("upload", upload);
server.register_handler("download", download);
2019-04-04 16:55:48 +08:00
server.register_handler("get_name", get_name);
2019-05-17 14:56:44 +08:00
server.register_handler<ExecMode::async>("async_echo", async_echo);
server.register_handler("echo", echo);
2019-03-21 16:03:24 +08:00
server.run();
std::string str;
std::cin >> str;
}