fork-sever-process-and-introduce-broadcast-api.patch
[profile/ivi/libwebsockets.git] / libwebsockets-api-doc.html
1 <h2>callback - User server actions</h2>
2 <i>int</i>
3 <b>callback</b>
4 (<i>struct libwebsocket *</i> <b>wsi</b>,
5 <i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
6 <i>void *</i> <b>user</b>,
7 <i>void *</i> <b>in</b>,
8 <i>size_t</i> <b>len</b>)
9 <h3>Arguments</h3>
10 <dl>
11 <dt><b>wsi</b>
12 <dd>Opaque websocket instance pointer
13 <dt><b>reason</b>
14 <dd>The reason for the call
15 <dt><b>user</b>
16 <dd>Pointer to per-session user data allocated by library
17 <dt><b>in</b>
18 <dd>Pointer used for some callback reasons
19 <dt><b>len</b>
20 <dd>Length set for some callback reasons
21 </dl>
22 <h3>Description</h3>
23 <blockquote>
24 This callback is the way the user controls what is served.  All the
25 protocol detail is hidden and handled by the library.
26 <p>
27 For each connection / session there is user data allocated that is
28 pointed to by "user".  You set the size of this user data area when
29 the library is initialized with libwebsocket_create_server.
30 <p>
31 You get an opportunity to initialize user data when called back with
32 LWS_CALLBACK_ESTABLISHED reason.
33 </blockquote>
34 <h3>LWS_CALLBACK_ESTABLISHED</h3>
35 <blockquote>
36 after successful websocket handshake
37 </blockquote>
38 <h3>LWS_CALLBACK_CLOSED</h3>
39 <blockquote>
40 when the websocket session ends
41 </blockquote>
42 <h3>LWS_CALLBACK_BROADCAST</h3>
43 <blockquote>
44 signal to send to client (you would use
45 <b>libwebsocket_write</b> taking care about the
46 special buffer requirements
47 </blockquote>
48 <h3>LWS_CALLBACK_RECEIVE</h3>
49 <blockquote>
50 data has appeared for the server, it can be
51 found at *in and is len bytes long
52 </blockquote>
53 <h3>LWS_CALLBACK_HTTP</h3>
54 <blockquote>
55 an http request has come from a client that is not
56 asking to upgrade the connection to a websocket
57 one.  This is a chance to serve http content,
58 for example, to send a script to the client
59 which will then open the websockets connection.
60 <tt><b>in</b></tt> points to the URI path requested and 
61 <b>libwebsockets_serve_http_file</b> makes it very
62 simple to send back a file to the client.
63 </blockquote>
64 <hr>
65 <h2>libwebsocket_create_server - Create the listening websockets server</h2>
66 <i>int</i>
67 <b>libwebsocket_create_server</b>
68 (<i>int</i> <b>port</b>,
69 <i>struct libwebsocket_protocols *</i> <b>protocols</b>,
70 <i>const char *</i> <b>ssl_cert_filepath</b>,
71 <i>const char *</i> <b>ssl_private_key_filepath</b>,
72 <i>int</i> <b>gid</b>,
73 <i>int</i> <b>uid</b>)
74 <h3>Arguments</h3>
75 <dl>
76 <dt><b>port</b>
77 <dd>Port to listen on
78 <dt><b>protocols</b>
79 <dd>Array of structures listing supported protocols and a protocol-
80 specific callback for each one.  The list is ended with an
81 entry that has a NULL callback pointer.
82 It's not const because we write the owning_server member
83 <dt><b>ssl_cert_filepath</b>
84 <dd>If libwebsockets was compiled to use ssl, and you want
85 to listen using SSL, set to the filepath to fetch the
86 server cert from, otherwise NULL for unencrypted
87 <dt><b>ssl_private_key_filepath</b>
88 <dd>filepath to private key if wanting SSL mode,
89 else ignored
90 <dt><b>gid</b>
91 <dd>group id to change to after setting listen socket, or -1.
92 <dt><b>uid</b>
93 <dd>user id to change to after setting listen socket, or -1.
94 </dl>
95 <h3>Description</h3>
96 <blockquote>
97 This function creates the listening socket and takes care
98 of all initialization in one step.
99 <p>
100 After initialization, it forks a thread that will sits in a service loop
101 and returns to the caller.  The actual service actions are performed by
102 user code in a per-protocol callback from the appropriate one selected
103 by the client from the list in <tt><b>protocols</b></tt>.
104 <p>
105 The protocol callback functions are called for a handful of events
106 including http requests coming in, websocket connections becoming
107 established, and data arriving; it's also called periodically to allow
108 async transmission.
109 <p>
110 HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
111 at that time websocket protocol has not been negotiated.  Other
112 protocols after the first one never see any HTTP callack activity.
113 <p>
114 The server created is a simple http server by default; part of the
115 websocket standard is upgrading this http connection to a websocket one.
116 <p>
117 This allows the same server to provide files like scripts and favicon /
118 images or whatever over http and dynamic data over websockets all in
119 one place; they're all handled in the user callback.
120 </blockquote>
121 <hr>
122 <h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
123 <i>const struct libwebsocket_protocols *</i>
124 <b>libwebsockets_get_protocol</b>
125 (<i>struct libwebsocket *</i> <b>wsi</b>)
126 <h3>Arguments</h3>
127 <dl>
128 <dt><b>wsi</b>
129 <dd>pointer to struct websocket you want to know the protocol of
130 </dl>
131 <h3>Description</h3>
132 <blockquote>
133 <p>
134 This is useful to get the protocol to broadcast back to from inside
135 the callback.
136 </blockquote>
137 <hr>
138 <h2>libwebsockets_broadcast - Sends a buffer to rthe callback for all active connections of the given protocol.</h2>
139 <i>int</i>
140 <b>libwebsockets_broadcast</b>
141 (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
142 <i>unsigned char *</i> <b>buf</b>,
143 <i>size_t</i> <b>len</b>)
144 <h3>Arguments</h3>
145 <dl>
146 <dt><b>protocol</b>
147 <dd>pointer to the protocol you will broadcast to all members of
148 <dt><b>buf</b>
149 <dd>buffer containing the data to be broadcase.  NOTE: this has to be
150 allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
151 the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
152 case you are calling this function from callback context.
153 <dt><b>len</b>
154 <dd>length of payload data in buf, starting from buf.
155 </dl>
156 <h3>Description</h3>
157 <blockquote>
158 This function allows bulk sending of a packet to every connection using
159 the given protocol.  It does not send the data directly; instead it calls
160 the callback with a reason type of LWS_CALLBACK_BROADCAST.  If the callback
161 wants to actually send the data for that connection, the callback itself
162 should call <b>libwebsocket_write</b>.
163 <p>
164 <b>libwebsockets_broadcast</b> can be called from another fork context without
165 having to take any care about data visibility between the processes, it'll
166 "just work".
167 </blockquote>
168 <hr>
169 <h2>libwebsocket_write - Apply protocol then write data to client</h2>
170 <i>int</i>
171 <b>libwebsocket_write</b>
172 (<i>struct libwebsocket *</i> <b>wsi</b>,
173 <i>unsigned char *</i> <b>buf</b>,
174 <i>size_t</i> <b>len</b>,
175 <i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
176 <h3>Arguments</h3>
177 <dl>
178 <dt><b>wsi</b>
179 <dd>Websocket instance (available from user callback)
180 <dt><b>buf</b>
181 <dd>The data to send.  For data being sent on a websocket
182 connection (ie, not default http), this buffer MUST have
183 LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
184 and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
185 in the buffer after (buf + len).  This is so the protocol
186 header and trailer data can be added in-situ.
187 <dt><b>len</b>
188 <dd>Count of the data bytes in the payload starting from buf
189 <dt><b>protocol</b>
190 <dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
191 of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
192 data on a websockets connection.  Remember to allow the extra
193 bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
194 are used.
195 </dl>
196 <h3>Description</h3>
197 <blockquote>
198 This function provides the way to issue data back to the client
199 for both http and websocket protocols.
200 <p>
201 In the case of sending using websocket protocol, be sure to allocate
202 valid storage before and after buf as explained above.  This scheme
203 allows maximum efficiency of sending data and protocol in a single
204 packet while not burdening the user code with any protocol knowledge.
205 </blockquote>
206 <hr>
207 <h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
208 <i>int</i>
209 <b>libwebsockets_serve_http_file</b>
210 (<i>struct libwebsocket *</i> <b>wsi</b>,
211 <i>const char *</i> <b>file</b>,
212 <i>const char *</i> <b>content_type</b>)
213 <h3>Arguments</h3>
214 <dl>
215 <dt><b>wsi</b>
216 <dd>Websocket instance (available from user callback)
217 <dt><b>file</b>
218 <dd>The file to issue over http
219 <dt><b>content_type</b>
220 <dd>The http content type, eg, text/html
221 </dl>
222 <h3>Description</h3>
223 <blockquote>
224 This function is intended to be called from the callback in response
225 to http requests from the client.  It allows the callback to issue
226 local files down the http link in a single step.
227 </blockquote>
228 <hr>
229 <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
230 <b>struct libwebsocket_protocols</b> {<br>
231 &nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
232 &nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
233 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
234 &nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
235 &nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
236 &nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
237 &nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
238 };<br>
239 <h3>Members</h3>
240 <dl>
241 <dt><b>name</b>
242 <dd>Protocol name that must match the one given in the client
243 Javascript new WebSocket(url, 'protocol') name
244 <dt><b>callback</b>
245 <dd>The service callback used for this protocol.  It allows the
246 service action for an entire protocol to be encapsulated in
247 the protocol-specific callback
248 <dt><b>per_session_data_size</b>
249 <dd>Each new connection using this protocol gets
250 this much memory allocated on connection establishment and
251 freed on connection takedown.  A pointer to this per-connection
252 allocation is passed into the callback in the 'user' parameter
253 <dt><b>owning_server</b>
254 <dd>the server init call fills in this opaque pointer when
255 registering this protocol with the server.
256 <dt><b>broadcast_socket_port</b>
257 <dd>the server init call fills this in with the
258 localhost port number used to forward broadcasts for this
259 protocol
260 <dt><b>broadcast_socket_user_fd</b>
261 <dd>the server init call fills this in ... the <b>main</b>
262 process context can write to this socket to perform broadcasts
263 (use the <b>libwebsockets_broadcast</b> api to do this instead,
264 it works from any process context)
265 <dt><b>protocol_index</b>
266 <dd>which protocol we are starting from zero
267 </dl>
268 <h3>Description</h3>
269 <blockquote>
270 This structure represents one protocol supported by the server.  An
271 array of these structures is passed to <b>libwebsocket_create_server</b>
272 allows as many protocols as you like to be handled by one server.
273 </blockquote>
274 <hr>