Branch data Line data Source code
1 : : // include/hpactor/net/registrar_serialization.hpp
2 : : #pragma once
3 : :
4 : : #include <hpactor/net/registrar.hpp>
5 : : #include <hpactor/registrar.pb.h>
6 : : #include <hpactor/types/types.hpp>
7 : :
8 : : #include <string>
9 : : #include <vector>
10 : :
11 : : namespace hpactor {
12 : : namespace net {
13 : :
14 : : // -----------------------------------------------------------------------------
15 : : // TCP message serialization
16 : : // -----------------------------------------------------------------------------
17 : :
18 : : // PbRegisterPayload - acceptors are at top level per spec
19 : 1 : inline PbRegisterPayload to_proto_register(const NodeEndpoint& ep) {
20 : 1 : PbRegisterPayload msg;
21 : 1 : msg.mutable_endpoint_info()->set_endpoint(
22 : 2 : endpoint_ops::to_string(ep.identity.endpoint));
23 : 1 : msg.mutable_endpoint_info()->set_host(ep.identity.host);
24 : 1 : msg.mutable_endpoint_info()->set_tcp_port(ep.tcp_port);
25 : 2 : for (const auto& acc : ep.identity.acceptors) {
26 : 1 : auto* a = msg.add_acceptors();
27 : 1 : a->set_port(acc.port);
28 : 1 : a->set_handshake_version(acc.handshake_version);
29 : 1 : a->set_protocol_version(acc.protocol_version);
30 : 1 : a->set_tls_required(acc.tls_required);
31 : : }
32 : 1 : return msg;
33 : : }
34 : :
35 : 1 : inline StreamBuffer serialize_register_payload(const NodeEndpoint& ep) {
36 : 1 : PbRegisterPayload msg = to_proto_register(ep);
37 : 1 : std::string serialized = msg.SerializeAsString();
38 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
39 : 1 : }
40 : :
41 : : inline bool
42 : 2 : parse_register_payload(const StreamBuffer& data, PbRegisterPayload& msg) {
43 : 2 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
44 : : }
45 : :
46 : : // PbAcceptPayload
47 : 1 : inline PbAcceptPayload to_proto_accept(uint8_t error_code) {
48 : 1 : PbAcceptPayload msg;
49 : 1 : msg.set_error_code(error_code);
50 : 1 : return msg;
51 : : }
52 : :
53 : 1 : inline StreamBuffer serialize_accept_payload(uint8_t error_code) {
54 : 1 : PbAcceptPayload msg = to_proto_accept(error_code);
55 : 1 : std::string serialized = msg.SerializeAsString();
56 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
57 : 1 : }
58 : :
59 : 1 : inline bool parse_accept_payload(const StreamBuffer& data, PbAcceptPayload& msg) {
60 : 1 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
61 : : }
62 : :
63 : : // PbNodeJoinPayload
64 : 1 : inline PbNodeJoinPayload to_proto_node_join(const NodeEndpoint& ep) {
65 : 1 : PbNodeJoinPayload msg;
66 : 1 : msg.mutable_endpoint_info()->set_endpoint(
67 : 2 : endpoint_ops::to_string(ep.identity.endpoint));
68 : 1 : msg.mutable_endpoint_info()->set_host(ep.identity.host);
69 : 1 : msg.mutable_endpoint_info()->set_tcp_port(ep.tcp_port);
70 : 1 : return msg;
71 : : }
72 : :
73 : 1 : inline StreamBuffer serialize_node_join_payload(const NodeEndpoint& ep) {
74 : 1 : PbNodeJoinPayload msg = to_proto_node_join(ep);
75 : 1 : std::string serialized = msg.SerializeAsString();
76 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
77 : 1 : }
78 : :
79 : : inline bool
80 : 1 : parse_node_join_payload(const StreamBuffer& data, PbNodeJoinPayload& msg) {
81 : 1 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
82 : : }
83 : :
84 : : // PbNodeLeavePayload
85 : 1 : inline PbNodeLeavePayload to_proto_node_leave(const EndPoint& ep) {
86 : 1 : PbNodeLeavePayload msg;
87 : 2 : msg.set_endpoint(endpoint_ops::to_string(ep));
88 : 1 : return msg;
89 : : }
90 : :
91 : 1 : inline StreamBuffer serialize_node_leave_payload(const EndPoint& ep) {
92 : 1 : PbNodeLeavePayload msg = to_proto_node_leave(ep);
93 : 1 : std::string serialized = msg.SerializeAsString();
94 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
95 : 1 : }
96 : :
97 : : inline bool
98 : 1 : parse_node_leave_payload(const StreamBuffer& data, PbNodeLeavePayload& msg) {
99 : 1 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
100 : : }
101 : :
102 : : // PbErrorPayload
103 : : inline PbErrorPayload to_proto_error(uint8_t code, const std::string& msg) {
104 : : PbErrorPayload pb;
105 : : pb.set_error_code(code);
106 : : pb.set_message(msg);
107 : : return pb;
108 : : }
109 : :
110 : : inline StreamBuffer serialize_error_payload(uint8_t code, const std::string& msg) {
111 : : PbErrorPayload pb = to_proto_error(code, msg);
112 : : std::string serialized = pb.SerializeAsString();
113 : : return StreamBuffer(serialized.begin(), serialized.end());
114 : : }
115 : :
116 : : // -----------------------------------------------------------------------------
117 : : // UDP message serialization
118 : : // -----------------------------------------------------------------------------
119 : :
120 : : // PbResolveQueryPayload
121 : 1 : inline PbResolveQueryPayload to_proto_resolve_query(const EndPoint& ep) {
122 : 1 : PbResolveQueryPayload msg;
123 : 2 : msg.set_target_endpoint(endpoint_ops::to_string(ep));
124 : 1 : return msg;
125 : : }
126 : :
127 : 1 : inline StreamBuffer serialize_resolve_query_payload(const EndPoint& ep) {
128 : 1 : PbResolveQueryPayload msg = to_proto_resolve_query(ep);
129 : 1 : std::string serialized = msg.SerializeAsString();
130 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
131 : 1 : }
132 : :
133 : : inline bool
134 : 1 : parse_resolve_query_payload(const StreamBuffer& data, PbResolveQueryPayload& msg) {
135 : 1 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
136 : : }
137 : :
138 : : // PbResolveResponsePayload
139 : 1 : inline PbResolveResponsePayload to_proto_resolve_response(const NodeEndpoint& ep) {
140 : 1 : PbResolveResponsePayload msg;
141 : 1 : msg.mutable_endpoint_info()->set_endpoint(
142 : 2 : endpoint_ops::to_string(ep.identity.endpoint));
143 : 1 : msg.mutable_endpoint_info()->set_host(ep.identity.host);
144 : 1 : msg.mutable_endpoint_info()->set_tcp_port(ep.tcp_port);
145 : 1 : return msg;
146 : : }
147 : :
148 : 1 : inline StreamBuffer serialize_resolve_response_payload(const NodeEndpoint& ep) {
149 : 1 : PbResolveResponsePayload msg = to_proto_resolve_response(ep);
150 : 1 : std::string serialized = msg.SerializeAsString();
151 : 1 : return StreamBuffer(serialized.begin(), serialized.end());
152 : 1 : }
153 : :
154 : 1 : inline bool parse_resolve_response_payload(const StreamBuffer& data,
155 : : PbResolveResponsePayload& msg) {
156 : 1 : return msg.ParseFromArray(data.data(), static_cast<int>(data.size()));
157 : : }
158 : :
159 : : } // namespace net
160 : : } // namespace hpactor
|