1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Make WS non blocking and handle timeout.

Implement a few helper methods as well.
This commit is contained in:
Sandu Liviu Catalin 2021-09-05 13:27:19 +03:00
parent 05e644b040
commit 1a11dd777e
2 changed files with 45 additions and 2 deletions

View File

@ -33,6 +33,8 @@ void Register_POCO_Net(HSQUIRRELVM vm, Table &)
.FmtFunc(_SC("SendStringFrame"), &WsClient::SendStringFrame) .FmtFunc(_SC("SendStringFrame"), &WsClient::SendStringFrame)
.Func(_SC("RecvFrame"), &WsClient::RecvFrame) .Func(_SC("RecvFrame"), &WsClient::RecvFrame)
.Func(_SC("RecvStringFrame"), &WsClient::RecvStringFrame) .Func(_SC("RecvStringFrame"), &WsClient::RecvStringFrame)
.CbFunc(_SC("RecvFrameIn"), &WsClient::RecvFrameIn)
.CbFunc(_SC("RecvStringFrameIn"), &WsClient::RecvStringFrameIn)
); );
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
RootTable(vm).Bind(_SC("SqNet"), ns); RootTable(vm).Bind(_SC("SqNet"), ns);

View File

@ -67,6 +67,7 @@ struct WsClient
mRequest(Poco::Net::HTTPRequest::HTTP_GET, uri.ToStr(), Poco::Net::HTTPRequest::HTTP_1_1), mResponse(), mRequest(Poco::Net::HTTPRequest::HTTP_GET, uri.ToStr(), Poco::Net::HTTPRequest::HTTP_1_1), mResponse(),
mWebSocket(mClient, mRequest, mResponse), mBuffer(0), mFlags(0), mState(0) mWebSocket(mClient, mRequest, mResponse), mBuffer(0), mFlags(0), mState(0)
{ {
mWebSocket.setBlocking(false); // Disable blocking
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -106,7 +107,11 @@ struct WsClient
LightObj RecvFrame() LightObj RecvFrame()
{ {
// Attempt to receive data // Attempt to receive data
mState = mWebSocket.receiveFrame(mBuffer, mFlags); try {
mState = mWebSocket.receiveFrame(mBuffer, mFlags);
} catch (const Poco::TimeoutException &) {
return LightObj{}; // We handle timeout so we can be non blocking
}
// If something was returned // If something was returned
if (mState != 0) if (mState != 0)
{ {
@ -131,7 +136,11 @@ struct WsClient
LightObj RecvStringFrame() LightObj RecvStringFrame()
{ {
// Attempt to receive data // Attempt to receive data
mState = mWebSocket.receiveFrame(mBuffer, mFlags); try {
mState = mWebSocket.receiveFrame(mBuffer, mFlags);
} catch (const Poco::TimeoutException &) {
return LightObj{}; // We handle timeout so we can be non blocking
}
// If something was returned // If something was returned
if (mState != 0) if (mState != 0)
{ {
@ -146,6 +155,38 @@ struct WsClient
return LightObj{}; return LightObj{};
} }
/* --------------------------------------------------------------------------------------------
* Receives a frame from the socket and return it as a buffer. Only invokes callback if response is valid.
* The frame's payload size must not exceed the maximum payload size set with SetMaxPayloadSize().
*/
SQInteger RecvFrameIn(Function & cb)
{
auto obj = RecvFrame();
// Only invoke the callback if we have a valid response
if (mState != 0 || mFlags != 0)
{
cb(obj, mState, mFlags);
}
// Return result
return mState;
}
/* --------------------------------------------------------------------------------------------
* Receives a frame from the socket and return it as a string. Only invokes callback if response is valid.
* The frame's payload size must not exceed the maximum payload size set with SetMaxPayloadSize().
*/
SQInteger RecvStringFrameIn(Function & cb)
{
auto obj = RecvStringFrame();
// Only invoke the callback if we have data response
if (mState != 0 || mFlags != 0)
{
cb(obj, mState, mFlags);
}
// Return result
return mState;
}
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Sets the maximum payload size for RecvFrame(). The default is std::numeric_limits<int>::max(). * Sets the maximum payload size for RecvFrame(). The default is std::numeric_limits<int>::max().
*/ */