librws.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef __LIBRWS_H__
  23. #define __LIBRWS_H__
  24. #include <stdio.h>
  25. #define RWS_VERSION_MAJOR 1
  26. #define RWS_VERSION_MINOR 2
  27. #define RWS_VERSION_PATCH 4
  28. // check windows
  29. #if defined(WIN32) || defined(_WIN32) || defined(WIN32_LEAN_AND_MEAN) || defined(_WIN64) || defined(WIN64)
  30. #define RWS_OS_WINDOWS 1
  31. #endif
  32. // extern
  33. #if defined(__cplusplus) || defined(_cplusplus)
  34. #define RWS_EXTERN extern "C"
  35. #else
  36. #define RWS_EXTERN extern
  37. #endif
  38. // attribute
  39. #if defined(__GNUC__)
  40. #if (__GNUC__ >= 4)
  41. #if defined(__cplusplus) || defined(_cplusplus)
  42. #define RWS_ATTRIB __attribute__((visibility("default")))
  43. #else
  44. #define RWS_ATTRIB __attribute__((visibility("default")))
  45. #endif
  46. #endif
  47. #endif
  48. // check attrib and define empty if not defined
  49. #if !defined(RWS_ATTRIB)
  50. #define RWS_ATTRIB
  51. #endif
  52. // dll api
  53. #if defined(RWS_OS_WINDOWS)
  54. #if defined(RWS_BUILD)
  55. #define RWS_DYLIB_API __declspec(dllexport)
  56. #else
  57. #define RWS_DYLIB_API __declspec(dllimport)
  58. #endif
  59. #endif
  60. // check dll api and define empty if not defined
  61. #if !defined(RWS_DYLIB_API)
  62. #define RWS_DYLIB_API
  63. #endif
  64. // combined lib api
  65. #define RWS_API(return_type) RWS_EXTERN RWS_ATTRIB RWS_DYLIB_API return_type
  66. // types
  67. /**
  68. @brief Boolean type as unsigned byte type.
  69. */
  70. typedef unsigned char rws_bool;
  71. #define rws_true 1
  72. #define rws_false 0
  73. /**
  74. @brief Type of all public objects.
  75. */
  76. typedef void *rws_handle;
  77. /**
  78. @brief Socket handle.
  79. */
  80. typedef rws_handle rws_socket;
  81. /**
  82. @brief Error object handle.
  83. */
  84. typedef rws_handle rws_error;
  85. /**
  86. @brief Mutex object handle.
  87. */
  88. typedef rws_handle rws_mutex;
  89. /**
  90. @brief Thread object handle.
  91. */
  92. typedef rws_handle rws_thread;
  93. /**
  94. @brief Callback type of thread function.
  95. @param user_object User object provided during thread creation.
  96. */
  97. typedef void (*rws_thread_funct)(void *user_object);
  98. /**
  99. @brief Callback type of socket object.
  100. @param socket Socket object.
  101. */
  102. typedef void (*rws_on_socket)(rws_socket socket);
  103. /**
  104. @brief Callback type on socket receive text frame.
  105. @param socket Socket object.
  106. @param text Pointer to reseived text.
  107. @param length Received text lenght without null terminated char.
  108. */
  109. typedef void (*rws_on_socket_recvd_text)(rws_socket socket, const char *text, const unsigned int length);
  110. /**
  111. @brief Callback type on socket receive binary frame.
  112. @param socket Socket object.
  113. @param data Received binary data.
  114. @param length Received binary data lenght.
  115. */
  116. typedef void (*rws_on_socket_recvd_bin)(rws_socket socket, const void *data, const unsigned int length);
  117. // socket
  118. /**
  119. @brief Create new socket.
  120. @return Socket handler or NULL on error.
  121. */
  122. RWS_API(rws_socket) rws_socket_create(void);
  123. /**
  124. @brief Set socket connect URL.
  125. @param socket Socket object.
  126. @param scheme Connect URL scheme, "http" or "ws"
  127. @param scheme Connect URL host, "echo.websocket.org"
  128. @param scheme Connect URL port.
  129. @param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
  130. @code
  131. rws_socket_set_url(socket, "http", "echo.websocket.org", 80, "/");
  132. rws_socket_set_url(socket, "ws", "echo.websocket.org", 80, "/");
  133. @endcode
  134. */
  135. RWS_API(void) rws_socket_set_url(rws_socket socket,
  136. const char *scheme,
  137. const char *host,
  138. const int port,
  139. const char *path);
  140. /**
  141. @brief Set socket connect URL scheme string.
  142. @param socket Socket object.
  143. @param scheme Connect URL scheme, "http" or "ws"
  144. @code
  145. rws_socket_set_scheme(socket, "http");
  146. rws_socket_set_scheme(socket, "ws");
  147. @endcode
  148. */
  149. RWS_API(void) rws_socket_set_scheme(rws_socket socket, const char *scheme);
  150. /**
  151. @brief Get socket connect URL scheme string.
  152. @param socket Socket object.
  153. @return Connect URL cheme or null.
  154. */
  155. RWS_API(const char *) rws_socket_get_scheme(rws_socket socket);
  156. /**
  157. @brief Set socket connect URL scheme string.
  158. @param socket Socket object.
  159. @param scheme Connect URL host, "echo.websocket.org"
  160. @code
  161. rws_socket_set_host(socket, "echo.websocket.org");
  162. @endcode
  163. */
  164. RWS_API(void) rws_socket_set_host(rws_socket socket, const char *host);
  165. /**
  166. @brief Get socket connect URL host string.
  167. @param socket Socket object.
  168. @return Connect URL host or null.
  169. */
  170. RWS_API(const char *) rws_socket_get_host(rws_socket socket);
  171. /**
  172. @brief Set socket connect URL port.
  173. @param socket Socket object.
  174. @param scheme Connect URL port.
  175. @code
  176. rws_socket_set_port(socket, 80);
  177. @endcode
  178. */
  179. RWS_API(void) rws_socket_set_port(rws_socket socket, const int port);
  180. /**
  181. @brief Get socket connect URL port.
  182. @param socket Socket object.
  183. @return Connect URL port or 0.
  184. */
  185. RWS_API(int) rws_socket_get_port(rws_socket socket);
  186. /**
  187. @brief Set socket connect URL path string.
  188. @param socket Socket object.
  189. @param scheme Connect URL path started with '/' character, "/" - for empty, "/path"
  190. @code
  191. rws_socket_set_path(socket, "/"); // empty path
  192. rws_socket_set_path(socket, "/path"); // some path
  193. @endcode
  194. */
  195. RWS_API(void) rws_socket_set_path(rws_socket socket, const char *path);
  196. /**
  197. @brief Get socket connect URL path string.
  198. @param socket Socket object.
  199. @return Connect URL path or null.
  200. */
  201. RWS_API(const char *) rws_socket_get_path(rws_socket socket);
  202. /**
  203. @brief Get socket last error object handle.
  204. @param socket Socket object.
  205. @return Last error object handle or null if no error.
  206. */
  207. RWS_API(rws_error) rws_socket_get_error(rws_socket socket);
  208. /**
  209. @brief Start connection.
  210. @detailed This method can generate error object.
  211. @param socket Socket object.
  212. @return rws_true - all params exists and connection started, otherwice rws_false.
  213. */
  214. RWS_API(rws_bool) rws_socket_connect(rws_socket socket);
  215. /**
  216. @brief Disconnect socket.
  217. @detailed Cleanup prev. send messages and start disconnection sequence.
  218. SHOULD forget about this socket handle and don't use it anymore.
  219. @warning Don't use this socket object handler after this command.
  220. @param socket Socket object.
  221. */
  222. RWS_API(void) rws_socket_disconnect_and_release(rws_socket socket);
  223. /**
  224. @brief Check is socket has connection to host and handshake(sucessfully done).
  225. @detailed Thread safe getter.
  226. @param socket Socket object.
  227. @return trw_true - connected to host and handshacked, otherwice rws_false.
  228. */
  229. RWS_API(rws_bool) rws_socket_is_connected(rws_socket socket);
  230. /**
  231. @brief Send text to connect socket.
  232. @detailed Thread safe method.
  233. @param socket Socket object.
  234. @param text Text string for sending.
  235. @return rws_true - socket and text exists and placed to send queue, otherwice rws_false.
  236. */
  237. RWS_API(rws_bool) rws_socket_send_text(rws_socket socket, const char *text);
  238. /**
  239. @brief Send bin to connect socket.
  240. @detailed Thread safe method.
  241. @param socket Socket object.
  242. @param bin bin string for sending.
  243. @param opcode opcode for frame
  244. @return rws_true - socket and bin exists and placed to send queue, otherwice rws_false.
  245. */
  246. RWS_API(rws_bool) rws_socket_send_bin(rws_socket socket, void *data, size_t len, int opcode, rws_bool is_fin);
  247. /**
  248. @brief Set socket user defined object pointer for identificating socket object.
  249. @param socket Socket object.
  250. @param user_object Void pointer to user object.
  251. */
  252. RWS_API(void) rws_socket_set_user_object(rws_socket socket, void *user_object);
  253. /**
  254. @brief Get socket user defined object.
  255. @param socket Socket object.
  256. @return User defined object pointer or null.
  257. */
  258. RWS_API(void *) rws_socket_get_user_object(rws_socket socket);
  259. RWS_API(void) rws_socket_set_on_connected(rws_socket socket, rws_on_socket callback);
  260. RWS_API(void) rws_socket_set_on_disconnected(rws_socket socket, rws_on_socket callback);
  261. RWS_API(void) rws_socket_set_on_received_text(rws_socket socket, rws_on_socket_recvd_text callback);
  262. RWS_API(void) rws_socket_set_on_received_bin(rws_socket socket, rws_on_socket_recvd_bin callback);
  263. #ifdef LIBRWS_USING_MBED_TLS
  264. RWS_API(void) rws_socket_set_server_cert(rws_socket socket, const char *server_cert, int server_cert_len);
  265. #endif
  266. // error
  267. typedef enum _rws_error_code
  268. {
  269. rws_error_code_none = 0,
  270. rws_error_code_missed_parameter,
  271. rws_error_code_send_handshake,
  272. rws_error_code_parse_handshake,
  273. rws_error_code_read_write_socket,
  274. rws_error_code_connect_to_host,
  275. /**
  276. @brief Connection was closed by endpoint.
  277. Reasons: an endpoint shutting down, an endpoint having received a frame too large, or an
  278. endpoint having received a frame that does not conform to the format expected by the endpoint.
  279. */
  280. rws_error_code_connection_closed,
  281. } rws_error_code;
  282. /**
  283. @return 0 - if error is empty or no error, otherwice error code.
  284. */
  285. RWS_API(int) rws_error_get_code(rws_error error);
  286. /**
  287. @return 0 - if error is empty or no error, otherwice HTTP error.
  288. */
  289. RWS_API(int) rws_error_get_http_error(rws_error error);
  290. /**
  291. @brief Get description of the error object.
  292. */
  293. RWS_API(const char *) rws_error_get_description(rws_error error);
  294. // mutex
  295. /**
  296. @brief Creates recursive mutex object.
  297. */
  298. RWS_API(rws_mutex) rws_mutex_create_recursive(void);
  299. /**
  300. @brief Lock mutex object.
  301. */
  302. RWS_API(void) rws_mutex_lock(rws_mutex mutex);
  303. /**
  304. @brief Unlock mutex object.
  305. */
  306. RWS_API(void) rws_mutex_unlock(rws_mutex mutex);
  307. /**
  308. @brief Unlock mutex object.
  309. */
  310. RWS_API(void) rws_mutex_delete(rws_mutex mutex);
  311. // thread
  312. /**
  313. @brief Create thread object that start immidiatelly.
  314. */
  315. RWS_API(rws_thread) rws_thread_create(rws_thread_funct thread_function, void *user_object);
  316. /**
  317. @brief Pause current thread for a number of milliseconds.
  318. */
  319. RWS_API(void) rws_thread_sleep(const unsigned int millisec);
  320. /* RT-Thread Team add */
  321. RWS_API(void) rws_socket_set_custom_mode(rws_socket socket);
  322. #define RWS_RECEIVE_HEADER_BUFF_SIZE ( 14)
  323. #define RWS_RECEIVE_PAYLOAD_BUFF_SIZE (2048)
  324. #endif