-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathio_service.hpp
335 lines (280 loc) · 10 KB
/
io_service.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
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
#ifndef IO_SERVICE_HPP
#define IO_SERVICE_HPP
#include "safe_vector.hpp"
#include "safe_queue.hpp"
#include <functional>
#include <sstream>
#include <vector>
#include <memory>
#include <typeinfo>
#include <chrono>
class Logging;
class SocketHandler;
typedef std::shared_ptr<SocketHandler> socket_handler_ptr;
const int SERVICE_TYPE_NONE = 0;
const int SERVICE_TYPE_READ = 1;
const int SERVICE_TYPE_WRITE = 2;
const int SERVICE_TYPE_CONNECT_TELNET = 3;
const int SERVICE_TYPE_CONNECT_SSH = 4;
const int SERVICE_TYPE_CONNECT_IRC = 5;
const int SERVICE_TYPE_LISTENER_TELNET = 6;
const int SERVICE_TYPE_LISTENER_SSH = 7;
const int SERVICE_TYPE_LISTENER_IRC = 8;
const int SERVICE_TYPE_ASYNC_TIMER = 9;
const int SERVICE_TYPE_BLOCK_TIMER = 10;
#define SERVICE_TIMER(x) ((int)(x) <= SERVICE_TYPE_BLOCK_TIMER \
&& (int)(x) >= SERVICE_TYPE_ASYNC_TIMER)
#define SERVICE_LISTENER(x) ((int)(x) <= SERVICE_TYPE_LISTENER_IRC \
&& (int)(x) >= SERVICE_TYPE_LISTENER_TELNET)
/**
* @class IOService
* @author Michael Griffin
* @date 06/12/2017
* @file io_service.hpp
* @brief Handle Async Socket Read/Write and function Callback on results.
*/
class IOService
{
public:
IOService();
~IOService();
using clock = std::chrono::system_clock;
using time_point_type = std::chrono::time_point<clock, std::chrono::milliseconds>;
/**
* Handles Call Back Functions, execute at the end of Asyn Job.
*/
typedef std::function<void(const std::error_code&, socket_handler_ptr)> StdFunctionCallbackHandler;
typedef std::function<void(int)> StdTimerCallBack;
static const int MAX_BUFFER_SIZE = 16384;
/**
* @class ServiceBase
* @author Michael Griffin
* @date 06/12/2017
* @file io_service.hpp
* @brief IO Service Job Base Template, needed to access virtual methods on ServiceJob
*/
class ServiceBase
{
public:
virtual ~ServiceBase()
{
//std::cout << "ServiceBase Destructor called\n";
}
virtual void setBuffer(unsigned char *buffer) = 0;
virtual std::vector<unsigned char> &getBuffer() = 0;
virtual std::string getStringSequence() = 0;
virtual socket_handler_ptr getSocketHandle() = 0;
virtual void executeCallback(const std::error_code &er, socket_handler_ptr conn) = 0;
virtual int getServiceType() = 0;
};
typedef std::shared_ptr<ServiceBase> service_base_ptr;
/**
* @class ServiceJob
* @author Michael Griffin
* @date 06/12/2017
* @file io_service.hpp
* @brief IO Service Job Template
*/
template <class MutableBufferSequence, class StringSequence, class SocketHandle, class Callback, class ServiceType>
class ServiceJob : public ServiceBase
{
public:
virtual ~ServiceJob()
{
//std::cout << "ServiceJob Destructor called\n";
m_socket_handle.reset();
}
virtual void setBuffer(unsigned char *buffer) override
{
for(int i = 0; i < MAX_BUFFER_SIZE; i++)
{
m_buffer.push_back(buffer[i]);
}
}
virtual std::vector<unsigned char> &getBuffer() override
{
return m_buffer;
}
virtual std::string getStringSequence() override
{
return m_string_sequence;
}
virtual socket_handler_ptr getSocketHandle() override
{
return m_socket_handle;
}
virtual void executeCallback(const std::error_code &err, socket_handler_ptr handle) override
{
StdFunctionCallbackHandler callback_method(m_callback);
callback_method(err, handle);
}
virtual int getServiceType() override
{
return m_service_type;
}
ServiceJob(MutableBufferSequence &buffer, StringSequence string_sequence, SocketHandle socket_handle,
Callback &callback, ServiceType service_type)
: m_buffer(buffer)
, m_string_sequence(string_sequence)
, m_socket_handle(socket_handle)
, m_callback(callback)
, m_service_type(service_type)
{ }
MutableBufferSequence &m_buffer;
StringSequence m_string_sequence;
SocketHandle m_socket_handle;
Callback m_callback;
ServiceType m_service_type;
};
/**
* @brief Add Async Jobs to the Vector Queue
* @param buffer
* @param string_sequence
* @param socket_handle
* @param callback
* @param service_type
*/
template <typename MutableBufferSequence, typename StringSequence, typename SocketHandle, typename Callback, typename ServiceType>
void addAsyncJob(MutableBufferSequence &buffer, StringSequence string_sequence, SocketHandle socket_handle,
Callback &callback, ServiceType service_type)
{
// We keep NEW here for Initial Pointer Creation due to Varidict Templates.
// Made Sure Virtual Descructors are called properly when Jobs are released.
ServiceJob<MutableBufferSequence, StringSequence, SocketHandle, Callback, ServiceType> *job
= new ServiceJob <MutableBufferSequence, StringSequence, SocketHandle, Callback, ServiceType>
(buffer, string_sequence, socket_handle, callback, service_type);
if(SERVICE_LISTENER(service_type))
{
// Server Connection Listener Job (1) for each Service.
m_listener_list.push_back(std::shared_ptr<ServiceBase>(job));
}
else
{
// Standard Async Job
m_service_list.enqueue(std::shared_ptr<ServiceBase>(job));
}
}
/**
* @class TimerBase
* @author Mercyful
* @date 16/12/23
* @file io_service.hpp
* @brief IO Service Timer Base Template, needed to access virtual methods on ServiceJob
*/
class TimerBase
{
public:
using clock = std::chrono::system_clock;
using time_point_type = std::chrono::time_point<clock, std::chrono::milliseconds>;
virtual ~TimerBase()
{
//std::cout << "TimerBase Destructor called\n";
}
virtual void executeCallback(int value) = 0;
virtual time_point_type getStartTime() = 0;
virtual int getExpiresFromNow() = 0;
virtual long getTimePassed() = 0;
virtual bool isExpired() = 0;
};
typedef std::shared_ptr<TimerBase> timer_base_ptr;
/**
* @class TimerJob
* @author Michael Griffin
* @date 12/12/2023
* @file io_service.hpp
* @brief IO Service Job Template
*/
template <class StartTime, class MilliSeconds, class Callback>
class TimerJob : public TimerBase
{
public:
using clock = std::chrono::system_clock;
using time_point_type = std::chrono::time_point<clock, std::chrono::milliseconds>;
virtual ~TimerJob()
{
//std::cout << "TimerJob Destructor called\n";
}
virtual void executeCallback(int value) override
{
StdTimerCallBack callback_method(m_callback);
callback_method(value);
}
virtual time_point_type getStartTime() override
{
return m_start_time;
}
virtual int getExpiresFromNow() override
{
return m_expires_from_now;
}
virtual long getTimePassed() override
{
time_point_type end = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
return (end - m_start_time).count();
}
virtual bool isExpired() override
{
if (getTimePassed() > m_expires_from_now)
{
return true;
}
return false;
}
TimerJob(StartTime start_time, MilliSeconds milli_seconds, Callback &callback)
: m_start_time(start_time)
, m_expires_from_now(milli_seconds)
, m_callback(callback)
{ }
StartTime m_start_time;
MilliSeconds m_expires_from_now;
Callback m_callback;
};
/**
* @brief Add Async Jobs to the Vector Queue
* @param buffer
* @param string_sequence
* @param socket_handle
* @param callback
* @param service_type
*/
template <typename StartTime, typename MilliSeconds, typename Callback>
void addTimerJob(StartTime start_time, MilliSeconds expires_from_now, Callback &callback)
{
// We keep NEW here for Initial Pointer Creation due to Varidict Templates.
// Made Sure Virtual Descructors are called properly when Jobs are released.
TimerJob<StartTime, MilliSeconds, Callback> *job = new TimerJob <StartTime, MilliSeconds, Callback>
(start_time, expires_from_now, callback);
// Timer (Priority List Job)
m_timer_list.enqueue(std::shared_ptr<TimerBase>(job));
}
/**
* @Brief Always check all timers (Priority each iteration)
*/
void checkPriorityTimers();
/**
* @brief Async Listener, check for incoming connections
*/
void checkAsyncListenersForConnections();
/**
* @brief Checks Timers for Execution of Callbacks.
*/
void checkAsyncTimers();
/**
* @brief Main looping method
*/
void run();
/**
* @brief Shutdown Async Polling Service
*/
void stop();
/**
* @brief Check if the IO Service Is Active
*/
bool isActive();
SafeVector<service_base_ptr> m_listener_list;
SafeQueue<service_base_ptr> m_service_list;
SafeQueue<timer_base_ptr> m_timer_list;
Logging &m_log;
bool m_is_active;
};
#endif // IO_SERVICE_HPP