wslay_event.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_EVENT_H
  26. #define WSLAY_EVENT_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <wslay/wslay.h>
  31. #include "wslay_queue.h"
  32. struct wslay_event_byte_chunk {
  33. struct wslay_queue_entry qe;
  34. uint8_t *data;
  35. size_t data_length;
  36. };
  37. struct wslay_event_imsg {
  38. uint8_t fin;
  39. uint8_t rsv;
  40. uint8_t opcode;
  41. uint32_t utf8state;
  42. struct wslay_queue chunks;
  43. size_t msg_length;
  44. };
  45. enum wslay_event_msg_type { WSLAY_NON_FRAGMENTED, WSLAY_FRAGMENTED };
  46. struct wslay_event_omsg {
  47. struct wslay_queue_entry qe;
  48. uint8_t fin;
  49. uint8_t opcode;
  50. uint8_t rsv;
  51. enum wslay_event_msg_type type;
  52. uint8_t *data;
  53. size_t data_length;
  54. union wslay_event_msg_source source;
  55. wslay_event_fragmented_msg_callback read_callback;
  56. };
  57. struct wslay_event_frame_user_data {
  58. wslay_event_context_ptr ctx;
  59. void *user_data;
  60. };
  61. enum wslay_event_close_status {
  62. WSLAY_CLOSE_RECEIVED = 1 << 0,
  63. WSLAY_CLOSE_QUEUED = 1 << 1,
  64. WSLAY_CLOSE_SENT = 1 << 2
  65. };
  66. enum wslay_event_config { WSLAY_CONFIG_NO_BUFFERING = 1 << 0 };
  67. struct wslay_event_context {
  68. /* config status, bitwise OR of enum wslay_event_config values*/
  69. uint32_t config;
  70. /* maximum message length that can be received */
  71. uint64_t max_recv_msg_length;
  72. /* 1 if initialized for server, otherwise 0 */
  73. uint8_t server;
  74. /* bitwise OR of enum wslay_event_close_status values */
  75. uint8_t close_status;
  76. /* status code in received close control frame */
  77. uint16_t status_code_recv;
  78. /* status code in sent close control frame */
  79. uint16_t status_code_sent;
  80. wslay_frame_context_ptr frame_ctx;
  81. /* 1 if reading is enabled, otherwise 0. Upon receiving close
  82. control frame this value set to 0. If any errors in read
  83. operation will also set this value to 0. */
  84. uint8_t read_enabled;
  85. /* 1 if writing is enabled, otherwise 0 Upon completing sending
  86. close control frame, this value set to 0. If any errors in write
  87. opration will also set this value to 0. */
  88. uint8_t write_enabled;
  89. /* imsg buffer to allow interleaved control frame between
  90. non-control frames. */
  91. struct wslay_event_imsg imsgs[2];
  92. /* Pointer to imsgs to indicate current used buffer. */
  93. struct wslay_event_imsg *imsg;
  94. /* payload length of frame currently being received. */
  95. uint64_t ipayloadlen;
  96. /* next byte offset of payload currently being received. */
  97. uint64_t ipayloadoff;
  98. /* error value set by user callback */
  99. int error;
  100. /* Pointer to the message currently being sent. NULL if no message
  101. is currently sent. */
  102. struct wslay_event_omsg *omsg;
  103. /* Queue for non-control frames */
  104. struct wslay_queue /*<wslay_omsg*>*/ send_queue;
  105. /* Queue for control frames */
  106. struct wslay_queue /*<wslay_omsg*>*/ send_ctrl_queue;
  107. /* Size of send_queue + size of send_ctrl_queue */
  108. size_t queued_msg_count;
  109. /* The sum of message length in send_queue */
  110. size_t queued_msg_length;
  111. /* Buffer used for fragmented messages */
  112. uint8_t obuf[4096];
  113. uint8_t *obuflimit;
  114. uint8_t *obufmark;
  115. /* payload length of frame currently being sent. */
  116. uint64_t opayloadlen;
  117. /* next byte offset of payload currently being sent. */
  118. uint64_t opayloadoff;
  119. struct wslay_event_callbacks callbacks;
  120. struct wslay_event_frame_user_data frame_user_data;
  121. void *user_data;
  122. uint8_t allowed_rsv_bits;
  123. };
  124. #endif /* WSLAY_EVENT_H */