blob: a34211ec40ec2aeca1aa400aa236891a1e59e358 [file] [log] [blame]
Andy Green6c1f64e2013-01-20 11:28:06 +08001Daemonization
2-------------
3
4There's a helper api lws_daemonize built by default that does everything you
5need to daemonize well, including creating a lock file. If you're making
6what's basically a daemon, just call this early in your init to fork to a
7headless background process and exit the starting process.
8
9Notice stdout, stderr, stdin are all redirected to /dev/null to enforce your
10daemon is headless, so you'll need to sort out alternative logging, by, eg,
11syslog.
12
13
14Maximum number of connections
15-----------------------------
16
17The maximum number of connections the library can deal with is decided when
18it starts by querying the OS to find out how many file descriptors it is
19allowed to open (1024 on Fedora for example). It then allocates arrays that
20allow up to that many connections, minus whatever other file descriptors are
21in use by the user code.
22
23If you want to restrict that allocation, or increase it, you can use ulimit or
24similar to change the avaiable number of file descriptors, and when restarted
25libwebsockets will adapt accordingly.
26
27
Andy Green6f520a52013-01-29 17:57:39 +080028Libwebsockets is singlethreaded
29-------------------------------
Andy Green52f28ce2013-01-25 17:34:15 +080030
Andy Green6f520a52013-01-29 17:57:39 +080031Directly performing websocket actions from other threads is not allowed.
32Aside from the internal data being inconsistent in forked() processes,
33the scope of a wsi (struct websocket) can end at any time during service
34with the socket closing and the wsi freed.
Andy Green52f28ce2013-01-25 17:34:15 +080035
Andy Green6f520a52013-01-29 17:57:39 +080036Websocket write activities should only take place in the
37"LWS_CALLBACK_SERVER_WRITEABLE" callback as described below.
Andy Green52f28ce2013-01-25 17:34:15 +080038
Andy Green6f520a52013-01-29 17:57:39 +080039Only live connections appear in the user callbacks, so this removes any
40possibility of trying to used closed and freed wsis.
41
42If you need to service other socket or file descriptors as well as the
43websocket ones, you can combine them together with the websocket ones
44in one poll loop, see "External Polling Loop support" below, and
45still do it all in one thread / process context.
46
47
48Only send data when socket writeable
49------------------------------------
50
51You should only send data on a websocket connection from the user callback
52"LWS_CALLBACK_SERVER_WRITEABLE" (or "LWS_CALLBACK_CLIENT_WRITEABLE" for
53clients).
54
55If you want to send something, do not just send it but request a callback
56when the socket is writeable using
57
58 - libwebsocket_callback_on_writable(context, wsi) for a specific wsi, or
59 - libwebsocket_callback_on_writable_all_protocol(protocol) for all connections
60using that protocol to get a callback when next writeable.
61
62Usually you will get called back immediately next time around the service
63loop, but if your peer is slow or temporarily inactive the callback will be
64delayed accordingly. Generating what to write and sending it should be done
65in the ...WRITEABLE callback.
66
67See the test server code for an example of how to do this.
Andy Green52f28ce2013-01-25 17:34:15 +080068
69
Andy Green099f7892013-02-15 10:25:58 +080070Closing connections from the user side
71--------------------------------------
72
73When you want to close a connection, you do it by returning -1 from a
74callback for that connection.
75
76You can provoke a callback by calling libwebsocket_callback_on_writable on
77the wsi, then notice in the callback you want to close it and just return -1.
78But usually, the decision to close is made in a callback already and returning
79-1 is simple.
80
81If the socket knows the connection is dead, because the peer closed or there
82was an affirmitive network error like a FIN coming, then libwebsockets will
83take care of closing the connection automatically.
84
85If you have a silently dead connection, it's possible to enter a state where
86the send pipe on the connection is choked but no ack will ever come, so the
87dead connection will never become writeable. To cover that, you can use TCP
88keepalives (see later in this document)
89
90
Andy Green6c1f64e2013-01-20 11:28:06 +080091Fragmented messages
92-------------------
93
94To support fragmented messages you need to check for the final
95frame of a message with libwebsocket_is_final_fragment. This
96check can be combined with libwebsockets_remaining_packet_payload
97to gather the whole contents of a message, eg:
98
99 case LWS_CALLBACK_RECEIVE:
100 {
101 Client * const client = (Client *)user;
102 const size_t remaining = libwebsockets_remaining_packet_payload(wsi);
103
104 if (!remaining && libwebsocket_is_final_fragment(wsi)) {
105 if (client->HasFragments()) {
106 client->AppendMessageFragment(in, len, 0);
107 in = (void *)client->GetMessage();
108 len = client->GetMessageLength();
109 }
110
111 client->ProcessMessage((char *)in, len, wsi);
112 client->ResetMessage();
113 } else
114 client->AppendMessageFragment(in, len, remaining);
115 }
116 break;
117
118The test app llibwebsockets-test-fraggle sources also show how to
119deal with fragmented messages.
120
Andy Green52f28ce2013-01-25 17:34:15 +0800121
Andy Green6c1f64e2013-01-20 11:28:06 +0800122Debug Logging
123-------------
124
125Also using lws_set_log_level api you may provide a custom callback to actually
126emit the log string. By default, this points to an internal emit function
127that sends to stderr. Setting it to NULL leaves it as it is instead.
128
129A helper function lwsl_emit_syslog() is exported from the library to simplify
130logging to syslog. You still need to use setlogmask, openlog and closelog
131in your user code.
132
133The logging apis are made available for user code.
134
135lwsl_err(...)
136lwsl_warn(...)
137lwsl_notice(...)
138lwsl_info(...)
139lwsl_debug(...)
140
141The difference between notice and info is that notice will be logged by default
142whereas info is ignored by default.
143
144
145External Polling Loop support
146-----------------------------
147
148libwebsockets maintains an internal poll() array for all of its
149sockets, but you can instead integrate the sockets into an
150external polling array. That's needed if libwebsockets will
151cooperate with an existing poll array maintained by another
152server.
153
154Four callbacks LWS_CALLBACK_ADD_POLL_FD, LWS_CALLBACK_DEL_POLL_FD,
155LWS_CALLBACK_SET_MODE_POLL_FD and LWS_CALLBACK_CLEAR_MODE_POLL_FD
156appear in the callback for protocol 0 and allow interface code to
157manage socket descriptors in other poll loops.
158
Andy Green58f214e2013-03-09 13:03:53 +0800159You can pass all pollfds that need service to libwebsocket_service_fd(), even
160if the socket or file does not belong to libwebsockets it is safe.
161
162If libwebsocket handled it, it zeros the pollfd revents field before returning.
163So you can let libwebsockets try and if pollfd->revents is nonzero on return,
164you know it needs handling by your code.
165
Andy Green6c1f64e2013-01-20 11:28:06 +0800166
Andy Green36eb70d2013-02-01 08:42:15 +0800167Using with in c++ apps
168----------------------
169
170The library is ready for use by C++ apps. You can get started quickly by
171copying the test server
172
173$ cp test-server/test-server.c test.cpp
174
175and building it in C++ like this
176
177$ g++ -DINSTALL_DATADIR=\"/usr/share\" -ocpptest test.cpp -lwebsockets
178
179INSTALL_DATADIR is only needed because the test server uses it as shipped, if
180you remove the references to it in your app you don't need to define it on
181the g++ line either.
Andy Greena2b3a362013-02-06 20:13:03 +0900182
183
184Availability of header information
185----------------------------------
186
187From v1.2 of the library onwards, the HTTP header content is free()d as soon
188as the websocket connection is established. For websocket servers, you can
189copy interesting headers by handling LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION
190callback, for clients there's a new callback just for this purpose
191LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH.
192
Andy Greena690cd02013-02-09 12:25:31 +0800193
194TCP Keepalive
195-------------
196
197It is possible for a connection which is not being used to send to die
198silently somewhere between the peer and the side not sending. In this case
199by default TCP will just not report anything and you will never get any more
200incoming data or sign the link is dead until you try to send.
201
202To deal with getting a notification of that situation, you can choose to
203enable TCP keepalives on all libwebsockets sockets, when you create the
204context.
205
206To enable keepalive, set the ka_time member of the context creation parameter
207struct to a nonzero value (in seconds) at context creation time. You should
208also fill ka_probes and ka_interval in that case.
209
210With keepalive enabled, the TCP layer will send control packets that should
211stimulate a response from the peer without affecting link traffic. If the
212response is not coming, the socket will announce an error at poll() forcing
213a close.
214
Andy Greena47865f2013-02-10 09:39:47 +0800215Note that BSDs don't support keepalive time / probes / inteveral per-socket
216like Linux does. On those systems you can enable keepalive by a nonzero
217value in ka_time, but the systemwide kernel settings for the time / probes/
218interval are used, regardless of what nonzero value is in ka_time.
Andy Green2672fb22013-02-22 09:54:35 +0800219
220Optimizing SSL connections
221--------------------------
222
223There's a member ssl_cipher_list in the lws_context_creation_info struct
224which allows the user code to restrict the possible cipher selection at
225context-creation time.
226
227You might want to look into that to stop the ssl peers selecting a ciher which
228is too computationally expensive. To use it, point it to a string like
229
230"RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
231
232if left NULL, then the "DEFAULT" set of ciphers are all possible to select.
233
Andy Green5dc62ea2013-09-20 20:26:12 +0800234
235Async nature of client connections
236----------------------------------
237
238When you call libwebsocket_client_connect(..) and get a wsi back, it does not
239mean your connection is active. It just mean it started trying to connect.
240
241Your client connection is actually active only when you receive
242LWS_CALLBACK_CLIENT_ESTABLISHED for it.
243
244There's a 5 second timeout for the connection, and it may give up or die for
245other reasons, if any of that happens you'll get a
246LWS_CALLBACK_CLIENT_CONNECTION_ERROR callback on protocol 0 instead for the
247wsi.
248
249After attempting the connection and getting back a non-NULL wsi you should
250loop calling libwebsocket_service() until one of the above callbacks occurs.
251
252As usual, see test-client.c for example code.
253