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