[Libwebsockets] Problems with big messages in custom protocol

"Andy Green (林安廸)" andy at warmcat.com
Sat Mar 15 03:03:37 CET 2014

On 14/03/14 16:35, the mail apparently from Mathias Rudolph included:
> Hej,
> I have a problem where the spill for big messages does not work properly.
> What I'm doing is, running two instances of my binary (both make use of the
> lib) on the same machine. One is executed on the console (openSuse x64) and
> the second is created from within with posix_spawn.

Sorry what should I understand from "created from within with 
posix_spawn", you create a child process fork-style and run a second 
instance of your app there?

Do these guys share file descriptors?

> The one within is only an echo server that allows me to test the
> functionality. The 'outer' instance runs some tests.
> So, my problem now is. If I send a message that is bigger than the rx
> buffer, it spills, but only as long as it is not more than 3x bigger than
> the rx buffer.

Hm.  If he finds you're giving him something to send the OS won't accept 
in one lump, he mallocs a buffer of the right size.  I don't think 
there's any code thinking about a 3 x limit.  So this might mean some 
bug around sequencing emptying this "emergency buffer".

On Linux, the OS reserves up to 2 x the receive buffer size for send on 
the socket.  Between 1 x and 2 x it may not need to use the "emergency 
buffer" malloc scheme.

The "emergency buffer" stuff should work, but it's better to only send 
things below the size of the rx buffer, which will definitely go through 
(on Linux anyway) if the socket is signalled writeable.

> My receive callback section looks like this:
>> remaining = libwebsockets_remaining_packet_payload(wsi);
>> std::copy((const char *) in, (const char *) in + len,
> std::back_inserter(rcvMessage));
>> if(remaining == 0) {
>>    std::string msg = std::string(reinterpret_cast< char const*
>> (rcvMessage.data()), rcvMessage.size());
>>    doSomething(msg);
>>    rcvMessage.clear();
>> }
> Where recvMessage is a std::vector<unsigned char>
> My send section:
>>   transmitBuffer = std::make_shared<std::vector<unsigned char>>();
>>   // make room for pre padding
>>   for(unsigned int idx = 0; idx < LWS_SEND_BUFFER_PRE_PADDING; idx++) {
>>     transmitBuffer->push_back((unsigned char)0);
>>   }

This is all horribly inefficient ^^

>>   // append actual message
>>   std::copy(message.begin(), message.end(),
> std::back_inserter(*transmitBuffer));
>>   // append post padding
>>   for(unsigned int idx = 0; idx < LWS_SEND_BUFFER_POST_PADDING; idx++) {
>>     transmitBuffer->push_back((unsigned char)0);
>>   }
>>   libwebsocket_write(wsi,
> transmitBuffer->data()+LWS_SEND_BUFFER_PRE_PADDING, message.size(),
> The transmitBuffer exists as long as not a new message is send or the
> sender side is shut down.
> My debug log looks like this:
>> Sender: sent buffer size: 20027
>> Recevier: Received data size: 4096, remaining 15909
>> Recevier: Received data size: 4096, remaining 11813
>> Recevier: Received data size: 4096, remaining 7717
> And thats it. I tried to debug it, and it looks like the library is going
> into an infinite loop.

Where is it stuck, and in which instance is it stuck?

> I tried different sizes of the rx buffer but that makes no difference.
> Is there something I missed?

I dunno I don't really understand what your code does yet, or which 
library code it is having problems with.


> Regards
> Mathias
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list