2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #ifndef __LIBWEBSOCKET_H__
23 #define __LIBWEBSOCKET_H__
26 enum libwebsocket_callback_reasons {
27 LWS_CALLBACK_ESTABLISHED,
31 LWS_CALLBACK_BROADCAST,
34 enum libwebsocket_write_protocol {
41 struct libwebsocket_context;
44 * struct libwebsocket_protocols - List of protocols and handlers server
46 * @name: Protocol name that must match the one given in the client
47 * Javascript new WebSocket(url, 'protocol') name
48 * @callback: The service callback used for this protocol. It allows the
49 * service action for an entire protocol to be encapsulated in
50 * the protocol-specific callback
51 * @per_session_data_size: Each new connection using this protocol gets
52 * this much memory allocated on connection establishment and
53 * freed on connection takedown. A pointer to this per-connection
54 * allocation is passed into the callback in the 'user' parameter
55 * @owning_server: the server init call fills in this opaque pointer when
56 * registering this protocol with the server.
57 * @broadcast_socket_port: the server init call fills this in with the
58 * localhost port number used to forward broadcasts for this
60 * @broadcast_socket_user_fd: the server init call fills this in ... the main()
61 * process context can write to this socket to perform broadcasts
62 * (use the libwebsockets_broadcast() api to do this instead,
63 * it works from any process context)
64 * @protocol_index: which protocol we are starting from zero
66 * This structure represents one protocol supported by the server. An
67 * array of these structures is passed to libwebsocket_create_server()
68 * allows as many protocols as you like to be handled by one server.
71 struct libwebsocket_protocols {
73 int (*callback)(struct libwebsocket *wsi,
74 enum libwebsocket_callback_reasons reason, void *user,
75 void *in, size_t len);
76 size_t per_session_data_size;
79 * below are filled in on server init and can be left uninitialized,
80 * no need for user to use them directly either
83 struct libwebsocket_context *owning_server;
84 int broadcast_socket_port;
85 int broadcast_socket_user_fd;
89 extern int libwebsocket_create_server(int port,
90 struct libwebsocket_protocols *protocols,
91 const char *ssl_cert_filepath,
92 const char *ssl_private_key_filepath, int gid, int uid);
97 * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
98 * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
99 * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
101 * This allows us to add protocol info before and after the data, and send as
102 * one packet on the network without payload copying, for maximum efficiency.
104 * So for example you need this kind of code to use libwebsocket_write with a
107 * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
109 * // fill your part of the buffer... for example here it's all zeros
110 * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
112 * libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128);
114 * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
115 * use the whole buffer without taking care of the above.
118 #define LWS_SEND_BUFFER_PRE_PADDING 12
119 #define LWS_SEND_BUFFER_POST_PADDING 1
122 libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
123 enum libwebsocket_write_protocol protocol);
126 libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file,
127 const char *content_type);
129 /* notice - you need the pre- and post- padding allocation for buf below */
132 libwebsockets_broadcast(const struct libwebsocket_protocols * protocol,
133 unsigned char *buf, size_t len);
135 extern const struct libwebsocket_protocols *
136 libwebsockets_get_protocol(struct libwebsocket *wsi);