forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaioresolver.hpp
160 lines (126 loc) · 2.88 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
// 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.
//
#ifndef SOLID_FRAME_AIO_RESOLVER_HPP
#define SOLID_FRAME_AIO_RESOLVER_HPP
#include "solid/utility/dynamictype.hpp"
#include "solid/system/function.hpp"
#include "solid/system/socketaddress.hpp"
#include "aioerror.hpp"
#include <string>
namespace solid{
namespace frame{
namespace aio{
struct ResolveBase: Dynamic<ResolveBase>{
ErrorCodeT error;
int flags;
ResolveBase(int _flags):flags(_flags){}
virtual ~ResolveBase();
virtual void run(bool _fail) = 0;
};
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(_cbk){
}
/*virtual*/ void run(bool _fail){
ResolveData rd;
if(!_fail){
rd = this->doRun();
}else{
this->error = error_resolver_direct;
}
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){}
/*virtual*/ void run(bool _fail){
if(!_fail){
this->doRun(cbk.hostString(), cbk.serviceString());
}else{
this->error = error_resolver_reverse;
}
cbk(this->error);
}
};
class Resolver{
public:
Resolver(size_t _thrcnt = 0);
~Resolver();
ErrorConditionT start(ushort _thrcnt = 0);
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
){
doSchedule(new DirectResolveCbk<Cbk>(_cbk, _host, _srvc, _flags, _family, _type, _proto));
}
template <class Cbk>
void requestResolve(
Cbk _cbk,
const SocketAddressStub &_rsa,
int _flags = 0
){
doSchedule(new ReverseResolveCbk<Cbk>(_rsa, _flags));
}
void stop();
private:
void doSchedule(ResolveBase *_pb);
private:
struct Data;
Data &d;
};
}//namespace aio
}//namespace frame
}//namespace solid
#endif