2023-10-01 11:15:12 +00:00
|
|
|
@use itertools::Itertools;
|
|
|
|
@use crate::data::RPC;
|
|
|
|
@use crate::generators::cpp_s::*;
|
|
|
|
|
|
|
|
@(rpc: &RPC)
|
|
|
|
#pragma once
|
|
|
|
#ifndef MRPC_GEN_H
|
|
|
|
#define MRPC_GEN_H
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <crow.h>
|
2023-10-02 07:50:27 +00:00
|
|
|
#define RAPIDJSON_HAS_STDSTRING 1
|
|
|
|
#include <rapidjson/stringbuffer.h>
|
|
|
|
#include <rapidjson/writer.h>
|
|
|
|
#include <rapidjson/document.h>
|
2023-10-01 11:15:12 +00:00
|
|
|
|
|
|
|
namespace mrpc @{
|
2023-10-02 07:50:27 +00:00
|
|
|
using MRPCJWriter = rapidjson::Writer<rapidjson::StringBuffer>;
|
2023-10-01 11:15:12 +00:00
|
|
|
@for e in &rpc.enums {
|
|
|
|
enum struct @e.name : std::uint64_t @{
|
|
|
|
@e.values.iter().map(|(k,v)| format!("{k} = {v}")).join(",\n ")
|
|
|
|
@};
|
|
|
|
}
|
|
|
|
@for s in &rpc.structs {
|
|
|
|
struct @s.name;
|
|
|
|
}
|
|
|
|
@for s in &rpc.structs {
|
|
|
|
struct @s.name @{
|
|
|
|
@for f in &s.fields { @ty_to_str(&f.ty) @f.name;
|
|
|
|
}
|
2023-10-02 07:50:27 +00:00
|
|
|
MRPCJWriter& operator >>(MRPCJWriter&) const;
|
|
|
|
@(s.name)& operator <<(const rapidjson::Value&);
|
2023-10-01 11:15:12 +00:00
|
|
|
@};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MRPCStreamImpl @{
|
|
|
|
virtual void close() noexcept final;
|
|
|
|
virtual void abort() noexcept final;
|
|
|
|
virtual bool is_open() noexcept final;
|
|
|
|
protected:
|
|
|
|
MRPCStreamImpl(crow::websocket::connection *conn, uint64_t id) : conn(conn), id(id) @{@}
|
|
|
|
crow::websocket::connection* conn;
|
|
|
|
std::uint64_t id;
|
|
|
|
@};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct MRPCStream final : MRPCStreamImpl @{
|
|
|
|
MRPCStream(crow::websocket::connection *conn, uint64_t id) : MRPCStreamImpl(conn, id) @{@}
|
|
|
|
bool send(const T &v) noexcept @{
|
|
|
|
if (!conn) return false;
|
|
|
|
try @{
|
2023-10-02 07:50:27 +00:00
|
|
|
rapidjson::StringBuffer s;
|
|
|
|
mrpc::MRPCJWriter writer@{s@};
|
|
|
|
writer.StartObject();
|
|
|
|
writer.Key("id");
|
|
|
|
writer.Uint64(id);
|
|
|
|
writer.Key("data");
|
|
|
|
v >> writer;
|
|
|
|
writer.EndObject();
|
|
|
|
conn->send_text(s.GetString());
|
2023-10-01 11:15:12 +00:00
|
|
|
@} catch (const std::exception &_) @{
|
|
|
|
abort();
|
|
|
|
return false;
|
|
|
|
@}
|
|
|
|
return true;
|
|
|
|
@}
|
|
|
|
@};
|
|
|
|
|
|
|
|
struct MRPCServer @{
|
|
|
|
virtual void install(crow::SimpleApp &app, std::string &&route) final;
|
|
|
|
private:
|
|
|
|
@for s in &rpc.services {@for m in &s.methods { virtual @method_ret(m) @(s.name)_@(m.name)(@method_args(m)) = 0;
|
|
|
|
}}
|
2023-10-02 07:50:27 +00:00
|
|
|
virtual void msg_handler(crow::websocket::connection&, std::string, bool) final;
|
2023-10-01 11:15:12 +00:00
|
|
|
|
|
|
|
std::mutex __streams_mutex;
|
|
|
|
std::unordered_multimap<crow::websocket::connection*, std::shared_ptr<MRPCStreamImpl>> __streams;
|
|
|
|
@};
|
|
|
|
@}
|
|
|
|
|
|
|
|
#endif // MRPC_GEN_H
|