znone
2021-10-05 a77db3e50bb5429b17e8d89bf71403dfdf39985c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#ifndef _QTL_ASIO_H_
#define _QTL_ASIO_H_
 
#include "qtl_async.hpp"
#include <asio/version.hpp>
#define ASIO_STANDALONE
#if defined(_MSC_VER) && _WIN32_WINNT<0x0600
#define ASIO_ENABLE_CANCELIO 1 
#endif
#if ASIO_VERSION < 101200
#include <asio/io_service.hpp>
#else
#include <asio/io_context.hpp>
#endif // ASIO_VERSION
#include <asio/strand.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/async_result.hpp>
#include <asio/steady_timer.hpp>
 
#if ASIO_VERSION < 100000
#error The asio version required by QTL is at least 10.0 
#endif
 
namespace qtl
{
 
namespace NS_ASIO = ::asio;
 
namespace asio
{
 
class service
{
public:
#if ASIO_VERSION < 101200
    typedef NS_ASIO::io_service service_type;
#else
    typedef NS_ASIO::io_context service_type;
#endif // ASIO_VERSION
 
    service() NOEXCEPT { }
    explicit service(int concurrency_hint) : _service(concurrency_hint) { }
 
    void reset()
    {
        _service.reset();
    }
 
    void run()
    {
        _service.run();
    }
 
    void stop()
    {
        _service.stop();
    }
 
    service_type& context() NOEXCEPT { return _service; }
 
private:
 
    class event_item : public qtl::event
    {
    public:
        event_item(service& service, qtl::socket_type fd)
            : _service(service), _strand(service.context()), _socket(service.context(), NS_ASIO::ip::tcp::v4(), fd), _timer(service.context()), _busying(false)
        {
        }
 
        NS_ASIO::ip::tcp::socket& next_layer() { return _socket; }
 
    public: // qtl::event
        virtual void set_io_handler(int flags, long timeout, std::function<void(int)>&& handler) override
        {
            if (flags&qtl::event::ef_read)
            {
#if ASIO_VERSION < 101200
                _socket.async_read_some(NS_ASIO::null_buffers(), _strand.wrap([this, handler](const NS_ASIO::error_code& ec, size_t bytes_transferred) {
#else
                _socket.async_wait(NS_ASIO::socket_base::wait_read, _strand.wrap([this, handler](const NS_ASIO::error_code& ec) {
#endif // ASIO_VERSION
                    if (!ec)
                        handler(qtl::event::ef_read);
                    else if (ec == NS_ASIO::error::make_error_code(NS_ASIO::error::operation_aborted))
                        handler(qtl::event::ef_timeout);
                    else
                        handler(qtl::event::ef_exception);
                    _busying = false;
                    _timer.cancel();
                }));
                _busying = true;
            }
            if (flags&qtl::event::ef_write)
            {
#if ASIO_VERSION < 101200
                _socket.async_write_some(NS_ASIO::null_buffers(), _strand.wrap([this, handler](const NS_ASIO::error_code& ec, size_t bytes_transferred) {
#else
                _socket.async_wait(NS_ASIO::socket_base::wait_write, _strand.wrap([this, handler](const NS_ASIO::error_code& ec) {
#endif //ASIO_VERSION
                    if (!ec)
                        handler(qtl::event::ef_write);
                    else if (ec == NS_ASIO::error::make_error_code(NS_ASIO::error::operation_aborted))
                        handler(qtl::event::ef_timeout);
                    else
                        handler(qtl::event::ef_exception);
                    _timer.cancel();
                    _busying = false;
                }));
                _busying = true;
            }
            if (timeout > 0)
            {
#if ASIO_VERSION < 101200
                _timer.expires_from_now(std::chrono::seconds(timeout));
#else
                _timer.expires_after(NS_ASIO::chrono::seconds(timeout));
#endif // ASIO_VERSION
                _timer.async_wait(_strand.wrap([this, handler](NS_ASIO::error_code ec) {
                    if (!ec)
                    {
                        _socket.cancel(ec);
                    }
                }));
            }
        }
 
        virtual void remove() override
        {
            if (_busying) return;
#if ASIO_VERSION >= 101200 && (!defined(_WIN32) || _WIN32_WINNT >= 0x0603 )
            _socket.release();
#endif //Windows 8.1
            _service.remove(this);
        }
        virtual bool is_busying() override
        {
            return _busying;
        }
 
    private:
        service& _service;
        service_type::strand _strand;
        NS_ASIO::ip::tcp::socket _socket;
        NS_ASIO::steady_timer _timer;
        bool _busying;
    };
 
public:
 
    template<typename Connection>
    event_item* add(Connection* connection)
    {
        event_item* item = new event_item(*this, connection->socket());
        std::lock_guard<std::mutex> lock(_mutex);
        _events.push_back(std::unique_ptr<event_item>(item));
        return item;
    }
 
private:
    service_type _service;
    std::mutex _mutex;
    std::vector<std::unique_ptr<event_item>> _events;
 
    void remove(event_item* item)
    {
        std::lock_guard<std::mutex> lock(_mutex);
        auto it = std::find_if(_events.begin(), _events.end(), [item](std::unique_ptr<event_item>& v) {
            return item==v.get();
        });
        if (it != _events.end()) _events.erase(it);
    }
};
 
#if ASIO_VERSION < 101200
 
template <typename Handler, typename Signature>
using async_init_type  = NS_ASIO::detail::async_result_init<Handler, Signature>;
 
template <typename Handler, typename Signature>
inline typename NS_ASIO::handler_type<Handler, Signature>::type 
get_async_handler(async_init_type<Handler, Signature>& init)
{
    return init.handler;
}
 
#else
 
template <typename Handler, typename Signature>
using async_init_type = NS_ASIO::async_completion<Handler, Signature>;
 
template <typename Handler, typename Signature>
inline typename async_init_type<Handler, Signature>::completion_handler_type 
get_async_handler(async_init_type<Handler, Signature>& init)
{
    return init.completion_handler;
}
 
#endif // ASIO_VERSION
 
template<typename Connection, typename OpenHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(OpenHandler, void(typename Connection::exception_type)) 
async_open(service& service, Connection& db, OpenHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<OpenHandler,
        void(typename Connection::exception_type)> init(std::forward<OpenHandler>(handler));
#else
    async_init_type<OpenHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.open(service, get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename CloseHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(CloseHandler, void()) 
async_close(Connection& db, CloseHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<CloseHandler,
        void()> init(std::forward<CloseHandler>(std::forward<CloseHandler>(handler)));
#else
    async_init_type<CloseHandler,
        void()> init(std::forward<CloseHandler>(handler));
#endif
 
    db.close(get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename ExecuteHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(ExecuteHandler, void(typename Connection::exception_type, uint64_t))  
async_execute(Connection& db, ExecuteHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(std::forward<ExecuteHandler>(handler));
#else
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(handler);
#endif
 
    db.execute(get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename ExecuteHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(ExecuteHandler, void(typename Connection::exception_type, uint64_t))  
async_execute_direct(Connection& db, ExecuteHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(std::forward<ExecuteHandler>(handler));
#else
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(handler);
#endif
 
    db.execute_direct(get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename ExecuteHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(ExecuteHandler, void(typename Connection::exception_type, uint64_t)) 
 async_insert(Connection& db, ExecuteHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(std::forward<ExecuteHandler>(handler));
#else
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(handler);
#endif
 
    db.insert(get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename ExecuteHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(ExecuteHandler, void(typename Connection::exception_type, uint64_t)) 
 async_insert_direct(Connection& db, ExecuteHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(std::forward<ExecuteHandler>(handler));
#else
    async_init_type<ExecuteHandler,
        void(typename Connection::exception_type, uint64_t)> init(handler);
#endif
 
    db.insert_direct(get_async_handler(init), std::forward<Args>(args)...);
    return init.result.get();
}
 
template<typename Connection, typename FinishHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type)) 
 async_query(Connection& db, FinishHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query(std::forward<Args>(args)..., get_async_handler(init));
    return init.result.get();
}
 
template<typename Connection, typename FinishHandler, typename... Args>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type)) 
async_query_explicit(Connection& db, FinishHandler&& handler, Args&&... args)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query_explicit(std::forward<Args>(args)..., get_async_handler(init));
    return init.result.get();
}
 
template<typename Connection, typename A1, typename A2, typename FinishHandler, typename... RowHandlers>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type))
async_query_multi_with_params(Connection& db, A1&& a1, A2&& a2, FinishHandler&& handler, RowHandlers&&... row_handlers)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query_multi_with_params(std::forward<A1>(a1), std::forward<A2>(a2), get_async_handler(init), std::forward<RowHandlers>(row_handlers)...);
    return init.result.get();
}
 
template<typename Connection, typename A1, typename FinishHandler, typename... RowHandlers>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type))
async_query_multi_with_params(Connection& db, A1&& a1, FinishHandler&& handler, RowHandlers&&... row_handlers)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query_multi_with_params(std::forward<A1>(a1), get_async_handler(init), std::forward<RowHandlers>(row_handlers)...);
    return init.result.get();
}
 
template<typename Connection, typename A1, typename A2, typename FinishHandler, typename... RowHandlers>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type))
async_query_multi(Connection& db, A1&& a1, A2&& a2, FinishHandler&& handler, RowHandlers&&... row_handlers)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query_multi(std::forward<A1>(a1), std::forward<A2>(a2), get_async_handler(init), std::forward<RowHandlers>(row_handlers)...);
    return init.result.get();
}
 
template<typename Connection, typename A1, typename FinishHandler, typename... RowHandlers>
inline ASIO_INITFN_RESULT_TYPE(FinishHandler, void(typename Connection::exception_type))
async_query_multi(Connection& db, A1&& a1, FinishHandler&& handler, RowHandlers&&... row_handlers)
{
#if ASIO_VERSION < 101200
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(std::forward<FinishHandler>(handler));
#else
    async_init_type<FinishHandler,
        void(typename Connection::exception_type)> init(handler);
#endif
 
    db.query_multi(std::forward<A1>(a1), get_async_handler(init), std::forward<RowHandlers>(row_handlers)...);
    return init.result.get();
}
 
}
 
}
 
#endif //_QTL_ASIO_H_