2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Sockaddr.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger SockAddr::Typename(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-11-14 13:06:30 +01:00
|
|
|
static const SQChar name[] = _SC("SqMMSockAddr");
|
2016-03-10 04:57:13 +01:00
|
|
|
sq_pushstring(vm, name, sizeof(name));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-11-14 13:06:30 +01:00
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
void SockAddr::Validate(CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
if (!m_Handle)
|
|
|
|
{
|
|
|
|
SqThrowF("Invalid sockaddr structure handle =>[%s:%d]", file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2016-03-10 04:57:13 +01:00
|
|
|
void SockAddr::Validate() const
|
|
|
|
{
|
|
|
|
if (!m_Handle)
|
2016-11-14 13:06:30 +01:00
|
|
|
{
|
|
|
|
SqThrowF("Invalid sockaddr structure handle");
|
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
2016-11-14 13:06:30 +01:00
|
|
|
#endif // _DEBUG
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
|
|
|
|
SockAddr::Pointer SockAddr::GetValid(CCStr file, Int32 line) const
|
|
|
|
{
|
|
|
|
Validate(file, line);
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
SockAddr::Pointer SockAddr::GetValid() const
|
|
|
|
{
|
|
|
|
Validate();
|
|
|
|
return m_Handle;
|
|
|
|
}
|
|
|
|
#endif // _DEBUG
|
2016-03-10 04:57:13 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SockAddr::SockAddr(CSStr addr)
|
2016-11-14 13:06:30 +01:00
|
|
|
: m_Handle(nullptr), m_Addres(_SC(""))
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
// Configure the hints structure
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
|
|
|
// We set ai_socktype so that we only get one result back
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
// Attempt to obtain information about the specified address
|
2016-11-14 13:06:30 +01:00
|
|
|
Int32 status = getaddrinfo(addr, nullptr, &hints, &m_Handle);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Validate the success of the operation
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
// See if we must free any handles (just in case)
|
|
|
|
if (m_Handle)
|
2016-11-14 13:06:30 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
freeaddrinfo(m_Handle);
|
2016-11-14 13:06:30 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Now it's safe to throw the error
|
2016-03-22 23:27:48 +01:00
|
|
|
STHROWF("Unable to query the specified address for information [%s]", gai_strerror(status));
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
// Save the specified string address
|
|
|
|
m_Addres.assign(addr ? addr : _SC(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SockAddr::~SockAddr()
|
|
|
|
{
|
|
|
|
if (m_Handle)
|
2016-11-14 13:06:30 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
freeaddrinfo(m_Handle);
|
2016-11-14 13:06:30 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:06:30 +01:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_SockAddr(Table & mmns)
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
2016-11-14 13:06:30 +01:00
|
|
|
mmns.Bind(_SC("SockAddr"),
|
|
|
|
Class< SockAddr, NoCopy< SockAddr > >(mmns.GetVM(), _SC("SqMMSockAddr"))
|
|
|
|
// Constructors
|
|
|
|
.Ctor()
|
|
|
|
.Ctor< CSStr >()
|
|
|
|
// Meta-methods
|
|
|
|
.SquirrelFunc(_SC("_typename"), &SockAddr::Typename)
|
|
|
|
.Func(_SC("_tostring"), &SockAddr::ToString)
|
|
|
|
// Properties
|
|
|
|
.Prop(_SC("IsValid"), &SockAddr::IsValid)
|
|
|
|
.Prop(_SC("Address"), &SockAddr::GetAddress)
|
|
|
|
);
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|