mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 08:47:17 +01:00
More code cleanup and fixes in the MaxmindDB module.
Implemented the SearchNode wrapper.
This commit is contained in:
parent
fb5a5b0090
commit
7d1493afd3
@ -442,6 +442,8 @@
|
||||
<Unit filename="../modules/mmdb/Metadata.cpp" />
|
||||
<Unit filename="../modules/mmdb/Metadata.hpp" />
|
||||
<Unit filename="../modules/mmdb/Module.cpp" />
|
||||
<Unit filename="../modules/mmdb/SearchNode.cpp" />
|
||||
<Unit filename="../modules/mmdb/SearchNode.hpp" />
|
||||
<Unit filename="../modules/mmdb/SockAddr.cpp" />
|
||||
<Unit filename="../modules/mmdb/Sockaddr.hpp" />
|
||||
<Unit filename="../shared/Base/Buffer.cpp" />
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "Database.hpp"
|
||||
#include "SockAddr.hpp"
|
||||
#include "Metadata.hpp"
|
||||
#include "SearchNode.hpp"
|
||||
#include "LookupResult.hpp"
|
||||
#include "EntryDataList.hpp"
|
||||
|
||||
@ -122,6 +123,24 @@ LookupResult Database::LookupSockAddr(SockAddr & addr)
|
||||
return LookupResult(m_Handle, result);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SearchNode Database::ReadNode(Uint32 node) const
|
||||
{
|
||||
// Validate the database handle
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Prepare a temporary search node
|
||||
MMDB_search_node_s search_node;
|
||||
// Attempt to retrieve the requested node from the database
|
||||
const int status = MMDB_read_node(&(SQMOD_GET_VALID(*this)->mDb), node, &search_node);
|
||||
// Validate the status code
|
||||
if (status != MMDB_SUCCESS)
|
||||
{
|
||||
STHROWF("Unable to get node [%s]", MMDB_strerror(status));
|
||||
}
|
||||
// Return the resulted list
|
||||
return SearchNode(m_Handle, search_node);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_Database(Table & mmns)
|
||||
{
|
||||
@ -143,6 +162,7 @@ void Register_Database(Table & mmns)
|
||||
.Func(_SC("Release"), &Database::Release)
|
||||
.Func(_SC("LookupString"), &Database::LookupString)
|
||||
.Func(_SC("LookupSockAddr"), &Database::LookupSockAddr)
|
||||
.Func(_SC("ReadNode"), &Database::ReadNode)
|
||||
// Member overloads
|
||||
.Overload< void (Database::*)(CSStr) >(_SC("Open"), &Database::Open)
|
||||
.Overload< void (Database::*)(CSStr, Uint32) >(_SC("Open"), &Database::Open)
|
||||
|
@ -12,6 +12,19 @@ namespace SqMod {
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef DbHnd::Type 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.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -21,9 +34,7 @@ protected:
|
||||
void Validate(CCStr file, Int32 line) const;
|
||||
#else
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
#endif //
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
@ -178,6 +189,10 @@ public:
|
||||
*/
|
||||
LookupResult LookupSockAddr(SockAddr & sockaddr);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve a speciffic node from the managed database.
|
||||
*/
|
||||
SearchNode ReadNode(Uint32 node) const;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -12,10 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class Description
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Metadata; // Only a valid meta-data instance can construct this type.
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_description_s Type; // The managed type.
|
||||
@ -28,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -37,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and description pointer and throw an error if invalid.
|
||||
*/
|
||||
@ -54,15 +51,6 @@ private:
|
||||
DbRef m_Handle; // The database associated with this meta-data description.
|
||||
Pointer m_Description; // The inspected meta-data description structure.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and with a specific meta-data description.
|
||||
*/
|
||||
Description(const DbRef & db, Pointer description)
|
||||
: m_Handle(db), m_Description(description)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -74,6 +62,15 @@ public:
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and with a specific meta-data description.
|
||||
*/
|
||||
Description(const DbRef & db, Pointer description)
|
||||
: m_Handle(db), m_Description(description)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
|
@ -12,10 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class EntryData
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class LookupResult; // Only a valid lookup result instance can construct this type.
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_entry_data_s Type; // The managed type.
|
||||
@ -28,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -37,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -54,6 +51,13 @@ private:
|
||||
DbRef m_Handle; // The database from which this result comes from.
|
||||
Type m_Entry; // The managed entry-data structure.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
EntryData();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and take ownership of a certain entry data.
|
||||
*/
|
||||
@ -63,13 +67,6 @@ private:
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
EntryData();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
|
@ -12,11 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class EntryDataList
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Database; // Only a valid database instance can construct this type.
|
||||
friend class LookupResult; // Only a valid lookup result instance can construct this type.
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_entry_data_list_s Type; // The managed type.
|
||||
@ -29,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -38,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -65,6 +61,8 @@ private:
|
||||
Pointer m_List; // The managed entry data list.
|
||||
Pointer m_Elem; // The currently processed element from the list.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and with a specific entry list.
|
||||
*/
|
||||
@ -74,8 +72,6 @@ private:
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
|
@ -115,6 +115,11 @@ SQInteger LookupResult::GetValue(HSQUIRRELVM vm)
|
||||
{
|
||||
return sq_throwerror(vm, "Invalid lookup result instance");
|
||||
}
|
||||
// See if there's a handle
|
||||
else if (!lookup->m_Handle)
|
||||
{
|
||||
return sq_throwerror(vm, "Invalid Maxmind database reference");
|
||||
}
|
||||
// See if there's an entry
|
||||
else if (!(lookup->m_Result.found_entry))
|
||||
{
|
||||
@ -147,7 +152,7 @@ SQInteger LookupResult::GetValue(HSQUIRRELVM vm)
|
||||
ptrlist.push_back(nullptr);
|
||||
|
||||
MMDB_entry_data_s entry_data;
|
||||
// Attempt to retrieve the entire entry data list at once
|
||||
// Attempt to retrieve the specified entry data
|
||||
const int status = MMDB_aget_value(&(lookup->m_Result.entry), &entry_data, ptrlist.data());
|
||||
// Validate the status code
|
||||
if (status != MMDB_SUCCESS)
|
||||
|
@ -12,10 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class LookupResult
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Database; // Only a valid database instance can construct this type.
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_lookup_result_s Type; // The managed type.
|
||||
@ -28,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -37,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -54,6 +51,13 @@ private:
|
||||
DbRef m_Handle; // The database from which this result comes from.
|
||||
Type m_Result; // The managed result structure.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
LookupResult();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and take ownership of a certain result.
|
||||
*/
|
||||
@ -63,13 +67,6 @@ private:
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
LookupResult();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
@ -80,14 +77,6 @@ public:
|
||||
*/
|
||||
LookupResult(LookupResult &&) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~LookupResult()
|
||||
{
|
||||
/* We let the smart reference deal with deallocations if necessary. */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
|
@ -12,10 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class Metadata
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Database; // Only a valid database instance can construct this type.
|
||||
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_metadata_s Type; // The managed type.
|
||||
@ -28,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -37,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
@ -54,15 +51,6 @@ private:
|
||||
DbRef m_Handle; // The database associated with this meta-data.
|
||||
Pointer m_Metadata; // The inspected meta-data structure.
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and with a specific meta-data.
|
||||
*/
|
||||
Metadata(const DbRef & db, Pointer metadata)
|
||||
: m_Handle(db), m_Metadata(metadata)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
@ -74,6 +62,15 @@ public:
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and with a specific meta-data.
|
||||
*/
|
||||
Metadata(const DbRef & db, Pointer metadata)
|
||||
: m_Handle(db), m_Metadata(metadata)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
|
@ -15,6 +15,7 @@ extern void Register_EntryData(Table & mmns);
|
||||
extern void Register_EntryDataList(Table & mmns);
|
||||
extern void Register_LookupResult(Table & mmns);
|
||||
extern void Register_Metadata(Table & mmns);
|
||||
extern void Register_SearchNode(Table & mmns);
|
||||
extern void Register_SockAddr(Table & mmns);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
@ -38,13 +39,60 @@ static bool RegisterAPI(HSQUIRRELVM vm)
|
||||
Register_EntryDataList(mmns);
|
||||
Register_LookupResult(mmns);
|
||||
Register_Metadata(mmns);
|
||||
Register_SearchNode(mmns);
|
||||
Register_SockAddr(mmns);
|
||||
|
||||
mmns.Func(_SC("StrError"), MMDB_strerror);
|
||||
mmns.Func(_SC("LibVersion"), MMDB_lib_version);
|
||||
mmns.Func(_SC("TypeStr"), AsTypeStr);
|
||||
|
||||
RootTable(vm).Bind(_SC("SqMMDB"), mmns);
|
||||
|
||||
ConstTable(vm).Enum(_SC("SqMMDataType"), Enumeration(vm)
|
||||
.Const(_SC("Extended"), MMDB_DATA_TYPE_EXTENDED)
|
||||
.Const(_SC("Pointer"), MMDB_DATA_TYPE_POINTER)
|
||||
.Const(_SC("Utf8String"), MMDB_DATA_TYPE_UTF8_STRING)
|
||||
.Const(_SC("Double"), MMDB_DATA_TYPE_DOUBLE)
|
||||
.Const(_SC("Bytes"), MMDB_DATA_TYPE_BYTES)
|
||||
.Const(_SC("Uint16"), MMDB_DATA_TYPE_UINT16)
|
||||
.Const(_SC("Uint32"), MMDB_DATA_TYPE_UINT32)
|
||||
.Const(_SC("Map"), MMDB_DATA_TYPE_MAP)
|
||||
.Const(_SC("Int32"), MMDB_DATA_TYPE_INT32)
|
||||
.Const(_SC("Uint64"), MMDB_DATA_TYPE_UINT64)
|
||||
.Const(_SC("Uint128"), MMDB_DATA_TYPE_UINT128)
|
||||
.Const(_SC("Array"), MMDB_DATA_TYPE_ARRAY)
|
||||
.Const(_SC("Container"), MMDB_DATA_TYPE_CONTAINER)
|
||||
.Const(_SC("EndMarker"), MMDB_DATA_TYPE_END_MARKER)
|
||||
.Const(_SC("Boolean"), MMDB_DATA_TYPE_BOOLEAN)
|
||||
.Const(_SC("Float"), MMDB_DATA_TYPE_FLOAT)
|
||||
);
|
||||
|
||||
ConstTable(vm).Enum(_SC("SqMMRecordType"), Enumeration(vm)
|
||||
.Const(_SC("SearchNode"), MMDB_RECORD_TYPE_SEARCH_NODE)
|
||||
.Const(_SC("Empty"), MMDB_RECORD_TYPE_EMPTY)
|
||||
.Const(_SC("Data"), MMDB_RECORD_TYPE_DATA)
|
||||
.Const(_SC("Invalid"), MMDB_RECORD_TYPE_INVALID)
|
||||
);
|
||||
|
||||
ConstTable(vm).Enum(_SC("SqMMErrCode"), Enumeration(vm)
|
||||
.Const(_SC("Success"), MMDB_SUCCESS)
|
||||
.Const(_SC("FileOpenError"), MMDB_FILE_OPEN_ERROR)
|
||||
.Const(_SC("CorruptSearchTreeError"), MMDB_CORRUPT_SEARCH_TREE_ERROR)
|
||||
.Const(_SC("InvalidMetadataError"), MMDB_INVALID_METADATA_ERROR)
|
||||
.Const(_SC("IOError"), MMDB_IO_ERROR)
|
||||
.Const(_SC("OutOfMemoryError"), MMDB_OUT_OF_MEMORY_ERROR)
|
||||
.Const(_SC("UnknownDatabaseFormatError"), MMDB_UNKNOWN_DATABASE_FORMAT_ERROR)
|
||||
.Const(_SC("InvalidDataError"), MMDB_INVALID_DATA_ERROR)
|
||||
.Const(_SC("InvalidLookupPathError"), MMDB_INVALID_LOOKUP_PATH_ERROR)
|
||||
.Const(_SC("LookupPathDoesNotMatchDataError"), MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR)
|
||||
.Const(_SC("InvalidNodeNumberError"), MMDB_INVALID_NODE_NUMBER_ERROR)
|
||||
.Const(_SC("Ipv6LookupInIpv4DatabaseError"), MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR)
|
||||
);
|
||||
|
||||
Sqrat::ConstTable(vm)
|
||||
.Const(_SC("MMDB_MODE_MMAP"), MMDB_MODE_MMAP)
|
||||
.Const(_SC("MMDB_MODE_MASK"), MMDB_MODE_MASK);
|
||||
|
||||
// Registration was successful
|
||||
return true;
|
||||
}
|
||||
|
@ -0,0 +1,218 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "SearchNode.hpp"
|
||||
#include "EntryData.hpp"
|
||||
#include "EntryDataList.hpp"
|
||||
#include "Database.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger SearchNode::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static const SQChar name[] = _SC("SqMMSearchNode");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
void SearchNode::Validate(CCStr file, Int32 line) const
|
||||
{
|
||||
if (!m_Handle)
|
||||
{
|
||||
SqThrowF("Invalid Maxmind database reference =>[%s:%d]", file, line);
|
||||
}
|
||||
}
|
||||
#else
|
||||
void SearchNode::Validate() const
|
||||
{
|
||||
if (!m_Handle)
|
||||
{
|
||||
SqThrowF("Invalid Maxmind database reference");
|
||||
}
|
||||
}
|
||||
#endif // _DEBUG
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
const DbRef & SearchNode::GetValid(CCStr file, Int32 line) const
|
||||
{
|
||||
Validate(file, line);
|
||||
return m_Handle;
|
||||
}
|
||||
#else
|
||||
const DbRef & SearchNode::GetValid() const
|
||||
{
|
||||
Validate();
|
||||
return m_Handle;
|
||||
}
|
||||
#endif // _DEBUG
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SearchNode::SearchNode()
|
||||
: m_Handle(), m_Node()
|
||||
{
|
||||
std::memset(&m_Node, 0, sizeof(Type));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void SearchNode::Release()
|
||||
{
|
||||
std::memset(&m_Node, 0, sizeof(Type));
|
||||
m_Handle.Reset();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Database SearchNode::GetDatabase() const
|
||||
{
|
||||
return Database(m_Handle);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object SearchNode::GetLeftRecordEntryDataList()
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Prepare a temporary entry data list pointer
|
||||
MMDB_entry_data_list_s * entry_data_list = nullptr;
|
||||
// Attempt to retrieve the entire entry data list at once
|
||||
const int status = MMDB_get_entry_data_list(&m_Node.left_record_entry, &entry_data_list);
|
||||
// Validate the status code
|
||||
if (status != MMDB_SUCCESS)
|
||||
{
|
||||
STHROWF("Unable to get entry data list [%s]", MMDB_strerror(status));
|
||||
}
|
||||
// Return the resulted list
|
||||
return Object(new EntryDataList(m_Handle, entry_data_list));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Object SearchNode::GetRightRecordEntryDataList()
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Prepare a temporary entry data list pointer
|
||||
MMDB_entry_data_list_s * entry_data_list = nullptr;
|
||||
// Attempt to retrieve the entire entry data list at once
|
||||
const int status = MMDB_get_entry_data_list(&m_Node.right_record_entry, &entry_data_list);
|
||||
// Validate the status code
|
||||
if (status != MMDB_SUCCESS)
|
||||
{
|
||||
STHROWF("Unable to get entry data list [%s]", MMDB_strerror(status));
|
||||
}
|
||||
// Return the resulted list
|
||||
return Object(new EntryDataList(m_Handle, entry_data_list));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger SearchNode::GetRecordEntryData(HSQUIRRELVM vm, bool right)
|
||||
{
|
||||
const Int32 top = sq_gettop(vm);
|
||||
// The search node result instance
|
||||
SearchNode * node = nullptr;
|
||||
// Attempt to extract the search node result instance
|
||||
try
|
||||
{
|
||||
node = Var< SearchNode * >(vm, 1).value;
|
||||
}
|
||||
catch (const Sqrat::Exception & e)
|
||||
{
|
||||
return sq_throwerror(vm, e.what());
|
||||
}
|
||||
// Do we have a valid search node result instance?
|
||||
if (!node)
|
||||
{
|
||||
return sq_throwerror(vm, "Invalid search node result instance");
|
||||
}
|
||||
// See if there's a handle
|
||||
else if (!node->m_Handle)
|
||||
{
|
||||
return sq_throwerror(vm, "Invalid Maxmind database reference");
|
||||
}
|
||||
|
||||
typedef std::vector< StackStrF > ArgList;
|
||||
// The list of extracted arguments
|
||||
ArgList arglist;
|
||||
// Extract each argument as a string
|
||||
for (SQInteger i = 2; i <= top; ++i)
|
||||
{
|
||||
arglist.emplace_back(vm, i, false);
|
||||
// Did we fail to extract the argument value?
|
||||
if (SQ_FAILED(arglist.back().mRes))
|
||||
{
|
||||
return arglist.back().mRes; // Propagate the error
|
||||
}
|
||||
}
|
||||
|
||||
typedef std::vector< CSStr > PtrList;
|
||||
// The list of pointers to path segments
|
||||
PtrList ptrlist;
|
||||
// Grab the pointers to argument values
|
||||
for (const auto & a : arglist)
|
||||
{
|
||||
ptrlist.push_back(a.mPtr);
|
||||
}
|
||||
// Push null to specify the end of the list
|
||||
ptrlist.push_back(nullptr);
|
||||
|
||||
// Grab the requested entry
|
||||
MMDB_entry_s * entry = &(right ? node->m_Node.right_record_entry : node->m_Node.left_record_entry);
|
||||
|
||||
MMDB_entry_data_s entry_data;
|
||||
// Attempt to retrieve the specified entry data
|
||||
const int status = MMDB_aget_value(entry, &entry_data, ptrlist.data());
|
||||
// Validate the status code
|
||||
if (status != MMDB_SUCCESS)
|
||||
{
|
||||
return sq_throwerror(vm, ToStrF("Unable to get entry data [%s]", MMDB_strerror(status)));
|
||||
}
|
||||
// Push the resulted list object onto the stack
|
||||
try
|
||||
{
|
||||
ClassType< EntryData >::PushInstance(vm, new EntryData(node->m_Handle, entry_data));
|
||||
}
|
||||
catch (const Sqrat::Exception & e)
|
||||
{
|
||||
return sq_throwerror(vm, e.what());
|
||||
}
|
||||
|
||||
// Specify that we returned a value
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_SearchNode(Table & mmns)
|
||||
{
|
||||
mmns.Bind(_SC("SearchNode"),
|
||||
Class< SearchNode >(mmns.GetVM(), _SC("SqMMSearchNode"))
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< const SearchNode & >()
|
||||
// Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &SearchNode::Typename)
|
||||
.Func(_SC("_tostring"), &SearchNode::ToString)
|
||||
// Properties
|
||||
.Prop(_SC("IsValid"), &SearchNode::IsValid)
|
||||
.Prop(_SC("Database"), &SearchNode::GetDatabase)
|
||||
.Prop(_SC("References"), &SearchNode::GetRefCount)
|
||||
.Prop(_SC("LeftRecord"), &SearchNode::GetLeftRecord)
|
||||
.Prop(_SC("RightRecord"), &SearchNode::GetRightRecord)
|
||||
.Prop(_SC("LeftRecordType"), &SearchNode::GetLeftRecordType)
|
||||
.Prop(_SC("RightRecordType"), &SearchNode::GetRightRecordType)
|
||||
.Prop(_SC("LeftRecordEntryDataList"), &SearchNode::GetLeftRecordEntryDataList)
|
||||
.Prop(_SC("RightRecordEntryDataList"), &SearchNode::GetRightRecordEntryDataList)
|
||||
// Member methods
|
||||
.Func(_SC("Release"), &SearchNode::Release)
|
||||
// Squirrel methods
|
||||
.SquirrelFunc(_SC("GetLeftRecordValue"), &SearchNode::GetLeftRecordEntryData)
|
||||
.SquirrelFunc(_SC("GetRightRecordValue"), &SearchNode::GetRightRecordEntryData)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
@ -0,0 +1,218 @@
|
||||
#ifndef _SQMMDB_SEARCHNODE_HPP_
|
||||
#define _SQMMDB_SEARCHNODE_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Handle/Database.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Class that can hold and be used to work with search nodes.
|
||||
*/
|
||||
class SearchNode
|
||||
{
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef MMDB_search_node_s 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.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
void Validate(CCStr file, Int32 line) const;
|
||||
#else
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed database handle and throw an error if invalid.
|
||||
*/
|
||||
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
||||
const DbRef & GetValid(CCStr file, Int32 line) const;
|
||||
#else
|
||||
const DbRef & GetValid() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
DbRef m_Handle; // The database from which this search node comes from.
|
||||
Type m_Node; // The managed search node structure.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor. (null)
|
||||
*/
|
||||
SearchNode();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Construct and take ownership of a certain search node.
|
||||
*/
|
||||
SearchNode(const DbRef & db, Reference node)
|
||||
: m_Handle(db), m_Node(node)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
SearchNode(const SearchNode &) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
SearchNode(SearchNode &&) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
SearchNode & operator = (const SearchNode &) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
SearchNode & operator = (SearchNode &&) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
CSStr ToString() const
|
||||
{
|
||||
return FmtStr("<%llu:%s,%llu:s>", m_Node.left_record, AsTypeStr(m_Node.left_record_type)
|
||||
, m_Node.right_record, AsTypeStr(m_Node.right_record_type));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to retrieve the name from instances of this type.
|
||||
*/
|
||||
static SQInteger Typename(HSQUIRRELVM vm);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* See whether this instance references a valid database and node structure.
|
||||
*/
|
||||
bool IsValid() const
|
||||
{
|
||||
return m_Handle;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Release the manages handles/pointers and become a null instance.
|
||||
*/
|
||||
void Release();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the database associated with the managed handle/pointer.
|
||||
*/
|
||||
Database GetDatabase() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Return the number of active references to the managed database instance.
|
||||
*/
|
||||
Uint32 GetRefCount() const
|
||||
{
|
||||
return m_Handle.Count();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the left record value.
|
||||
*/
|
||||
Object GetLeftRecord() const
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg;
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(m_Node.left_record));
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the right record value.
|
||||
*/
|
||||
Object GetRightRecord() const
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Obtain the initial stack size
|
||||
const StackGuard sg;
|
||||
// Push a long integer instance with the requested value on the stack
|
||||
SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(m_Node.right_record));
|
||||
// Obtain the object from the stack and return it
|
||||
return Var< Object >(DefaultVM::Get(), -1).value;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the left record value type.
|
||||
*/
|
||||
SQInteger GetLeftRecordType() const
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Return the requested information
|
||||
return static_cast< SQInteger >(m_Node.left_record_type);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the right record value type.
|
||||
*/
|
||||
SQInteger GetRightRecordType() const
|
||||
{
|
||||
// Validate the managed handles
|
||||
SQMOD_VALIDATE(*this);
|
||||
// Return the requested information
|
||||
return static_cast< SQInteger >(m_Node.right_record_type);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the left record entry data list.
|
||||
*/
|
||||
Object GetLeftRecordEntryDataList();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the right record entry data list.
|
||||
*/
|
||||
Object GetRightRecordEntryDataList();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the left record entry data.
|
||||
*/
|
||||
static SQInteger GetLeftRecordEntryData(HSQUIRRELVM vm)
|
||||
{
|
||||
return GetRecordEntryData(vm, false);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the left record entry data.
|
||||
*/
|
||||
static SQInteger GetRightRecordEntryData(HSQUIRRELVM vm)
|
||||
{
|
||||
return GetRecordEntryData(vm, true);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Shared function to retrieve the left/right record entry data.
|
||||
*/
|
||||
static SQInteger GetRecordEntryData(HSQUIRRELVM vm, bool right);
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _SQMMDB_SEARCHNODE_HPP_
|
@ -12,7 +12,7 @@ namespace SqMod {
|
||||
*/
|
||||
class SockAddr
|
||||
{
|
||||
protected:
|
||||
public:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
typedef struct addrinfo Type; // The managed type.
|
||||
@ -25,6 +25,8 @@ protected:
|
||||
typedef Type& Reference; // Reference to the managed type.
|
||||
typedef const Type& ConstRef; // Constant reference to the managed type.
|
||||
|
||||
protected:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed sockaddr pointer and throw an error if invalid.
|
||||
*/
|
||||
@ -34,8 +36,6 @@ protected:
|
||||
void Validate() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Validate the managed sockaddr pointer and throw an error if invalid.
|
||||
*/
|
||||
@ -44,6 +44,7 @@ private:
|
||||
#else
|
||||
Pointer GetValid() const;
|
||||
#endif // _DEBUG
|
||||
|
||||
private:
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user