-
Notifications
You must be signed in to change notification settings - Fork 20
/
ktsocket.h
394 lines (378 loc) · 11.4 KB
/
ktsocket.h
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
/*************************************************************************************************
* Network functions
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#ifndef _KTSOCKET_H // duplication check
#define _KTSOCKET_H
#include <ktcommon.h>
#include <ktutil.h>
namespace kyototycoon { // common namespace
/**
* Interface of poolable I/O event.
*/
class Pollable {
friend class Poller;
public:
/**
* Event flags.
*/
enum EventFlag {
EVINPUT = 1 << 0, ///< input
EVOUTPUT = 1 << 1, ///< output
EVEXCEPT = 1 << 2 ///< exception
};
/**
* Default constructor.
*/
explicit Pollable() : opq_(NULL) {}
/**
* Destructor.
*/
virtual ~Pollable() {}
/**
* Get the descriptor integer.
* @return the descriptor integer, or -1 on failure.
*/
virtual int32_t descriptor() = 0;
/**
* Set event flags.
* @param flags specifies the event mode. The following may be added by bitwise-or:
* Pollable::EVINPUT for input events, Pollable::EVOUTPUT for output events, Pollable::EVEXCEPT
* for exception events.
*/
virtual void set_event_flags(uint32_t flags) = 0;
/**
* Get the current event flags.
* @return the current event flags.
*/
virtual uint32_t event_flags() = 0;
private:
/** Dummy constructor to forbid the use. */
Pollable(const Pollable&);
/** Dummy Operator to forbid the use. */
Pollable& operator =(const Pollable&);
/** Opaque pointer. */
void* opq_;
};
/**
* Network stream abstraction based on TCP/IP.
*/
class Socket : public Pollable {
friend class ServerSocket;
public:
/**
* Default constructor.
*/
explicit Socket();
/**
* Destructor.
*/
~Socket();
/**
* Get the last happened error information.
* @return the last happened error information.
*/
const char* error();
bool open(const std::string& expr);
/*
* Open a client socket.
* @param expr an expression of the address and the port of the server.
* @param bool secure if this is to be a secure socket
* @param char* ca path of the ca
* @param char* pk path of the private key
* @param char* cert path of the certificate
* @return true on success, or false on failure.
*/
bool open(const std::string& expr, bool secure = false,
const char* ca = NULL, const char* pk = NULL, const char* cert = NULL);
/**
* Close the socket.
* @param grace true for graceful shutdown, or false for immediate disconnection.
* @return true on success, or false on failure.
*/
bool close(bool grace = true);
/**
* Send data.
* @param buf the pointer to a data region to send.
* @param size the size of the data region.
* @return true on success, or false on failure.
*/
bool send(const void* buf, size_t size);
/**
* Send data.
* @param str a string to send.
* @return true on success, or false on failure.
*/
bool send(const std::string& str);
/**
* Send formatted data.
* @param format the printf-like format string. The conversion character `%' can be used with
* such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', and `%'.
* @param ... used according to the format string.
* @return true on success, or false on failure.
*/
bool printf(const char* format, ...);
/**
* Send formatted data.
* @param format the printf-like format string. The conversion character `%' can be used with
* such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', and `%'.
* @param ap used according to the format string.
* @return true on success, or false on failure.
*/
bool vprintf(const char* format, va_list ap);
/**
* Receive data.
* @param buf the pointer to the buffer into which the received data is written.
* @param size the size of the data to receive.
* @return true on success, or false on failure.
*/
bool receive(void* buf, size_t size);
/**
* Receive one byte.
* @return the received byte or -1 on failure.
*/
int32_t receive_byte();
/**
* Push one byte back.
* @param c specifies the byte.
* @return true on success, or false on failure.
* @note The character is available for subsequent receive operations. Only one pushback is
* guaranteed.
*/
bool undo_receive_byte(int32_t c);
/**
* Receive one line of characters.
* @param buf the pointer to the buffer into which the received data is written.
* @param max the maximum size of the data to receive. It must be more than 0.
* @return true on success, or false on failure.
*/
bool receive_line(void* buf, size_t max);
/**
* Get the size of left data in the receiving buffer.
* @return the size of left data in the receiving buffer.
*/
size_t left_size();
/**
* Abort the current operation.
* @return true on success, or false on failure.
*/
bool abort();
/**
* Set the timeout of each operation.
* @param timeout the timeout of each operation in seconds.
* @return true on success, or false on failure.
*/
bool set_timeout(double timeout);
/**
* Get the expression of the socket.
* @return the expression of the socket or an empty string on failure.
*/
const std::string expression();
/**
* Get the descriptor integer.
* @return the descriptor integer, or -1 on failure.
*/
int32_t descriptor();
/**
* Set event flags.
* @param flags specifies the event mode. The following may be added by bitwise-or:
* Socket::EVINPUT for input events, Socket::EVOUTPUT for output events, Socket::EVEXCEPT for
* exception events.
*/
void set_event_flags(uint32_t flags);
/**
* Get the current event flags.
* @return the current event flags.
*/
uint32_t event_flags();
/**
* Get the primary name of the local host.
* @return the host name, or an empty string on failure.
*/
static std::string get_local_host_name();
/**
* Get the address of a host.
* @return the host address, or an empty string on failure.
*/
static std::string get_host_address(const std::string& name);
private:
/** Dummy constructor to forbid the use. */
Socket(const Socket&);
/** Dummy Operator to forbid the use. */
Socket& operator =(const Socket&);
/** Opaque pointer. */
void* opq_;
};
/**
* Network server abstraction based on TCP/IP.
*/
class ServerSocket : public Pollable {
public:
/**
* Default constructor.
*/
explicit ServerSocket();
/**
* Destructor.
*/
~ServerSocket();
/**
* Get the last happened error information.
* @return the last happened error information.
*/
const char* error();
/**
* Open a server socket.
* @param expr an expression of the address and the port of the server.
* @return true on success, or false on failure.
*/
bool open(const std::string& expr);
/**
* Close the socket.
* @return true on success, or false on failure.
*/
bool close();
/**
* Accept a connection from a client.
* @param sock the socket object to manage the connection.
* @param bool secure if this is to be a secure socket
* @param char* ca path of the ca
* @param char* pk path of the private key
* @param char* cert path of the certificate
* @return true on success, or false on failure.
*/
bool accept(Socket* sock, bool secure = false,
const char* ca = NULL, const char* pk = NULL, const char* cert = NULL);
/**
* Abort the current operation.
* @return true on success, or false on failure.
*/
bool abort();
/**
* Set the timeout of each operation.
* @param timeout the timeout of each operation in seconds.
* @return true on success, or false on failure.
*/
bool set_timeout(double timeout);
/**
* Get the expression of the socket.
* @return the expression of the socket or an empty string on failure.
*/
const std::string expression();
/**
* Get the descriptor integer.
* @return the descriptor integer, or -1 on failure.
*/
int32_t descriptor();
/**
* Set event flags.
* @param flags specifies the event mode. The following may be added by bitwise-or:
* ServerSocket::EVINPUT for input events, ServerSocket::EVOUTPUT for output events,
* ServerSocket::EVERROR for error events.
*/
void set_event_flags(uint32_t flags);
/**
* Get the current event flags.
* @return the current event flags.
*/
uint32_t event_flags();
private:
/** Dummy constructor to forbid the use. */
ServerSocket(const Socket&);
/** Dummy Operator to forbid the use. */
ServerSocket& operator =(const ServerSocket&);
/** Opaque pointer. */
void* opq_;
};
/**
* I/O event notification.
*/
class Poller {
public:
/**
* Default constructor.
*/
explicit Poller();
/**
* Destructor.
*/
~Poller();
/**
* Get the last happened error information.
* @return the last happened error information.
*/
const char* error();
/**
* Open the poller.
* @return true on success, or false on failure.
*/
bool open();
/**
* Close the poller.
* @return true on success, or false on failure.
*/
bool close();
/**
* Add a pollable I/O event to the monitored list.
* @param event the pollable event object.
* @return true on success, or false on failure.
*/
bool deposit(Pollable* event);
/**
* Remove a pollable I/O from the monitored list.
* @param event the pollable event object.
* @return true on success, or false on failure.
*/
bool withdraw(Pollable* event);
/**
* Fetch the next notified I/O event.
* @return the event object, or NULL on failure.
*/
Pollable* next();
/**
* Enable the next notification of a pollable event.
* @param event the pollable event object.
* @return true on success, or false on failure.
*/
bool undo(Pollable* event);
/**
* Wait one or more notifying events.
* @param timeout the timeout in seconds. If it is not more than 0, no timeout is specified.
* @return true on success, or false on failure.
*/
bool wait(double timeout = -1);
/**
* Notify all registered events.
* @return true on success, or false on failure.
*/
bool flush();
/**
* Get the number of events to watch.
* @return the number of events to watch, or -1 on failure.
*/
int64_t count();
/**
* Abort the current operation.
* @return true on success, or false on failure.
*/
bool abort();
private:
/** Dummy constructor to forbid the use. */
Poller(const Poller&);
/** Dummy Operator to forbid the use. */
Poller& operator =(const Poller&);
/** Opaque pointer. */
void* opq_;
};
} // common namespace
#endif // duplication check
// END OF FILE