blob: 05bc09fcd9e0ec0eca8f8b5721bbf9e5b1235a1d [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 Green52f28ce2013-01-25 17:34:15 +080028Procedure for sending data from other threads or process contexts
29-----------------------------------------------------------------
30
31Libwebsockets is carefully designed to work with no blocking in a single thread.
32In some cases where you will add libwebsockets to something else that uses the
33same single thread approach, you can so a safe implementation by combining the
34poll() loops as described in "External Polling loop support" below.
35
36In other cases, you find you have asynchronous events coming from other thread
37or process contexts and there's not much you can do about it. If you just try
38to randomly send, or broadcast using libwebsockets_broadcast() from these other
39places things will blow up either quickly or when the events on the two threads
40interefere with each other. It's not legal to do this.
41
42For those situations, you can use libwebsockets_broadcast_foreign(). This
43serializes the data you're sending using a private, per-protocol socket, so the
44service thread picks it up when it's ready, and it is serviced from the service
45thread context only.
46
47
Andy Green6c1f64e2013-01-20 11:28:06 +080048Fragmented messages
49-------------------
50
51To support fragmented messages you need to check for the final
52frame of a message with libwebsocket_is_final_fragment. This
53check can be combined with libwebsockets_remaining_packet_payload
54to gather the whole contents of a message, eg:
55
56 case LWS_CALLBACK_RECEIVE:
57 {
58 Client * const client = (Client *)user;
59 const size_t remaining = libwebsockets_remaining_packet_payload(wsi);
60
61 if (!remaining && libwebsocket_is_final_fragment(wsi)) {
62 if (client->HasFragments()) {
63 client->AppendMessageFragment(in, len, 0);
64 in = (void *)client->GetMessage();
65 len = client->GetMessageLength();
66 }
67
68 client->ProcessMessage((char *)in, len, wsi);
69 client->ResetMessage();
70 } else
71 client->AppendMessageFragment(in, len, remaining);
72 }
73 break;
74
75The test app llibwebsockets-test-fraggle sources also show how to
76deal with fragmented messages.
77
Andy Green52f28ce2013-01-25 17:34:15 +080078
Andy Green6c1f64e2013-01-20 11:28:06 +080079Debug Logging
80-------------
81
82Also using lws_set_log_level api you may provide a custom callback to actually
83emit the log string. By default, this points to an internal emit function
84that sends to stderr. Setting it to NULL leaves it as it is instead.
85
86A helper function lwsl_emit_syslog() is exported from the library to simplify
87logging to syslog. You still need to use setlogmask, openlog and closelog
88in your user code.
89
90The logging apis are made available for user code.
91
92lwsl_err(...)
93lwsl_warn(...)
94lwsl_notice(...)
95lwsl_info(...)
96lwsl_debug(...)
97
98The difference between notice and info is that notice will be logged by default
99whereas info is ignored by default.
100
101
102External Polling Loop support
103-----------------------------
104
105libwebsockets maintains an internal poll() array for all of its
106sockets, but you can instead integrate the sockets into an
107external polling array. That's needed if libwebsockets will
108cooperate with an existing poll array maintained by another
109server.
110
111Four callbacks LWS_CALLBACK_ADD_POLL_FD, LWS_CALLBACK_DEL_POLL_FD,
112LWS_CALLBACK_SET_MODE_POLL_FD and LWS_CALLBACK_CLEAR_MODE_POLL_FD
113appear in the callback for protocol 0 and allow interface code to
114manage socket descriptors in other poll loops.
115
116