Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <ftl/net/dispatcher.hpp>
void ftl::net::Dispatcher::dispatch(const std::string &msg) {
auto unpacked = msgpack::unpack(msg.data(), msg.size());
dispatch(unpacked.get());
}
void ftl::net::Dispatcher::dispatch(const msgpack::object &msg) {
switch (msg.via.array.size) {
case 3:
dispatch_notification(msg);
case 4:
dispatch_call(msg);
default:
return;
}
}
void ftl::net::Dispatcher::dispatch_call(const msgpack::object &msg) {
call_t the_call;
msg.convert(the_call);
// TODO: proper validation of protocol (and responding to it)
// auto &&type = std::get<0>(the_call);
// assert(type == 0);
// auto &&id = std::get<1>(the_call);
auto &&name = std::get<2>(the_call);
auto &&args = std::get<3>(the_call);
auto it_func = funcs_.find(name);
if (it_func != end(funcs_)) {
try {
auto result = (it_func->second)(args);
// TODO SEND RESULTS
} catch (...) {
throw;
}
}
}
void ftl::net::Dispatcher::dispatch_notification(msgpack::object const &msg) {
notification_t the_call;
msg.convert(the_call);
// TODO: proper validation of protocol (and responding to it)
// auto &&type = std::get<0>(the_call);
// assert(type == static_cast<uint8_t>(request_type::notification));
auto &&name = std::get<1>(the_call);
auto &&args = std::get<2>(the_call);
auto it_func = funcs_.find(name);
if (it_func != end(funcs_)) {
try {
auto result = (it_func->second)(args);
} catch (...) {
throw;
}
}
}
void ftl::net::Dispatcher::enforce_arg_count(std::string const &func, std::size_t found,
std::size_t expected) {
if (found != expected) {
throw;
}
}
void ftl::net::Dispatcher::enforce_unique_name(std::string const &func) {
auto pos = funcs_.find(func);
if (pos != end(funcs_)) {
throw;
}
}