2

I'm having libwebsocket client to send binary data. I've saved my binary data into a buffer [i.e. buf] and writing to socket.

n = libwebsocket_write(wsi,
       &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_BINARY);

My problem is socket is able to write 22392 bytes only [As I've received n= 22392]. when my l >=22392 bytes it is noticed as partial write. This value varies for various architectures.

Question:

  • Is there any limitation of data size in libwebsocket?

  • Any way to extend the buffer payload value?

Pushpa
  • 892
  • 1
  • 12
  • 30
  • Thanks @ David Schwartz. I'm a newbie to libwebsocket. I would also like to know what prevents for writing more bytes than 22392 bytes. – Pushpa May 04 '15 at 10:53
  • If you set a socket to non-blocking, partial writes will occur when the socket would block if it tried to send more. If you set a socket to non-blocking and then try to write to it, obviously you *must* handle partial writes. – David Schwartz May 04 '15 at 11:08

2 Answers2

6

I got the answer for my question. That is the payload value which prevented my data. Adjust the payload value according to the data size.

struct libwebsocket_protocols {
   const char * name;
   callback_function * callback;
   size_t per_session_data_size;
   size_t rx_buffer_size;
   unsigned int id;
   struct libwebsocket_context * owning_server;
   int protocol_index;
};

rx_buffer_size
you should set this to the size of the biggest legal frame that you support.

After setting rx_buffer_size to 65536 I'm able to write data completely.

Here's links helped to me
https://libwebsockets.org/trac/libwebsockets/ticket/40
https://libwebsockets.org/libwebsockets-api-doc.html

Pushpa
  • 892
  • 1
  • 12
  • 30
0

Either don't use this library or use blocking sockets. This library provides no sane way to do non-blocking I/O. To do sane non-blocking I/O, on a partial write, you need to know how many bytes were sent and how many are left to send so you can try to write the rest when the socket is writable again. But this library provides you only one number. There is no way to tell both things.

(You can't just subtract from the number of bytes you wanted to send because the encoded data is larger than the unencoded data.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • This makes my program delay as I need to send data chunk by chunk with the limited size. Any possibility of extending the payload size? – Pushpa May 04 '15 at 11:02
  • It really has almost nothing to do with the payload size. You could encounter this problem even with a one byte payload. It's a problem with using libwebsocket with non-blocking sockets. If you don't see it with smaller payloads, you're just getting lucky. Either use blocking sockets or use a better websocket library. – David Schwartz May 04 '15 at 11:07
  • so, which library do you recommend, then? – br1 Feb 13 '18 at 15:33