dhbaird / easywsclient

A short and sweet WebSocket client for C++
MIT License
747 stars 204 forks source link

wait/poll function? #23

Closed donpillou closed 10 years ago

donpillou commented 10 years ago

Hello and thank you for sharing easywsclient,

right now easywsclient does not allow us to wait for arriving data via a nonblocking recv or select/poll. I guess some projects might be ok with that, but actually this is kinda illogical. The point of Websockets is to avoid active polling and hence, it would be nice if easywsclient provides a mechanism (e.g. a blocking function) to wait for data.

Best regards, Donald Pillou

dhbaird commented 10 years ago

@donpillou - this is a very good suggestion. When waiting to recv() a message, the solution is very simple, for example just use select() to block. However, that alone is not good enough when the user wants to interrupt the blocking to send() a message. Since select() is blocked, that means the only way to wake it up is from another thread, and so we'd have to consider a thread-safe way to wake up select(). So, here is a proposal:

  1. Change void poll() signature to void poll(int timeout = 0) (timeout in milliseconds) or void poll(bool block = false). Then, to block, call poll(1000) // the timeout variation or poll(true) // the bool variation.
  2. Add a special thread-safe function void wakeup() which can be called from another thread to interrupt void poll(...) so that the non-thread-safe send(...) function can then be used. In UNIX, an easy way to achieve this could be with pipe(), but in Windows, a different mechanism would have to be used.

So, I'm all in favor with going ahead with this, but there needs to be a little planning. Can you review this proposal and give your feedback? I'm eager to accept patches, or I can try implementing it on my own as well. Thanks!

dhbaird commented 10 years ago

@donpillou - take a look at 81ed98ca9e8fadcf9b268a44c25d1e0b3a679a71 and let us know if it helps. It implements only the timeout, but there is currently no way to wake it up from sleeping if a send() is needed, and thus send() can only be used after the timeout expires. It should work great for recv()-only use cases. It's on my wishlist to do a better job of this.

donpillou commented 10 years ago

Hello, I really like the 81ed98c patch. It is pretty much exactly what I have been thinking of.

donpillou commented 10 years ago

To avoid unnecessary data send delays, it might be a good idea to move the "send block" in poll before the "select bock". :)

donpillou commented 10 years ago

Nevermind, please forget the previous comment. I just realized that if someone is sending data, the call to select will return immediately and hence there is no delay.

dhbaird commented 10 years ago

Cool, I'll close this ticket for now.

oboolean commented 6 years ago

@donpillou I just realized that if someone is sending data, the call to select will return immediately and hence there is no delay. how to do that