[Libwebsockets] Lws_service with specific socket

Andy Green andy at warmcat.com
Tue Dec 19 14:51:45 CET 2017



On December 19, 2017 10:21:06 PM GMT+09:00, Edwin van den Oetelaar <oetelaar.automatisering at gmail.com> wrote:
>On Tue, Dec 19, 2017 at 2:08 PM, Théo BRIGNOL <theo.brignol at imerir.com>
>wrote:
>
>> Ok sorry for my unclear explanation.
>>
>> Simply :
>>
>> I have a file descriptor of a socket which is open. I'd like to know
>if
>> it's possible to construct a context using this socket. And to start
>a
>> lws_service on it.
>>
>>
>If the socket is just a clean socket, no data has flowed through it
>(like a
>socket you get from accept() ) then you might be able to do it.
>(I have no example, but technically it should be possible to start data
>flow and handshaking on it)

Lws does have "adopt" apis where it can take over responsibility for a socket something else accepted.  It can even do that if the socket is "dirty", ie accepted and read-from... you have to pass into the adopt api whatever was read in that case.

But there's no equivalent for adopting a listen socket.  Lws knows how to create and configure a listen socket per vhost when the vhost is created, so there is no point.  If that's what it's about it's dynamic vhost creation, which lws supports... maybe that's why the EXPLICIT_VHOST flag was mentioned before.

Generally, magicking up random listen sockets isn't the right answer... a listen socket usually implies some firewall or port-forward arrangements supporting it.  There should be a way to do everything on one port; some very complex websites manage with just :443. 

If the OP must have it for some reason, the key point is one context can hold many vhosts, and each vhost can manage a listen socket at a specified port.  But lws creates those listen sockets.  In the case more than one vhost listens on the same port, SNI and / or the Host: header are used to bind the incoming connection to the right vhost.

-Andy

>If the socket has been used for http/ws-protocol, like you suggested
>(because you want to start different sub-programs based on something
>that
>came through the socket) I think you will not succeed.
>
>Why not use the library as it was designed?
>Use a more distributed design, let the WS server take the network-calls
>and
>go from there.
>Take a look at loosly coupled services and more scalable designs. Look
>at
>ZeroMQ book for inspiration.
>
>good luck,
>Edwin
>
>
>>
>> Thanks,
>> Theo
>>
>>
>> 2017-12-19 12:04 GMT+01:00 Edwin van den Oetelaar <
>> oetelaar.automatisering at gmail.com>:
>>
>>>
>>>
>>> On Tue, Dec 19, 2017 at 11:42 AM, Théo BRIGNOL
><theo.brignol at imerir.com>
>>> wrote:
>>>
>>>>
>>>> Hello,
>>>>
>>>> I have a question regarding a simple case.
>>>>
>>>> I want to create a main socket in my main program.
>>>> In order to do that, I create a main context with default protocols
>and
>>>> one specific listening port.
>>>>
>>>
>>> This I understand.
>>>
>>>
>>>>
>>>> This main program will launch as many as sub-programs that whe
>decided
>>>> with the function "execl".
>>>>
>>>
>>> This makes no sense to me, you want to replace the current process
>with
>>> another?
>>> You destroy all state in the orignal connection (WS state, it not
>just a
>>> file descriptor)
>>>
>>> ref : https://linux.die.net/man/3/execl
>>>
>>> The *exec*() family of functions replaces the current process image
>with
>>> a new process image.
>>>
>>>
>>>>
>>>> I want this sub-programs to redefine protocols, and accept
>connection on
>>>> the main port on the main socket (the "father" socket). Note that
>with
>>>> execl I can't pass main context as a parameter.
>>>>
>>>> The fact is that I tried many solutions without succes
>>>> (LWS_EXPLICIT_VHOST option, external poll fd, etc..).
>>>>
>>>> Have you any idea in order to do that ?
>>>>
>>>
>>> I have not a clue what you try to achieve here. Or why?
>>> Do you have any example where this approach is a good solution to a
>>> problem?
>>> I do not know everything, so I am willing to learn here.
>>>
>>>
>>>>
>>>> Thank you for your comprehension.
>>>>
>>>
>>> ;-) I am completely without comprehension ;-)
>>>
>>> Greetings,
>>> Edwin
>>>
>>>
>>>> Best Regards,
>>>>
>>>> Theo
>>>>
>>>> _______________________________________________
>>>> Libwebsockets mailing list
>>>> Libwebsockets at ml.libwebsockets.org
>>>> https://libwebsockets.org/mailman/listinfo/libwebsockets
>>>>
>>>>
>>>
>>



More information about the Libwebsockets mailing list