diff --git a/changelog b/changelog index 87a6942cd..1883c8567 100644 --- a/changelog +++ b/changelog @@ -208,9 +208,17 @@ discover how many threads were actually allowed when the context was created. It's required to implement locking in the user code in the same way that libwebsockets-test-server-pthread does it, for the FD locking callbacks. -There is no knowledge or dependency in lws itself about pthreads. How the -locking is implemented is entirely up to the user code. +If LWS_MAX_SMP=1, then there is no code related to pthreads compiled in the +library. If more than 1, a small amount of pthread mutex code is built into +the library. +8) New API + +LWS_VISIBLE struct lws * +lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd) + +allows foreign sockets accepted by non-lws code to be adopted by lws as if they +had just been accepted by lws' own listen socket. User api changes ---------------- diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 982516f0d..dcc24581c 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1673,6 +1673,9 @@ lws_client_connect_extended(struct lws_context *clients, const char *address, LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT lws_client_connect_via_info(struct lws_client_connect_info * ccinfo); +LWS_VISIBLE LWS_EXTERN struct lws * +lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd); + LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT lws_canonical_hostname(struct lws_context *context); diff --git a/lib/server.c b/lib/server.c index 319e637f0..564a2c472 100644 --- a/lib/server.c +++ b/lib/server.c @@ -702,9 +702,16 @@ lws_http_transaction_completed(struct lws *wsi) return 0; } -/* - * either returns new wsi bound to accept_fd, or closes accept_fd and - * returns NULL, having cleaned up any new wsi pieces +/** + * lws_adopt_socket() - adopt foreign socket as if listen socket accepted it + * @context: lws context + * @accept_fd: fd of already-accepted socket to adopt + * + * Either returns new wsi bound to accept_fd, or closes accept_fd and + * returns NULL, having cleaned up any new wsi pieces. + * + * LWS adopts the socket in http serving mode, it's ready to accept an upgrade + * to ws or just serve http. */ LWS_VISIBLE struct lws * diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index e6772fc18..423af55e4 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -1,122 +1,51 @@ -
-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. -
-Return may be -1 for a fatal error needing connection close, or a -positive number reflecting the amount of bytes actually sent. This -can be less than the requested number of bytes due to OS memory -pressure at any given time. -
-Returns 1 if the HTTP connection must close now -Returns 0 and resets connection to wait for new HTTP header / -transaction if possible --
-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. ---Returning <0 indicates error and the wsi should be closed. Returning ->0 indicates the file was completely sent and -lws_http_transaction_completed called on the wsi (and close if != 0) -==0 indicates the file transfer is started and needs more service later, -the wsi should be left alone. -
-Helper to report HTTP errors back to the client cleanly and -consistently --
+connection to reset ++
+network address of the new server ++
+port to connect to ++
+uri path to connect to on the new server ++
+host header to send to the new server ++
This function creates a connection to a remote server
-This function takes a pollfd that has POLLIN or POLLOUT activity and -services it according to the state of the associated -struct lws. --The one call deals with all "service" that might happen on a socket -including listen accepts, http files as well as websocket protocol. -
-If a pollfd says it has something, you can just pass it to -lws_service_fd whether it is a socket handled by lws or not. -If it sees it is a lws socket, the traffic will be handled and -pollfd->revents will be zeroed now. -
-If the socket is foreign to lws, it leaves revents alone. So you can -see if you should service yourself by checking the pollfd revents -after letting lws try to service it. +This function creates a connection to a remote server
+++returns a const char * to a string like "1.1 178d78c" +representing the library version followed by the git head hash it +was built from +
+This function creates the listening socket (if serving) and takes care +of all initialization in one step. +++After initialization, it returns a struct lws_context * that +represents this server. After calling, user code needs to take care +of calling lws_service with the context pointer to get the +server's sockets serviced. This must be done in the same process +context as the initialization call. +
+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 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) 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. +This function closes any active connections and then frees the +context. After calling this, any further use of the context is +undefined.
-The returned length does not include the space for a -terminating '\0' --
-The returned length does not include the space for a -terminating '\0' --
-The buffer length len must include space for an additional -terminating '\0', or it will fail returning -1. -If the requested fragment index is not present, it fails -returning -1. --
-The buffer length len must include space for an additional -terminating '\0', or it will fail returning -1. --
-This is intended to be called from the LWS_CALLBACK_RECEIVE callback if -it's interested to see if the frame it's dealing with was sent in binary -mode. --
+Helper to report HTTP errors back to the client cleanly and +consistently ++
-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 lws_remaining_packet_payload will return -0.-Many protocols won't care becuse their packets are always small. +You will not need this unless you are doing something special
---You will not need this unless you are doing something special -
--returns a const char * to a string like "1.1 178d78c" -representing the library version followed by the git head hash it -was built from +Notice it does so by dropping '\0' into input string
-This function creates the listening socket (if serving) and takes care -of all initialization in one step. ---After initialization, it returns a struct lws_context * that -represents this server. After calling, user code needs to take care -of calling lws_service with the context pointer to get the -server's sockets serviced. This must be done in the same process -context as the initialization call. -
-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 function closes any active connections and then frees the -context. After calling this, any further use of the context is -undefined. +This function let a call to lws_service waiting for a timeout +immediately return. +++There is no poll in MBED3, he will fire callbacks when he feels like +it. +
+This function let a call to lws_service waiting for a timeout +immediately return. ++
+This function let a call to lws_service waiting for a timeout +immediately return. ++
+This function let a call to lws_service waiting for a timeout +immediately return. ++
+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. +
+Return may be -1 for a fatal error needing connection close, or a +positive number reflecting the amount of bytes actually sent. This +can be less than the requested number of bytes due to OS memory +pressure at any given time. +
+The returned length does not include the space for a +terminating '\0' ++
+The returned length does not include the space for a +terminating '\0' ++
+The buffer length len must include space for an additional +terminating '\0', or it will fail returning -1. +If the requested fragment index is not present, it fails +returning -1. ++
+The buffer length len must include space for an additional +terminating '\0', or it will fail returning -1. ++
+This is intended to be called from the LWS_CALLBACK_RECEIVE callback if +it's interested to see if the frame it's dealing with was sent in binary +mode. ++
+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 lws_remaining_packet_payload will return +0. ++Many protocols won't care becuse their packets are always small.
-This function let a call to lws_service waiting for a timeout -immediately return. +Returns 1 if the HTTP connection must close now +Returns 0 and resets connection to wait for new HTTP header / +transaction if possible
-This function let a call to lws_service waiting for a timeout -immediately return. --
-This function let a call to lws_service waiting for a timeout -immediately return. +Either returns new wsi bound to accept_fd, or closes accept_fd and +returns NULL, having cleaned up any new wsi pieces.+-There is no poll in MBED3, he will fire callbacks when he feels like -it. +LWS adopts the socket in http serving mode, it's ready to accept an upgrade +to ws or just serve http. +
+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. +++Returning <0 indicates error and the wsi should be closed. Returning +>0 indicates the file was completely sent and +lws_http_transaction_completed called on the wsi (and close if != 0) +==0 indicates the file transfer is started and needs more service later, +the wsi should be left alone. +
+This function takes a pollfd that has POLLIN or POLLOUT activity and +services it according to the state of the associated +struct lws. +++The one call deals with all "service" that might happen on a socket +including listen accepts, http files as well as websocket protocol. +
+If a pollfd says it has something, you can just pass it to +lws_service_fd whether it is a socket handled by lws or not. +If it sees it is a lws socket, the traffic will be handled and +pollfd->revents will be zeroed now. +
+If the socket is foreign to lws, it leaves revents alone. So you can +see if you should service yourself by checking the pollfd revents +after letting lws try to service it. +
+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) 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.