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

Compare commits

..

4 Commits

Author SHA1 Message Date
Sandu Liviu Catalin
5a57bf2fbf Prevent double closing event in web-sockets.
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-08-14 20:29:32 +03:00
Sandu Liviu Catalin
15e85f1394 Update Net.hpp 2022-08-14 20:11:53 +03:00
Sandu Liviu Catalin
e8fa9e0259 Update sqratLightObj.h 2022-08-14 18:53:36 +03:00
Sandu Liviu Catalin
c551390999 Helper Base32/64 methods on SqBufer type. 2022-08-14 18:53:28 +03:00
5 changed files with 115 additions and 21 deletions

View File

@ -11,6 +11,13 @@
// ------------------------------------------------------------------------------------------------
#include <Poco/Checksum.h>
#include <Poco/Base32Encoder.h>
#include <Poco/Base32Decoder.h>
#include <Poco/Base64Encoder.h>
#include <Poco/Base64Decoder.h>
// ------------------------------------------------------------------------------------------------
#include <sstream>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -30,7 +37,7 @@ SQInteger SqBuffer::WriteRawString(StackStrF & val) const
}
// Calculate the string length
Buffer::SzType length = ConvTo< Buffer::SzType >::From(val.mLen);
// Write the the string contents
// Write the string contents
m_Buffer->AppendS(val.mPtr, length);
// Return the length of the written string
return val.mLen;
@ -224,7 +231,7 @@ AABB SqBuffer::ReadAABB() const
// Advance the buffer cursor
m_Buffer->Advance< AABB >(1);
// Return the requested information
return AABB(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -237,7 +244,7 @@ Circle SqBuffer::ReadCircle() const
// Advance the buffer cursor
m_Buffer->Advance< Circle >(1);
// Return the requested information
return Circle(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -250,7 +257,7 @@ Color3 SqBuffer::ReadColor3() const
// Advance the buffer cursor
m_Buffer->Advance< Color3 >(1);
// Return the requested information
return Color3(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -263,7 +270,7 @@ Color4 SqBuffer::ReadColor4() const
// Advance the buffer cursor
m_Buffer->Advance< Color4 >(1);
// Return the requested information
return Color4(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -276,7 +283,7 @@ Quaternion SqBuffer::ReadQuaternion() const
// Advance the buffer cursor
m_Buffer->Advance< Quaternion >(1);
// Return the requested information
return Quaternion(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -289,7 +296,7 @@ Sphere SqBuffer::ReadSphere() const
// Advance the buffer cursor
m_Buffer->Advance< Sphere >(1);
// Return the requested information
return Sphere(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -302,7 +309,7 @@ Vector2 SqBuffer::ReadVector2() const
// Advance the buffer cursor
m_Buffer->Advance< Vector2 >(1);
// Return the requested information
return Vector2(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -315,7 +322,7 @@ Vector2i SqBuffer::ReadVector2i() const
// Advance the buffer cursor
m_Buffer->Advance< Vector2i >(1);
// Return the requested information
return Vector2i(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -328,7 +335,7 @@ Vector3 SqBuffer::ReadVector3() const
// Advance the buffer cursor
m_Buffer->Advance< Vector3 >(1);
// Return the requested information
return Vector3(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -341,7 +348,7 @@ Vector4 SqBuffer::ReadVector4() const
// Advance the buffer cursor
m_Buffer->Advance< Vector4 >(1);
// Return the requested information
return Vector4(value);
return {value};
}
// ------------------------------------------------------------------------------------------------
@ -352,7 +359,7 @@ SQInteger SqBuffer::GetCRC32(SQInteger n) const
// Create the checksum computer
Poco::Checksum c(Poco::Checksum::TYPE_CRC32);
// Give it the data to process
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
c.update(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// return the result
return static_cast< SQInteger >(c.checksum());
}
@ -365,12 +372,46 @@ SQInteger SqBuffer::GetADLER32(SQInteger n) const
// Create the checksum computer
Poco::Checksum c(Poco::Checksum::TYPE_ADLER32);
// Give it the data to process
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
c.update(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// return the result
return static_cast< SQInteger >(c.checksum());
}
// ------------------------------------------------------------------------------------------------
LightObj SqBuffer::GetBase32(SQInteger n) const
{
// Validate the managed buffer reference
ValidateDeeper();
// Create a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base32Encoder enc(out);
// Encode the string
enc.write(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// Close the encoder
enc.close();
// Return the resulted string
return LightObj{out.str()};
}
// ------------------------------------------------------------------------------------------------
LightObj SqBuffer::GetBase64(SQInteger n) const
{
// Validate the managed buffer reference
ValidateDeeper();
// Create a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base64Encoder enc(out);
// Encode the string
enc.write(&m_Buffer->Cursor< char >(), ClampRemaining(n));
// Close the encoder
enc.close();
// Return the resulted string
return LightObj{out.str()};
}
// ================================================================================================
void Register_Buffer(HSQUIRRELVM vm)
{
@ -457,6 +498,8 @@ void Register_Buffer(HSQUIRRELVM vm)
.Func(_SC("ReadVector4"), &SqBuffer::ReadVector4)
.Func(_SC("CRC32"), &SqBuffer::GetCRC32)
.Func(_SC("ADLER32"), &SqBuffer::GetADLER32)
.Func(_SC("Base32"), &SqBuffer::GetBase32)
.Func(_SC("Base64"), &SqBuffer::GetBase64)
);
}

View File

@ -193,6 +193,24 @@ public:
return *m_Buffer;
}
/* --------------------------------------------------------------------------------------------
* Limit the specified amount at to the range of the cursor and the end of the buffer.
*/
SQMOD_NODISCARD SzType ClampRemaining(SQInteger n) const
{
// Do we even specify a buffer amount?
if (n >= 0)
{
// Is it within the range we currently have left?
if (static_cast< SzType >(n) <= m_Buffer->Remaining())
{
return static_cast< SzType >(n);
}
}
// Fall back to the actual remaining data
return m_Buffer->Remaining();
}
/* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position.
*/
@ -761,6 +779,16 @@ public:
* Compute the Adler-32 checksums on the data in the buffer.
*/
SQMOD_NODISCARD SQInteger GetADLER32(SQInteger n) const;
/* --------------------------------------------------------------------------------------------
* Encode the specified range of data as base32 and return it.
*/
SQMOD_NODISCARD LightObj GetBase32(SQInteger n) const;
/* --------------------------------------------------------------------------------------------
* Encode the specified range of data as base64 and return it.
*/
SQMOD_NODISCARD LightObj GetBase64(SQInteger n) const;
};
} // Namespace:: SqMod

View File

@ -89,6 +89,9 @@ WebSocketClient & WebSocketClient::Connect()
{
STHROWF("Connection failed: {}", err_buf);
}
// Reset memebrs
mClosing.store(false);
mClosed.store(false);
// Allow chaining
return *this;
}
@ -113,6 +116,9 @@ WebSocketClient & WebSocketClient::ConnectExt()
{
STHROWF("Connection failed: {}", err_buf);
}
// Reset memebrs
mClosing.store(false);
mClosed.store(false);
// Allow chaining
return *this;
}

View File

@ -154,6 +154,11 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
std::atomic< bool > mClosing{false};
/* --------------------------------------------------------------------------------------------
* Whether the closing callback was inoked (avoid recursive calls).
*/
std::atomic< bool > mClosed{false};
/* --------------------------------------------------------------------------------------------
* Server host to connect to, i.e. "echo.websocket.org" or "192.168.1.1" or "localhost".
*/
@ -179,7 +184,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient()
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(0), mSecure(false), mClosing(false), mHost(), mPath(), mOrigin(), mExtensions()
, mPort(0), mSecure(false), mClosing(false), mClosed(false), mHost(), mPath(), mOrigin(), mExtensions()
{
ChainInstance(); // Remember this instance
}
@ -189,7 +194,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(false), mClosing(false)
, mPort(port), mSecure(false), mClosing(false), mClosed(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(), mExtensions()
@ -202,7 +207,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(), mExtensions()
@ -215,7 +220,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure, StackStrF & origin)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(origin.mPtr, origin.GetSize())
@ -229,7 +234,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
*/
WebSocketClient(StackStrF & host, uint16_t port, StackStrF & path, bool secure, StackStrF & origin, StackStrF & ext)
: Base(), mHandle(nullptr), mQueue(1024), mOnData(), mOnClose(), mTag(), mData()
, mPort(port), mSecure(secure), mClosing(false)
, mPort(port), mSecure(secure), mClosing(false), mClosed(false)
, mHost(host.mPtr, host.GetSize())
, mPath(path.mPtr, path.GetSize())
, mOrigin(origin.mPtr, origin.GetSize())
@ -648,7 +653,7 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
/* --------------------------------------------------------------------------------------------
* Sends the contents of the given buffer through the socket as a single frame.
*/
SQMOD_NODISCARD SQInteger SendOpCode(SqBuffer & buf, SQInteger opcode)
SQMOD_NODISCARD SQInteger SendOpCode(SQInteger opcode)
{
return mg_websocket_client_write(Valid(), static_cast< int >(opcode), nullptr, 0);
}
@ -706,9 +711,12 @@ struct WebSocketClient : public SqChainedInstances< WebSocketClient >
}
}
// Is the server closing the connection?
if (closing && !mOnClose.IsNull())
if (closing && !mClosed.load() && !mOnClose.IsNull())
{
mOnClose.Execute(); // Let the user know
// Let the user know
mOnClose.Execute();
// Prevent calling this callback again
mClosed.store(true);
}
}

View File

@ -88,6 +88,15 @@ struct LightObj {
sq_addref(SqVM(), &mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObj from a string object
///
/// \param o Squirrel object
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit LightObj(const string & s) : LightObj(s.c_str(), static_cast< SQInteger >(s.size())) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a LightObj from a Squirrel object at a certain index on the stack
///