2023-10-01 11:15:12 +00:00
|
|
|
@use crate::data::RPC;
|
|
|
|
@use crate::generators::cpp_s::*;
|
2023-10-13 13:43:56 +00:00
|
|
|
@use super::cpp_server_json_cxx;
|
2023-10-01 11:15:12 +00:00
|
|
|
|
|
|
|
@(header_name: &str, rpc: &RPC)
|
|
|
|
#include "@header_name"
|
2023-10-02 13:41:00 +00:00
|
|
|
#include <corvusoft/restbed/session.hpp>
|
|
|
|
#include <corvusoft/restbed/resource.hpp>
|
|
|
|
#include <corvusoft/restbed/request.hpp>
|
|
|
|
|
2023-10-13 13:43:56 +00:00
|
|
|
using namespace mrpc;
|
|
|
|
|
|
|
|
@:cpp_server_json_cxx(rpc)
|
2023-10-01 11:15:12 +00:00
|
|
|
|
2023-10-02 13:41:00 +00:00
|
|
|
template<typename T>
|
|
|
|
void send_msg(const std::shared_ptr<restbed::Session> &c, const T &v) @{
|
|
|
|
if (c->is_closed())
|
|
|
|
return;
|
2023-10-02 07:50:27 +00:00
|
|
|
rapidjson::StringBuffer s;
|
|
|
|
mrpc::MRPCJWriter writer@{s@};
|
2023-10-02 13:41:00 +00:00
|
|
|
v >> writer;
|
|
|
|
const auto body_ptr = s.GetString();
|
|
|
|
const auto body = restbed::Bytes@{body_ptr, body_ptr+s.GetLength()@};
|
|
|
|
c->yield(
|
|
|
|
200,
|
|
|
|
body,
|
|
|
|
std::multimap<std::string, std::string>@{
|
|
|
|
@{"Content-Type", "application/json"@},
|
|
|
|
@{"Content-Length", std::to_string(body.size())@}
|
|
|
|
@}
|
|
|
|
);
|
2023-10-01 11:15:12 +00:00
|
|
|
@}
|
|
|
|
|
2023-10-02 13:41:00 +00:00
|
|
|
template<typename T>
|
|
|
|
void send_sse_msg(const std::shared_ptr<restbed::Session> &c, const T &v) @{
|
|
|
|
if (c->is_closed())
|
|
|
|
return;
|
|
|
|
rapidjson::StringBuffer s;
|
|
|
|
std::memcpy(s.Push(5), "data:", 5);
|
|
|
|
mrpc::MRPCJWriter writer@{s@};
|
|
|
|
v >> writer;
|
|
|
|
std::memcpy(s.Push(2), "\n\n", 2);
|
|
|
|
const auto body_ptr = s.GetString();
|
|
|
|
const auto body = restbed::Bytes@{body_ptr, body_ptr+s.GetLength()@};
|
|
|
|
c->yield(body);
|
|
|
|
@}
|
|
|
|
|
|
|
|
mrpc::MRPCStreamImpl::MRPCStreamImpl(const std::shared_ptr<restbed::Session> &conn) : conn(conn) @{
|
|
|
|
conn->yield(
|
|
|
|
200,
|
|
|
|
std::multimap<std::string, std::string>@{
|
|
|
|
@{"Cache-Control", "no-cache"@},
|
|
|
|
@{"Content-Type", "text/event-stream"@}
|
|
|
|
@}
|
|
|
|
);
|
2023-10-01 11:15:12 +00:00
|
|
|
@}
|
|
|
|
|
2023-10-02 13:41:00 +00:00
|
|
|
void mrpc::MRPCStreamImpl::close() const noexcept @{ conn->close("data:null\n\n"); @}
|
|
|
|
bool mrpc::MRPCStreamImpl::is_open() const noexcept @{ return conn->is_open(); @}
|
2023-10-13 13:43:56 +00:00
|
|
|
@for s in streams_required(rpc) {template<> void MRPCStream<@s>::send(const @s &v) const noexcept @{ send_sse_msg(conn, v); @}
|
2023-10-02 13:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mrpc::MRPCServer::MRPCServer(std::shared_ptr<restbed::Resource> &r) @{
|
|
|
|
r->set_method_handler("POST", [this](const std::shared_ptr<restbed::Session>& s) @{
|
|
|
|
const auto req = s->get_request();
|
|
|
|
const auto body_len = req->get_header("Content-Length", 0);
|
|
|
|
s->fetch(body_len, [this](const std::shared_ptr<restbed::Session> &s, auto &&body) @{
|
|
|
|
try @{ msg_handler(s, body); @}
|
|
|
|
catch (const std::exception &_) @{ s->close(400); @}
|
2023-10-01 11:15:12 +00:00
|
|
|
@});
|
2023-10-02 13:41:00 +00:00
|
|
|
@});
|
2023-10-01 11:15:12 +00:00
|
|
|
@}
|
2023-10-02 13:41:00 +00:00
|
|
|
|
|
|
|
void mrpc::MRPCServer::msg_handler(const std::shared_ptr<restbed::Session> __c, const restbed::Bytes &__msg) @{
|
2023-10-02 07:50:27 +00:00
|
|
|
rapidjson::Document __j;
|
2023-10-02 13:41:00 +00:00
|
|
|
__j.Parse((const char*)__msg.data(), __msg.size());
|
2023-10-02 07:50:27 +00:00
|
|
|
if (__j.HasParseError())
|
|
|
|
throw std::exception@{@};
|
2023-10-02 13:41:00 +00:00
|
|
|
std::string __service, __method;
|
2023-10-13 13:43:56 +00:00
|
|
|
__service << json_get(__j, "service");
|
|
|
|
__method << json_get(__j, "method");
|
2023-10-02 13:41:00 +00:00
|
|
|
auto __data_member = __j.FindMember("data");
|
|
|
|
if (__data_member == __j.MemberEnd() || !__data_member->value.IsObject())
|
|
|
|
throw std::exception@{@};
|
|
|
|
auto &__data = __data_member->value;
|
|
|
|
@for (si, s) in rpc.services.iter().enumerate() {@if si > 0 { else }else{ }if (__service == "@s.name") @{
|
|
|
|
@for (mi, m) in s.methods.iter().enumerate() {@if mi > 0 { else }else{ }if (__method == "@m.name") @{
|
|
|
|
@if m.ret_stream {auto __stream = MRPCStream<@ty_to_str(m.ret.as_ref().unwrap())>@{__c@};
|
|
|
|
}
|
2023-10-13 13:43:56 +00:00
|
|
|
@for (name, ty) in m.args.iter().map(|a| (&a.name, ty_to_str(&a.ty))) { @ty @name; @name << json_get(__data, "@name");
|
2023-10-02 13:41:00 +00:00
|
|
|
}
|
2023-12-22 14:27:37 +00:00
|
|
|
@if m.ret_stream {@(s.name)_@(m.name)(@call_args(m));}
|
|
|
|
else if m.ret.is_some() {send_msg(__c, @(s.name)_@(m.name)(@call_args(m)));}
|
|
|
|
else {@(s.name)_@(m.name)(@call_args(m)); send_msg(__c, nullptr);}
|
2023-10-02 13:41:00 +00:00
|
|
|
@}}
|
2023-10-01 11:15:12 +00:00
|
|
|
else @{ throw std::exception@{@}; @}
|
2023-10-02 13:41:00 +00:00
|
|
|
@}}
|
|
|
|
else @{ throw std::exception@{@}; @}
|
2023-10-01 11:15:12 +00:00
|
|
|
@}
|
|
|
|
@}
|