update, add upload/download example

This commit is contained in:
qicosmos
2019-03-28 12:58:18 +08:00
parent 2eb0d4f7df
commit 029d08b5f9
8 changed files with 153 additions and 59 deletions
+26 -1
View File
@@ -1,7 +1,7 @@
#include <rpc_server.h>
using namespace rest_rpc;
using namespace rpc_service;
#include <fstream>
struct dummy{
int add(connection* conn, int a, int b) { return a + b; }
};
@@ -34,6 +34,29 @@ person get_person(connection* conn) {
return { 1, "tom", 20 };
}
void upload(connection* 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(connection* 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;
}
int main() {
rpc_server server(9000, 4);
@@ -43,6 +66,8 @@ int main() {
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.run();