[Libwebsockets] Synchronous API wrapping libwebsockets?

Duy Lan Nguyen ndlan2k at gmail.com
Sun Nov 9 02:17:12 CET 2014


Hello,

Appreciate if you could help. I'm trying to implement a simple Synchronous
API wrapping libwebsockets (for client side, not server side), roughly as
follows.

MYSOCK = Connect(...);

STATUS Send(MYSOCK, char* pbData, uint cbData, uint dwFlags);

STATUS Receive(MYSOCK, char* pbData, uint cbData, uint dwFlags);


Close(MYSOCK);



DO YOU HAVE any ADVICE on HOW to do that most efficiently?



I actually have the following design


A global queue of RecvRequest struct

A global queue of SendRequest struct


Connect():

Create a separate thread

- calling libwebsocket_create_context()

- calling libwebsocket_client_connect()

- running the loop libwebsocket_service()


Close():

Just shutdown the above thread and callings properly


Send():

Add its SendRequest struct to the queue

Call libwebsocket_callback_on_writable()

Wait for its event SendRequest.CompleteEvent

return STATUS


Receive():

Add its RecvRequest struct to the queue

Wait for its event RecvRequest.CompleteEvent

return STATUS


CallBack():

case LWS_CALLBACK_CLIENT_WRITEABLE:

- Throw the previous SendRequest.CompleteEvent, if there is any.

- Call libwebsocket_write() to send current SendRequest.Data in the queue,
if there is any.

- Remove the current SendRequest in the queue to be the "previous" one.

- Call libwebsocket_callback_on_writable()

case LWS_CALLBACK_CLIENT_RECEIVE:

- Pass received data to RecvRequest.Data in the queue, if there is any.

- Remove the current RecvRequest in the queue.

- Throw the its RecvRequest.CompleteEvent.



DO YOU have any COMMENT on this design?



Many thanks,

Lan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20141108/0cb6a4f0/attachment-0001.html>


More information about the Libwebsockets mailing list