blob: 4f4c27a52e0cb2070bbb0143343c7fd31f8cfc63 [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 Green6c1f64e2013-01-20 11:28:06 +080070Fragmented messages
71-------------------
72
73To support fragmented messages you need to check for the final
74frame of a message with libwebsocket_is_final_fragment. This
75check can be combined with libwebsockets_remaining_packet_payload
76to gather the whole contents of a message, eg:
77
78 case LWS_CALLBACK_RECEIVE:
79 {
80 Client * const client = (Client *)user;
81 const size_t remaining = libwebsockets_remaining_packet_payload(wsi);
82
83 if (!remaining && libwebsocket_is_final_fragment(wsi)) {
84 if (client->HasFragments()) {
85 client->AppendMessageFragment(in, len, 0);
86 in = (void *)client->GetMessage();
87 len = client->GetMessageLength();
88 }
89
90 client->ProcessMessage((char *)in, len, wsi);
91 client->ResetMessage();
92 } else
93 client->AppendMessageFragment(in, len, remaining);
94 }
95 break;
96
97The test app llibwebsockets-test-fraggle sources also show how to
98deal with fragmented messages.
99
Andy Green52f28ce2013-01-25 17:34:15 +0800100
Andy Green6c1f64e2013-01-20 11:28:06 +0800101Debug Logging
102-------------
103
104Also using lws_set_log_level api you may provide a custom callback to actually
105emit the log string. By default, this points to an internal emit function
106that sends to stderr. Setting it to NULL leaves it as it is instead.
107
108A helper function lwsl_emit_syslog() is exported from the library to simplify
109logging to syslog. You still need to use setlogmask, openlog and closelog
110in your user code.
111
112The logging apis are made available for user code.
113
114lwsl_err(...)
115lwsl_warn(...)
116lwsl_notice(...)
117lwsl_info(...)
118lwsl_debug(...)
119
120The difference between notice and info is that notice will be logged by default
121whereas info is ignored by default.
122
123
124External Polling Loop support
125-----------------------------
126
127libwebsockets maintains an internal poll() array for all of its
128sockets, but you can instead integrate the sockets into an
129external polling array. That's needed if libwebsockets will
130cooperate with an existing poll array maintained by another
131server.
132
133Four callbacks LWS_CALLBACK_ADD_POLL_FD, LWS_CALLBACK_DEL_POLL_FD,
134LWS_CALLBACK_SET_MODE_POLL_FD and LWS_CALLBACK_CLEAR_MODE_POLL_FD
135appear in the callback for protocol 0 and allow interface code to
136manage socket descriptors in other poll loops.
137
138
Andy Green36eb70d2013-02-01 08:42:15 +0800139Using with in c++ apps
140----------------------
141
142The library is ready for use by C++ apps. You can get started quickly by
143copying the test server
144
145$ cp test-server/test-server.c test.cpp
146
147and building it in C++ like this
148
149$ g++ -DINSTALL_DATADIR=\"/usr/share\" -ocpptest test.cpp -lwebsockets
150
151INSTALL_DATADIR is only needed because the test server uses it as shipped, if
152you remove the references to it in your app you don't need to define it on
153the g++ line either.
Andy Greena2b3a362013-02-06 20:13:03 +0900154
155
156Availability of header information
157----------------------------------
158
159From v1.2 of the library onwards, the HTTP header content is free()d as soon
160as the websocket connection is established. For websocket servers, you can
161copy interesting headers by handling LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION
162callback, for clients there's a new callback just for this purpose
163LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH.
164
Andy Greena690cd02013-02-09 12:25:31 +0800165
166TCP Keepalive
167-------------
168
169It is possible for a connection which is not being used to send to die
170silently somewhere between the peer and the side not sending. In this case
171by default TCP will just not report anything and you will never get any more
172incoming data or sign the link is dead until you try to send.
173
174To deal with getting a notification of that situation, you can choose to
175enable TCP keepalives on all libwebsockets sockets, when you create the
176context.
177
178To enable keepalive, set the ka_time member of the context creation parameter
179struct to a nonzero value (in seconds) at context creation time. You should
180also fill ka_probes and ka_interval in that case.
181
182With keepalive enabled, the TCP layer will send control packets that should
183stimulate a response from the peer without affecting link traffic. If the
184response is not coming, the socket will announce an error at poll() forcing
185a close.
186
Andy Greena47865f2013-02-10 09:39:47 +0800187Note that BSDs don't support keepalive time / probes / inteveral per-socket
188like Linux does. On those systems you can enable keepalive by a nonzero
189value in ka_time, but the systemwide kernel settings for the time / probes/
190interval are used, regardless of what nonzero value is in ka_time.