1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-20 19:57:12 +01:00

Remove incomplete Mongoose module.

This commit is contained in:
Sandu Liviu Catalin 2017-05-25 22:16:52 +03:00
parent 59f64e9532
commit cdfc3f58bc
21 changed files with 0 additions and 2019 deletions

View File

@ -1,10 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,90 +0,0 @@
#ifndef _SQMG_COMMON_HPP_
#define _SQMG_COMMON_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <mongoose.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* SOFTWARE INFORMATION
*/
#define SQMG_NAME "Squirrel Mongoose Module"
#define SQMG_AUTHOR "Sandu Liviu Catalin (S.L.C)"
#define SQMG_COPYRIGHT "Copyright (C) 2017 Sandu Liviu Catalin"
#define SQMG_HOST_NAME "SqModMongooseHost"
#define SQMG_VERSION 001
#define SQMG_VERSION_STR "0.0.1"
#define SQMG_VERSION_MAJOR 0
#define SQMG_VERSION_MINOR 0
#define SQMG_VERSION_PATCH 1
// ------------------------------------------------------------------------------------------------
enum struct HndInit
{
InitHnd = 0
};
// ------------------------------------------------------------------------------------------------
class Manager;
class Connection;
/* ------------------------------------------------------------------------------------------------
* Types of events supported by a connection.
*/
enum ConnectionEvents
{
MGEV_UNKNOWN = SQMOD_UNKNOWN,
MGCE_POLL,
MGCE_ACCEPT,
MGCE_CONNECT,
MGCE_RECV,
MGCE_SEND,
MGCE_CLOSE,
MGCE_TIMER,
MGCE_HTTP_REQUEST,
MGCE_HTTP_REPLY,
MGCE_HTTP_CHUNK,
MGCE_SSI_CALL,
MGCE_WEBSOCKET_HANDSHAKE_REQUEST,
MGCE_WEBSOCKET_HANDSHAKE_DONE,
MGCE_WEBSOCKET_FRAME,
MGCE_WEBSOCKET_CONTROL_FRAME,
MGCE_HTTP_MULTIPART_REQUEST,
MGCE_HTTP_PART_BEGIN,
MGCE_HTTP_PART_DATA,
MGCE_HTTP_PART_END,
MGCE_MQTT_CONNECT,
MGCE_MQTT_CONNACK,
MGCE_MQTT_PUBLISH,
MGCE_MQTT_PUBACK,
MGCE_MQTT_PUBREC,
MGCE_MQTT_PUBREL,
MGCE_MQTT_PUBCOMP,
MGCE_MQTT_SUBSCRIBE,
MGCE_MQTT_SUBACK,
MGCE_MQTT_UNSUBSCRIBE,
MGCE_MQTT_UNSUBACK,
MGCE_MQTT_PINGREQ,
MGCE_MQTT_PINGRESP,
MGCE_MQTT_DISCONNECT,
MGCE_MQTT_CONNACK_ACCEPTED,
MGCE_MQTT_CONNACK_UNACCEPTABLE_VERSION,
MGCE_MQTT_CONNACK_IDENTIFIER_REJECTED,
MGCE_MQTT_CONNACK_SERVER_UNAVAILABLE,
MGCE_MQTT_CONNACK_BAD_AUTH,
MGCE_MQTT_CONNACK_NOT_AUTHORIZED,
MGCE_COAP_CON,
MGCE_COAP_NOC,
MGCE_COAP_ACK,
MGCE_COAP_RST,
MGCE_MAX
};
} // Namespace:: SqMod
#endif // _SQMG_COMMON_HPP_

View File

@ -1,42 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Connection.hpp"
#include "Manager.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger Connection::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("SqMgConnection");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Int32 Connection::Cmp(const Connection & o) const
{
if (m_Handle == o.m_Handle)
{
return 0;
}
else if (m_Handle.HndPtr() > o.m_Handle.HndPtr())
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
void Connection::Bind(Int32 ev, Object & env, Function & func) const
{
}
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,94 +0,0 @@
#ifndef _SQMG_CONNECTION_HPP_
#define _SQMG_CONNECTION_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Allows management and interaction with a connection handle.
*/
class Connection
{
private:
// --------------------------------------------------------------------------------------------
ConnectionHnd m_Handle; // Reference to the connection instance.
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Connection()
: m_Handle()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
Connection(const ConnectionHnd & c)
: m_Handle(c)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Connection(const Connection & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Connection(Connection && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Connection()
{
// Let the reference handle the deallocation!
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Connection & operator = (const Connection & o);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Connection & operator = (Connection && o);
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Connection & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const
{
return _SC("");
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Bind to an event emitted by this connection.
*/
void Bind(Int32 ev, Object & env, Function & func) const;
};
} // Namespace:: SqMod
#endif // _SQMG_CONNECTION_HPP_

View File

@ -1,39 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "HTTP.hpp"
#include "Manager.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger HTTP::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("SqMgHTTP");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Int32 HTTP::Cmp(const HTTP & o) const
{
if (m_Handle == o.m_Handle)
{
return 0;
}
else if (m_Handle.HndPtr() > o.m_Handle.HndPtr())
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,94 +0,0 @@
#ifndef _SQMG_HTTP_HPP_
#define _SQMG_HTTP_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Allows management of the connection handle.
*/
class HTTP
{
private:
// --------------------------------------------------------------------------------------------
ConnectionHnd m_Handle; // Reference to the connection instance.
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
HTTP()
: m_Handle()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
HTTP(const ConnectionHnd & c)
: m_Handle(c)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
HTTP(const HTTP & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
HTTP(HTTP && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~HTTP()
{
// Let the reference handle the deallocation!
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
HTTP & operator = (const HTTP & o);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
HTTP & operator = (HTTP && o);
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const HTTP & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const
{
return _SC("");
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
*
*/
};
} // Namespace:: SqMod
#endif // _SQMG_HTTP_HPP_

View File

@ -1,181 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
void ConnectionHnd::Validate() const
{
// Is the handle valid?
if ((m_Hnd == nullptr))
{
STHROWF("Invalid Mongoose connection reference");
}
}
// ------------------------------------------------------------------------------------------------
void ConnectionHnd::Validate(CSStr act) const
{
// Is the handle valid?
if ((m_Hnd == nullptr))
{
STHROWF("Invalid Mongoose connection reference while: %s", act);
}
}
// ------------------------------------------------------------------------------------------------
void ConnectionHnd::EvFwd(Pointer nc, Int32 ev_type, void * ev_data)
{
if (!nc->user_data)
{
SqMod_LogErr("Event dispatched without valid instance");
}
else
{
static_cast< Handle * >(nc->user_data)->Event(ev_type, ev_data);
}
}
// ------------------------------------------------------------------------------------------------
ConnectionHnd::Handle::Handle(Pointer con)
: mCon(con)
, mRef(1)
{
// Validate the connection handle
if (!mCon)
{
STHROWF("Invalid Mongoose connection handle");
}
// Associate with the connection instance
mCon->user_data = this;
}
// ------------------------------------------------------------------------------------------------
ConnectionHnd::Handle::~Handle()
{
// Tell the manager to close this connection as soon as possible
mCon->flags |= MG_F_CLOSE_IMMEDIATELY;
}
// ------------------------------------------------------------------------------------------------
void ConnectionHnd::Handle::Event(Int32 ev_type, void * ev_data)
{
switch (ev_type)
{
case MG_EV_POLL:
{
if (!mOnPOLL.IsNull())
{
mOnPOLL.Execute();
}
} break;
case MG_EV_ACCEPT:
{
if (!mOnACCEPT.IsNull())
{
mOnACCEPT.Execute();
}
} break;
case MG_EV_CONNECT:
{
if (!mOnCONNECT.IsNull())
{
mOnCONNECT.Execute(*static_cast< Int32 * >(ev_data));
}
} break;
case MG_EV_RECV:
{
if (!mOnRECV.IsNull())
{
mOnRECV.Execute(*static_cast< Int32 * >(ev_data));
}
} break;
case MG_EV_SEND:
{
if (!mOnSEND.IsNull())
{
mOnSEND.Execute(*static_cast< Int32 * >(ev_data));
}
} break;
case MG_EV_CLOSE:
{
if (!mOnCLOSE.IsNull())
{
mOnCLOSE.Execute();
}
} break;
case MG_EV_TIMER:
{
if (!mOnTIMER.IsNull())
{
mOnTIMER.Execute();
}
} break;
default:
{
if (!mOnUNKNOWN.IsNull())
{
mOnUNKNOWN.Execute();
}
} break;
}
}
// ------------------------------------------------------------------------------------------------
Function & ConnectionHnd::Handle::GetEvent(Int32 ev_type)
{
// Identify the requested event type
switch (ev_type)
{
case MGCE_POLL: return mOnPOLL;
case MGCE_ACCEPT: return mOnACCEPT;
case MGCE_CONNECT: return mOnCONNECT;
case MGCE_RECV: return mOnRECV;
case MGCE_SEND: return mOnSEND;
case MGCE_CLOSE: return mOnCLOSE;
case MGCE_TIMER: return mOnTIMER;
case MGCE_HTTP_REQUEST: return mOnHTTP_REQUEST;
case MGCE_HTTP_REPLY: return mOnHTTP_REPLY;
case MGCE_HTTP_CHUNK: return mOnHTTP_CHUNK;
case MGCE_SSI_CALL: return mOnSSI_CALL;
case MGCE_WEBSOCKET_HANDSHAKE_REQUEST: return mOnWEBSOCKET_HANDSHAKE_REQUEST;
case MGCE_WEBSOCKET_HANDSHAKE_DONE: return mOnWEBSOCKET_HANDSHAKE_DONE;
case MGCE_WEBSOCKET_FRAME: return mOnWEBSOCKET_FRAME;
case MGCE_WEBSOCKET_CONTROL_FRAME: return mOnWEBSOCKET_CONTROL_FRAME;
case MGCE_HTTP_MULTIPART_REQUEST: return mOnHTTP_MULTIPART_REQUEST;
case MGCE_HTTP_PART_BEGIN: return mOnHTTP_PART_BEGIN;
case MGCE_HTTP_PART_DATA: return mOnHTTP_PART_DATA;
case MGCE_HTTP_PART_END: return mOnHTTP_PART_END;
case MGCE_MQTT_CONNECT: return mOnMQTT_CONNECT;
case MGCE_MQTT_CONNACK: return mOnMQTT_CONNACK;
case MGCE_MQTT_PUBLISH: return mOnMQTT_PUBLISH;
case MGCE_MQTT_PUBACK: return mOnMQTT_PUBACK;
case MGCE_MQTT_PUBREC: return mOnMQTT_PUBREC;
case MGCE_MQTT_PUBREL: return mOnMQTT_PUBREL;
case MGCE_MQTT_PUBCOMP: return mOnMQTT_PUBCOMP;
case MGCE_MQTT_SUBSCRIBE: return mOnMQTT_SUBSCRIBE;
case MGCE_MQTT_SUBACK: return mOnMQTT_SUBACK;
case MGCE_MQTT_UNSUBSCRIBE: return mOnMQTT_UNSUBSCRIBE;
case MGCE_MQTT_UNSUBACK: return mOnMQTT_UNSUBACK;
case MGCE_MQTT_PINGREQ: return mOnMQTT_PINGREQ;
case MGCE_MQTT_PINGRESP: return mOnMQTT_PINGRESP;
case MGCE_MQTT_DISCONNECT: return mOnMQTT_DISCONNECT;
case MGCE_MQTT_CONNACK_ACCEPTED: return mOnMQTT_CONNACK_ACCEPTED;
case MGCE_MQTT_CONNACK_UNACCEPTABLE_VERSION: return mOnMQTT_CONNACK_UNACCEPTABLE_VERSION;
case MGCE_MQTT_CONNACK_IDENTIFIER_REJECTED: return mOnMQTT_CONNACK_IDENTIFIER_REJECTED;
case MGCE_MQTT_CONNACK_SERVER_UNAVAILABLE: return mOnMQTT_CONNACK_SERVER_UNAVAILABLE;
case MGCE_MQTT_CONNACK_BAD_AUTH: return mOnMQTT_CONNACK_BAD_AUTH;
case MGCE_MQTT_CONNACK_NOT_AUTHORIZED: return mOnMQTT_CONNACK_NOT_AUTHORIZED;
case MGCE_COAP_CON: return mOnCOAP_CON;
case MGCE_COAP_NOC: return mOnCOAP_NOC;
case MGCE_COAP_ACK: return mOnCOAP_ACK;
case MGCE_COAP_RST: return mOnCOAP_RST;
case MGEV_UNKNOWN: return mOnUNKNOWN;
default: break;
}
// Default to a null function
return NullFunction();
}
} // Namespace:: SqMod

View File

@ -1,341 +0,0 @@
#ifndef _SQMG_HANDLE_CONNECTION_HPP_
#define _SQMG_HANDLE_CONNECTION_HPP_
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages a reference counted connection and its associated resources.
*/
class ConnectionHnd
{
// --------------------------------------------------------------------------------------------
friend class Manager;
friend class Connection;
public:
// --------------------------------------------------------------------------------------------
typedef mg_connection Type; // The managed type.
// --------------------------------------------------------------------------------------------
typedef Type* Pointer; // Pointer to the managed type.
typedef const Type* ConstPtr; // Constant pointer to the managed type.
// --------------------------------------------------------------------------------------------
typedef Type& Reference; // Reference to the managed type.
typedef const Type& ConstRef; // Constant reference to the managed type.
// --------------------------------------------------------------------------------------------
typedef unsigned int Counter; // Reference counter type.
/* --------------------------------------------------------------------------------------------
* Validate the connection handle and throw an error if invalid.
*/
void Validate() const;
/* --------------------------------------------------------------------------------------------
* Validate the connection handle and throw an error if invalid.
*/
void Validate(CSStr act) const;
/* --------------------------------------------------------------------------------------------
* Forward connection events to the associated instance.
*/
static void EvFwd(Pointer nc, Int32 ev_type, void * ev_data);
protected:
/* --------------------------------------------------------------------------------------------
* The structure that holds the data associated with a physically simulated world.
*/
struct Handle
{
// ----------------------------------------------------------------------------------------
Pointer mCon; // The managed connection structure.
Counter mRef; // Reference count to the managed handles.
// ----------------------------------------------------------------------------------------
Function mOnPOLL;
Function mOnACCEPT;
Function mOnCONNECT;
Function mOnRECV;
Function mOnSEND;
Function mOnCLOSE;
Function mOnTIMER;
Function mOnHTTP_REQUEST;
Function mOnHTTP_REPLY;
Function mOnHTTP_CHUNK;
Function mOnSSI_CALL;
Function mOnWEBSOCKET_HANDSHAKE_REQUEST;
Function mOnWEBSOCKET_HANDSHAKE_DONE;
Function mOnWEBSOCKET_FRAME;
Function mOnWEBSOCKET_CONTROL_FRAME;
Function mOnHTTP_MULTIPART_REQUEST;
Function mOnHTTP_PART_BEGIN;
Function mOnHTTP_PART_DATA;
Function mOnHTTP_PART_END;
Function mOnMQTT_CONNECT;
Function mOnMQTT_CONNACK;
Function mOnMQTT_PUBLISH;
Function mOnMQTT_PUBACK;
Function mOnMQTT_PUBREC;
Function mOnMQTT_PUBREL;
Function mOnMQTT_PUBCOMP;
Function mOnMQTT_SUBSCRIBE;
Function mOnMQTT_SUBACK;
Function mOnMQTT_UNSUBSCRIBE;
Function mOnMQTT_UNSUBACK;
Function mOnMQTT_PINGREQ;
Function mOnMQTT_PINGRESP;
Function mOnMQTT_DISCONNECT;
Function mOnMQTT_CONNACK_ACCEPTED;
Function mOnMQTT_CONNACK_UNACCEPTABLE_VERSION;
Function mOnMQTT_CONNACK_IDENTIFIER_REJECTED;
Function mOnMQTT_CONNACK_SERVER_UNAVAILABLE;
Function mOnMQTT_CONNACK_BAD_AUTH;
Function mOnMQTT_CONNACK_NOT_AUTHORIZED;
Function mOnCOAP_CON;
Function mOnCOAP_NOC;
Function mOnCOAP_ACK;
Function mOnCOAP_RST;
Function mOnUNKNOWN;
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Handle(Pointer con);
/* ----------------------------------------------------------------------------------------
* Destructor.
*/
~Handle();
/* ----------------------------------------------------------------------------------------
* Handle a certain event for this connection.
*/
void Event(Int32 ev_type, void * ev_data);
/* ----------------------------------------------------------------------------------------
* Retrieve the sript callback associated with a certain identifier.
*/
Function & GetEvent(Int32 ev_type);
};
private:
// --------------------------------------------------------------------------------------------
Handle * m_Hnd; // The managed handle instance.
/* --------------------------------------------------------------------------------------------
* Grab a strong reference to a connection handle.
*/
void Grab()
{
if (m_Hnd)
{
++(m_Hnd->mRef);
}
}
/* --------------------------------------------------------------------------------------------
* Drop a strong reference to a connection handle.
*/
void Drop()
{
if (m_Hnd && --(m_Hnd->mRef) == 0)
{
delete m_Hnd; // Let the destructor take care of cleaning up (if necessary)
}
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
ConnectionHnd(Pointer con)
: m_Hnd(new Handle(con))
{
/* ... */
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
ConnectionHnd()
: m_Hnd(nullptr)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
ConnectionHnd(Handle * hnd)
: m_Hnd(hnd)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
ConnectionHnd(const ConnectionHnd & o)
: m_Hnd(o.m_Hnd)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
ConnectionHnd(ConnectionHnd && o)
: m_Hnd(o.m_Hnd)
{
o.m_Hnd = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~ConnectionHnd()
{
Drop();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
ConnectionHnd & operator = (const ConnectionHnd & o)
{
if (m_Hnd != o.m_Hnd)
{
Drop();
m_Hnd = o.m_Hnd;
Grab();
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
ConnectionHnd & operator = (ConnectionHnd && o)
{
if (m_Hnd != o.m_Hnd)
{
m_Hnd = o.m_Hnd;
o.m_Hnd = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two connection handles.
*/
bool operator == (const ConnectionHnd & o) const
{
return (m_Hnd == o.m_Hnd);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two connection handles.
*/
bool operator != (const ConnectionHnd & o) const
{
return (m_Hnd != o.m_Hnd);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean for use in boolean operations.
*/
operator bool () const
{
return (m_Hnd != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Pointer ()
{
return (m_Hnd != nullptr) ? (m_Hnd->mCon) : nullptr;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Pointer () const
{
return (m_Hnd != nullptr) ? (m_Hnd->mCon) : nullptr;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Reference ()
{
assert((m_Hnd != nullptr));
return *(m_Hnd->mCon);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator ConstRef () const
{
assert((m_Hnd != nullptr));
return *(m_Hnd->mCon);
}
/* --------------------------------------------------------------------------------------------
* Member operator for dereferencing the managed pointer.
*/
Handle * operator -> () const
{
assert(m_Hnd != nullptr);
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Indirection operator for obtaining a reference of the managed pointer.
*/
Handle & operator * () const
{
assert(m_Hnd != nullptr);
return *m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the raw handle structure pointer.
*/
Handle * HndPtr()
{
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the raw handle structure pointer.
*/
Handle * HndPtr() const
{
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of active references to the managed instance.
*/
Counter Count() const
{
return (m_Hnd != nullptr) ? m_Hnd->mRef : 0;
}
};
} // Namespace:: SqMod
#endif // _SQMG_HANDLE_CONNECTION_HPP_

View File

@ -1,74 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Handle/Manager.hpp"
// ------------------------------------------------------------------------------------------------
#include <ctime>
#include <algorithm>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
ManagerHnd::Managers ManagerHnd::s_Managers;
// ------------------------------------------------------------------------------------------------
void ManagerHnd::Validate() const
{
// Is the handle valid?
if ((m_Hnd == nullptr))
{
STHROWF("Invalid Mongoose manager reference");
}
}
// ------------------------------------------------------------------------------------------------
void ManagerHnd::Validate(CSStr act) const
{
// Is the handle valid?
if ((m_Hnd == nullptr))
{
STHROWF("Invalid Mongoose manager reference while: %s", act);
}
}
// ------------------------------------------------------------------------------------------------
ManagerHnd::Handle::Handle()
: mMgr()
, mRef(1)
{
// Initialize event manager object
mg_mgr_init(&mMgr, this);
// Add this instance to the pool
s_Managers.push_back(this);
}
// ------------------------------------------------------------------------------------------------
ManagerHnd::Handle::~Handle()
{
// Attempt to release manager resources
mg_mgr_free(&mMgr);
// Remove this from the pool
s_Managers.erase(std::remove(s_Managers.begin(), s_Managers.end(), this), s_Managers.end());
}
// ------------------------------------------------------------------------------------------------
Int64 ManagerHnd::Handle::Pool(Int32 milli)
{
// Pool the manager for events and store the returned timestamp
std::time_t timestamp = mg_mgr_poll(&mMgr, milli);
// Create a tm structure pointing to the unix epoch time
std::tm epoch_strt;
epoch_strt.tm_sec = 0;
epoch_strt.tm_min = 0;
epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 0;
epoch_strt.tm_year = 70;
epoch_strt.tm_isdst = -1;
// Convert it to a time structure
std::time_t basetime = std::mktime(&epoch_strt);
// Return obtained timestamp
return static_cast< Int64 >(std::difftime(timestamp, basetime));
}
} // Namespace:: SqMod

View File

@ -1,291 +0,0 @@
#ifndef _SQMG_HANDLE_MANAGER_HPP_
#define _SQMG_HANDLE_MANAGER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Manages a reference counted manager and its associated resources.
*/
class ManagerHnd
{
// --------------------------------------------------------------------------------------------
friend class Manager;
friend class Connection;
public:
// --------------------------------------------------------------------------------------------
typedef mg_mgr Type; // The managed type.
// --------------------------------------------------------------------------------------------
typedef Type* Pointer; // Pointer to the managed type.
typedef const Type* ConstPtr; // Constant pointer to the managed type.
// --------------------------------------------------------------------------------------------
typedef Type& Reference; // Reference to the managed type.
typedef const Type& ConstRef; // Constant reference to the managed type.
// --------------------------------------------------------------------------------------------
typedef unsigned int Counter; // Reference counter type.
/* --------------------------------------------------------------------------------------------
* Validate the manager handle and throw an error if invalid.
*/
void Validate() const;
/* --------------------------------------------------------------------------------------------
* Validate the manager handle and throw an error if invalid.
*/
void Validate(CSStr act) const;
protected:
/* --------------------------------------------------------------------------------------------
* The structure that holds the data associated with a physically simulated world.
*/
struct Handle
{
// ----------------------------------------------------------------------------------------
Type mMgr; // The managed manager structure.
Counter mRef; // Reference count to the managed handles.
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Handle();
/* ----------------------------------------------------------------------------------------
* Destructor.
*/
~Handle();
/* ----------------------------------------------------------------------------------------
* Pool the managed connections for events.
*/
Int64 Pool(Int32 milli);
};
// --------------------------------------------------------------------------------------------
typedef std::vector< Handle * > Managers; // List of created managers.
// --------------------------------------------------------------------------------------------
static Managers s_Managers;
private:
// --------------------------------------------------------------------------------------------
Handle * m_Hnd; // The managed handle instance.
/* --------------------------------------------------------------------------------------------
* Grab a strong reference to a connection handle.
*/
void Grab()
{
if (m_Hnd)
{
++(m_Hnd->mRef);
}
}
/* --------------------------------------------------------------------------------------------
* Drop a strong reference to a connection handle.
*/
void Drop()
{
if (m_Hnd && --(m_Hnd->mRef) == 0)
{
delete m_Hnd; // Let the destructor take care of cleaning up (if necessary)
}
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
ManagerHnd(HndInit init)
: m_Hnd(init == HndInit::InitHnd ? new Handle() : nullptr)
{
/* ... */
}
public:
/* --------------------------------------------------------------------------------------------
* Default constructor (null).
*/
ManagerHnd()
: m_Hnd(nullptr)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
ManagerHnd(Handle * hnd)
: m_Hnd(hnd)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
ManagerHnd(const ManagerHnd & o)
: m_Hnd(o.m_Hnd)
{
Grab();
}
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
ManagerHnd(ManagerHnd && o)
: m_Hnd(o.m_Hnd)
{
o.m_Hnd = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~ManagerHnd()
{
Drop();
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
ManagerHnd & operator = (const ManagerHnd & o)
{
if (m_Hnd != o.m_Hnd)
{
Drop();
m_Hnd = o.m_Hnd;
Grab();
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
ManagerHnd & operator = (ManagerHnd && o)
{
if (m_Hnd != o.m_Hnd)
{
m_Hnd = o.m_Hnd;
o.m_Hnd = nullptr;
}
return *this;
}
/* --------------------------------------------------------------------------------------------
* Perform an equality comparison between two connection handles.
*/
bool operator == (const ManagerHnd & o) const
{
return (m_Hnd == o.m_Hnd);
}
/* --------------------------------------------------------------------------------------------
* Perform an inequality comparison between two connection handles.
*/
bool operator != (const ManagerHnd & o) const
{
return (m_Hnd != o.m_Hnd);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean for use in boolean operations.
*/
operator bool () const
{
return (m_Hnd != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Pointer ()
{
return (m_Hnd != nullptr) ? &(m_Hnd->mMgr) : nullptr;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Pointer () const
{
return (m_Hnd != nullptr) ? &(m_Hnd->mMgr) : nullptr;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator Reference ()
{
assert((m_Hnd != nullptr));
return (m_Hnd->mMgr);
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed instance.
*/
operator ConstRef () const
{
assert((m_Hnd != nullptr));
return (m_Hnd->mMgr);
}
/* --------------------------------------------------------------------------------------------
* Member operator for dereferencing the managed pointer.
*/
Handle * operator -> () const
{
assert(m_Hnd != nullptr);
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Indirection operator for obtaining a reference of the managed pointer.
*/
Handle & operator * () const
{
assert(m_Hnd != nullptr);
return *m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the raw handle structure pointer.
*/
Handle * HndPtr()
{
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the raw handle structure pointer.
*/
Handle * HndPtr() const
{
return m_Hnd;
}
/* --------------------------------------------------------------------------------------------
* Retrieve the number of active references to the managed instance.
*/
Counter Count() const
{
return (m_Hnd != nullptr) ? m_Hnd->mRef : 0;
}
};
} // Namespace:: SqMod
#endif // _SQMG_HANDLE_MANAGER_HPP_

View File

@ -1,152 +0,0 @@
// -----------------------------------------------------------------------------------------------
#include "Manager.hpp"
#include "Connection.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstring>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger Manager::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("SqMgManager");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Int32 Manager::Cmp(const Manager & o) const
{
if (m_Handle == o.m_Handle)
{
return 0;
}
else if (m_Handle.HndPtr() > o.m_Handle.HndPtr())
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
Connection Manager::Bind(CSStr addr) const
{
return Bind(addr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::Bind(CSStr addr, Uint32 flags) const
{
// Validate the managed handle
m_Handle.Validate();
// Validate the specified address
if (!addr)
{
STHROWF("Invalid bind address: null");
}
// Create a binding options structure
mg_bind_opts opts;
// Initialize the structure to 0
std::memset(&opts, 0, sizeof(mg_bind_opts));
// Assign the specified flags
opts.flags = flags;
// Attemmt to create the connection handle
mg_connection * connection = mg_bind_opt(m_Handle, addr, ConnectionHnd::EvFwd, opts);
// Validate the obtained connection handle
if (!connection)
{
STHROWF("Unable to create listening connection [%s]", *opts.error_string);
}
// Return the obtained connection
return Connection(ConnectionHnd(connection));
}
// ------------------------------------------------------------------------------------------------
Connection Manager::Connect(CSStr addr) const
{
return Connect(addr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::Connect(CSStr addr, Uint32 flags) const
{
// Validate the managed handle
m_Handle.Validate();
// Validate the specified address
if (!addr)
{
STHROWF("Invalid bind address: null");
}
// Create a binding options structure
mg_connect_opts opts;
// Initialize the structure to 0
std::memset(&opts, 0, sizeof(mg_connect_opts));
// Assign the specified flags
opts.flags = flags;
// Attemmt to create the connection handle
mg_connection * connection = mg_connect_opt(m_Handle, addr, ConnectionHnd::EvFwd, opts);
// Validate the obtained connection handle
if (!connection)
{
STHROWF("Unable to create listening connection [%s]", *opts.error_string);
}
// Return the obtained connection
return Connection(ConnectionHnd(connection));
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectWS(CSStr url) const
{
return ConnectWS(url, nullptr, nullptr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectWS(CSStr url, CSStr protocol) const
{
return ConnectWS(url, protocol, nullptr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectWS(CSStr url, CSStr protocol, CSStr headers) const
{
return ConnectWS(url, protocol, headers, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectWS(CSStr url, CSStr protocol, CSStr headers, Uint32 flags) const
{
return Connection();
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectHTTP(CSStr url) const
{
return ConnectHTTP(url, nullptr, nullptr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectHTTP(CSStr url, CSStr headers) const
{
return ConnectHTTP(url, headers, nullptr, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectHTTP(CSStr url, CSStr headers, CSStr post) const
{
return ConnectHTTP(url, headers, post, 0);
}
// ------------------------------------------------------------------------------------------------
Connection Manager::ConnectHTTP(CSStr url, CSStr headers, CSStr post, Uint32 flags) const
{
return Connection();
}
} // Namespace:: SqMod

View File

@ -1,149 +0,0 @@
#ifndef _SQMG_MANAGER_HPP_
#define _SQMG_MANAGER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Manager.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Allows management of the connection manager.
*/
class Manager
{
private:
// --------------------------------------------------------------------------------------------
ManagerHnd m_Handle; // Reference to the manager instance.
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Manager()
: m_Handle(HndInit::InitHnd)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
Manager(const ManagerHnd & m)
: m_Handle(m)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Manager(const Manager & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Manager(Manager && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Manager()
{
// Let the reference handle the deallocation!
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Manager & operator = (const Manager & o);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Manager & operator = (Manager && o);
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Manager & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const
{
return _SC("");
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Create listening connection.
*/
Connection Bind(CSStr addr) const;
/* --------------------------------------------------------------------------------------------
* Create listening connection.
*/
Connection Bind(CSStr addr, Uint32 flags) const;
/* --------------------------------------------------------------------------------------------
* Connect to a remote host.
*/
Connection Connect(CSStr addr) const;
/* --------------------------------------------------------------------------------------------
* Connect to a remote host.
*/
Connection Connect(CSStr addr, Uint32 flags) const;
/* --------------------------------------------------------------------------------------------
* Creates an outbound WebSocket connection.
*/
Connection ConnectWS(CSStr url) const;
/* --------------------------------------------------------------------------------------------
* Creates an outbound WebSocket connection.
*/
Connection ConnectWS(CSStr url, CSStr protocol) const;
/* --------------------------------------------------------------------------------------------
* Creates an outbound WebSocket connection.
*/
Connection ConnectWS(CSStr url, CSStr protocol, CSStr headers) const;
/* --------------------------------------------------------------------------------------------
* Creates an outbound WebSocket connection.
*/
Connection ConnectWS(CSStr url, CSStr protocol, CSStr headers, Uint32 flags) const;
/* --------------------------------------------------------------------------------------------
* Creates outbound HTTP connection.
*/
Connection ConnectHTTP(CSStr url) const;
/* --------------------------------------------------------------------------------------------
* Creates outbound HTTP connection.
*/
Connection ConnectHTTP(CSStr url, CSStr headers) const;
/* --------------------------------------------------------------------------------------------
* Creates outbound HTTP connection.
*/
Connection ConnectHTTP(CSStr url, CSStr headers, CSStr post) const;
/* --------------------------------------------------------------------------------------------
* Creates outbound HTTP connection.
*/
Connection ConnectHTTP(CSStr url, CSStr headers, CSStr post, Uint32 flags) const;
};
} // Namespace:: SqMod
#endif // _SQMG_MANAGER_HPP_

View File

View File

View File

@ -1,308 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
#include "Manager.hpp"
#include "Connection.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Register the module API under the obtained virtual machine.
*/
static bool RegisterAPI(HSQUIRRELVM vm)
{
// Make sure there's a valid virtual machine before proceeding
if (!vm)
{
OutputError("%s: Cannot register API without a valid virtual machine", SQMG_NAME);
// Registration failed
return false;
}
Table mgns(vm);
mgns.Bind(_SC("Manager"), Class< Manager >(vm, _SC("SqMgManager"))
// Constructors
.Ctor()
.Ctor< const Manager & >()
// Core Meta-methods
.Func(_SC("_cmp"), &Manager::Cmp)
.SquirrelFunc(_SC("_typename"), &Manager::Typename)
.Func(_SC("_tostring"), &Manager::ToString)
// Properties
//.Prop(_SC("Valid"), &Manager::IsValid)
// Member Methods
//.Func(_SC("Check"), &Manager::Check)
// Overloaded Methods
.Overload< Connection (Manager::*)(CSStr) const >(_SC("Bind"), &Manager::Bind)
.Overload< Connection (Manager::*)(CSStr, Uint32) const >(_SC("Bind"), &Manager::Bind)
.Overload< Connection (Manager::*)(CSStr) const >(_SC("Connect"), &Manager::Connect)
.Overload< Connection (Manager::*)(CSStr, Uint32) const >(_SC("Connect"), &Manager::Connect)
);
mgns.Bind(_SC("Connection"), Class< Connection >(vm, _SC("SqMgConnection"))
// Constructors
.Ctor()
.Ctor< const Connection & >()
// Core Meta-methods
.Func(_SC("_cmp"), &Connection::Cmp)
.SquirrelFunc(_SC("_typename"), &Connection::Typename)
.Func(_SC("_tostring"), &Connection::ToString)
// Properties
//.Prop(_SC("Valid"), &Connection::IsValid)
// Member Methods
//.Func(_SC("Check"), &Connection::Check)
);
RootTable(vm).Bind(_SC("SqMg"), mgns);
ConstTable(vm).Enum(_SC("EMgF"), Enumeration(vm)
.Const(_SC("LISTENING"), MG_F_LISTENING)
.Const(_SC("UDP"), MG_F_UDP)
.Const(_SC("RESOLVING"), MG_F_RESOLVING)
.Const(_SC("CONNECTING"), MG_F_CONNECTING)
.Const(_SC("SSL_HANDSHAKE_DONE"), MG_F_SSL_HANDSHAKE_DONE)
.Const(_SC("WANT_READ"), MG_F_WANT_READ)
.Const(_SC("WANT_WRITE"), MG_F_WANT_WRITE)
.Const(_SC("IS_WEBSOCKET"), MG_F_IS_WEBSOCKET)
.Const(_SC("SEND_AND_CLOSE"), MG_F_SEND_AND_CLOSE)
.Const(_SC("CLOSE_IMMEDIATELY"), MG_F_CLOSE_IMMEDIATELY)
.Const(_SC("WEBSOCKET_NO_DEFRAG"), MG_F_WEBSOCKET_NO_DEFRAG)
.Const(_SC("DELETE_CHUNK"), MG_F_DELETE_CHUNK)
.Const(_SC("USER_1"), MG_F_USER_1)
.Const(_SC("USER_2"), MG_F_USER_2)
.Const(_SC("USER_3"), MG_F_USER_3)
.Const(_SC("USER_4"), MG_F_USER_4)
.Const(_SC("USER_5"), MG_F_USER_5)
.Const(_SC("USER_6"), MG_F_USER_6)
);
ConstTable(vm).Enum(_SC("MgEv"), Enumeration(vm)
.Const(_SC("UNKNOWN"), static_cast< Int32 >(MGEV_UNKNOWN))
.Const(_SC("POLL"), static_cast< Int32 >(MGCE_POLL))
.Const(_SC("ACCEPT"), static_cast< Int32 >(MGCE_ACCEPT))
.Const(_SC("CONNECT"), static_cast< Int32 >(MGCE_CONNECT))
.Const(_SC("RECV"), static_cast< Int32 >(MGCE_RECV))
.Const(_SC("SEND"), static_cast< Int32 >(MGCE_SEND))
.Const(_SC("CLOSE"), static_cast< Int32 >(MGCE_CLOSE))
.Const(_SC("TIMER"), static_cast< Int32 >(MGCE_TIMER))
.Const(_SC("HTTP_REQUEST"), static_cast< Int32 >(MGCE_HTTP_REQUEST))
.Const(_SC("HTTP_REPLY"), static_cast< Int32 >(MGCE_HTTP_REPLY))
.Const(_SC("HTTP_CHUNK"), static_cast< Int32 >(MGCE_HTTP_CHUNK))
.Const(_SC("SSI_CALL"), static_cast< Int32 >(MGCE_SSI_CALL))
.Const(_SC("WEBSOCKET_HANDSHAKE_REQUEST"), static_cast< Int32 >(MGCE_WEBSOCKET_HANDSHAKE_REQUEST))
.Const(_SC("WEBSOCKET_HANDSHAKE_DONE"), static_cast< Int32 >(MGCE_WEBSOCKET_HANDSHAKE_DONE))
.Const(_SC("WEBSOCKET_FRAME"), static_cast< Int32 >(MGCE_WEBSOCKET_FRAME))
.Const(_SC("WEBSOCKET_CONTROL_FRAME"), static_cast< Int32 >(MGCE_WEBSOCKET_CONTROL_FRAME))
.Const(_SC("HTTP_MULTIPART_REQUEST"), static_cast< Int32 >(MGCE_HTTP_MULTIPART_REQUEST))
.Const(_SC("HTTP_PART_BEGIN"), static_cast< Int32 >(MGCE_HTTP_PART_BEGIN))
.Const(_SC("HTTP_PART_DATA"), static_cast< Int32 >(MGCE_HTTP_PART_DATA))
.Const(_SC("HTTP_PART_END"), static_cast< Int32 >(MGCE_HTTP_PART_END))
.Const(_SC("MQTT_CONNECT"), static_cast< Int32 >(MGCE_MQTT_CONNECT))
.Const(_SC("MQTT_CONNACK"), static_cast< Int32 >(MGCE_MQTT_CONNACK))
.Const(_SC("MQTT_PUBLISH"), static_cast< Int32 >(MGCE_MQTT_PUBLISH))
.Const(_SC("MQTT_PUBACK"), static_cast< Int32 >(MGCE_MQTT_PUBACK))
.Const(_SC("MQTT_PUBREC"), static_cast< Int32 >(MGCE_MQTT_PUBREC))
.Const(_SC("MQTT_PUBREL"), static_cast< Int32 >(MGCE_MQTT_PUBREL))
.Const(_SC("MQTT_PUBCOMP"), static_cast< Int32 >(MGCE_MQTT_PUBCOMP))
.Const(_SC("MQTT_SUBSCRIBE"), static_cast< Int32 >(MGCE_MQTT_SUBSCRIBE))
.Const(_SC("MQTT_SUBACK"), static_cast< Int32 >(MGCE_MQTT_SUBACK))
.Const(_SC("MQTT_UNSUBSCRIBE"), static_cast< Int32 >(MGCE_MQTT_UNSUBSCRIBE))
.Const(_SC("MQTT_UNSUBACK"), static_cast< Int32 >(MGCE_MQTT_UNSUBACK))
.Const(_SC("MQTT_PINGREQ"), static_cast< Int32 >(MGCE_MQTT_PINGREQ))
.Const(_SC("MQTT_PINGRESP"), static_cast< Int32 >(MGCE_MQTT_PINGRESP))
.Const(_SC("MQTT_DISCONNECT"), static_cast< Int32 >(MGCE_MQTT_DISCONNECT))
.Const(_SC("MQTT_CONNACK_ACCEPTED"), static_cast< Int32 >(MGCE_MQTT_CONNACK_ACCEPTED))
.Const(_SC("MQTT_CONNACK_UNACCEPTABLE_VERSION"), static_cast< Int32 >(MGCE_MQTT_CONNACK_UNACCEPTABLE_VERSION))
.Const(_SC("MQTT_CONNACK_IDENTIFIER_REJECTED"), static_cast< Int32 >(MGCE_MQTT_CONNACK_IDENTIFIER_REJECTED))
.Const(_SC("MQTT_CONNACK_SERVER_UNAVAILABLE"), static_cast< Int32 >(MGCE_MQTT_CONNACK_SERVER_UNAVAILABLE))
.Const(_SC("MQTT_CONNACK_BAD_AUTH"), static_cast< Int32 >(MGCE_MQTT_CONNACK_BAD_AUTH))
.Const(_SC("MQTT_CONNACK_NOT_AUTHORIZED"), static_cast< Int32 >(MGCE_MQTT_CONNACK_NOT_AUTHORIZED))
.Const(_SC("COAP_CON"), static_cast< Int32 >(MGCE_COAP_CON))
.Const(_SC("COAP_NOC"), static_cast< Int32 >(MGCE_COAP_NOC))
.Const(_SC("COAP_ACK"), static_cast< Int32 >(MGCE_COAP_ACK))
.Const(_SC("COAP_RST"), static_cast< Int32 >(MGCE_COAP_RST))
.Const(_SC("MAX"), static_cast< Int32 >(MGCE_MAX))
);
// Registration was successful
return true;
}
/* ------------------------------------------------------------------------------------------------
* Load the module on the virtual machine provided by the host module.
*/
static bool OnSquirrelLoad()
{
// Make sure that we have a valid module API
if (!SqMod_GetSquirrelVM)
{
OutputError("%s: Cannot obtain the Squirrel virtual machine without the module API", SQMG_NAME);
// Unable to proceed!
return false;
}
// Obtain the Squirrel virtual machine from the host plug-in
DefaultVM::Set(SqMod_GetSquirrelVM());
// Make sure that a valid virtual machine exists
if (!DefaultVM::Get())
{
OutputError("%s: Squirrel virtual machine obtained from the host plug-in is invalid", SQMG_NAME);
// Unable to proceed!
return false;
}
// Prevent common null objects from using dead virtual machines
NullArray() = Array();
NullTable() = Table();
NullObject() = Object();
NullFunction() = Function();
// Register the module API
if (RegisterAPI(DefaultVM::Get()))
{
OutputMessage("Registered: %s", SQMG_NAME);
}
else
{
return false;
}
// At this point, the module was successfully loaded
return true;
}
/* ------------------------------------------------------------------------------------------------
* The virtual machine is about to be terminated and script resources should be released.
*/
static void OnSquirrelTerminate()
{
OutputMessage("Terminating: %s", SQMG_NAME);
// Release null objects just in case
NullObject().Release();
NullTable().Release();
NullArray().Release();
NullFunction().ReleaseGently();
// Release script resources...
}
/* ------------------------------------------------------------------------------------------------
* The virtual machined is about to be closed. Last chance to release anything manually.
*/
static void OnSquirrelClosing()
{
// Nothing to release manually...
}
/* ------------------------------------------------------------------------------------------------
* The virtual machined was closed and all memory associated with it was released.
*/
static void OnSquirrelReleased()
{
// Release the current virtual machine, if any
DefaultVM::Set(nullptr);
}
/* ------------------------------------------------------------------------------------------------
* React to command sent by other plug-ins.
*/
static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
{
switch(command_identifier)
{
case SQMOD_INITIALIZE_CMD:
{
if (CheckModuleAPIVer(message, SQMG_NAME))
{
try
{
ImportModuleAPI(_Func, SQMG_NAME);
}
catch (const Sqrat::Exception & e)
{
OutputError("%s", e.what());
// Failed to initialize
return 0;
}
}
} break;
case SQMOD_LOAD_CMD:
{
return OnSquirrelLoad();
} break;
case SQMOD_TERMINATE_CMD:
{
OnSquirrelTerminate();
} break;
case SQMOD_CLOSING_CMD:
{
OnSquirrelClosing();
} break;
case SQMOD_RELEASED_CMD:
{
OnSquirrelReleased();
} break;
default: break;
}
return 1;
}
/* ------------------------------------------------------------------------------------------------
* The server was initialized and this plug-in was loaded successfully.
*/
static uint8_t OnServerInitialise()
{
return 1; // Initialization was successful
}
/* ------------------------------------------------------------------------------------------------
* The server is about to shutdown gracefully.
*/
static void OnServerShutdown(void)
{
// The server may still send callbacks
_Clbk->OnServerInitialise = nullptr;
_Clbk->OnServerShutdown = nullptr;
_Clbk->OnPluginCommand = nullptr;
}
} // Namespace:: SqMod
// ------------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCallbacks * callbacks, PluginInfo * info)
{
using namespace SqMod;
// Output plug-in header
std::puts("");
OutputMessage("--------------------------------------------------------------------");
OutputMessage("Plug-in: %s", SQMG_NAME);
OutputMessage("Author: %s", SQMG_AUTHOR);
OutputMessage("Legal: %s", SQMG_COPYRIGHT);
OutputMessage("--------------------------------------------------------------------");
std::puts("");
// Make sure that the module was loaded after the host plug-in
if (!CheckModuleOrder(functions, info->pluginId, SQMG_NAME))
{
return SQMOD_FAILURE;
}
// Store server proxies
_Func = functions;
_Clbk = callbacks;
_Info = info;
// Assign plug-in version
_Info->pluginVersion = SQMG_VERSION;
_Info->apiMajorVersion = PLUGIN_API_MAJOR;
_Info->apiMinorVersion = PLUGIN_API_MINOR;
// Assign the plug-in name
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQMG_HOST_NAME);
// Bind to the server callbacks
_Clbk->OnServerInitialise = OnServerInitialise;
_Clbk->OnServerShutdown = OnServerShutdown;
_Clbk->OnPluginCommand = OnPluginCommand;
// Notify that the plug-in was successfully loaded
OutputMessage("Successfully loaded %s", SQMG_NAME);
// Dummy spacing
std::puts("");
// Done!
return SQMOD_SUCCESS;
}

View File

@ -1,9 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Options.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,12 +0,0 @@
#ifndef _OPTIONS_HPP_
#define _OPTIONS_HPP_
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
} // Namespace:: SqMod
#endif // _OPTIONS_HPP_

View File

View File

View File

@ -1,39 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "WebSocket.hpp"
#include "Manager.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger WebSocket::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("SqMgWebSocket");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
Int32 WebSocket::Cmp(const WebSocket & o) const
{
if (m_Handle == o.m_Handle)
{
return 0;
}
else if (m_Handle.HndPtr() > o.m_Handle.HndPtr())
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,94 +0,0 @@
#ifndef _SQMG_WEBSOCKET_HPP_
#define _SQMG_WEBSOCKET_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/Connection.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Allows management of the connection handle.
*/
class WebSocket
{
private:
// --------------------------------------------------------------------------------------------
ConnectionHnd m_Handle; // Reference to the connection instance.
public:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
WebSocket()
: m_Handle()
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
WebSocket(const ConnectionHnd & c)
: m_Handle(c)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
WebSocket(const WebSocket & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
WebSocket(WebSocket && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~WebSocket()
{
// Let the reference handle the deallocation!
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
WebSocket & operator = (const WebSocket & o);
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
WebSocket & operator = (WebSocket && o);
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const WebSocket & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const
{
return _SC("");
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
*
*/
};
} // Namespace:: SqMod
#endif // _SQMG_WEBSOCKET_HPP_