This function fills in name and rip with the name and IP of the client connected with socket descriptor fd. Names may be truncated if there is not enough room. If either cannot be determined, they will be returned as valid zero-length strings.
This function closes any active connections and then frees the context. After calling this, any further use of the context is undefined.
This function closes any active connections and then frees the context. After calling this, any further use of the context is undefined.
This function deals with any pending websocket traffic, for three kinds of event. It handles these events on both server and client types of connection the same.1) Accept new connections to our context's server
2) Perform pending broadcast writes initiated from other forked processes (effectively serializing asynchronous broadcasts)
3) Call the receive callback for incoming frame data received by server or client connections.
You need to call this service function periodically to all the above functions to happen; if your application is single-threaded you can just call it in your main event loop.
Alternatively you can fork a new process that asynchronously handles calling this service in a loop. In that case you are happy if this call blocks your thread until it needs to take care of something and would call it with a large nonzero timeout. Your loop then takes no CPU while there is nothing happening.
If you are calling it in a single-threaded app, you don't want it to wait around blocking other things in your loop from happening, so you would call it with a timeout_ms of 0, so it returns immediately if nothing is pending, or as soon as it services whatever was pending.
You will not need this unless you are doing something special
You will not need this unless you are doing something special
If the output side of a server process becomes choked, this allows flow control for the input side.
This is typically used by client code to fill in the host parameter when making a client connection. You can only call it after the context has been created.
This function creates the listening socket and takes care of all initialization in one step.After initialization, it returns a struct libwebsocket_context * that represents this server. After calling, user code needs to take care of calling libwebsocket_service with the context pointer to get the server's sockets serviced. This can be done in the same process context or a forked process, or another thread,
The protocol callback functions are called for a handful of events including http requests coming in, websocket connections becoming established, and data arriving; it's also called periodically to allow async transmission.
HTTP requests are sent always to the FIRST protocol in protocol, since at that time websocket protocol has not been negotiated. Other protocols after the first one never see any HTTP callack activity.
The server created is a simple http server by default; part of the websocket standard is upgrading this http connection to a websocket one.
This allows the same server to provide files like scripts and favicon / images or whatever over http and dynamic data over websockets all in one place; they're all handled in the user callback.
This is useful to get the protocol to broadcast back to from inside the callback.
This function allows bulk sending of a packet to every connection using the given protocol. It does not send the data directly; instead it calls the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback wants to actually send the data for that connection, the callback itself should call libwebsocket_write.libwebsockets_broadcast can be called from another fork context without having to take any care about data visibility between the processes, it'll "just work".
This function provides the way to issue data back to the client for both http and websocket protocols.In the case of sending using websocket protocol, be sure to allocate valid storage before and after buf as explained above. This scheme allows maximum efficiency of sending data and protocol in a single packet while not burdening the user code with any protocol knowledge.
This function is intended to be called from the callback in response to http requests from the client. It allows the callback to issue local files down the http link in a single step.
This function is intended to be called from the callback if the user code is interested in "complete packets" from the client. libwebsockets just passes through payload as it comes and issues a buffer additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE callback handler can use this API to find out if the buffer it has just been given is the last piece of a "complete packet" from the client -- when that is the case libwebsockets_remaining_packet_payload will return 0.Many protocols won't care becuse their packets are always small.
This function creates a connection to a remote server
This callback is the way the user controls what is served. All the protocol detail is hidden and handled by the library.For each connection / session there is user data allocated that is pointed to by "user". You set the size of this user data area when the library is initialized with libwebsocket_create_server.
You get an opportunity to initialize user data when called back with LWS_CALLBACK_ESTABLISHED reason.
after the server completes a handshake with an incoming client
after your client connection completed a handshake with the remote server
when the websocket session ends
signal to send to client (you would use libwebsocket_write taking care about the special buffer requirements
data has appeared for this server endpoint from a remote client, it can be found at *in and is len bytes long
if you elected to see PONG packets, they appear with this callback reason. PONG packets only exist in 04+ protocol
data has appeared from the server for the client connection, it can be found at *in and is len bytes long
an http request has come from a client that is not asking to upgrade the connection to a websocket one. This is a chance to serve http content, for example, to send a script to the client which will then open the websockets connection. in points to the URI path requested and libwebsockets_serve_http_file makes it very simple to send back a file to the client.
if you call libwebsocket_callback_on_writable on a connection, you will get this callback coming when the connection socket is able to accept another write packet without blocking. If it already was able to take another packet without blocking, you'll get this callback at the next call to the service loop function.
called when a client connects to the server at network level; the connection is accepted but then passed to this callback to decide whether to hang up immediately or not, based on the client IP. user contains the connection socket's descriptor. Return non-zero to terminate the connection before sending or receiving anything. Because this happens immediately after the network connection from the client, there's no websocket protocol selected yet so this callback is issued only to protocol 0.
called when the handshake has been received and parsed from the client, but the response is not sent yet. Return non-zero to disallow the connection. user is a pointer to an array of struct lws_tokens, you can use the header enums lws_token_indexes from libwebsockets.h to check for and read the supported header presence and content before deciding to allow the handshake to proceed or to kill the connection.
if configured for including OpenSSL support, this callback allows your user code to perform extra SSL_CTX_load_verify_locations or similar calls to direct OpenSSL where to find certificates the client can use to confirm the remote server identity. user is the OpenSSL SSL_CTX*
if configured for including OpenSSL support, this callback allows your user code to load extra certifcates into the server which allow it to verify the validity of certificates returned by clients. user is the server's OpenSSL SSL_CTX*
if the libwebsockets context was created with the option LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this callback is generated during OpenSSL verification of the cert sent from the client. It is sent to protocol[0] callback as no protocol has been negotiated on the connection yet. Notice that the libwebsockets context and wsi are both NULL during this callback. See
//www.openssl.org/docs/ssl/SSL_CTX_set_verify.html to understand more detail about the OpenSSL callback that generates this libwebsockets callback and the meanings of the arguments passed. In this callback, user is the x509_ctx, in is the ssl pointer and len is preverify_ok Notice that this callback maintains libwebsocket return conventions, return 0 to mean the cert is OK or 1 to fail it. This also means that if you don't handle this callback then the default callback action of returning 0 allows the client certificates.The next four reasons are optional and only need taking care of if you will be integrating libwebsockets sockets into an external polling array.
libwebsocket deals with its poll loop internally, but in the case you are integrating with another server you will need to have libwebsocket sockets share a polling array with the other server. This and the other POLL_FD related callbacks let you put your specialized poll array interface code in the callback for protocol 0, the first protocol you support, usually the HTTP protocol in the serving case. This callback happens when a socket needs to be
user contains the fd, and len is the events bitmap (like, POLLIN). If you are using the internal polling loop (the "service" callback), you can just ignore these callbacks.
This callback happens when a socket descriptor needs to be removed from an external polling array. user is the socket desricptor. If you are using the internal polling loop, you can just ignore it.
This callback happens when libwebsockets wants to modify the events for the socket descriptor in user. The handler should OR len on to the events member of the pollfd struct for this socket descriptor. If you are using the internal polling loop, you can just ignore it.
This callback occurs when libwebsockets wants to modify the events for the socket descriptor in user. The handler should AND ~len on to the events member of the pollfd struct for this socket descriptor. If you are using the internal polling loop, you can just ignore it.
This structure represents one protocol supported by the server. An array of these structures is passed to libwebsocket_create_server allows as many protocols as you like to be handled by one server.