wslay_event.h 4.4 KB

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