<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:arial,sans-serif;font-size:13px">The choices seem to boil down to this kind of "add a fake descriptor" thing (although everything, including the "interrupt the poll" descriptor and the use of it should be defined inside the library), or maybe change to use ppoll() and fire signals at it.</span></blockquote>

<div>ppoll() could be an interesting solution but it only interrupt the poll. I was thinking of interrupting poll() using a named pipe in which I would have told lws which wsi it needs to write to. The complete process would have been the following: </div>

<div>1) Before libwebsocket_create_context(), I create the named pipe.</div><div>2) For every client connection, I book for a shm in LWS_CALLBACK_ESTABLISHED through which I will share incoming data with my main thread</div>

<div>3) My main thread process the incoming data and store the answer into the shm. It then indicates lws that an answer is ready for a given wsi indicating the ID of the shm in the named pipe</div><div>4) lws poll() is interupted and it knows immediatly which wsi it needs to write to thanks to the ID of the shm. If the wsi is closed in the meanwhile, lws indicate it to the shm in LWS_CALLBACK_CLOSED</div>

<div><br></div><div>If I use ppoll(), I could keep almost the same principle but I would then need to add a SIGUSR1 and a handler OR loop through my client shm array each time ppoll() got interupted with EINTR flag set... Finally I am still wondering whether my solution is not simpler?</div>

<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:arial,sans-serif;font-size:13px">Either way lws_change_pollfd() is central to the solution.</span></blockquote>

<div>With my solution or even ppoll one, I don't see when I need to call<span style="font-family:arial,sans-serif;font-size:13px"> </span><span style="font-family:arial,sans-serif;font-size:13px">lws_change_pollfd() especially as </span><font face="arial, sans-serif">lws_change_pollfd needs a pointer to wsi which I cannot give as my interrupt concerns the complete context and not a special wsi...?</font><span style="font-family:arial,sans-serif"> </span><span style="font-family:arial,sans-serif">I must miss a point. </span></div>

<br style="font-family:arial,sans-serif;font-size:13px"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:arial,sans-serif;font-size:13px">If you pick a signal like SIGUSR1 and install a do-nothing handler for it, firing SIGUSR1 at the process from itself in lws_change_pollfd() and using ppoll() could be a really small and robust solution.</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">Since the signal is handled it doesn't do anything except interrupt the ppoll causing a pollfd reload.</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">You only need to fire the signal the first time anything wants to interrupt the wait *from another thread* (because if the lws thread is in poll(), it isn't doing anything else).  If a pollfd raced it and changed first, there's no problem with an additional signal interrupting the next ppoll loop.</span></blockquote>

<div><span style="font-family:arial,sans-serif">Ideally, if it is not too much to ask, a simple example of code would be ideal.</span></div><div><span style="font-family:arial,sans-serif"><br></span></div><div><span style="font-family:arial,sans-serif">BR, Thomas</span></div>

<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/12/25 "Andy Green (林安廸)" <span dir="ltr"><<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 25/12/13 02:06, the mail apparently from Thomas Spitz included:<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dear Andy,<br>
<br>
In accordance to your advice, I have created a patch to signal lws to<br>
break out and reload the poll() when an external thread needs to<br>
interact with lws. This is done by sending a filedescriptor to lws<br>
on libwebsocket_create_context().<br>
<br>
I am novice with lws so please let me know whether it is correct.<br>
</blockquote>
<br></div>
Thanks... it's not incorrect, but I'm wondering if this is the best way.<br>
<br>
The choices seem to boil down to this kind of "add a fake descriptor" thing (although everything, including the "interrupt the poll" descriptor and the use of it should be defined inside the library), or maybe change to use ppoll() and fire signals at it.<br>


<br>
Either way lws_change_pollfd() is central to the solution.<br>
<br>
ppoll() is the same as poll() but it lets you basically choose signals to wait on inside the poll loop as well as the fds.<br>
<br>
If you pick a signal like SIGUSR1 and install a do-nothing handler for it, firing SIGUSR1 at the process from itself in lws_change_pollfd() and using ppoll() could be a really small and robust solution.<br>
<br>
Since the signal is handled it doesn't do anything except interrupt the ppoll causing a pollfd reload.<br>
<br>
You only need to fire the signal the first time anything wants to interrupt the wait *from another thread* (because if the lws thread is in poll(), it isn't doing anything else).  If a pollfd raced it and changed first, there's no problem with an additional signal interrupting the next ppoll loop.<br>


<br>
-Andy<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
Have a nice christmas,<br>
<br>
Best regards,<br>
Thomas<br>
<br>
<br>
2013/12/21 "Andy Green (林安廸)" <<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a><br></div>
<mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>>><div><div class="h5"><br>
<br>
    On 21/12/13 10:49, the mail apparently from "Andy Green (林安廸)"<br>
    included:<br>
<br>
        On 20/12/13 19:38, the mail apparently from Thomas Spitz included:<br>
<br>
            Hello Andy,<br>
<br>
            In fact, in my application, I mostly often only have 0 to 5<br>
            active<br>
            connections at the same time with a maximum number of<br>
            simultaneous<br>
            connections restricted to 32 (Besides, why is there no<br>
            simple solution<br>
            to reduce the maximum simultaneous connection? I have seen<br>
            that we need<br>
            to reduce the max system fds...).<br>
<br>
<br>
        You should be able to do<br>
<br>
        ulimit -n 64<br>
<br>
        before starting the server.  It only affects that shell and<br>
        children. If<br>
        you're bothered by the memory footprint of the parent shell, it<br>
        should<br>
        be workable to do an exec instead of running as a child.<br>
<br>
        The default is 1024 on most systems... the way we deal with fd<br>
        lookups<br>
        it's just not that expensive to allow 1024 (a few KB).<br>
<br>
            Therefore, I think I should implement your following<br>
            proposal using<br>
            External_Poll I guess:<br>
<br>
                 Poll can only wait on file or socket descriptors... you<br>
            could<br>
                 reserve a socket to signal to break out and reload the<br>
            poll()<br>
                 because somebody's attribute changed.<br>
<br>
<br>
        Actually I was imagining adding it in lws.<br>
<br>
<br>
    I just pushed a patch<br>
<br></div></div>
    <a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__commit/?id=__91f19d8d799fdce0459c80357b3993__0b3ac55a9c" target="_blank">http://git.libwebsockets.org/_<u></u>_cgi-bin/cgit/libwebsockets/__<u></u>commit/?id=__<u></u>91f19d8d799fdce0459c80357b3993<u></u>__0b3ac55a9c</a><div>

<div class="h5"><br>
    <<a href="http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=91f19d8d799fdce0459c80357b39930b3ac55a9c" target="_blank">http://git.libwebsockets.org/<u></u>cgi-bin/cgit/libwebsockets/<u></u>commit/?id=<u></u>91f19d8d799fdce0459c80357b3993<u></u>0b3ac55a9c</a>><br>


<br>
    which should give you a single place to signal all pollfd changes<br>
    from if you want to have a go at a patch to implement this on lws side.<br>
<br>
    If you make a new socket or other descriptor-based thing in the<br>
    context create code that is open for the lifetime of the client or<br>
    server, and add it to the pollfs there with POLLIN event set,<br>
    writing a byte to it from lws_change_pollfd() the first time<br>
    something changed before the outer loop reloads should do what you want.<br>
<br>
    -Andy<br>
<br>
            I tried to do it but my thread is still not able to break<br>
            out the<br>
            poll(). In my test below (using test-server.c as skeletton),<br>
              Although I<br>
<br>
<br>
        I think it must mean your test is broken.  Otherwise you're<br>
        saying that<br>
        poll() doesn't work.<br>
<br>
        -Andy<br>
<br>
            write in the tube that is monitored by poll I see that my<br>
            asyncSending<br>
            is never able to break up poll(). poll() always break up on<br>
            10s timeout<br>
            whereas my thread write into the tube every second.<br>
<br>
            int tube[2];<br>
<br>
            (...)<br>
<br>
            void *asyncSending(void *arg) {<br>
<br>
            unsigned char c=0;<br>
            printf("START asynchronous sending\n");<br>
<br>
            while(1){<br>
            c++;<br>
            printf("write in tube\n");<br>
            if(write(tube[1],&c,1)!=1){<br>
            printf("Erreur write %s\n", strerror(errno));<br>
            }<br></div></div>
            //libwebsocket_callback_on___<u></u>writable_all_protocol(&__<u></u>protocols[PROTOCOL_DUMB___<u></u>INCREMENT]);<div class="im"><br>
<br>
            }<br>
<br>
            printf("END asynchronous sending\n");<br>
            return NULL ;<br>
<br>
            }<br>
<br>
<br>
               int main(int argc, char **argv) {<br>
<br>
                      (...)<br>
<br>
            n = 0;<br>
<br>
            printf("Tube creation\n");<br>
            if(pipe(tube)!=0){<br>
            printf("Pipe creation error %s\n", strerror(errno));<br>
            }<br>
<br>
            if (count_pollfds >= max_poll_elements) {<br></div>
            lwsl_err("LWS_CALLBACK_ADD___<u></u>POLL_FD: too many sockets to<div class="im"><br>
            track\n");<br>
            return 1;<br>
            }<br>
<br>
            fd_lookup[tube[0]] = count_pollfds;<br>
            pollfds[count_pollfds].fd = fd_lookup[tube[0]];<br>
            pollfds[count_pollfds].events = POLLIN | POLLPRI |POLL_OUT;<br></div>
            pollfds[count_pollfds++].__<u></u>revents = 0;<div class="im"><br>
<br>
            int statut_Thread;<br>
            pthread_t Thread;<br>
<br>
            statut_Thread = pthread_create(&Thread, NULL, asyncSending,<br>
            context);<br>
            if (statut_Thread != 0) {<br>
            printf("Thread creation errror \n");<br>
            }<br>
<br>
            while (n >= 0 && !force_exit) {<br>
<br>
            /*<br>
            * this represents an existing server's single poll action<br>
            * which also includes libwebsocket sockets<br>
            */<br>
<br>
            n = poll(pollfds, count_pollfds, 10000);//10s<br>
            if (n < 0)<br>
            continue;<br>
<br>
            if (n)<br>
            for (n = 0; n < count_pollfds; n++)<br>
            if (pollfds[n].revents)<br>
            /*<br>
            * returns immediately if the fd does not<br>
            * match anything under libwebsockets<br>
            * control<br>
            */<br></div>
            if (libwebsocket_service_fd(__<u></u>context,<div class="im"><br>
            &pollfds[n]) < 0)<br>
            goto done;<br>
<br>
                             (...)<br>
                     }<br>
            }<br>
<br>
            Thanks again in advance for your help.<br>
<br>
            BR, Thomas<br>
<br>
<br>
            2013/12/19 "Andy Green (林安廸)" <<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a><br>
            <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>><br></div>
            <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a> <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>>>><br>
<br><div class="im">
                 On 19/12/13 02:12, the mail apparently from Thomas<br>
            Spitz included:<br>
<br>
                     Hello Andy, hello Eugene,<br>
<br>
                     If I understood it correctly, thanks to your patch,<br>
            we can now<br>
                     prevent<br>
                     our external thread(s) from calling<br></div>
                     libwebsocket_callback_on_____<u></u>writable<div class="im"><br>
                     while lws is actively dealing with POLLIN and<br>
            POLLOUT of fds<br>
                     using our<br>
                     own mutex? Thus we are sure that the request(s)<br>
            from our external<br>
                     thread(s) will not be lost, or at least trigger<br>
                     a LWS_CALLBACK_SERVER_WRITEABLE?<br>
<br>
<br>
                 I'm not sure it won't get lost, but it won't leave<br>
            things in an<br>
                 indeterminate state.  It can still happen the<br>
            non-service thread<br>
                 asks to set POLLOUT just as we're about to take the<br>
            mutex and<br>
            clear it.<br>
<br>
                 At the moment that'll still break at low probability<br>
            but it should<br>
                 be solveable.<br>
<br>
<br>
                     My second question is related to performance:<br>
<br>
                     In the documentation it is said<br>
<br>
                          If you want to send something, do not just<br>
            send it but<br>
                     request a<br>
                          callback |when the socket is writeable |<br>
<br>
                     That is why our external thread call<br></div>
                     libwebsocket_callback_on_____<u></u>writable<div><div class="h5"><br>
                     (). Then it is written<br>
<br>
                          |Usually you will get called back immediately<br>
            next time<br>
                     around the<br>
                          service loop|<br>
<br>
                     which seems to mean that  (according to my tests)<br>
            we have to<br>
                     wait libwebsocket_service() to end up before the<br>
            socket is<br>
            writeable<br>
                     again. That implies to set a very short timeout<br>
<br>
<br>
                 Well... you are right if we have one connection.  But<br>
            if you have 50<br>
                 or 50,000 connections, somebody almost always has<br>
            something going on<br>
                 before the timeout.<br>
<br>
                 Also in your user callback, you can take the approach<br>
            like in the<br>
                 test apps to loop using lws_send_pipe_choked() test to<br>
            absolutely<br>
                 stuff your pipe each time you get service.  That<br>
            doesn't delay<br>
                 anything looping there because it only loops while the<br>
            send won't<br>
                 block.  And it means the kernel is passing out what you<br>
            stuffed into<br>
                 the pipe while you wait through any latency in the next<br>
            "writeable"<br>
                 service.<br>
<br>
<br>
                     for libwebsocket_service(context, VERYSHORTTIMEOUT)<br>
            otherwise it<br>
                     will<br>
                     take a long time before the external thread request<br>
            could be<br>
                     handled...<br>
                     This brings me to wonder why it is not possible to<br>
            loop on<br>
                     libwebsocket_service(context, VERYLONGTIMEOUT) that<br>
            would be<br>
                     interrupted<br>
                     by our external thread instead? Thus lws thread is<br>
            asleeped most<br>
                     of the<br>
                     time when there is no incoming IP traffic but can<br>
            be awaken as<br>
                     soon as<br>
                     some outgoing traffic needs to be sent (generated<br>
            from the<br>
                     external thread).<br>
<br>
<br>
                 Well lws is a singlethreaded nonblocking<br>
            implementation.  It is<br>
                 designed to run on small resource-costrained platforms<br>
            at the same<br>
                 time a being able to service huge numbers of<br>
            connections in a<br>
                 lightweight way.  It does not require threads.<br>
<br>
                 Poll can only wait on file or socket descriptors... you<br>
            could<br>
                 reserve a socket to signal to break out and reload the<br>
            poll()<br>
                 because somebody's attribute changed.  But in most real<br>
            usage<br>
                 scenarios, some other connection is always wanting<br>
            service before<br>
                 the timeout anyway and as more connections are seen the<br>
            less it<br>
                 matters about latency from waiting for poll() reload.<br>
<br>
                 -Andy<br>
<br>
                     I hope my two questions are clear..<br>
<br>
                     Thanks in advance for your rely.<br>
<br>
                     Best regards, Thomas<br>
<br>
                     2013/12/18 "Andy Green (林安廸)" <<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a><br>
            <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>><br>
                     <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a> <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>>><br>
                     <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a> <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>><br>
            <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a> <mailto:<a href="mailto:andy@warmcat.com" target="_blank">andy@warmcat.com</a>>>>><br>
<br>
<br>
                          On 18/12/13 06:40, the mail apparently from<br>
            Andy Green<br>
                     included:<br>
<br>
<br>
<br>
                              Eugene Agafonov <<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><br>
            <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><u></u>><br>
                     <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><br>
            <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><u></u>>__><br>
                              <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><br>
            <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><u></u>><br>
                     <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><br></div></div><div class="im">
            <mailto:<a href="mailto:e.a.agafonov@gmail.com" target="_blank">e.a.agafonov@gmail.com</a><u></u>>__>__>> wrote:<br>
<br>
                                  Hi!<br>
<br>
                                  I've just implemented the similar<br>
            scenario just<br>
                     like it is<br>
                                  stated<br>
                                  in document<br>
<br></div>
            <a href="http://git.libwebsockets.org/______cgi-bin/cgit/libwebsockets/______tree/README.coding" target="_blank">http://git.libwebsockets.org/_<u></u>_____cgi-bin/cgit/<u></u>libwebsockets/______tree/<u></u>README.coding</a><br>


            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____tree/README.<u></u>coding</a>><div class="im">

<br>
<br>
<br>
            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____tree/README.<u></u>coding</a><br>


            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_tree/README.coding</a>>><br>


<br>
<br>
<br>
<br>
            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____tree/README.<u></u>coding</a><br>


            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_tree/README.coding</a>><br>


<br>
<br>
            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_tree/README.coding</a><br>


            <<a href="http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/tree/README.coding" target="_blank">http://git.libwebsockets.org/<u></u>cgi-bin/cgit/libwebsockets/<u></u>tree/README.coding</a>>>><br>


<br>
<br>
<br>
<br>
                          I have a mutex-protected queue of outgoing<br>
            events (as a<br>
                     std::list of<br>
<br>
                                  strings). Each string is intended to<br>
            be send in<br>
                     separated<br>
                                  message.<br>
<br>
                                  The writer thread posts data onto<br>
            queue and calls<br>
<br></div>
              libwebsocket_callback_on______<u></u>_writable(context, wsi)<div><div class="h5"><br>
<br>
<br>
                                  Protocol callback gets<br>
                     LWS_CALLBACK_SERVER_WRITEABLE as soon as<br>
                                  websocket thread is ready to send<br>
            data. Once it<br>
                     comes, the<br>
                                  callback<br>
                                  extracts strings from the queue one by<br>
            one and<br>
                     writes them to<br>
                                  socket using libwebsocket_write.<br>
<br>
                                  I'm quite newby in writing WS server with<br>
                     libwebsockets. Any<br>
                                  comments from WS gurus are welcome<br>
<br>
<br>
                              What you're doing is a good way to<br>
            interact with what<br>
                     lws needs and<br>
                              get what you want from threading.<br>
<br>
                              There's one danger with it, but that is a<br>
            very tightly<br>
                     defined race<br>
                              rather than corruption or whatever.<br>
<br>
<br>
                          Well... there's another thing to take care<br>
            about... you<br>
            need to<br>
                          manage your private list of live wsis<br>
            carefully.  It means<br>
                     managing<br>
                          that list (with any of your locking needed) at the<br>
                     ESTABLISHED /<br>
                          CLOSED callback for the WSI.  That should be<br>
            enough I think.<br>
<br>
<br>
                              The issue is that the 'wait for writeable'<br>
            is oneshot,<br>
                     so after it<br>
                              becomes true in the poll lws will clear<br>
            the POLLOUT<br>
                     wait flag.  But<br>
                              because the threads are asynchronous,<br>
            eventually the<br>
                     other thread<br>
                              will come at the wrong time and set it<br>
            just as we're<br>
                     clearing it<br>
                              with<br>
                              indeterminate results.  Because it's<br>
            read-modify-write<br>
                     and flow<br>
                              control code also modifies this, there's a low<br>
                     probability deadlock<br>
                              case where POLLIN or POLLOUT from one<br>
            thread or another<br>
                     did not get<br>
                              set for wait as the thread thinks it is.<br>
<br>
                              If the extpoll stuff is used the issue can<br>
            be worse<br>
                     depending on<br>
                              what<br>
                              it is doing.<br>
<br>
                              However the approach you're taking is<br>
            otherwise really<br>
                     workable I<br>
                              think.  To make it rock solid you'll need<br>
            to patch<br>
            lws to<br>
                              mutex-protect the few places it changes<br>
            poll wait flags.<br>
<br>
<br>
                          I added a patch<br>
<br>
<br></div></div>
            <a href="http://git.libwebsockets.org/______cgi-bin/cgit/libwebsockets/______commit/?id=______7a1327977ac10bdbace0012274b8ae______889219880e" target="_blank">http://git.libwebsockets.org/_<u></u>_____cgi-bin/cgit/<u></u>libwebsockets/______commit/?<u></u>id=______<u></u>7a1327977ac10bdbace0012274b8ae<u></u>______889219880e</a><br>


            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____commit/?id=____7a1327977ac10bdbace0012274b8ae____889219880e" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____commit/?id=_<u></u>___<u></u>7a1327977ac10bdbace0012274b8ae<u></u>____889219880e</a>><div>

<div class="h5"><br>
<br>
<br>
            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____commit/?id=____7a1327977ac10bdbace0012274b8ae____889219880e" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____commit/?id=_<u></u>___<u></u>7a1327977ac10bdbace0012274b8ae<u></u>____889219880e</a><br>


            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__commit/?id=__7a1327977ac10bdbace0012274b8ae__889219880e" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_commit/?id=__<u></u>7a1327977ac10bdbace0012274b8ae<u></u>__889219880e</a>>><br>


<br>
<br>
<br>
<br>
            <<a href="http://git.libwebsockets.org/____cgi-bin/cgit/libwebsockets/____commit/?id=____7a1327977ac10bdbace0012274b8ae____889219880e" target="_blank">http://git.libwebsockets.org/<u></u>____cgi-bin/cgit/<u></u>libwebsockets/____commit/?id=_<u></u>___<u></u>7a1327977ac10bdbace0012274b8ae<u></u>____889219880e</a><br>


            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__commit/?id=__7a1327977ac10bdbace0012274b8ae__889219880e" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_commit/?id=__<u></u>7a1327977ac10bdbace0012274b8ae<u></u>__889219880e</a>><br>


<br>
<br>
            <<a href="http://git.libwebsockets.org/__cgi-bin/cgit/libwebsockets/__commit/?id=__7a1327977ac10bdbace0012274b8ae__889219880e" target="_blank">http://git.libwebsockets.org/<u></u>__cgi-bin/cgit/libwebsockets/_<u></u>_commit/?id=__<u></u>7a1327977ac10bdbace0012274b8ae<u></u>__889219880e</a><br>


            <<a href="http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=7a1327977ac10bdbace0012274b8ae889219880e" target="_blank">http://git.libwebsockets.org/<u></u>cgi-bin/cgit/libwebsockets/<u></u>commit/?id=<u></u>7a1327977ac10bdbace0012274b8ae<u></u>889219880e</a>>>><br>


<br>
<br>
                          that lets you do this in a lock and unlock<br>
            callback in<br>
            the user<br>
                          code, in one place.<br>
<br>
                          If you're not using threading you don't need<br>
            to do anything<br>
                     new.<br>
<br>
                          -Andy<br>
<br>
<br>
                              -Andy<br>
<br>
                                  BR, Eugene Agafonov.<br>
<br>
                                  On Tuesday 17 December 2013 12:03:43<br>
            Thomas Spitz<br>
                     wrote:<br>
<br>
                                      Hello everyone,<br>
<br>
                                      I am trying to find a way for<br>
            external threads<br>
                     to ask<br>
                                      libwebsocket<br>
<br>
                                  server<br>
<br>
                                      to send websocket message on their<br>
            behalf (as<br>
            they<br>
                                      cannot do it<br>
<br>
                                  directly).<br>
<br>
                                      The only way I see at the present<br>
            is to make a<br>
                     loop with<br>
                                      libwebsocket_service (context,<br>
            VERYSHORTTIME);<br>
                     and test<br>
                                      potential<br>
<br>
                                  incoming<br>
<br>
                                      messages from other threads. This<br>
            is not very<br>
                     elegant as CPU<br>
                                      computes<br>
<br>
                                  all<br>
<br>
                                      the time and not very reliable as<br>
            I should put<br>
                                      VERYSHORTTIME at 0<br>
                                      if<br>
<br>
                                  I<br>
<br>
                                      don't want to miss any message.<br>
<br>
                                      I tried the following modification in<br>
                     test-server.c :<br>
<br>
                                      void *connexionServeur(void *arg) {<br>
<br>
                                          struct libwebsocket_context<br>
            *context = arg;<br>
                     while (1) {<br>
<br></div></div>
              if(libwebsocket_service(______<u></u>context,<div class="im"><br>
            60000)<0){<br>
<br>
<br>
                                          printf("Problem!\n");<br>
<br>
                                          break; } }<br>
<br>
                                          return NULL ;<br>
<br>
                                          } int statut_Thread; pthread_t<br>
            Thread;<br>
                     statut_Thread =<br>
                                          pthread_create(&Thread, NULL,<br>
            connexionServeur,<br>
<br>
                                  context<br>
<br>
                                          );<br>
<br>
                                          if (statut_Thread != 0) {<br>
<br>
                                          printf("Problem with thread<br>
            creation \n");<br>
                     } while<br>
                                          (n >= 0 &&<br>
                                          !force_exit) { struct timeval tv;<br>
                     gettimeofday(&tv,<br>
                                          NULL); if<br>
                                          (((unsigned int)tv.tv_usec -<br>
            oldus) ><br>
            50000) {<br>
<br>
<br>
<br>
<br></div>
            libwebsocket_callback_on______<u></u>_writable_all_protocol(&______<u></u>protocols[PROTOCOL_DUMB_I<div class="im"><br>
<br>
<br>
<br>
<br>
<br>
                          NCREMENT]); oldus = tv.tv_usec;<br>
<br>
                                          libwebsocket_service(context, 0);<br>
<br>
                                          }<br>
<br>
                                          usleep(10000); }<br>
<br>
<br>
                                      but it crashes quickly as soon as<br>
            I try<br>
            drawing . I<br>
                                      suppose that<br>
                                      libwebsocket_service is called<br>
            twice at the<br>
                     same time and it<br>
                                      creates conflicts that lead to<br>
            stop crash the<br>
                     websocket.<br>
<br>
                                      I have seen in the mailing list<br>
            that Thomas<br>
                     Koeper has<br>
                                      already<br>
                                      had a similar question<br>
<br>
<br></div>
            <a href="http://ml.libwebsockets.org/______pipermail/libwebsockets/__2013-____April/000403.html" target="_blank">http://ml.libwebsockets.org/__<u></u>____pipermail/libwebsockets/__<u></u>2013-____April/000403.html</a><br>


            <<a href="http://ml.libwebsockets.org/____pipermail/libwebsockets/2013-____April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>___pipermail/libwebsockets/<u></u>2013-____April/000403.html</a>><div class="im">

<br>
<br>
<br>
            <<a href="http://ml.libwebsockets.org/____pipermail/libwebsockets/2013-____April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>___pipermail/libwebsockets/<u></u>2013-____April/000403.html</a><br>


            <<a href="http://ml.libwebsockets.org/__pipermail/libwebsockets/2013-__April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>_pipermail/libwebsockets/2013-<u></u>__April/000403.html</a>>><br>


<br>
<br>
<br>
<br>
            <<a href="http://ml.libwebsockets.org/____pipermail/libwebsockets/2013-____April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>___pipermail/libwebsockets/<u></u>2013-____April/000403.html</a><br>


            <<a href="http://ml.libwebsockets.org/__pipermail/libwebsockets/2013-__April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>_pipermail/libwebsockets/2013-<u></u>__April/000403.html</a>><br>


<br>
<br>
            <<a href="http://ml.libwebsockets.org/__pipermail/libwebsockets/2013-__April/000403.html" target="_blank">http://ml.libwebsockets.org/_<u></u>_pipermail/libwebsockets/2013-<u></u>__April/000403.html</a><br>


            <<a href="http://ml.libwebsockets.org/pipermail/libwebsockets/2013-April/000403.html" target="_blank">http://ml.libwebsockets.org/<u></u>pipermail/libwebsockets/2013-<u></u>April/000403.html</a>>>><br>


<br>
<br>
<br>
<br>
                          . At that time, he was thinking of creating a<br>
            mutex on<br>
<br>
                                      libwebsocket_service. Do you think<br>
            this is the<br>
                     right<br>
                                      solution?<br>
<br>
                                      Thanks in advance for your help<br>
            Best regards,<br>
                     Thomas<br>
<br>
<br></div>
              ______________________________<u></u>_______________________<br>
                     Libwebsockets<br>
                                  mailing list<br>
            Libwebsockets@ml.__<a href="http://libwebsocke____ts.org" target="_blank">libwebsocke<u></u>____ts.org</a><br>
            <<a href="http://libwebsocke__ts.org" target="_blank">http://libwebsocke__ts.org</a>><br>
                     <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                                  <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.<br>
            <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.>__<a href="http://lib__websockets.org" target="_blank">li<u></u>b__websockets.org</a><div class="im"><br>
            <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                     <mailto:<a href="mailto:Libwebsockets@ml." target="_blank">Libwebsockets@ml.</a>__<a href="http://libwebsockets.org" target="_blank">lib<u></u>websockets.org</a><br>
            <mailto:<a href="mailto:Libwebsockets@ml.libwebsockets.org" target="_blank">Libwebsockets@ml.<u></u>libwebsockets.org</a>>>><br></div>
            <a href="http://ml.libwebsockets.org/______mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/__<u></u>____mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>>><br>
<br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><div class="im"><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a> <<a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/<u></u>mailman/listinfo/libwebsockets</a><u></u>>__>__><br>


<br>
<br>
<br></div>
              ______________________________<u></u>_______________________<br>
                     Libwebsockets<br>
                              mailing<br>
                              list<br>
            Libwebsockets@ml.__<a href="http://libwebsocke____ts.org" target="_blank">libwebsocke<u></u>____ts.org</a><br>
            <<a href="http://libwebsocke__ts.org" target="_blank">http://libwebsocke__ts.org</a>><br>
                     <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                              <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.<br>
            <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.>__<a href="http://lib__websockets.org" target="_blank">li<u></u>b__websockets.org</a><div class="im"><br>
            <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                     <mailto:<a href="mailto:Libwebsockets@ml." target="_blank">Libwebsockets@ml.</a>__<a href="http://libwebsockets.org" target="_blank">lib<u></u>websockets.org</a><br>
            <mailto:<a href="mailto:Libwebsockets@ml.libwebsockets.org" target="_blank">Libwebsockets@ml.<u></u>libwebsockets.org</a>>>><br></div>
            <a href="http://ml.libwebsockets.org/______mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/__<u></u>____mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>>><br>
<br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><div class="im"><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a> <<a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/<u></u>mailman/listinfo/libwebsockets</a><u></u>>__>__><br>


<br>
<br>
<br></div>
              ______________________________<u></u>_______________________<br>
                          Libwebsockets mailing list<br>
                          Libwebsockets@ml.__<a href="http://libwebsocke____ts.org" target="_blank">libwebsocke<u></u>____ts.org</a><br>
            <<a href="http://libwebsocke__ts.org" target="_blank">http://libwebsocke__ts.org</a>><br>
                     <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                          <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.<br>
            <mailto:<a href="mailto:Libwebsockets@ml" target="_blank">Libwebsockets@ml</a>.>__<a href="http://lib__websockets.org" target="_blank">li<u></u>b__websockets.org</a><div class="im"><br>
            <<a href="http://libwebsockets.org" target="_blank">http://libwebsockets.org</a>><br>
                     <mailto:<a href="mailto:Libwebsockets@ml." target="_blank">Libwebsockets@ml.</a>__<a href="http://libwebsockets.org" target="_blank">lib<u></u>websockets.org</a><br>
            <mailto:<a href="mailto:Libwebsockets@ml.libwebsockets.org" target="_blank">Libwebsockets@ml.<u></u>libwebsockets.org</a>>>><br></div>
            <a href="http://ml.libwebsockets.org/______mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/__<u></u>____mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>>><br>
<br>
<br>
            <<a href="http://ml.libwebsockets.org/____mailman/listinfo/__libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>___mailman/listinfo/__<u></u>libwebsockets</a><div class="im"><br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a>><br>
<br>
            <<a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/_<u></u>_mailman/listinfo/<u></u>libwebsockets</a> <<a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/<u></u>mailman/listinfo/libwebsockets</a><u></u>>__>__><br>


<br>
<br>
<br>
<br></div><div class="im">
        ______________________________<u></u>___________________<br>
        Libwebsockets mailing list<br>
        Libwebsockets@ml.__<a href="http://libwebsockets.org" target="_blank">libwebsocke<u></u>ts.org</a><br>
        <mailto:<a href="mailto:Libwebsockets@ml.libwebsockets.org" target="_blank">Libwebsockets@ml.<u></u>libwebsockets.org</a>><br>
        <a href="http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/__<u></u>mailman/listinfo/libwebsockets</a><br>
        <<a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets" target="_blank">http://ml.libwebsockets.org/<u></u>mailman/listinfo/libwebsockets</a><u></u>><br>
<br>
<br>
<br>
</div></blockquote>
<br>
</blockquote></div><br></div>