[Libwebsockets] How to send messages frequently ?

Andy Green andy at warmcat.com
Sun Jan 22 22:25:56 CET 2017



On 22 January 2017 22:57:51 GMT+08:00, hanguofu <hanguofu at 163.com> wrote:
>
>
>
>
>At 2017-01-22 07:38:03, "Andy Green" <andy at warmcat.com> wrote:
>>
>>
>>On 22 January 2017 04:15:02 GMT+08:00, hanguofu <hanguofu at 163.com>
>wrote:
>>>I develop an application based on example libwebsockets-test-client.
>>>The application has 2 threads : in main thread there is routine
>>>callback_dumb_increment  and the message loop : lws_service(context,
>50
>>>);
>>>
>>>
>>>At the other thread , the application send messages with
>>>lws_callback_on_writable.
>>>Unfortunately , the interval of message vary much and it may be as 
>>>short as 1 millisecond sometimes.
>>
>>On the network, with tcp, you are not really in control of sending
>stuff like that.
>>
>>Once the tcp window is filled, your kernel will not accept to buffer
>anything more, let alone send it.
>>
>>The reality is the receiver and every step between decides when you
>may send something.  You can 'have something new to send every ms' but
>there's nothing to say you will be able to send it within any time
>limit.
>>
>>So there's maybe a conceptual problem here between how tcp network
>programming really works and how you are thinking about what you are
>trying to do.  Udp lets you do this kind of sending but ws is tcp only,
>and udp offers no guarantee your packets will actually arrive.
>>
>Hi , Andy : 
>My application may generates the sending messages at short interval ,
>but the through-put is quite low : 1024byte/s at most.
>Both large latency  and send-in-wrong-order are O.K. to the messages ,

Oh... well if you don't care about latency, look at the mirror protocol on the test server.

He generates a lot of data quickly when you draw on the canvas and faces the same situation that he may not be able to send it to one or more of the clients quickly enough.  The code uses a fifo with a common head but tail counters are managed individually for each client; the individual clients draw down the common fifo data at their own rate.

>as long as they reach the other side completely.  
>>>Is it a must to change the message loop interval to 
>>>lws_service(context, 1 ) ? I found out some messages are dropped 
>>>without any notice if  the "Timeout for poll" is large .
>>
>>What is your platform and lws version?
>>
>>Are we using ssl?
>>
>>What do we mean 'messages are dropped', it means the ones you are
>sending?
>>
>>-Andy
>>
>I am running the test in win10 and the lws version is v2.1.0
>Currently I only play with ws and in the future I will need ssl.
>Yes . I meant the sending messages . Before the application thread
>calls  lws_callback_on_writable , 
>it will print out the sending message. Likewise , the callback function
>LWS_CALLBACK_CLIENT_WRITEABLE
>at the main thread will print out the sending message too.
>Unfortunately , some of the sending messages never appears at the
>print-out of main thread , 

On Linux, when you do something to change the poll wait, a pipe is used to interrupt the wait as a NOP.  When it goes around the loop and enters poll() again, it waits on the new list of flags immediately.  I dunno what the windows version does, but you might want to check lws master.

>which means they are dropped btwn. the two threads.

It sounds like you stay in poll() with the old wait conditions until the poll timeout ends.

>I am wondering if it is the right way to feed lws routine with the
>sending messages from the other thread:
>I create my own per_session_data structure :
>struct per_session_data__dumb_increment {
> char msg_to_other[1024] ; // this is the message sent to peer.
>};

It's right but you need to introduce a mirror style fifo.  The problem is two or more events are coming before you could send something.

And unless it can never happen with your protocol, you should think about dealing with multiple clients consuming the data at their own rate, again like mirror protocol.

>and pass the pointer to this per_session_data__dumb_increment  and the
>pointer to the wsi ( only 1 wsi in the whole app. )
>to the applicaion thread so that the thread can call
>lws_callback_on_writable .
>When the application wants to send a message, it will fill out the
>per_session_data__dumb_increment and calls 
>lws_callback_on_writable at the other thread. 
>Could you please shed some light on it ?

It's ok... just look at mirror protocol in the test server.

-Andy

>Thankyou !
>
>Guofu Han

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.



More information about the Libwebsockets mailing list