1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-01-18 19:47:15 +01:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Sandu Liviu Catalin
05e644b040 Minor changes and bugfixes to WS client. 2021-09-05 12:30:49 +03:00
Sandu Liviu Catalin
f3d4dab454 Basic web-socket client implementation. 2021-09-05 12:05:38 +03:00
Sandu Liviu Catalin
d787803fd8 Implement stealing the memory from POCO buffer.
So I don't have to allocate one if I don't have to.
2021-09-05 12:03:57 +03:00
Sandu Liviu Catalin
81893bf236 Simplify internal buffer implementation where possible. 2021-09-05 12:03:04 +03:00
5 changed files with 308 additions and 192 deletions

View File

@ -22,19 +22,13 @@ SQMOD_DECL_TYPENAME(Typename, _SC("SqBuffer"))
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteInt64(const SLongInt & val) void SqBuffer::WriteInt64(const SLongInt & val)
{ {
// Validate the managed buffer reference Valid().Push< int64_t >(val.GetNum());
Validate();
// Perform the requested operation
m_Buffer->Push< int64_t >(val.GetNum());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteUint64(const ULongInt & val) void SqBuffer::WriteUint64(const ULongInt & val)
{ {
// Validate the managed buffer reference Valid().Push< uint64_t >(val.GetNum());
Validate();
// Perform the requested operation
m_Buffer->Push< uint64_t >(val.GetNum());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -83,91 +77,61 @@ SQInteger SqBuffer::WriteClientString(StackStrF & val)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteAABB(const AABB & val) void SqBuffer::WriteAABB(const AABB & val)
{ {
// Validate the managed buffer reference Valid().Push< AABB >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< AABB >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteCircle(const Circle & val) void SqBuffer::WriteCircle(const Circle & val)
{ {
// Validate the managed buffer reference Valid().Push< Circle >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Circle >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteColor3(const Color3 & val) void SqBuffer::WriteColor3(const Color3 & val)
{ {
// Validate the managed buffer reference Valid().Push< Color3 >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Color3 >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteColor4(const Color4 & val) void SqBuffer::WriteColor4(const Color4 & val)
{ {
// Validate the managed buffer reference Valid().Push< Color4 >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Color4 >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteQuaternion(const Quaternion & val) void SqBuffer::WriteQuaternion(const Quaternion & val)
{ {
// Validate the managed buffer reference Valid().Push< Quaternion >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Quaternion >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteSphere(const Sphere &val) void SqBuffer::WriteSphere(const Sphere &val)
{ {
// Validate the managed buffer reference Valid().Push< Sphere >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Sphere >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector2(const Vector2 & val) void SqBuffer::WriteVector2(const Vector2 & val)
{ {
// Validate the managed buffer reference Valid().Push< Vector2 >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Vector2 >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector2i(const Vector2i & val) void SqBuffer::WriteVector2i(const Vector2i & val)
{ {
// Validate the managed buffer reference Valid().Push< Vector2i >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Vector2i >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector3(const Vector3 & val) void SqBuffer::WriteVector3(const Vector3 & val)
{ {
// Validate the managed buffer reference Valid().Push< Vector3 >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Vector3 >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SqBuffer::WriteVector4(const Vector4 & val) void SqBuffer::WriteVector4(const Vector4 & val)
{ {
// Validate the managed buffer reference Valid().Push< Vector4 >(val);
Validate();
// Perform the requested operation
m_Buffer->Push< Vector4 >(val);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -45,7 +45,6 @@ public:
SqBuffer() SqBuffer()
: m_Buffer(new Buffer()) : m_Buffer(new Buffer())
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -54,7 +53,6 @@ public:
explicit SqBuffer(SQInteger n) explicit SqBuffer(SQInteger n)
: m_Buffer(new Buffer(ConvTo< SzType >::From(n))) : m_Buffer(new Buffer(ConvTo< SzType >::From(n)))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -63,7 +61,6 @@ public:
SqBuffer(SQInteger n, SQInteger c) SqBuffer(SQInteger n, SQInteger c)
: m_Buffer(new Buffer(ConvTo< SzType >::From(n), ConvTo< SzType >::From(c))) : m_Buffer(new Buffer(ConvTo< SzType >::From(n), ConvTo< SzType >::From(c)))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -72,7 +69,6 @@ public:
SqBuffer(ConstPtr p, SQInteger n) SqBuffer(ConstPtr p, SQInteger n)
: m_Buffer(new Buffer(p, ConvTo< SzType >::From(n))) : m_Buffer(new Buffer(p, ConvTo< SzType >::From(n)))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -81,7 +77,6 @@ public:
SqBuffer(ConstPtr p, SQInteger n, SQInteger c) SqBuffer(ConstPtr p, SQInteger n, SQInteger c)
: m_Buffer(new Buffer(p, ConvTo< SzType >::From(n), ConvTo< SzType >::From(c))) : m_Buffer(new Buffer(p, ConvTo< SzType >::From(n), ConvTo< SzType >::From(c)))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -90,7 +85,6 @@ public:
explicit SqBuffer(const SRef & ref) // NOLINT(modernize-pass-by-value) explicit SqBuffer(const SRef & ref) // NOLINT(modernize-pass-by-value)
: m_Buffer(ref) : m_Buffer(ref)
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -99,7 +93,6 @@ public:
explicit SqBuffer(const Buffer & b) explicit SqBuffer(const Buffer & b)
: m_Buffer(new Buffer(b)) : m_Buffer(new Buffer(b))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -108,7 +101,6 @@ public:
explicit SqBuffer(Buffer && b) explicit SqBuffer(Buffer && b)
: m_Buffer(new Buffer(std::move(b))) : m_Buffer(new Buffer(std::move(b)))
{ {
/* ... */
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -181,15 +173,32 @@ public:
} }
} }
/* --------------------------------------------------------------------------------------------
* Validate the managed memory buffer reference.
*/
Buffer & Valid() const
{
Valid();
// Return the buffer
return *m_Buffer;
}
/* --------------------------------------------------------------------------------------------
* Validate the managed memory buffer reference and the buffer itself.
*/
Buffer & ValidDeeper() const
{
ValidateDeeper();
// Return the buffer
return *m_Buffer;
}
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Retrieve a certain element type at the specified position. * Retrieve a certain element type at the specified position.
*/ */
SQMOD_NODISCARD Value Get(SQInteger n) const SQMOD_NODISCARD Value Get(SQInteger n) const
{ {
// Validate the managed buffer reference return Valid().At(ConvTo< SzType >::From(n));
Validate();
// Return the requested element
return m_Buffer->At(ConvTo< SzType >::From(n));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -197,10 +206,7 @@ public:
*/ */
void Set(SQInteger n, SQInteger v) void Set(SQInteger n, SQInteger v)
{ {
// Validate the managed buffer reference Valid().At(ConvTo< SzType >::From(n)) = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->At(ConvTo< SzType >::From(n)) = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -208,10 +214,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetFront() const SQMOD_NODISCARD Value GetFront() const
{ {
// Validate the managed buffer reference return Valid().Front();
Validate();
// Return the requested element
return m_Buffer->Front();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -219,10 +222,7 @@ public:
*/ */
void SetFront(SQInteger v) void SetFront(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Front() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Front() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -230,10 +230,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetNext() const SQMOD_NODISCARD Value GetNext() const
{ {
// Validate the managed buffer reference return Valid().Next();
Validate();
// Return the requested element
return m_Buffer->Next();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -241,10 +238,7 @@ public:
*/ */
void SetNext(SQInteger v) void SetNext(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Next() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Next() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -252,10 +246,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetBack() const SQMOD_NODISCARD Value GetBack() const
{ {
// Validate the managed buffer reference return Valid().Back();
Validate();
// Return the requested element
return m_Buffer->Back();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -263,10 +254,7 @@ public:
*/ */
void SetBack(SQInteger v) void SetBack(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Back() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Back() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -274,10 +262,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetPrev() const SQMOD_NODISCARD Value GetPrev() const
{ {
// Validate the managed buffer reference return Valid().Prev();
Validate();
// Return the requested element
return m_Buffer->Prev();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -285,10 +270,7 @@ public:
*/ */
void SetPrev(SQInteger v) void SetPrev(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Prev() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Prev() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -296,10 +278,7 @@ public:
*/ */
void Advance(SQInteger n) void Advance(SQInteger n)
{ {
// Validate the managed buffer reference Valid().Advance(ConvTo< SzType >::From(n));
Validate();
// Perform the requested operation
m_Buffer->Advance(ConvTo< SzType >::From(n));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -307,10 +286,7 @@ public:
*/ */
void Retreat(SQInteger n) void Retreat(SQInteger n)
{ {
// Validate the managed buffer reference Valid().Retreat(ConvTo< SzType >::From(n));
Validate();
// Perform the requested operation
m_Buffer->Retreat(ConvTo< SzType >::From(n));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -318,10 +294,7 @@ public:
*/ */
void Move(SQInteger n) void Move(SQInteger n)
{ {
// Validate the managed buffer reference Valid().Move(ConvTo< SzType >::From(n));
Validate();
// Perform the requested operation
m_Buffer->Move(ConvTo< SzType >::From(n));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -329,10 +302,7 @@ public:
*/ */
void Push(SQInteger v) void Push(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Push(ConvTo< Value >::From(v));
Validate();
// Perform the requested operation
m_Buffer->Push(ConvTo< Value >::From(v));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -340,10 +310,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetCursor() const SQMOD_NODISCARD Value GetCursor() const
{ {
// Validate the managed buffer reference return Valid().Cursor();
Validate();
// Return the requested element
return m_Buffer->Cursor();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -351,10 +318,7 @@ public:
*/ */
void SetCursor(SQInteger v) void SetCursor(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Cursor() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Cursor() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -362,10 +326,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetBefore() const SQMOD_NODISCARD Value GetBefore() const
{ {
// Validate the managed buffer reference return Valid().Before();
Validate();
// Return the requested element
return m_Buffer->Before();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -373,10 +334,7 @@ public:
*/ */
void SetBefore(SQInteger v) void SetBefore(SQInteger v)
{ {
// Validate the managed buffer reference Valid().Before() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->Before() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -384,10 +342,7 @@ public:
*/ */
SQMOD_NODISCARD Value GetAfter() const SQMOD_NODISCARD Value GetAfter() const
{ {
// Validate the managed buffer reference return Valid().After();
Validate();
// Return the requested element
return m_Buffer->After();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -395,10 +350,7 @@ public:
*/ */
void SetAfter(SQInteger v) void SetAfter(SQInteger v)
{ {
// Validate the managed buffer reference Valid().After() = ConvTo< Value >::From(v);
Validate();
// Return the requested element
m_Buffer->After() = ConvTo< Value >::From(v);
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -414,10 +366,7 @@ public:
*/ */
SQMOD_NODISCARD SzType GetSize() const SQMOD_NODISCARD SzType GetSize() const
{ {
// Validate the managed buffer reference return Valid().CapacityAs< Value >();
Validate();
// Return the requested information
return m_Buffer->CapacityAs< Value >();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -425,10 +374,7 @@ public:
*/ */
SQMOD_NODISCARD SzType GetCapacity() const SQMOD_NODISCARD SzType GetCapacity() const
{ {
// Validate the managed buffer reference return Valid().Capacity();
Validate();
// Return the requested information
return m_Buffer->Capacity();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -436,10 +382,7 @@ public:
*/ */
SQMOD_NODISCARD SzType GetPosition() const SQMOD_NODISCARD SzType GetPosition() const
{ {
// Validate the managed buffer reference return Valid().Position();
Validate();
// Return the requested information
return m_Buffer->Position();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -447,10 +390,7 @@ public:
*/ */
SQMOD_NODISCARD SzType GetRemaining() const SQMOD_NODISCARD SzType GetRemaining() const
{ {
// Validate the managed buffer reference return Valid().Remaining();
Validate();
// Return the requested information
return m_Buffer->Remaining();
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -458,10 +398,7 @@ public:
*/ */
void Grow(SQInteger n) void Grow(SQInteger n)
{ {
// Validate the managed buffer reference return Valid().Grow(ConvTo< SzType >::From(n) * sizeof(Value));
Validate();
// Perform the requested operation
return m_Buffer->Grow(ConvTo< SzType >::From(n) * sizeof(Value));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -490,10 +427,7 @@ public:
*/ */
void WriteInt8(SQInteger val) void WriteInt8(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< int8_t >(ConvTo< int8_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< int8_t >(ConvTo< int8_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -501,10 +435,7 @@ public:
*/ */
void WriteUint8(SQInteger val) void WriteUint8(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< uint8_t >(ConvTo< uint8_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< uint8_t >(ConvTo< uint8_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -512,10 +443,7 @@ public:
*/ */
void WriteInt16(SQInteger val) void WriteInt16(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< int16_t >(ConvTo< int16_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< int16_t >(ConvTo< int16_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -523,10 +451,7 @@ public:
*/ */
void WriteUint16(SQInteger val) void WriteUint16(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< uint16_t >(ConvTo< uint16_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< uint16_t >(ConvTo< uint16_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -534,10 +459,7 @@ public:
*/ */
void WriteInt32(SQInteger val) void WriteInt32(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< int32_t >(ConvTo< int32_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< int32_t >(ConvTo< int32_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -545,10 +467,7 @@ public:
*/ */
void WriteUint32(SQInteger val) void WriteUint32(SQInteger val)
{ {
// Validate the managed buffer reference Valid().Push< uint32_t >(ConvTo< uint32_t >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< uint32_t >(ConvTo< uint32_t >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -566,10 +485,7 @@ public:
*/ */
void WriteFloat32(SQFloat val) void WriteFloat32(SQFloat val)
{ {
// Validate the managed buffer reference Valid().Push< float >(ConvTo< float >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< float >(ConvTo< float >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
@ -577,10 +493,7 @@ public:
*/ */
void WriteFloat64(SQFloat val) void WriteFloat64(SQFloat val)
{ {
// Validate the managed buffer reference Valid().Push< double >(ConvTo< double >::From(val));
Validate();
// Perform the requested operation
m_Buffer->Push< double >(ConvTo< double >::From(val));
} }
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------

View File

@ -1,18 +1,91 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "PocoLib/Net.hpp" #include "PocoLib/Net.hpp"
// ------------------------------------------------------------------------------------------------
#include <sqratConst.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
SQMOD_DECL_TYPENAME(SqWsClient, _SC("SqWsClient"))
// ================================================================================================ // ================================================================================================
void Register_POCO_Net(HSQUIRRELVM vm, Table &) void Register_POCO_Net(HSQUIRRELVM vm, Table &)
{ {
//Table ns(vm); Table ns(vm);
// --------------------------------------------------------------------------------------------
//RootTable(vm).Bind(_SC("SqNet"), ns); ns.Bind(_SC("WsClient"),
Class< WsClient, NoCopy< WsClient > >(ns.GetVM(), SqWsClient::Str)
// Constructors
.Ctor< StackStrF &, uint16_t, StackStrF & >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqWsClient::Fn)
// Member Variables
.Var(_SC("Flags"), &WsClient::mFlags)
.Var(_SC("State"), &WsClient::mState)
// Properties
.Prop(_SC("MaxPayloadSize"), &WsClient::GetMaxPayloadSize, &WsClient::SetMaxPayloadSize)
// Member Methods
.Func(_SC("Shutdown"), &WsClient::Shutdown)
.FmtFunc(_SC("ShutdownWith"), &WsClient::ShutdownWith)
.Func(_SC("SendFrame"), &WsClient::SendFrame)
.FmtFunc(_SC("SendStringFrame"), &WsClient::SendStringFrame)
.Func(_SC("RecvFrame"), &WsClient::RecvFrame)
.Func(_SC("RecvStringFrame"), &WsClient::RecvStringFrame)
);
// --------------------------------------------------------------------------------------------
RootTable(vm).Bind(_SC("SqNet"), ns);
// --------------------------------------------------------------------------------------------
ConstTable(vm).Enum(_SC("SqWsFrameFlags"), Enumeration(vm)
.Const(_SC("FIN"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_FLAG_FIN))
.Const(_SC("RSV1"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_FLAG_RSV1))
.Const(_SC("RSV2"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_FLAG_RSV2))
.Const(_SC("RSV3"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_FLAG_RSV3))
);
// --------------------------------------------------------------------------------------------
ConstTable(vm).Enum(_SC("SqWsFrameOpcodes"), Enumeration(vm)
.Const(_SC("CONT"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_CONT))
.Const(_SC("TEXT"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_TEXT))
.Const(_SC("BINARY"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_BINARY))
.Const(_SC("CLOSE"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_CLOSE))
.Const(_SC("PING"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_PING))
.Const(_SC("PONG"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_PONG))
.Const(_SC("BITMASK"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_BITMASK))
.Const(_SC("SETRAW"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_OP_SETRAW))
);
// --------------------------------------------------------------------------------------------
ConstTable(vm).Enum(_SC("SqWsSendFlags"), Enumeration(vm)
.Const(_SC("Text"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_TEXT))
.Const(_SC("Binary"), static_cast< SQInteger >(Poco::Net::WebSocket::FRAME_BINARY))
);
// --------------------------------------------------------------------------------------------
ConstTable(vm).Enum(_SC("SqWsStatusCodes"), Enumeration(vm)
.Const(_SC("NormalClose"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_NORMAL_CLOSE))
.Const(_SC("EndpointGoingAway"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY))
.Const(_SC("ProtocolError"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_PROTOCOL_ERROR))
.Const(_SC("PayloadNotAcceptable"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_PAYLOAD_NOT_ACCEPTABLE))
.Const(_SC("Reserved"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_RESERVED))
.Const(_SC("ReservedNoStatusCode"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_RESERVED_NO_STATUS_CODE))
.Const(_SC("ReservedAbnormalClose"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_RESERVED_ABNORMAL_CLOSE))
.Const(_SC("MalformedPayload"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_MALFORMED_PAYLOAD))
.Const(_SC("PolicyViolation"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_POLICY_VIOLATION))
.Const(_SC("PayloadTooBig"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_PAYLOAD_TOO_BIG))
.Const(_SC("ExtensionRequired"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_EXTENSION_REQUIRED))
.Const(_SC("UnexpectedCondition"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_UNEXPECTED_CONDITION))
.Const(_SC("ReservedTlsFailure"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_RESERVED_TLS_FAILURE))
);
// --------------------------------------------------------------------------------------------
ConstTable(vm).Enum(_SC("SqWsErrorCodes"), Enumeration(vm)
.Const(_SC("NoHandshake"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_NO_HANDSHAKE))
.Const(_SC("HandshakeNoVersion"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_VERSION))
.Const(_SC("HandshakeUnsupportedVersion"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION))
.Const(_SC("HandshakeNoKey"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_HANDSHAKE_NO_KEY))
.Const(_SC("HandshakeAccept"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_HANDSHAKE_ACCEPT))
.Const(_SC("Unauthorized"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_UNAUTHORIZED))
.Const(_SC("PayloadTooBig"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_PAYLOAD_TOO_BIG))
.Const(_SC("IncompleteFrame"), static_cast< SQInteger >(Poco::Net::WebSocket::WS_ERR_INCOMPLETE_FRAME))
);
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -2,10 +2,166 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Core/Common.hpp" #include "Core/Common.hpp"
#include "Library/IO/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <vector>
// ------------------------------------------------------------------------------------------------
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPMessage.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/WebSocket.h>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Implements a WebSocket according to the WebSocket protocol specification in RFC 6455.
*/
struct WsClient
{
/* --------------------------------------------------------------------------------------------
* HTTP client session instance.
*/
Poco::Net::HTTPClientSession mClient;
/* --------------------------------------------------------------------------------------------
* HTTP request instance.
*/
Poco::Net::HTTPRequest mRequest;
/* --------------------------------------------------------------------------------------------
* HTTP response instance.
*/
Poco::Net::HTTPResponse mResponse;
/* --------------------------------------------------------------------------------------------
* WebSocket instance.
*/
Poco::Net::WebSocket mWebSocket;
/* --------------------------------------------------------------------------------------------
* Receiving buffer instance.
*/
Poco::Buffer< char > mBuffer;
/* --------------------------------------------------------------------------------------------
* Flags received in the last call to Recv[String]Frame() (will be overwritten on next call).
*/
int mFlags{0};
/* --------------------------------------------------------------------------------------------
* Return value from the last call to Recv[String]Frame() (will be overwritten on next call).
* A return value of 0, with flags also 0, means that the peer has shut down or closed the connection.
* A return value of 0, with non-zero flags, indicates an reception of an empty frame (e.g., in case of a PING).
*/
int mState{0};
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
WsClient(StackStrF & host, uint16_t port, StackStrF & uri)
: mClient(host.ToStr(), port),
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)
{
}
/* --------------------------------------------------------------------------------------------
* Sends a Close control frame to the server end of the connection to initiate an orderly shutdown of the connection.
*/
void Shutdown() {
mWebSocket.shutdown();
}
/* --------------------------------------------------------------------------------------------
* Sends a Close control frame to the server end of the connection to initiate an orderly shutdown of the connection.
*/
void ShutdownWith(SQInteger code, StackStrF & msg) {
mWebSocket.shutdown(static_cast< uint16_t >(code), msg.ToStr());
}
/* --------------------------------------------------------------------------------------------
* Sends the contents of the given buffer through the socket as a single frame.
* Returns the number of bytes sent, which may be less than the number of bytes specified.
*/
SQInteger SendFrame(SqBuffer & buf, SQInteger flags) {
return mWebSocket.sendFrame(buf.Valid().Data(), static_cast< int >(buf.Valid().Position()), static_cast< int >(flags));
}
/* --------------------------------------------------------------------------------------------
* Sends the contents of the given string through the socket as a single frame.
* Returns the number of bytes sent, which may be less than the number of bytes specified.
*/
SQInteger SendStringFrame(SQInteger flags, StackStrF & str) {
return mWebSocket.sendFrame(str.mPtr, static_cast< int >(str.mLen), static_cast< int >(flags));
}
/* --------------------------------------------------------------------------------------------
* Receives a frame from the socket and return it as a buffer.
* The frame's payload size must not exceed the maximum payload size set with SetMaxPayloadSize().
*/
LightObj RecvFrame()
{
// Attempt to receive data
mState = mWebSocket.receiveFrame(mBuffer, mFlags);
// If something was returned
if (mState != 0)
{
// Fetch buffer information
const auto cap = static_cast< Buffer::SzType >(mBuffer.capacityBytes());
const auto len = static_cast< Buffer::SzType >(mBuffer.sizeBytes());
// Steal buffer memory
Buffer::Pointer ptr = mBuffer.steal();
// Construct our buffer
Buffer b(ptr, cap, len, Buffer::OwnIt{});
// Transform it into a script object and return it
return LightObj(SqTypeIdentity< SqBuffer >{}, SqVM(), std::move(b));
}
// Default to null
return LightObj{};
}
/* --------------------------------------------------------------------------------------------
* Receives a frame from the socket and return it as a string.
* The frame's payload size must not exceed the maximum payload size set with SetMaxPayloadSize().
*/
LightObj RecvStringFrame()
{
// Attempt to receive data
mState = mWebSocket.receiveFrame(mBuffer, mFlags);
// If something was returned
if (mState != 0)
{
// Create a string with buffer contents
LightObj obj(const_cast< const SQChar * >(mBuffer.begin()), static_cast< SQInteger >(mBuffer.sizeBytes()), SqVM());
// Discard buffer contents for next request
mBuffer.resize(0);
// Return the string object
return obj;
}
// Default to null
return LightObj{};
}
/* --------------------------------------------------------------------------------------------
* Sets the maximum payload size for RecvFrame(). The default is std::numeric_limits<int>::max().
*/
WsClient & SetMaxPayloadSize(SQInteger size)
{
mWebSocket.setMaxPayloadSize(static_cast< int >(size));
return *this;
}
/* --------------------------------------------------------------------------------------------
* Returns the maximum payload size for RecvFrame(). The default is std::numeric_limits<int>::max().
*/
SQMOD_NODISCARD SQInteger GetMaxPayloadSize() const
{
return mWebSocket.getMaxPayloadSize();
}
};
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -259,6 +259,16 @@ public:
swap(_ownMem, other._ownMem); swap(_ownMem, other._ownMem);
} }
T* steal()
/// Releases ownership of the buffer. Whoever gets hold of the buffer must invoke delete [] on it.
{
T* ptr = _ptr;
_ptr = 0;
_capacity = _used = 0; // Retrieve this before calling this method
_ownMem = false; // Consult this before calling this method
return ptr;
}
bool operator == (const Buffer& other) const bool operator == (const Buffer& other) const
/// Compare operator. /// Compare operator.
{ {