-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathrouter.hpp
234 lines (200 loc) · 7.21 KB
/
router.hpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once
#include <utility>
#include <map>
#include <string>
#include <mutex>
#include <memory>
#include <boost/lexical_cast.hpp>
#include <boost/noncopyable.hpp>
#include "token_parser.hpp"
#include "function_traits.hpp"
#include "common.h"
#include "utils.hpp"
class connection;
class invoker_function
{
public:
invoker_function() = default;
invoker_function(const std::function<void(token_parser &, std::string&)>& function, std::size_t size) : function_(function), param_size_(size)
{
}
void operator()(token_parser &parser, std::string& result)
{
function_(parser, result);
}
const std::size_t param_size() const
{
return param_size_;
}
private:
std::function<void(token_parser &, std::string& result)> function_;
std::size_t param_size_;
};
class router : boost::noncopyable
{
public:
static router& get()
{
static router instance;
return instance;
}
template<typename Function>
void register_handler(std::string const & name, const Function& f)
{
return register_nonmember_func(name, f);
}
template<typename Function, typename Self>
void register_handler(std::string const & name, const Function& f, Self* self)
{
return register_member_func(name, f, self);
}
void remove_handler(std::string const& name)
{
this->map_invokers_.erase(name);
}
void set_callback(const std::function<void(const std::string&, const char*, std::shared_ptr<connection>, bool)>& callback)
{
callback_to_server_ = callback;
}
template<typename T>
void route(const char* text, std::size_t length, T conn)
{
token_parser parser;
parser.parse(text, length);
while (!parser.empty())
{
std::string result = "";
std::string func_name = parser.get<std::string>();
auto it = map_invokers_.find(func_name);
if (it == map_invokers_.end())
{
result = get_json(result_code::ARGUMENT_EXCEPTION, "unknown function: " + func_name);
callback_to_server_(func_name, result.c_str(), conn, true);
return;
}
if (it->second.param_size() != parser.param_size()) //参数个数不匹配
{
result = get_json(result_code::ARGUMENT_EXCEPTION, std::string("parameter number is not match"));
callback_to_server_(func_name, result.c_str(), conn, true);
break;
}
//找到的function中,开始将字符串转换为函数实参并调用
it->second(parser, result);
//response(result.c_str()); //callback to connection
if(callback_to_server_)
callback_to_server_(func_name, result.c_str(), conn, false);
}
}
router() = default;
private:
router(const router&) = delete;
router(router&&) = delete;
template<typename F, size_t... I, typename ... Args>
static auto call_helper(const F& f, const std::index_sequence<I...>&, const std::tuple<Args...>& tup)
{
return f(std::get<I>(tup)...);
}
template<typename F, typename ... Args>
static typename std::enable_if<std::is_void<typename std::result_of<F(Args...)>::type>::value>::type call(const F& f, std::string& result, const std::tuple<Args...>& tp)
{
call_helper(f, std::make_index_sequence<sizeof... (Args)>{}, tp);
result = get_json(result_code::OK, 0);
}
template<typename F, typename ... Args>
static typename std::enable_if<!std::is_void<typename std::result_of<F(Args...)>::type>::value>::type call(const F& f, std::string& result, const std::tuple<Args...>& tp)
{
auto r = call_helper(f, std::make_index_sequence<sizeof... (Args)>{}, tp);
result = get_json(result_code::OK, r);
}
template<typename F, typename Self, size_t... Indexes, typename ... Args>
static auto call_member_helper(const F& f, Self* self, const std::index_sequence<Indexes...>&, const std::tuple<Args...>& tup)
{
return (*self.*f)(std::get<Indexes>(tup)...);
}
template<typename F, typename Self, typename ... Args>
static typename std::enable_if<std::is_void<typename std::result_of<F(Self, Args...)>::type>::value>::type
call_member(const F& f, Self* self, std::string& result, const std::tuple<Args...>& tp)
{
call_member_helper(f, self, typename std::make_index_sequence<sizeof... (Args)>{}, tp);
result = get_json(result_code::OK, 0);
}
template<typename F, typename Self, typename ... Args>
static typename std::enable_if<!std::is_void<typename std::result_of<F(Self, Args...)>::type>::value>::type
call_member(const F& f, Self* self, std::string& result, const std::tuple<Args...>& tp)
{
auto r = call_member_helper(f, self, typename std::make_index_sequence<sizeof... (Args)>{}, tp);
result = get_json(result_code::OK, r);
}
//template<typename Function, class Signature = Function, size_t N = 0, size_t M = function_traits<Signature>::arity>
//struct invoker;
//遍历function的实参类型,将字符串参数转换为实参并添加到tuple中
template<typename Function, size_t N = 0, size_t M = function_traits<Function>::arity>
struct invoker
{
template<typename Args>
static inline void apply(const Function& func, token_parser & parser, std::string& result, Args const & args)
{
typedef typename function_traits<Function>::template args<N>::type arg_type;
try
{
invoker<Function, N + 1, M>::apply(func, parser, result,
std::tuple_cat(args, std::make_tuple(parser.get<arg_type>())));
}
catch (std::invalid_argument& e)
{
result = get_json(result_code::ARGUMENT_EXCEPTION, e.what());
}
catch (std::exception& e)
{
result = get_json(result_code::EXCEPTION, e.what());
}
}
template<typename Args, typename Self>
static inline void apply_member(Function func, Self* self, token_parser & parser, std::string& result, const Args& args)
{
typedef typename function_traits<Function>::template args<N>::type arg_type;
try
{
invoker<Function, N + 1, M>::apply_member(func, self, parser, result, std::tuple_cat(args, std::make_tuple(parser.get<arg_type>())));
}
catch (std::invalid_argument& e)
{
result = get_json(result_code::ARGUMENT_EXCEPTION, e.what());
}
catch (const std::exception& e)
{
result = get_json(result_code::EXCEPTION, e.what());
}
}
};
template<typename Function, size_t M>
struct invoker<Function, M, M>
{
template<typename Args>
static inline void apply(const Function& func, token_parser &, std::string& result, Args const & args)
{
//参数列表已经准备好,可以调用function了
call(func, result, args);
}
template<typename Args, typename Self>
static inline void apply_member(const Function& func, Self* self, token_parser &parser, std::string& result, const Args& args)
{
call_member(func, self, result, args);
}
};
//将注册的handler保存在map中
template<typename Function>
void register_nonmember_func(std::string const & name, const Function& f)
{
this->map_invokers_[name] = { std::bind(&invoker<Function>::template apply<std::tuple<>>, f,
std::placeholders::_1, std::placeholders::_2, std::tuple<>()), function_traits<Function>::arity };
}
template<typename Function, typename Self>
void register_member_func(const std::string& name, const Function& f, Self* self)
{
this->map_invokers_[name] = { std::bind(&invoker<Function>::template apply_member<std::tuple<>, Self>, f, self, std::placeholders::_1,
std::placeholders::_2, std::tuple<>()), function_traits<Function>::arity };
}
std::map<std::string, invoker_function> map_invokers_;
std::function<void(const std::string&, const char*, std::shared_ptr<connection>, bool)> callback_to_server_;
};