wslay.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Wslay - The WebSocket Library
  3. *
  4. * Copyright (c) 2011, 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef WSLAY_H
  26. #define WSLAY_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <sys/types.h>
  33. /*
  34. * wslay/wslayver.h is generated from wslay/wslayver.h.in by
  35. * configure. The projects which do not use autotools can set
  36. * WSLAY_VERSION macro from outside to avoid to generating wslayver.h
  37. */
  38. #ifndef WSLAY_VERSION
  39. # include <wslay/wslayver.h>
  40. #endif /* WSLAY_VERSION */
  41. enum wslay_error {
  42. WSLAY_ERR_WANT_READ = -100,
  43. WSLAY_ERR_WANT_WRITE = -101,
  44. WSLAY_ERR_PROTO = -200,
  45. WSLAY_ERR_INVALID_ARGUMENT = -300,
  46. WSLAY_ERR_INVALID_CALLBACK = -301,
  47. WSLAY_ERR_NO_MORE_MSG = -302,
  48. WSLAY_ERR_CALLBACK_FAILURE = -400,
  49. WSLAY_ERR_WOULDBLOCK = -401,
  50. WSLAY_ERR_NOMEM = -500
  51. };
  52. /*
  53. * Status codes defined in RFC6455
  54. */
  55. enum wslay_status_code {
  56. WSLAY_CODE_NORMAL_CLOSURE = 1000,
  57. WSLAY_CODE_GOING_AWAY = 1001,
  58. WSLAY_CODE_PROTOCOL_ERROR = 1002,
  59. WSLAY_CODE_UNSUPPORTED_DATA = 1003,
  60. WSLAY_CODE_NO_STATUS_RCVD = 1005,
  61. WSLAY_CODE_ABNORMAL_CLOSURE = 1006,
  62. WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA = 1007,
  63. WSLAY_CODE_POLICY_VIOLATION = 1008,
  64. WSLAY_CODE_MESSAGE_TOO_BIG = 1009,
  65. WSLAY_CODE_MANDATORY_EXT = 1010,
  66. WSLAY_CODE_INTERNAL_SERVER_ERROR = 1011,
  67. WSLAY_CODE_TLS_HANDSHAKE = 1015
  68. };
  69. enum wslay_io_flags {
  70. /*
  71. * There is more data to send.
  72. */
  73. WSLAY_MSG_MORE = 1
  74. };
  75. /*
  76. * Callback function used by wslay_frame_send() function when it needs
  77. * to send data. The implementation of this function must send at most
  78. * len bytes of data in data. flags is the bitwise OR of zero or more
  79. * of the following flag:
  80. *
  81. * WSLAY_MSG_MORE
  82. * There is more data to send
  83. *
  84. * It provides some hints to tune performance and behaviour. user_data
  85. * is one given in wslay_frame_context_init() function. The
  86. * implementation of this function must return the number of bytes
  87. * sent. If there is an error, return -1. The return value 0 is also
  88. * treated an error by the library.
  89. */
  90. typedef ssize_t (*wslay_frame_send_callback)(const uint8_t *data, size_t len,
  91. int flags, void *user_data);
  92. /*
  93. * Callback function used by wslay_frame_recv() function when it needs
  94. * more data. The implementation of this function must fill at most
  95. * len bytes of data into buf. The memory area of buf is allocated by
  96. * library and not be freed by the application code. flags is always 0
  97. * in this version. user_data is one given in
  98. * wslay_frame_context_init() function. The implementation of this
  99. * function must return the number of bytes filled. If there is an
  100. * error, return -1. The return value 0 is also treated an error by
  101. * the library.
  102. */
  103. typedef ssize_t (*wslay_frame_recv_callback)(uint8_t *buf, size_t len,
  104. int flags, void *user_data);
  105. /*
  106. * Callback function used by wslay_frame_send() function when it needs
  107. * new mask key. The implementation of this function must write
  108. * exactly len bytes of mask key to buf. user_data is one given in
  109. * wslay_frame_context_init() function. The implementation of this
  110. * function return 0 on success. If there is an error, return -1.
  111. */
  112. typedef int (*wslay_frame_genmask_callback)(uint8_t *buf, size_t len,
  113. void *user_data);
  114. struct wslay_frame_callbacks {
  115. wslay_frame_send_callback send_callback;
  116. wslay_frame_recv_callback recv_callback;
  117. wslay_frame_genmask_callback genmask_callback;
  118. };
  119. /*
  120. * The opcode defined in RFC6455.
  121. */
  122. enum wslay_opcode {
  123. WSLAY_CONTINUATION_FRAME = 0x0u,
  124. WSLAY_TEXT_FRAME = 0x1u,
  125. WSLAY_BINARY_FRAME = 0x2u,
  126. WSLAY_CONNECTION_CLOSE = 0x8u,
  127. WSLAY_PING = 0x9u,
  128. WSLAY_PONG = 0xau
  129. };
  130. /*
  131. * Macro that returns 1 if opcode is control frame opcode, otherwise
  132. * returns 0.
  133. */
  134. #define wslay_is_ctrl_frame(opcode) ((opcode >> 3) & 1)
  135. /*
  136. * Macros that returns reserved bits: RSV1, RSV2, RSV3. These macros
  137. * assumes that rsv is constructed by ((RSV1 << 2) | (RSV2 << 1) |
  138. * RSV3)
  139. */
  140. #define wslay_get_rsv1(rsv) ((rsv >> 2) & 1)
  141. #define wslay_get_rsv2(rsv) ((rsv >> 1) & 1)
  142. #define wslay_get_rsv3(rsv) (rsv & 1)
  143. struct wslay_frame_iocb {
  144. /* 1 for fragmented final frame, 0 for otherwise */
  145. uint8_t fin;
  146. /*
  147. * reserved 3 bits. rsv = ((RSV1 << 2) | (RSV << 1) | RSV3).
  148. * RFC6455 requires 0 unless extensions are negotiated.
  149. */
  150. uint8_t rsv;
  151. /* 4 bit opcode */
  152. uint8_t opcode;
  153. /* payload length [0, 2**63-1] */
  154. uint64_t payload_length;
  155. /* 1 for masked frame, 0 for unmasked */
  156. uint8_t mask;
  157. /* part of payload data */
  158. const uint8_t *data;
  159. /* bytes of data defined above */
  160. size_t data_length;
  161. };
  162. struct wslay_frame_context;
  163. typedef struct wslay_frame_context *wslay_frame_context_ptr;
  164. /*
  165. * Initializes ctx using given callbacks and user_data. This function
  166. * allocates memory for struct wslay_frame_context and stores the
  167. * result to *ctx. The callback functions specified in callbacks are
  168. * copied to ctx. user_data is stored in ctx and it will be passed to
  169. * callback functions. When the user code finished using ctx, it must
  170. * call wslay_frame_context_free to deallocate memory.
  171. */
  172. int wslay_frame_context_init(wslay_frame_context_ptr *ctx,
  173. const struct wslay_frame_callbacks *callbacks,
  174. void *user_data);
  175. /*
  176. * Deallocates memory pointed by ctx.
  177. */
  178. void wslay_frame_context_free(wslay_frame_context_ptr ctx);
  179. /*
  180. * Send WebSocket frame specified in iocb. ctx must be initialized
  181. * using wslay_frame_context_init() function. iocb->fin must be 1 if
  182. * this is a fin frame, otherwise 0. iocb->rsv is reserved bits.
  183. * iocb->opcode must be the opcode of this frame. iocb->mask must be
  184. * 1 if this is masked frame, otherwise 0. iocb->payload_length is
  185. * the payload_length of this frame. iocb->data must point to the
  186. * payload data to be sent. iocb->data_length must be the length of
  187. * the data. This function calls recv_callback function if it needs
  188. * to send bytes. This function calls gen_mask_callback function if
  189. * it needs new mask key. This function returns the number of payload
  190. * bytes sent. Please note that it does not include any number of
  191. * header bytes. If it cannot send any single bytes of payload, it
  192. * returns WSLAY_ERR_WANT_WRITE. If the library detects error in iocb,
  193. * this function returns WSLAY_ERR_INVALID_ARGUMENT. If callback
  194. * functions report a failure, this function returns
  195. * WSLAY_ERR_INVALID_CALLBACK. This function does not always send all
  196. * given data in iocb. If there are remaining data to be sent, adjust
  197. * data and data_length in iocb accordingly and call this function
  198. * again.
  199. */
  200. ssize_t wslay_frame_send(wslay_frame_context_ptr ctx,
  201. struct wslay_frame_iocb *iocb);
  202. /*
  203. * Receives WebSocket frame and stores it in iocb. This function
  204. * returns the number of payload bytes received. This does not
  205. * include header bytes. In this case, iocb will be populated as
  206. * follows: iocb->fin is 1 if received frame is fin frame, otherwise
  207. * 0. iocb->rsv is reserved bits of received frame. iocb->opcode is
  208. * opcode of received frame. iocb->mask is 1 if received frame is
  209. * masked, otherwise 0. iocb->payload_length is the payload length of
  210. * received frame. iocb->data is pointed to the buffer containing
  211. * received payload data. This buffer is allocated by the library and
  212. * must be read-only. iocb->data_length is the number of payload
  213. * bytes recieved. This function calls recv_callback if it needs to
  214. * receive additional bytes. If it cannot receive any single bytes of
  215. * payload, it returns WSLAY_ERR_WANT_READ. If the library detects
  216. * protocol violation in a received frame, this function returns
  217. * WSLAY_ERR_PROTO. If callback functions report a failure, this
  218. * function returns WSLAY_ERR_INVALID_CALLBACK. This function does
  219. * not always receive whole frame in a single call. If there are
  220. * remaining data to be received, call this function again. This
  221. * function ensures frame alignment.
  222. */
  223. ssize_t wslay_frame_recv(wslay_frame_context_ptr ctx,
  224. struct wslay_frame_iocb *iocb);
  225. struct wslay_event_context;
  226. /* Pointer to the event-based API context */
  227. typedef struct wslay_event_context *wslay_event_context_ptr;
  228. struct wslay_event_on_msg_recv_arg {
  229. /* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
  230. uint8_t rsv;
  231. /* opcode */
  232. uint8_t opcode;
  233. /* received message */
  234. const uint8_t *msg;
  235. /* message length */
  236. size_t msg_length;
  237. /*
  238. * Status code iff opcode == WSLAY_CONNECTION_CLOSE. If no status
  239. * code is included in the close control frame, it is set to 0.
  240. */
  241. uint16_t status_code;
  242. };
  243. /*
  244. * Callback function invoked by wslay_event_recv() when a message is
  245. * completely received.
  246. */
  247. typedef void (*wslay_event_on_msg_recv_callback)
  248. (wslay_event_context_ptr ctx,
  249. const struct wslay_event_on_msg_recv_arg *arg, void *user_data);
  250. struct wslay_event_on_frame_recv_start_arg {
  251. /* fin bit; 1 for final frame, or 0. */
  252. uint8_t fin;
  253. /* reserved bits: rsv = (RSV1 << 2) | (RSV2 << 1) | RSV3 */
  254. uint8_t rsv;
  255. /* opcode of the frame */
  256. uint8_t opcode;
  257. /* payload length of ths frame */
  258. uint64_t payload_length;
  259. };
  260. /*
  261. * Callback function invoked by wslay_event_recv() when a new frame
  262. * starts to be received. This callback function is only invoked once
  263. * for each frame.
  264. */
  265. typedef void (*wslay_event_on_frame_recv_start_callback)
  266. (wslay_event_context_ptr ctx,
  267. const struct wslay_event_on_frame_recv_start_arg *arg, void *user_data);
  268. struct wslay_event_on_frame_recv_chunk_arg {
  269. /* chunk of payload data */
  270. const uint8_t *data;
  271. /* length of data */
  272. size_t data_length;
  273. };
  274. /*
  275. * Callback function invoked by wslay_event_recv() when a chunk of
  276. * frame payload is received.
  277. */
  278. typedef void (*wslay_event_on_frame_recv_chunk_callback)
  279. (wslay_event_context_ptr ctx,
  280. const struct wslay_event_on_frame_recv_chunk_arg *arg, void *user_data);
  281. /*
  282. * Callback function invoked by wslay_event_recv() when a frame is
  283. * completely received.
  284. */
  285. typedef void (*wslay_event_on_frame_recv_end_callback)
  286. (wslay_event_context_ptr ctx, void *user_data);
  287. /*
  288. * Callback function invoked by wslay_event_recv() when it wants to
  289. * receive more data from peer. The implementation of this callback
  290. * function must read data at most len bytes from peer and store them
  291. * in buf and return the number of bytes read. flags is always 0 in
  292. * this version.
  293. *
  294. * If there is an error, return -1 and set error code
  295. * WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
  296. * event-based API on the whole assumes non-blocking I/O. If the cause
  297. * of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
  298. * instead. This is important because it tells wslay_event_recv() to
  299. * stop receiving further data and return.
  300. */
  301. typedef ssize_t (*wslay_event_recv_callback)(wslay_event_context_ptr ctx,
  302. uint8_t *buf, size_t len,
  303. int flags, void *user_data);
  304. /*
  305. * Callback function invoked by wslay_event_send() when it wants to
  306. * send more data to peer. The implementation of this callback
  307. * function must send data at most len bytes to peer and return the
  308. * number of bytes sent. flags is the bitwise OR of zero or more of
  309. * the following flag:
  310. *
  311. * WSLAY_MSG_MORE
  312. * There is more data to send
  313. *
  314. * It provides some hints to tune performance and behaviour.
  315. *
  316. * If there is an error, return -1 and set error code
  317. * WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error(). Wslay
  318. * event-based API on the whole assumes non-blocking I/O. If the cause
  319. * of error is EAGAIN or EWOULDBLOCK, set WSLAY_ERR_WOULDBLOCK
  320. * instead. This is important because it tells wslay_event_send() to
  321. * stop sending data and return.
  322. */
  323. typedef ssize_t (*wslay_event_send_callback)(wslay_event_context_ptr ctx,
  324. const uint8_t *data, size_t len,
  325. int flags, void *user_data);
  326. /*
  327. * Callback function invoked by wslay_event_send() when it wants new
  328. * mask key. As described in RFC6455, only the traffic from WebSocket
  329. * client is masked, so this callback function is only needed if an
  330. * event-based API is initialized for WebSocket client use.
  331. */
  332. typedef int (*wslay_event_genmask_callback)(wslay_event_context_ptr ctx,
  333. uint8_t *buf, size_t len,
  334. void *user_data);
  335. struct wslay_event_callbacks {
  336. wslay_event_recv_callback recv_callback;
  337. wslay_event_send_callback send_callback;
  338. wslay_event_genmask_callback genmask_callback;
  339. wslay_event_on_frame_recv_start_callback on_frame_recv_start_callback;
  340. wslay_event_on_frame_recv_chunk_callback on_frame_recv_chunk_callback;
  341. wslay_event_on_frame_recv_end_callback on_frame_recv_end_callback;
  342. wslay_event_on_msg_recv_callback on_msg_recv_callback;
  343. };
  344. /*
  345. * Initializes ctx as WebSocket Server. user_data is an arbitrary
  346. * pointer, which is directly passed to each callback functions as
  347. * user_data argument.
  348. *
  349. * On success, returns 0. On error, returns one of following negative
  350. * values:
  351. *
  352. * WSLAY_ERR_NOMEM
  353. * Out of memory.
  354. */
  355. int wslay_event_context_server_init
  356. (wslay_event_context_ptr *ctx,
  357. const struct wslay_event_callbacks *callbacks, void *user_data);
  358. /*
  359. * Initializes ctx as WebSocket client. user_data is an arbitrary
  360. * pointer, which is directly passed to each callback functions as
  361. * user_data argument.
  362. *
  363. * On success, returns 0. On error, returns one of following negative
  364. * values:
  365. *
  366. * WSLAY_ERR_NOMEM
  367. * Out of memory.
  368. */
  369. int wslay_event_context_client_init
  370. (wslay_event_context_ptr *ctx,
  371. const struct wslay_event_callbacks *callbacks, void *user_data);
  372. /*
  373. * Releases allocated resources for ctx.
  374. */
  375. void wslay_event_context_free(wslay_event_context_ptr ctx);
  376. /*
  377. * Enables or disables buffering of an entire message for non-control
  378. * frames. If val is 0, buffering is enabled. Otherwise, buffering is
  379. * disabled. If wslay_event_on_msg_recv_callback is invoked when
  380. * buffering is disabled, the msg_length member of struct
  381. * wslay_event_on_msg_recv_arg is set to 0.
  382. *
  383. * The control frames are always buffered regardless of this function call.
  384. *
  385. * This function must not be used after the first invocation of
  386. * wslay_event_recv() function.
  387. */
  388. void wslay_event_config_set_no_buffering(wslay_event_context_ptr ctx, int val);
  389. /*
  390. * Sets maximum length of a message that can be received. The length
  391. * of message is checked by wslay_event_recv() function. If the length
  392. * of a message is larger than this value, reading operation is
  393. * disabled (same effect with wslay_event_shutdown_read() call) and
  394. * close control frame with WSLAY_CODE_MESSAGE_TOO_BIG is queued. If
  395. * buffering for non-control frames is disabled, the library checks
  396. * each frame payload length and does not check length of entire
  397. * message.
  398. *
  399. * The default value is (1u << 31)-1.
  400. */
  401. void wslay_event_config_set_max_recv_msg_length(wslay_event_context_ptr ctx,
  402. uint64_t val);
  403. /*
  404. * Sets callbacks to ctx. The callbacks previouly set by this function
  405. * or wslay_event_context_server_init() or
  406. * wslay_event_context_client_init() are replaced with callbacks.
  407. */
  408. void wslay_event_config_set_callbacks
  409. (wslay_event_context_ptr ctx, const struct wslay_event_callbacks *callbacks);
  410. /*
  411. * Receives messages from peer. When receiving
  412. * messages, it uses wslay_event_recv_callback function. Single call
  413. * of this function receives multiple messages until
  414. * wslay_event_recv_callback function sets error code
  415. * WSLAY_ERR_WOULDBLOCK.
  416. *
  417. * When close control frame is received, this function automatically
  418. * queues close control frame. Also this function calls
  419. * wslay_event_set_read_enabled() with second argument 0 to disable
  420. * further read from peer.
  421. *
  422. * When ping control frame is received, this function automatically
  423. * queues pong control frame.
  424. *
  425. * In case of a fatal errror which leads to negative return code, this
  426. * function calls wslay_event_set_read_enabled() with second argument
  427. * 0 to disable further read from peer.
  428. *
  429. * wslay_event_recv() returns 0 if it succeeds, or one of the
  430. * following negative error codes:
  431. *
  432. * WSLAY_ERR_CALLBACK_FAILURE
  433. * User defined callback function is failed.
  434. *
  435. * WSLAY_ERR_NOMEM
  436. * Out of memory.
  437. *
  438. * When negative error code is returned, application must not make any
  439. * further call of wslay_event_recv() and must close WebSocket
  440. * connection.
  441. */
  442. int wslay_event_recv(wslay_event_context_ptr ctx);
  443. /*
  444. * Sends queued messages to peer. When sending a
  445. * message, it uses wslay_event_send_callback function. Single call of
  446. * wslay_event_send() sends multiple messages until
  447. * wslay_event_send_callback sets error code WSLAY_ERR_WOULDBLOCK.
  448. *
  449. * If ctx is initialized for WebSocket client use, wslay_event_send()
  450. * uses wslay_event_genmask_callback to get new mask key.
  451. *
  452. * When a message queued using wslay_event_queue_fragmented_msg() is
  453. * sent, wslay_event_send() invokes
  454. * wslay_event_fragmented_msg_callback for that message.
  455. *
  456. * After close control frame is sent, this function calls
  457. * wslay_event_set_write_enabled() with second argument 0 to disable
  458. * further transmission to peer.
  459. *
  460. * If there are any pending messages, wslay_event_want_write() returns
  461. * 1, otherwise returns 0.
  462. *
  463. * In case of a fatal errror which leads to negative return code, this
  464. * function calls wslay_event_set_write_enabled() with second argument
  465. * 0 to disable further transmission to peer.
  466. *
  467. * wslay_event_send() returns 0 if it succeeds, or one of the
  468. * following negative error codes:
  469. *
  470. * WSLAY_ERR_CALLBACK_FAILURE
  471. * User defined callback function is failed.
  472. *
  473. * WSLAY_ERR_NOMEM
  474. * Out of memory.
  475. *
  476. * When negative error code is returned, application must not make any
  477. * further call of wslay_event_send() and must close WebSocket
  478. * connection.
  479. */
  480. int wslay_event_send(wslay_event_context_ptr ctx);
  481. struct wslay_event_msg {
  482. uint8_t opcode;
  483. const uint8_t *msg;
  484. size_t msg_length;
  485. };
  486. /*
  487. * Queues message specified in arg.
  488. *
  489. * This function supports both control and non-control messages and
  490. * the given message is sent without fragmentation. If fragmentation
  491. * is needed, use wslay_event_queue_fragmented_msg() function instead.
  492. *
  493. * This function just queues a message and does not send
  494. * it. wslay_event_send() function call sends these queued messages.
  495. *
  496. * wslay_event_queue_msg() returns 0 if it succeeds, or returns the
  497. * following negative error codes:
  498. *
  499. * WSLAY_ERR_NO_MORE_MSG
  500. * Could not queue given message. The one of possible reason is that
  501. * close control frame has been queued/sent and no further queueing
  502. * message is not allowed.
  503. *
  504. * WSLAY_ERR_INVALID_ARGUMENT
  505. * The given message is invalid.
  506. *
  507. * WSLAY_ERR_NOMEM
  508. * Out of memory.
  509. */
  510. int wslay_event_queue_msg(wslay_event_context_ptr ctx,
  511. const struct wslay_event_msg *arg);
  512. /*
  513. * Specify "source" to generate message.
  514. */
  515. union wslay_event_msg_source {
  516. int fd;
  517. void *data;
  518. };
  519. /*
  520. * Callback function called by wslay_event_send() to read message data
  521. * from source. The implementation of
  522. * wslay_event_fragmented_msg_callback must store at most len bytes of
  523. * data to buf and return the number of stored bytes. If all data is
  524. * read (i.e., EOF), set *eof to 1. If no data can be generated at the
  525. * moment, return 0. If there is an error, return -1 and set error
  526. * code WSLAY_ERR_CALLBACK_FAILURE using wslay_event_set_error().
  527. */
  528. typedef ssize_t (*wslay_event_fragmented_msg_callback)
  529. (wslay_event_context_ptr ctx,
  530. uint8_t *buf, size_t len, const union wslay_event_msg_source *source,
  531. int *eof, void *user_data);
  532. struct wslay_event_fragmented_msg {
  533. /* opcode */
  534. uint8_t opcode;
  535. /* "source" to generate message data */
  536. union wslay_event_msg_source source;
  537. /* Callback function to read message data from source. */
  538. wslay_event_fragmented_msg_callback read_callback;
  539. };
  540. /*
  541. * Queues a fragmented message specified in arg.
  542. *
  543. * This function supports non-control messages only. For control frames,
  544. * use wslay_event_queue_msg() or wslay_event_queue_close().
  545. *
  546. * This function just queues a message and does not send
  547. * it. wslay_event_send() function call sends these queued messages.
  548. *
  549. * wslay_event_queue_fragmented_msg() returns 0 if it succeeds, or
  550. * returns the following negative error codes:
  551. *
  552. * WSLAY_ERR_NO_MORE_MSG
  553. * Could not queue given message. The one of possible reason is that
  554. * close control frame has been queued/sent and no further queueing
  555. * message is not allowed.
  556. *
  557. * WSLAY_ERR_INVALID_ARGUMENT
  558. * The given message is invalid.
  559. *
  560. * WSLAY_ERR_NOMEM
  561. * Out of memory.
  562. */
  563. int wslay_event_queue_fragmented_msg
  564. (wslay_event_context_ptr ctx, const struct wslay_event_fragmented_msg *arg);
  565. /*
  566. * Queues close control frame. This function is provided just for
  567. * convenience. wslay_event_queue_msg() can queue a close control
  568. * frame as well. status_code is the status code of close control
  569. * frame. reason is the close reason encoded in UTF-8. reason_length
  570. * is the length of reason in bytes. reason_length must be less than
  571. * 123 bytes.
  572. *
  573. * If status_code is 0, reason and reason_length is not used and close
  574. * control frame with zero-length payload will be queued.
  575. *
  576. * This function just queues a message and does not send
  577. * it. wslay_event_send() function call sends these queued messages.
  578. *
  579. * wslay_event_queue_close() returns 0 if it succeeds, or returns the
  580. * following negative error codes:
  581. *
  582. * WSLAY_ERR_NO_MORE_MSG
  583. * Could not queue given message. The one of possible reason is that
  584. * close control frame has been queued/sent and no further queueing
  585. * message is not allowed.
  586. *
  587. * WSLAY_ERR_INVALID_ARGUMENT
  588. * The given message is invalid.
  589. *
  590. * WSLAY_ERR_NOMEM
  591. * Out of memory.
  592. */
  593. int wslay_event_queue_close(wslay_event_context_ptr ctx,
  594. uint16_t status_code,
  595. const uint8_t *reason, size_t reason_length);
  596. /*
  597. * Sets error code to tell the library there is an error. This
  598. * function is typically used in user defined callback functions. See
  599. * the description of callback function to know which error code
  600. * should be used.
  601. */
  602. void wslay_event_set_error(wslay_event_context_ptr ctx, int val);
  603. /*
  604. * Query whehter the library want to read more data from peer.
  605. *
  606. * wslay_event_want_read() returns 1 if the library want to read more
  607. * data from peer, or returns 0.
  608. */
  609. int wslay_event_want_read(wslay_event_context_ptr ctx);
  610. /*
  611. * Query whehter the library want to send more data to peer.
  612. *
  613. * wslay_event_want_write() returns 1 if the library want to send more
  614. * data to peer, or returns 0.
  615. */
  616. int wslay_event_want_write(wslay_event_context_ptr ctx);
  617. /*
  618. * Prevents the event-based API context from reading any further data
  619. * from peer.
  620. *
  621. * This function may be used with wslay_event_queue_close() if the
  622. * application detects error in the data received and wants to fail
  623. * WebSocket connection.
  624. */
  625. void wslay_event_shutdown_read(wslay_event_context_ptr ctx);
  626. /*
  627. * Prevents the event-based API context from sending any further data
  628. * to peer.
  629. */
  630. void wslay_event_shutdown_write(wslay_event_context_ptr ctx);
  631. /*
  632. * Returns 1 if the event-based API context allows read operation, or
  633. * return 0.
  634. *
  635. * After wslay_event_shutdown_read() is called,
  636. * wslay_event_get_read_enabled() returns 0.
  637. */
  638. int wslay_event_get_read_enabled(wslay_event_context_ptr ctx);
  639. /*
  640. * Returns 1 if the event-based API context allows write operation, or
  641. * return 0.
  642. *
  643. * After wslay_event_shutdown_write() is called,
  644. * wslay_event_get_write_enabled() returns 0.
  645. */
  646. int wslay_event_get_write_enabled(wslay_event_context_ptr ctx);
  647. /*
  648. * Returns 1 if a close control frame has been received from peer, or
  649. * returns 0.
  650. */
  651. int wslay_event_get_close_received(wslay_event_context_ptr ctx);
  652. /*
  653. * Returns 1 if a close control frame has been sent to peer, or
  654. * returns 0.
  655. */
  656. int wslay_event_get_close_sent(wslay_event_context_ptr ctx);
  657. /*
  658. * Returns status code received in close control frame. If no close
  659. * control frame has not been received, returns
  660. * WSLAY_CODE_ABNORMAL_CLOSURE. If received close control frame has no
  661. * status code, returns WSLAY_CODE_NO_STATUS_RCVD.
  662. */
  663. uint16_t wslay_event_get_status_code_received(wslay_event_context_ptr ctx);
  664. /*
  665. * Returns status code sent in close control frame. If no close
  666. * control frame has not been sent, returns
  667. * WSLAY_CODE_ABNORMAL_CLOSURE. If sent close control frame has no
  668. * status code, returns WSLAY_CODE_NO_STATUS_RCVD.
  669. */
  670. uint16_t wslay_event_get_status_code_sent(wslay_event_context_ptr ctx);
  671. /*
  672. * Returns the number of queued messages.
  673. */
  674. size_t wslay_event_get_queued_msg_count(wslay_event_context_ptr ctx);
  675. /*
  676. * Returns the sum of queued message length. It only counts the
  677. * message length queued using wslay_event_queue_msg() or
  678. * wslay_event_queue_close().
  679. */
  680. size_t wslay_event_get_queued_msg_length(wslay_event_context_ptr ctx);
  681. #ifdef __cplusplus
  682. }
  683. #endif
  684. #endif /* WSLAY_H */