libwebsockets
Lightweight C library for HTML5 websockets
lws-write.h
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
30#if !defined(LWS_SIZEOFPTR)
31#define LWS_SIZEOFPTR ((int)sizeof (void *))
32#endif
33
34#if defined(__x86_64__)
35#define _LWS_PAD_SIZE 16 /* Intel recommended for best performance */
36#else
37#define _LWS_PAD_SIZE LWS_SIZEOFPTR /* Size of a pointer on the target arch */
38#endif
39#define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
40 ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
41/* last 2 is for lws-meta */
42#define LWS_PRE _LWS_PAD(4 + 10 + 2)
43/* used prior to 1.7 and retained for backward compatibility */
44#define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
45#define LWS_SEND_BUFFER_POST_PADDING 0
46
47#define LWS_WRITE_RAW LWS_WRITE_HTTP
48
49/*
50 * NOTE: These public enums are part of the abi. If you want to add one,
51 * add it at where specified so existing users are unaffected.
52 */
53enum lws_write_protocol {
54 LWS_WRITE_TEXT = 0,
59 LWS_WRITE_BINARY = 1,
64 LWS_WRITE_CONTINUATION = 2,
67 LWS_WRITE_HTTP = 3,
70 /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
71 LWS_WRITE_PING = 5,
72 LWS_WRITE_PONG = 6,
73
74 /* Same as write_http but we know this write ends the transaction */
75 LWS_WRITE_HTTP_FINAL = 7,
76
77 /* HTTP2 */
78
79 LWS_WRITE_HTTP_HEADERS = 8,
85 LWS_WRITE_HTTP_HEADERS_CONTINUATION = 9,
89 /****** add new things just above ---^ ******/
90
91 /* flags */
92
93 LWS_WRITE_BUFLIST = 0x20,
101 LWS_WRITE_NO_FIN = 0x40,
104 LWS_WRITE_H2_STREAM_END = 0x80,
109 LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
113};
114
115/* used with LWS_CALLBACK_CHILD_WRITE_VIA_PARENT */
116
118 struct lws *wsi;
119 unsigned char *buf;
120 size_t len;
121 enum lws_write_protocol wp;
122};
123
124
225LWS_VISIBLE LWS_EXTERN int
226lws_write(struct lws *wsi, unsigned char *buf, size_t len,
227 enum lws_write_protocol protocol);
228
229/* helper for case where buffer may be const */
230#define lws_write_http(wsi, buf, len) \
231 lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
232
244static LWS_INLINE int
245lws_write_ws_flags(int initial, int is_start, int is_end)
246{
247 int r;
248
249 if (is_start)
250 r = initial;
251 else
252 r = LWS_WRITE_CONTINUATION;
253
254 if (!is_end)
255 r |= LWS_WRITE_NO_FIN;
256
257 return r;
258}
259
273LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
274lws_raw_transaction_completed(struct lws *wsi);
275
Definition: lws-write.h:117