-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaioresolver.hpp
162 lines (133 loc) · 3.25 KB
/
aioresolver.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
// solid/frame/aio/aioresolver.hpp
//
// Copyright (c) 2014 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include <functional>
#include <string>
#include "aioerror.hpp"
#include "solid/system/pimpl.hpp"
#include "solid/system/socketaddress.hpp"
namespace solid {
namespace frame {
namespace aio {
struct ResolveBase {
ErrorCodeT error;
int flags;
ResolveBase(int _flags)
: flags(_flags)
{
}
};
struct DirectResolve : ResolveBase {
std::string host;
std::string srvc;
int family;
int type;
int proto;
DirectResolve(
const char* _host,
const char* _srvc,
int _flags,
int _family,
int _type,
int _proto)
: ResolveBase(_flags)
, host(_host)
, srvc(_srvc)
, family(_family)
, type(_type)
, proto(_proto)
{
}
ResolveData doRun();
};
struct ReverseResolve : ResolveBase {
SocketAddressInet addr;
ReverseResolve(
const SocketAddressStub& _rsa,
int _flags)
: ResolveBase(_flags)
, addr(_rsa)
{
}
void doRun(std::string& _rhost, std::string& _rsrvc);
};
template <class Cbk>
struct DirectResolveCbk : DirectResolve {
Cbk cbk;
DirectResolveCbk(
Cbk _cbk,
const char* _host,
const char* _srvc,
int _flags,
int _family,
int _type,
int _proto)
: DirectResolve(_host, _srvc, _flags, _family, _type, _proto)
, cbk(std::move(_cbk))
{
}
void operator()()
{
ResolveData rd;
rd = this->doRun();
cbk(rd, this->error);
}
};
template <class Cbk>
struct ReverseResolveCbk : ReverseResolve {
Cbk cbk;
ReverseResolveCbk(
Cbk _cbk,
const SocketAddressStub& _rsa,
int _flags)
: ReverseResolve(_rsa, _flags)
, cbk(_cbk)
{
}
void operator()()
{
this->doRun(cbk.hostString(), cbk.serviceString());
cbk(this->error);
}
};
class Resolver {
using PushFunctionT = std::function<void(std::function<void()>&&)>;
PushFunctionT push_fnc_;
public:
template <class F>
Resolver(F _f)
: push_fnc_(_f)
{
}
~Resolver() {}
template <class Cbk>
void requestResolve(
Cbk _cbk,
const char* _host,
const char* _srvc,
int _flags = 0,
int _family = -1,
int _type = -1,
int _proto = -1)
{
push_fnc_(DirectResolveCbk<Cbk>(std::move(_cbk), _host, _srvc, _flags, _family, _type, _proto));
}
template <class Cbk>
void requestResolve(
Cbk _cbk,
const SocketAddressStub& _rsa,
int _flags = 0)
{
push_fnc_(ReverseResolveCbk<Cbk>(_rsa, _flags));
}
};
} // namespace aio
} // namespace frame
} // namespace solid