wslay_event.h 4.3 KB

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