<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Oopz, sorry, other email client does wrong email on reply....<br>
    <div class="moz-forward-container"><br>
      -------- Original Message --------
      <table class="moz-email-headers-table" cellpadding="0"
        cellspacing="0" border="0">
        <tbody>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">Subject:
            </th>
            <td>Re: [Libwebsockets] How to send data asynchronously to
              the client?</td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">Date: </th>
            <td>Thu, 26 Jun 2014 21:45:24 +0200</td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">From: </th>
            <td>Bas Appelo <a class="moz-txt-link-rfc2396E" href="mailto:bas@appelo.nl"><bas@appelo.nl></a></td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">To: </th>
            <td>Mike M <a class="moz-txt-link-rfc2396E" href="mailto:andy.green@linaro.org"><andy.green@linaro.org></a></td>
          </tr>
        </tbody>
      </table>
      <br>
      <br>
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      Hi All,
      <div><br>
      </div>
      <div>Two things, first the cancel is not thread-unsafe, it nicely
        writes to a pipe which is being polled, so I will keep using
        this, and consider it thread safe.</div>
      <div><br>
      </div>
      <div>But :(</div>
      <div><br>
      </div>
      <div>I donít really understand what Andy means with this:</div>
      <div>
        <blockquote type="cite">
          <div bgcolor="#FFFFFF" text="#000000">
            <blockquote cite="mid:53A42DBD.2070301@appelo.nl"
              type="cite">
              <blockquote cite="mid:53A42B64.9090004@mordent.com"
                type="cite">
                <blockquote cite="mid:53A4293C.9070507@appelo.nl"
                  type="cite">
                  <blockquote cite="mid:53A4269F.7030108@mordent.com"
                    type="cite">
                    <pre>>Libwebsockets is basically single-threaded.  But, with care from another 
>server thread, you can call either</pre>
                  </blockquote>
                </blockquote>
              </blockquote>
            </blockquote>
          </div>
        </blockquote>
        <div>What does ĎWith careí means, I rewrote my code so that it
          doesnít call service_cancel, but skips that and now does † †
          libwebsocket_callback_on_writable(context, wsi) from what I
          called thread-2. This works! But only the first time (could be
          because then there is also other trafficÖ) But in a quite
          environment, I can see it does something, but Iím not
          receiving LWS_CALLBACK_SERVER_WRITEABLE.</div>
        <div><br>
        </div>
        <div>Iím doing this, because Iím still having issues with my
          first approach, it works like charm in a single
          libwebsocket_context, but if I create two, Iím not receiving
          any LWS_CALLBACK_SERVER_WRITEABLE, with both approaches
          (cancel thread-2 libwebsocket_callback_on_writable thread-1 or
          libwebsocket_callback_on_writable thread -2)</div>
        <div><br>
        </div>
        <div>If you would like any additional info (like a small prog
          that reproduces this, or the dump logging (which donít tell me
          much, no errors)) please ask!</div>
        <div><br>
        </div>
        <div>Or am I expecting too much threading functionality?</div>
        <div><br>
        </div>
        <div>Iím halfway debugging, but kinda stuck :(</div>
        <div><br>
        </div>
        <div>Thanks!</div>
        <div>Bas</div>
        <div><br>
        </div>
        <div>
          <div>
            <div>On 20 Jun 2014, at 16:05, Mike M <<a
                moz-do-not-send="true"
                href="mailto:andy.green@linaro.org">andy.green@linaro.org</a>>
              wrote:</div>
            <br class="Apple-interchange-newline">
            <blockquote type="cite">
              <meta content="text/html; charset=windows-1252"
                http-equiv="Content-Type">
              <div bgcolor="#FFFFFF" text="#000000"> Hi,<br>
                <br>
                After looking at the source for cancel, it appears that
                it uses data from the internal structure.† So, it's not
                really safe to call it from another thread.† If I want
                to make all this threadsafe, I need to lock in the
                service thread before calling service, and also lock in
                the other thread before I call cancel.† This will
                produce a deadlock because service is waiting forever
                and already locked, so cancel will never happen.<br>
                <br>
                -m<br>
                <br>
                <div class="moz-cite-prefix">On 06/20/2014 08:49 AM, Bas
                  Appelo wrote:<br>
                </div>
                <blockquote cite="mid:53A42DBD.2070301@appelo.nl"
                  type="cite">
                  <meta content="text/html; charset=windows-1252"
                    http-equiv="Content-Type">
                  Ah, yes, I'm on the development version, I can
                  recommend using it, it has some extra (badly needed as
                  you discovered) functions, but I did not discover any
                  drawback yet, I do consider it stable, I'm using it
                  quite a lot: <br>
                  <br>
                  <pre class="wiki">git clone <a moz-do-not-send="true" href="git://git.libwebsockets.org/libwebsockets">git://git.libwebsockets.org/libwebsockets</a>

</pre>
                  Good luck!<br>
                  <div class="moz-cite-prefix"><br>
                    On 06/20/2014 02:39 PM, Mike M wrote:<br>
                  </div>
                  <blockquote cite="mid:53A42B64.9090004@mordent.com"
                    type="cite">
                    <meta content="text/html; charset=windows-1252"
                      http-equiv="Content-Type">
                    Unfortunately, that call doesn't seem to exist in
                    the version I'm using.† What version is the latest
                    stable version?<br>
                    <br>
                    Thanks!<br>
                    -m<br>
                    <br>
                    <div class="moz-cite-prefix">On 06/20/2014 08:29 AM,
                      Bas Appelo wrote:<br>
                    </div>
                    <blockquote cite="mid:53A4293C.9070507@appelo.nl"
                      type="cite">
                      <meta content="text/html; charset=windows-1252"
                        http-equiv="Content-Type">
                      thread-1:<br>
                      ††† libwebsocket_service(context, LARGE_NUMBER);
                      //blocks<br>
                      <br>
                      thread-2:<br>
                      ††† libwebsocket_cancel_service(context); // wakes
                      up thread-1<br>
                      <br>
                      thread-1:<br>
                      ††† libwebsocket_callback_on_writable(context,
                      wsi);<br>
                      ††† libwebsocket_service(context, LARGE_NUMBER);
                      //blocks <= will call the callback!<br>
                      <br>
                      Now you will receive a callback, it works like a
                      charm.<br>
                      So to write data you'll need to be in a callback,
                      but the way above shows you how to get
                      there/enforce one.<br>
                      <br>
                      <br>
                      <div class="moz-cite-prefix">On 06/20/2014 02:18
                        PM, Mike M wrote:<br>
                      </div>
                      <blockquote
                        cite="mid:53A4269F.7030108@mordent.com"
                        type="cite">
                        <meta http-equiv="content-type"
                          content="text/html; charset=windows-1252">
                        <pre>>><i> Hi,
</i>>><i>
</i>>><i> 1)Is there a way to asynchronously send the data to the client? That is,
</i>>><i> without being in a callback, server spontaneously initiating data
</i>>><i> transfer to the client..!!
</i>>
>Libwebsockets is basically single-threaded.  But, with care from another 
>server thread, you can call either
>
>libwebsocket_callback_on_writable(), or
>libwebsocket_callback_on_writable_all_protocol()
>
>but you need to manage your own list of live wsi / connections with the 
>first option (by tracking their creating and destruction in the 
>ESTABLISHED and CLOSED callbacks).
>
>You will get a LWS_CALLBACK_SERVER_WRITABLE as soon as it's possible to 
>write on that connection then, and you should figure out what to write 
>and write it from there.
>
>-Andy


</pre>
                        <small>Hi,<br>
                          <br>
                          I am doing exactly this, but it doesn't work.<br>
                          <br>
                          I have a thread that calls
                          libwebsocket_service with a huge timeout.†
                          Writes, etc, are only ever done from within
                          the callback.† When I need to write something
                          from another thread, I queue up the message
                          that needs to be written and then immediately
                          call
                          libwebsocket_callback_on_writeable_all_protocol
                          on the proto I'm writing for.<br>
                          <br>
                          Unfortunately, I never get
                          LWS_CALLBACK_SERVER_WRITEABLE until the
                          libwebsocket_service times out and gets called
                          again.† This is totally broken.† I saw a trac
                          issue (#56) about using ppoll.† Unfortunately,
                          the version of the library we're using doesn't
                          seem to have that code in it.† Any ideas?<br>
                          <br>
                          Thanks,<br>
                          -m<br>
                          <br>
                        </small> <br>
                        <fieldset class="mimeAttachmentHeader"></fieldset>
                        <br>
                        <pre wrap="">_______________________________________________
Libwebsockets mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Libwebsockets@ml.libwebsockets.org">Libwebsockets@ml.libwebsockets.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
</pre>
                      </blockquote>
                      <br>
                      <br>
                      <fieldset class="mimeAttachmentHeader"></fieldset>
                      <br>
                      <pre wrap="">_______________________________________________
Libwebsockets mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Libwebsockets@ml.libwebsockets.org">Libwebsockets@ml.libwebsockets.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
</pre>
                    </blockquote>
                    <br>
                    <br>
                    <fieldset class="mimeAttachmentHeader"></fieldset>
                    <br>
                    <pre wrap="">_______________________________________________
Libwebsockets mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Libwebsockets@ml.libwebsockets.org">Libwebsockets@ml.libwebsockets.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
</pre>
                  </blockquote>
                  <br>
                  <br>
                  <fieldset class="mimeAttachmentHeader"></fieldset>
                  <br>
                  <pre wrap="">_______________________________________________
Libwebsockets mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Libwebsockets@ml.libwebsockets.org">Libwebsockets@ml.libwebsockets.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
</pre>
                </blockquote>
                <br>
              </div>
              _______________________________________________<br>
              Libwebsockets mailing list<br>
              <a moz-do-not-send="true"
                href="mailto:Libwebsockets@ml.libwebsockets.org">Libwebsockets@ml.libwebsockets.org</a><br>
              <a class="moz-txt-link-freetext" href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a><br>
            </blockquote>
          </div>
          <br>
        </div>
      </div>
      <br>
    </div>
    <br>
  </body>
</html>