This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
librws.h
420 lines (307 loc) · 9.97 KB
/
librws.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) 2014 - 2019 Oleh Kulykov <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __LIBRWS_H__
#define __LIBRWS_H__ 1
#include <stdio.h>
#define RWS_VERSION_MAJOR 1
#define RWS_VERSION_MINOR 2
#define RWS_VERSION_PATCH 4
// check windows
#if defined(WIN32) || defined(_WIN32) || defined(WIN32_LEAN_AND_MEAN) || defined(_WIN64) || defined(WIN64)
#define RWS_OS_WINDOWS 1
#endif
// extern
#if defined(__cplusplus) || defined(_cplusplus)
#define RWS_EXTERN extern "C"
#else
#define RWS_EXTERN extern
#endif
// attribute
#if defined(__GNUC__)
#if (__GNUC__ >= 4)
#if defined(__cplusplus) || defined(_cplusplus)
#define RWS_ATTRIB __attribute__((visibility("default")))
#else
#define RWS_ATTRIB __attribute__((visibility("default")))
#endif
#endif
#endif
// check attrib and define empty if not defined
#if !defined(RWS_ATTRIB)
#define RWS_ATTRIB
#endif
// dll api
#if defined(RWS_OS_WINDOWS)
#if defined(RWS_BUILD)
#define RWS_DYLIB_API __declspec(dllexport)
#else
#define RWS_DYLIB_API __declspec(dllimport)
#endif
#endif
// check dll api and define empty if not defined
#if !defined(RWS_DYLIB_API)
#define RWS_DYLIB_API
#endif
// combined lib api
#define RWS_API(return_type) RWS_EXTERN RWS_ATTRIB RWS_DYLIB_API return_type
// types
/**
@brief Boolean type as unsigned byte type.
*/
typedef unsigned char rws_bool;
#define rws_true 1
#define rws_false 0
/**
@brief Type of all public objects.
*/
typedef void* rws_handle;
/**
@brief Socket handle.
*/
typedef struct rws_socket_struct * rws_socket;
/**
@brief Error object handle.
*/
typedef struct rws_error_struct * rws_error;
/**
@brief Mutex object handle.
*/
typedef rws_handle rws_mutex;
/**
@brief Thread object handle.
*/
typedef struct rws_thread_struct * rws_thread;
/**
@brief Callback type of thread function.
@param user_object User object provided during thread creation.
*/
typedef void (*rws_thread_funct)(void * user_object);
/**
@brief Callback type of socket object.
@param socket Socket object.
*/
typedef void (*rws_on_socket)(rws_socket socket);
/**
@brief Callback type on socket receive text frame.
@param socket Socket object.
@param text Pointer to reseived text.
@param length Received text lenght without null terminated char.
*/
typedef void (*rws_on_socket_recvd_text)(rws_socket socket, const char * text, const unsigned int length);
/**
@brief Callback type on socket receive binary frame.
@param socket Socket object.
@param data Received binary data.
@param length Received binary data lenght.
*/
typedef void (*rws_on_socket_recvd_bin)(rws_socket socket, const void * data, const unsigned int length);
// socket
/**
@brief Create new socket.
@return Socket handler or NULL on error.
*/
RWS_API(rws_socket) rws_socket_create(void);
/**
@brief Set socket connect URL.
@param socket Socket object.
@param scheme Connect URL scheme, "http" or "ws"
@param scheme Connect URL host, "echo.websocket.org"
@param scheme Connect URL port.
@param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
@code
rws_socket_set_url(socket, "http", "echo.websocket.org", 80, "/");
rws_socket_set_url(socket, "ws", "echo.websocket.org", 80, "/");
@endcode
*/
RWS_API(void) rws_socket_set_url(rws_socket socket,
const char * scheme,
const char * host,
const int port,
const char * path);
/**
@brief Set socket connect URL scheme string.
@param socket Socket object.
@param scheme Connect URL scheme, "http" or "ws"
@code
rws_socket_set_scheme(socket, "http");
rws_socket_set_scheme(socket, "ws");
@endcode
*/
RWS_API(void) rws_socket_set_scheme(rws_socket socket, const char * scheme);
/**
@brief Get socket connect URL scheme string.
@param socket Socket object.
@return Connect URL cheme or null.
*/
RWS_API(const char *) rws_socket_get_scheme(rws_socket socket);
/**
@brief Set socket connect URL scheme string.
@param socket Socket object.
@param scheme Connect URL host, "echo.websocket.org"
@code
rws_socket_set_host(socket, "echo.websocket.org");
@endcode
*/
RWS_API(void) rws_socket_set_host(rws_socket socket, const char * host);
/**
@brief Get socket connect URL host string.
@param socket Socket object.
@return Connect URL host or null.
*/
RWS_API(const char *) rws_socket_get_host(rws_socket socket);
/**
@brief Set socket connect URL port.
@param socket Socket object.
@param scheme Connect URL port.
@code
rws_socket_set_port(socket, 80);
@endcode
*/
RWS_API(void) rws_socket_set_port(rws_socket socket, const int port);
/**
@brief Get socket connect URL port.
@param socket Socket object.
@return Connect URL port or 0.
*/
RWS_API(int) rws_socket_get_port(rws_socket socket);
/**
@brief Set socket connect URL path string.
@param socket Socket object.
@param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
@code
rws_socket_set_path(socket, "/"); // empty path
rws_socket_set_path(socket, "/path"); // some path
@endcode
*/
RWS_API(void) rws_socket_set_path(rws_socket socket, const char * path);
/**
@brief Get socket connect URL path string.
@param socket Socket object.
@return Connect URL path or null.
*/
RWS_API(const char *) rws_socket_get_path(rws_socket socket);
/**
@brief Get socket last error object handle.
@param socket Socket object.
@return Last error object handle or null if no error.
*/
RWS_API(rws_error) rws_socket_get_error(rws_socket socket);
/**
@brief Start connection.
@detailed This method can generate error object.
@param socket Socket object.
@return rws_true - all params exists and connection started, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_connect(rws_socket socket);
/**
@brief Disconnect socket.
@detailed Cleanup prev. send messages and start disconnection sequence.
SHOULD forget about this socket handle and don't use it anymore.
@warning Don't use this socket object handler after this command.
@param socket Socket object.
*/
RWS_API(void) rws_socket_disconnect_and_release(rws_socket socket);
/**
@brief Check is socket has connection to host and handshake(sucessfully done).
@detailed Thread safe getter.
@param socket Socket object.
@return trw_true - connected to host and handshacked, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_is_connected(rws_socket socket);
/**
@brief Send text to connect socket.
@detailed Thread safe method.
@param socket Socket object.
@param text Text string for sending.
@return rws_true - socket and text exists and placed to send queue, otherwice rws_false.
*/
RWS_API(rws_bool) rws_socket_send_text(rws_socket socket, const char * text);
/**
@brief Set socket user defined object pointer for identificating socket object.
@param socket Socket object.
@param user_object Void pointer to user object.
*/
RWS_API(void) rws_socket_set_user_object(rws_socket socket, void * user_object);
/**
@brief Get socket user defined object.
@param socket Socket object.
@return User defined object pointer or null.
*/
RWS_API(void *) rws_socket_get_user_object(rws_socket socket);
RWS_API(void) rws_socket_set_on_connected(rws_socket socket, rws_on_socket callback);
RWS_API(void) rws_socket_set_on_disconnected(rws_socket socket, rws_on_socket callback);
RWS_API(void) rws_socket_set_on_received_text(rws_socket socket, rws_on_socket_recvd_text callback);
RWS_API(void) rws_socket_set_on_received_bin(rws_socket socket, rws_on_socket_recvd_bin callback);
// error
typedef enum _rws_error_code {
rws_error_code_none = 0,
rws_error_code_missed_parameter,
rws_error_code_send_handshake,
rws_error_code_parse_handshake,
rws_error_code_read_write_socket,
rws_error_code_connect_to_host,
/**
@brief Connection was closed by endpoint.
Reasons: an endpoint shutting down, an endpoint having received a frame too large, or an
endpoint having received a frame that does not conform to the format expected by the endpoint.
*/
rws_error_code_connection_closed,
} rws_error_code;
/**
@return 0 - if error is empty or no error, otherwice error code.
*/
RWS_API(int) rws_error_get_code(rws_error error);
/**
@return 0 - if error is empty or no error, otherwice HTTP error.
*/
RWS_API(int) rws_error_get_http_error(rws_error error);
/**
@brief Get description of the error object.
*/
RWS_API(const char *) rws_error_get_description(rws_error error);
// mutex
/**
@brief Creates recursive mutex object.
*/
RWS_API(rws_mutex) rws_mutex_create_recursive(void);
/**
@brief Lock mutex object.
*/
RWS_API(void) rws_mutex_lock(rws_mutex mutex);
/**
@brief Unlock mutex object.
*/
RWS_API(void) rws_mutex_unlock(rws_mutex mutex);
/**
@brief Unlock mutex object.
*/
RWS_API(void) rws_mutex_delete(rws_mutex mutex);
// thread
/**
@brief Create thread object that start immidiatelly.
*/
RWS_API(rws_thread) rws_thread_create(rws_thread_funct thread_function, void * user_object);
/**
@brief Pause current thread for a number of milliseconds.
*/
RWS_API(void) rws_thread_sleep(const unsigned int millisec);
#endif