This has been killing me for a week ![Frown :( :(](/styles/default/xenforo/vbSmilies/Normal/frown.gif)
I have a C++ web-sockets server using Poco library.
The socket needs to be blocking for websockets but I set a fairly short timeout. The problem is the socket blocks on send, sometimes far longer than the specified timeout which is when things screw up.
This websocket server receives a UDP data feed that contains various events. An event listener class/lib listens to these events, and there is a subscriber mechanism with a function callback so when a certain event arrives in the data feed a method in the subscribing class is triggered passing on the data to the callback. The websocket should basically pass on the data from the listener/callback to the client.
The problem is if this callback doesn't return then the whole subscription/listener will hang (it just loops over all subscription callbacks). So when the send on socket blocks any other events coming through the data feed won't trigger the callback. When the send blocks for long the socket is closed, when the socket is closed my whole websocket handler is deleted and the callback function will thus never return.
To resolve this I tried making the callback spawn a new thread to do the sending so the callback always returns. The issue here is when the websocket handler gets deleted any thread that is blocked sending will crash the server because the socket has been deleted.
So how can I deal with a socket that sometimes blocks for far longer than the timeout?
![Frown :( :(](/styles/default/xenforo/vbSmilies/Normal/frown.gif)
I have a C++ web-sockets server using Poco library.
The socket needs to be blocking for websockets but I set a fairly short timeout. The problem is the socket blocks on send, sometimes far longer than the specified timeout which is when things screw up.
This websocket server receives a UDP data feed that contains various events. An event listener class/lib listens to these events, and there is a subscriber mechanism with a function callback so when a certain event arrives in the data feed a method in the subscribing class is triggered passing on the data to the callback. The websocket should basically pass on the data from the listener/callback to the client.
The problem is if this callback doesn't return then the whole subscription/listener will hang (it just loops over all subscription callbacks). So when the send on socket blocks any other events coming through the data feed won't trigger the callback. When the send blocks for long the socket is closed, when the socket is closed my whole websocket handler is deleted and the callback function will thus never return.
To resolve this I tried making the callback spawn a new thread to do the sending so the callback always returns. The issue here is when the websocket handler gets deleted any thread that is blocked sending will crash the server because the socket has been deleted.
So how can I deal with a socket that sometimes blocks for far longer than the timeout?