blob: feb6638e340a6339d8ae2d89ae00d07669f99502 [file] [log] [blame]
Andy Greenf7ee5492011-02-13 09:04:21 +00001<h2>libwebsockets_hangup_on_client - Server calls to terminate client connection</h2>
2<i>void</i>
3<b>libwebsockets_hangup_on_client</b>
4(<i>struct libwebsocket_context *</i> <b>this</b>,
5<i>int</i> <b>fd</b>)
6<h3>Arguments</h3>
7<dl>
8<dt><b>this</b>
9<dd>libwebsockets context
10<dt><b>fd</b>
11<dd>Connection socket descriptor
12</dl>
13<hr>
Andy Green07034092011-02-13 08:37:12 +000014<h2>libwebsockets_get_peer_addresses - Get client address information</h2>
15<i>void</i>
16<b>libwebsockets_get_peer_addresses</b>
17(<i>int</i> <b>fd</b>,
18<i>char *</i> <b>name</b>,
19<i>int</i> <b>name_len</b>,
20<i>char *</i> <b>rip</b>,
21<i>int</i> <b>rip_len</b>)
22<h3>Arguments</h3>
23<dl>
24<dt><b>fd</b>
25<dd>Connection socket descriptor
26<dt><b>name</b>
27<dd>Buffer to take client address name
28<dt><b>name_len</b>
29<dd>Length of client address name buffer
30<dt><b>rip</b>
31<dd>Buffer to take client address IP qotted quad
32<dt><b>rip_len</b>
33<dd>Length of client address IP buffer
34</dl>
35<h3>Description</h3>
36<blockquote>
37This function fills in <tt><b>name</b></tt> and <tt><b>rip</b></tt> with the name and IP of
38the client connected with socket descriptor <tt><b>fd</b></tt>. Names may be
39truncated if there is not enough room. If either cannot be
40determined, they will be returned as valid zero-length strings.
41</blockquote>
42<hr>
Andy Green9f990342011-02-12 11:57:45 +000043<h2>libwebsocket_service_fd - Service polled socket with something waiting</h2>
44<i>int</i>
45<b>libwebsocket_service_fd</b>
46(<i>struct libwebsocket_context *</i> <b>this</b>,
47<i>struct pollfd *</i> <b>pollfd</b>)
48<h3>Arguments</h3>
49<dl>
50<dt><b>this</b>
51<dd>Websocket context
52<dt><b>pollfd</b>
53<dd>The pollfd entry describing the socket fd and which events
54happened.
55</dl>
56<h3>Description</h3>
57<blockquote>
58This function closes any active connections and then frees the
59context. After calling this, any further use of the context is
60undefined.
61</blockquote>
62<hr>
Andy Green6964bb52011-01-23 16:50:33 +000063<h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
64<i>void</i>
65<b>libwebsocket_context_destroy</b>
66(<i>struct libwebsocket_context *</i> <b>this</b>)
67<h3>Arguments</h3>
68<dl>
69<dt><b>this</b>
70<dd>Websocket context
71</dl>
72<h3>Description</h3>
73<blockquote>
74This function closes any active connections and then frees the
75context. After calling this, any further use of the context is
76undefined.
77</blockquote>
78<hr>
79<h2>libwebsocket_service - Service any pending websocket activity</h2>
80<i>int</i>
81<b>libwebsocket_service</b>
82(<i>struct libwebsocket_context *</i> <b>this</b>,
83<i>int</i> <b>timeout_ms</b>)
84<h3>Arguments</h3>
85<dl>
86<dt><b>this</b>
87<dd>Websocket context
88<dt><b>timeout_ms</b>
89<dd>Timeout for poll; 0 means return immediately if nothing needed
90service otherwise block and service immediately, returning
91after the timeout if nothing needed service.
92</dl>
93<h3>Description</h3>
94<blockquote>
95This function deals with any pending websocket traffic, for three
96kinds of event. It handles these events on both server and client
97types of connection the same.
98<p>
991) Accept new connections to our context's server
100<p>
1012) Perform pending broadcast writes initiated from other forked
102processes (effectively serializing asynchronous broadcasts)
103<p>
1043) Call the receive callback for incoming frame data received by
105server or client connections.
106<p>
107You need to call this service function periodically to all the above
108functions to happen; if your application is single-threaded you can
109just call it in your main event loop.
110<p>
111Alternatively you can fork a new process that asynchronously handles
112calling this service in a loop. In that case you are happy if this
113call blocks your thread until it needs to take care of something and
114would call it with a large nonzero timeout. Your loop then takes no
115CPU while there is nothing happening.
116<p>
117If you are calling it in a single-threaded app, you don't want it to
118wait around blocking other things in your loop from happening, so you
119would call it with a timeout_ms of 0, so it returns immediately if
120nothing is pending, or as soon as it services whatever was pending.
121</blockquote>
122<hr>
Andy Green3221f922011-02-12 13:14:11 +0000123<h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking *</h2>
Andy Green90c7cbc2011-01-27 06:26:52 +0000124<i>int</i>
125<b>libwebsocket_callback_on_writable</b>
Andy Green62c54d22011-02-14 09:14:25 +0000126(<i>struct libwebsocket_context *</i> <b>this</b>,
127<i>struct libwebsocket *</i> <b>wsi</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000128<h3>Arguments</h3>
129<dl>
130<dt><b>wsi</b>
131<dd>Websocket connection instance to get callback for
132</dl>
133<hr>
134<h2>libwebsocket_callback_on_writable_all_protocol - Request a callback for all connections using the given protocol when it becomes possible to write to each socket without blocking in turn.</h2>
135<i>int</i>
136<b>libwebsocket_callback_on_writable_all_protocol</b>
137(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
138<h3>Arguments</h3>
139<dl>
140<dt><b>protocol</b>
141<dd>Protocol whose connections will get callbacks
142</dl>
143<hr>
Andy Greena6cbece2011-01-27 20:06:03 +0000144<h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
145<i>int</i>
146<b>libwebsocket_get_socket_fd</b>
147(<i>struct libwebsocket *</i> <b>wsi</b>)
148<h3>Arguments</h3>
149<dl>
150<dt><b>wsi</b>
151<dd>Websocket connection instance
152</dl>
153<h3>Description</h3>
154<blockquote>
155<p>
156You will not need this unless you are doing something special
157</blockquote>
158<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000159<h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
160<i>int</i>
161<b>libwebsocket_rx_flow_control</b>
162(<i>struct libwebsocket *</i> <b>wsi</b>,
163<i>int</i> <b>enable</b>)
164<h3>Arguments</h3>
165<dl>
166<dt><b>wsi</b>
167<dd>Websocket connection instance to get callback for
168<dt><b>enable</b>
169<dd>0 = disable read servicing for this connection, 1 = enable
170</dl>
171<h3>Description</h3>
172<blockquote>
173<p>
174If the output side of a server process becomes choked, this allows flow
175control for the input side.
176</blockquote>
177<hr>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000178<h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
179<i>const char *</i>
180<b>libwebsocket_canonical_hostname</b>
181(<i>struct libwebsocket_context *</i> <b>this</b>)
182<h3>Arguments</h3>
183<dl>
184<dt><b>this</b>
185<dd>Websocket context
186</dl>
187<h3>Description</h3>
188<blockquote>
189<p>
190This is typically used by client code to fill in the host parameter
191when making a client connection. You can only call it after the context
192has been created.
193</blockquote>
194<hr>
Andy Green4739e5c2011-01-22 12:51:57 +0000195<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +0000196<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +0000197<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +0000198(<i>int</i> <b>port</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000199<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000200<i>const char *</i> <b>ssl_cert_filepath</b>,
201<i>const char *</i> <b>ssl_private_key_filepath</b>,
202<i>int</i> <b>gid</b>,
Andy Green8014b292011-01-30 20:57:25 +0000203<i>int</i> <b>uid</b>,
204<i>unsigned int</i> <b>options</b>)
Andy Green62a12932010-11-03 11:19:23 +0000205<h3>Arguments</h3>
206<dl>
207<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000208<dd>Port to listen on... you can use 0 to suppress listening on
209any port, that's what you want if you are not running a
210websocket server at all but just using it as a client
Andy Green4f3943a2010-11-12 10:44:16 +0000211<dt><b>protocols</b>
212<dd>Array of structures listing supported protocols and a protocol-
213specific callback for each one. The list is ended with an
214entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000215It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +0000216<dt><b>ssl_cert_filepath</b>
217<dd>If libwebsockets was compiled to use ssl, and you want
218to listen using SSL, set to the filepath to fetch the
219server cert from, otherwise NULL for unencrypted
220<dt><b>ssl_private_key_filepath</b>
221<dd>filepath to private key if wanting SSL mode,
222else ignored
223<dt><b>gid</b>
224<dd>group id to change to after setting listen socket, or -1.
225<dt><b>uid</b>
226<dd>user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000227<dt><b>options</b>
228<dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green62a12932010-11-03 11:19:23 +0000229</dl>
230<h3>Description</h3>
231<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000232This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000233of all initialization in one step.
234<p>
Andy Greene92cd172011-01-19 13:11:55 +0000235After initialization, it returns a struct libwebsocket_context * that
236represents this server. After calling, user code needs to take care
237of calling <b>libwebsocket_service</b> with the context pointer to get the
238server's sockets serviced. This can be done in the same process context
239or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000240<p>
241The protocol callback functions are called for a handful of events
242including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000243established, and data arriving; it's also called periodically to allow
244async transmission.
245<p>
Andy Green47943ae2010-11-12 11:15:49 +0000246HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
247at that time websocket protocol has not been negotiated. Other
248protocols after the first one never see any HTTP callack activity.
249<p>
Andy Green62a12932010-11-03 11:19:23 +0000250The server created is a simple http server by default; part of the
251websocket standard is upgrading this http connection to a websocket one.
252<p>
253This allows the same server to provide files like scripts and favicon /
254images or whatever over http and dynamic data over websockets all in
255one place; they're all handled in the user callback.
256</blockquote>
257<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000258<h2>libwebsockets_fork_service_loop - Optional helper function forks off a process for the websocket server loop. You don't have to use this but if not, you have to make sure you are calling libwebsocket_service periodically to service the websocket traffic</h2>
259<i>int</i>
260<b>libwebsockets_fork_service_loop</b>
261(<i>struct libwebsocket_context *</i> <b>this</b>)
262<h3>Arguments</h3>
263<dl>
264<dt><b>this</b>
265<dd>server context returned by creation function
266</dl>
267<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000268<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
269<i>const struct libwebsocket_protocols *</i>
270<b>libwebsockets_get_protocol</b>
271(<i>struct libwebsocket *</i> <b>wsi</b>)
272<h3>Arguments</h3>
273<dl>
274<dt><b>wsi</b>
275<dd>pointer to struct websocket you want to know the protocol of
276</dl>
277<h3>Description</h3>
278<blockquote>
279<p>
280This is useful to get the protocol to broadcast back to from inside
281the callback.
282</blockquote>
283<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000284<h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2>
Andy Greenb45993c2010-12-18 15:13:50 +0000285<i>int</i>
286<b>libwebsockets_broadcast</b>
287(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
288<i>unsigned char *</i> <b>buf</b>,
289<i>size_t</i> <b>len</b>)
290<h3>Arguments</h3>
291<dl>
292<dt><b>protocol</b>
293<dd>pointer to the protocol you will broadcast to all members of
294<dt><b>buf</b>
295<dd>buffer containing the data to be broadcase. NOTE: this has to be
296allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
297the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
298case you are calling this function from callback context.
299<dt><b>len</b>
300<dd>length of payload data in buf, starting from buf.
301</dl>
302<h3>Description</h3>
303<blockquote>
304This function allows bulk sending of a packet to every connection using
305the given protocol. It does not send the data directly; instead it calls
306the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
307wants to actually send the data for that connection, the callback itself
308should call <b>libwebsocket_write</b>.
309<p>
310<b>libwebsockets_broadcast</b> can be called from another fork context without
311having to take any care about data visibility between the processes, it'll
312"just work".
313</blockquote>
314<hr>
Andy Green62a12932010-11-03 11:19:23 +0000315<h2>libwebsocket_write - Apply protocol then write data to client</h2>
316<i>int</i>
317<b>libwebsocket_write</b>
318(<i>struct libwebsocket *</i> <b>wsi</b>,
319<i>unsigned char *</i> <b>buf</b>,
320<i>size_t</i> <b>len</b>,
321<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
322<h3>Arguments</h3>
323<dl>
324<dt><b>wsi</b>
325<dd>Websocket instance (available from user callback)
326<dt><b>buf</b>
327<dd>The data to send. For data being sent on a websocket
328connection (ie, not default http), this buffer MUST have
329LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
330and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
331in the buffer after (buf + len). This is so the protocol
332header and trailer data can be added in-situ.
333<dt><b>len</b>
334<dd>Count of the data bytes in the payload starting from buf
335<dt><b>protocol</b>
336<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
337of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
338data on a websockets connection. Remember to allow the extra
339bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
340are used.
341</dl>
342<h3>Description</h3>
343<blockquote>
344This function provides the way to issue data back to the client
345for both http and websocket protocols.
346<p>
347In the case of sending using websocket protocol, be sure to allocate
348valid storage before and after buf as explained above. This scheme
349allows maximum efficiency of sending data and protocol in a single
350packet while not burdening the user code with any protocol knowledge.
351</blockquote>
352<hr>
353<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
354<i>int</i>
355<b>libwebsockets_serve_http_file</b>
356(<i>struct libwebsocket *</i> <b>wsi</b>,
357<i>const char *</i> <b>file</b>,
358<i>const char *</i> <b>content_type</b>)
359<h3>Arguments</h3>
360<dl>
361<dt><b>wsi</b>
362<dd>Websocket instance (available from user callback)
363<dt><b>file</b>
364<dd>The file to issue over http
365<dt><b>content_type</b>
366<dd>The http content type, eg, text/html
367</dl>
368<h3>Description</h3>
369<blockquote>
370This function is intended to be called from the callback in response
371to http requests from the client. It allows the callback to issue
372local files down the http link in a single step.
373</blockquote>
374<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000375<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
376<i>size_t</i>
377<b>libwebsockets_remaining_packet_payload</b>
378(<i>struct libwebsocket *</i> <b>wsi</b>)
379<h3>Arguments</h3>
380<dl>
381<dt><b>wsi</b>
382<dd>Websocket instance (available from user callback)
383</dl>
384<h3>Description</h3>
385<blockquote>
386This function is intended to be called from the callback if the
387user code is interested in "complete packets" from the client.
388libwebsockets just passes through payload as it comes and issues a buffer
389additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
390callback handler can use this API to find out if the buffer it has just
391been given is the last piece of a "complete packet" from the client --
392when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
3930.
394<p>
395Many protocols won't care becuse their packets are always small.
396</blockquote>
397<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000398<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
399<i>struct libwebsocket *</i>
400<b>libwebsocket_client_connect</b>
401(<i>struct libwebsocket_context *</i> <b>this</b>,
402<i>const char *</i> <b>address</b>,
403<i>int</i> <b>port</b>,
404<i>int</i> <b>ssl_connection</b>,
405<i>const char *</i> <b>path</b>,
406<i>const char *</i> <b>host</b>,
407<i>const char *</i> <b>origin</b>,
Andy Greenbfb051f2011-02-09 08:49:14 +0000408<i>const char *</i> <b>protocol</b>,
409<i>int</i> <b>ietf_version_or_minus_one</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000410<h3>Arguments</h3>
411<dl>
412<dt><b>this</b>
413<dd>Websocket context
414<dt><b>address</b>
415<dd>Remote server address, eg, "myserver.com"
416<dt><b>port</b>
417<dd>Port to connect to on the remote server, eg, 80
418<dt><b>ssl_connection</b>
419<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
420signed certs
421<dt><b>path</b>
422<dd>Websocket path on server
423<dt><b>host</b>
424<dd>Hostname on server
425<dt><b>origin</b>
426<dd>Socket origin name
427<dt><b>protocol</b>
428<dd>Comma-separated list of protocols being asked for from
429the server, or just one. The server will pick the one it
430likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000431<dt><b>ietf_version_or_minus_one</b>
432<dd>-1 to ask to connect using the default, latest
433protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000434</dl>
435<h3>Description</h3>
436<blockquote>
437This function creates a connection to a remote server
438</blockquote>
439<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000440<h2>callback - User server actions</h2>
441<i>int</i>
442<b>callback</b>
Darin Willitsc19456f2011-02-14 17:52:39 +0000443(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000444<i>struct libwebsocket *</i> <b>wsi</b>,
Andy Green8f037e42010-12-19 22:13:26 +0000445<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
446<i>void *</i> <b>user</b>,
447<i>void *</i> <b>in</b>,
448<i>size_t</i> <b>len</b>)
449<h3>Arguments</h3>
450<dl>
451<dt><b>wsi</b>
452<dd>Opaque websocket instance pointer
453<dt><b>reason</b>
454<dd>The reason for the call
455<dt><b>user</b>
456<dd>Pointer to per-session user data allocated by library
457<dt><b>in</b>
458<dd>Pointer used for some callback reasons
459<dt><b>len</b>
460<dd>Length set for some callback reasons
461</dl>
462<h3>Description</h3>
463<blockquote>
464This callback is the way the user controls what is served. All the
465protocol detail is hidden and handled by the library.
466<p>
467For each connection / session there is user data allocated that is
468pointed to by "user". You set the size of this user data area when
469the library is initialized with libwebsocket_create_server.
470<p>
471You get an opportunity to initialize user data when called back with
472LWS_CALLBACK_ESTABLISHED reason.
473</blockquote>
474<h3>LWS_CALLBACK_ESTABLISHED</h3>
475<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000476after the server completes a handshake with
477an incoming client
478</blockquote>
479<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
480<blockquote>
481after your client connection completed
482a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000483</blockquote>
484<h3>LWS_CALLBACK_CLOSED</h3>
485<blockquote>
486when the websocket session ends
487</blockquote>
488<h3>LWS_CALLBACK_BROADCAST</h3>
489<blockquote>
490signal to send to client (you would use
491<b>libwebsocket_write</b> taking care about the
492special buffer requirements
493</blockquote>
494<h3>LWS_CALLBACK_RECEIVE</h3>
495<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000496data has appeared for this server endpoint from a
497remote client, it can be found at *in and is
498len bytes long
499</blockquote>
Andy Greena6cbece2011-01-27 20:06:03 +0000500<h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
501<blockquote>
502if you elected to see PONG packets,
503they appear with this callback reason. PONG
504packets only exist in 04+ protocol
505</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000506<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
507<blockquote>
508data has appeared from the server for the
509client connection, it can be found at *in and
510is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000511</blockquote>
512<h3>LWS_CALLBACK_HTTP</h3>
513<blockquote>
514an http request has come from a client that is not
515asking to upgrade the connection to a websocket
516one. This is a chance to serve http content,
517for example, to send a script to the client
518which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000519<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000520<b>libwebsockets_serve_http_file</b> makes it very
521simple to send back a file to the client.
522</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000523<h3>LWS_CALLBACK_CLIENT_WRITEABLE</h3>
524<blockquote>
525if you call
526<b>libwebsocket_callback_on_writable</b> on a connection, you will
527get this callback coming when the connection socket is able to
528accept another write packet without blocking. If it already
529was able to take another packet without blocking, you'll get
530this callback at the next call to the service loop function.
531</blockquote>
Andy Green07034092011-02-13 08:37:12 +0000532<h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
533<blockquote>
534called when a client connects to
535the server at network level; the connection is accepted but then
536passed to this callback to decide whether to hang up immediately
537or not, based on the client IP. <tt><b>user</b></tt> contains the connection
538socket's descriptor. Return non-zero to terminate
539the connection before sending or receiving anything.
540Because this happens immediately after the network connection
541from the client, there's no websocket protocol selected yet so
542this callback is issued only to protocol 0.
543</blockquote>
Andy Greenc85619d2011-02-13 08:25:26 +0000544<h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
545<blockquote>
546called when the handshake has
547been received and parsed from the client, but the response is
548not sent yet. Return non-zero to disallow the connection.
Andy Green07034092011-02-13 08:37:12 +0000549<tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
550use the header enums lws_token_indexes from libwebsockets.h
551to check for and read the supported header presence and
552content before deciding to allow the handshake to proceed or
553to kill the connection.
Andy Greenc85619d2011-02-13 08:25:26 +0000554<p>
555<p>
556The next four reasons are optional and only need taking care of if you
557will be integrating libwebsockets sockets into an external polling
558array.
559</blockquote>
560<h3>LWS_CALLBACK_ADD_POLL_FD</h3>
561<blockquote>
562libwebsocket deals with its <b>poll</b> loop
563internally, but in the case you are integrating with another
564server you will need to have libwebsocket sockets share a
565polling array with the other server. This and the other
566POLL_FD related callbacks let you put your specialized
567poll array interface code in the callback for protocol 0, the
568first protocol you support, usually the HTTP protocol in the
569serving case. This callback happens when a socket needs to be
570</blockquote>
571<h3>added to the polling loop</h3>
572<blockquote>
573<tt><b>user</b></tt> contains the fd, and
574<tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
575internal polling loop (the "service" callback), you can just
576ignore these callbacks.
577</blockquote>
578<h3>LWS_CALLBACK_DEL_POLL_FD</h3>
579<blockquote>
580This callback happens when a socket descriptor
581needs to be removed from an external polling array. <tt><b>user</b></tt> is
582the socket desricptor. If you are using the internal polling
583loop, you can just ignore it.
584</blockquote>
585<h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
586<blockquote>
587This callback happens when libwebsockets
588wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
589The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
590struct for this socket descriptor. If you are using the
591internal polling loop, you can just ignore it.
592</blockquote>
593<h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
594<blockquote>
595This callback occurs when libwebsockets
596wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
597The handler should AND ~<tt><b>len</b></tt> on to the events member of the
598pollfd struct for this socket descriptor. If you are using the
599internal polling loop, you can just ignore it.
600</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000601<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000602<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
603<b>struct libwebsocket_protocols</b> {<br>
604&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Darin Willitsc19456f2011-02-14 17:52:39 +0000605&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context * context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000606&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000607&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
608&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
609&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
610&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000611};<br>
612<h3>Members</h3>
613<dl>
614<dt><b>name</b>
615<dd>Protocol name that must match the one given in the client
616Javascript new WebSocket(url, 'protocol') name
617<dt><b>callback</b>
618<dd>The service callback used for this protocol. It allows the
619service action for an entire protocol to be encapsulated in
620the protocol-specific callback
621<dt><b>per_session_data_size</b>
622<dd>Each new connection using this protocol gets
623this much memory allocated on connection establishment and
624freed on connection takedown. A pointer to this per-connection
625allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000626<dt><b>owning_server</b>
627<dd>the server init call fills in this opaque pointer when
628registering this protocol with the server.
629<dt><b>broadcast_socket_port</b>
630<dd>the server init call fills this in with the
631localhost port number used to forward broadcasts for this
632protocol
633<dt><b>broadcast_socket_user_fd</b>
634<dd>the server init call fills this in ... the <b>main</b>
635process context can write to this socket to perform broadcasts
636(use the <b>libwebsockets_broadcast</b> api to do this instead,
637it works from any process context)
638<dt><b>protocol_index</b>
639<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000640</dl>
641<h3>Description</h3>
642<blockquote>
643This structure represents one protocol supported by the server. An
644array of these structures is passed to <b>libwebsocket_create_server</b>
645allows as many protocols as you like to be handled by one server.
646</blockquote>
647<hr>