initial commit
This commit is contained in:
79
third_party/libhv/examples/protorpc/handler/calc.h
vendored
Executable file
79
third_party/libhv/examples/protorpc/handler/calc.h
vendored
Executable file
@@ -0,0 +1,79 @@
|
||||
#ifndef HV_PROTO_RPC_HANDLER_CALC_H_
|
||||
#define HV_PROTO_RPC_HANDLER_CALC_H_
|
||||
|
||||
#include "../router.h"
|
||||
|
||||
#include "../generated/calc.pb.h"
|
||||
|
||||
void calc_add(const protorpc::Request& req, protorpc::Response* res) {
|
||||
// params
|
||||
if (req.params_size() != 2) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
protorpc::CalcParam param1, param2;
|
||||
if (!param1.ParseFromString(req.params(0)) ||
|
||||
!param2.ParseFromString(req.params(1))) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
|
||||
// result
|
||||
protorpc::CalcResult result;
|
||||
result.set_num(param1.num() + param2.num());
|
||||
res->set_result(result.SerializeAsString());
|
||||
}
|
||||
|
||||
void calc_sub(const protorpc::Request& req, protorpc::Response* res) {
|
||||
// params
|
||||
if (req.params_size() != 2) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
protorpc::CalcParam param1, param2;
|
||||
if (!param1.ParseFromString(req.params(0)) ||
|
||||
!param2.ParseFromString(req.params(1))) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
|
||||
// result
|
||||
protorpc::CalcResult result;
|
||||
result.set_num(param1.num() - param2.num());
|
||||
res->set_result(result.SerializeAsString());
|
||||
}
|
||||
|
||||
void calc_mul(const protorpc::Request& req, protorpc::Response* res) {
|
||||
// params
|
||||
if (req.params_size() != 2) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
protorpc::CalcParam param1, param2;
|
||||
if (!param1.ParseFromString(req.params(0)) ||
|
||||
!param2.ParseFromString(req.params(1))) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
|
||||
// result
|
||||
protorpc::CalcResult result;
|
||||
result.set_num(param1.num() * param2.num());
|
||||
res->set_result(result.SerializeAsString());
|
||||
}
|
||||
|
||||
void calc_div(const protorpc::Request& req, protorpc::Response* res) {
|
||||
// params
|
||||
if (req.params_size() != 2) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
protorpc::CalcParam param1, param2;
|
||||
if (!param1.ParseFromString(req.params(0)) ||
|
||||
!param2.ParseFromString(req.params(1))) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
if (param2.num() == 0) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
|
||||
// result
|
||||
protorpc::CalcResult result;
|
||||
result.set_num(param1.num() / param2.num());
|
||||
res->set_result(result.SerializeAsString());
|
||||
}
|
||||
|
||||
#endif // HV_PROTO_RPC_HANDLER_CALC_H_
|
||||
19
third_party/libhv/examples/protorpc/handler/handler.h
vendored
Executable file
19
third_party/libhv/examples/protorpc/handler/handler.h
vendored
Executable file
@@ -0,0 +1,19 @@
|
||||
#ifndef HV_PROTO_RPC_HANDLER_H_
|
||||
#define HV_PROTO_RPC_HANDLER_H_
|
||||
|
||||
#include "../router.h"
|
||||
|
||||
void error_response(protorpc::Response* res, int code, const std::string& message) {
|
||||
res->mutable_error()->set_code(code);
|
||||
res->mutable_error()->set_message(message);
|
||||
}
|
||||
|
||||
void not_found(const protorpc::Request& req, protorpc::Response* res) {
|
||||
error_response(res, 404, "Not Found");
|
||||
}
|
||||
|
||||
void bad_request(const protorpc::Request& req, protorpc::Response* res) {
|
||||
error_response(res, 400, "Bad Request");
|
||||
}
|
||||
|
||||
#endif // HV_PROTO_RPC_HANDLER_H_
|
||||
25
third_party/libhv/examples/protorpc/handler/login.h
vendored
Executable file
25
third_party/libhv/examples/protorpc/handler/login.h
vendored
Executable file
@@ -0,0 +1,25 @@
|
||||
#ifndef HV_PROTO_RPC_HANDLER_LOGIN_H_
|
||||
#define HV_PROTO_RPC_HANDLER_LOGIN_H_
|
||||
|
||||
#include "../router.h"
|
||||
|
||||
#include "../generated/login.pb.h"
|
||||
|
||||
void login(const protorpc::Request& req, protorpc::Response* res) {
|
||||
// params
|
||||
if (req.params_size() == 0) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
protorpc::LoginParam param;
|
||||
if (!param.ParseFromString(req.params(0))) {
|
||||
return bad_request(req, res);
|
||||
}
|
||||
|
||||
// result
|
||||
protorpc::LoginResult result;
|
||||
result.set_user_id(123456);
|
||||
result.set_token(param.username() + ":" + param.password());
|
||||
res->set_result(result.SerializeAsString());
|
||||
}
|
||||
|
||||
#endif // HV_PROTO_RPC_HANDLER_LOGIN_H_
|
||||
Reference in New Issue
Block a user