cNetwork: Exported lookup functions to Lua API.

Also added an example in the NetworkTest plugin.
This commit is contained in:
Mattes D
2015-02-04 08:40:52 +01:00
parent 04347084d6
commit 17498a97a2
7 changed files with 282 additions and 3 deletions
+82 -1
View File
@@ -9,6 +9,7 @@
#include "tolua++/include/tolua++.h"
#include "LuaState.h"
#include "LuaTCPLink.h"
#include "LuaNameLookup.h"
@@ -71,6 +72,86 @@ static int tolua_cNetwork_Connect(lua_State * L)
static int tolua_cNetwork_HostnameToIP(lua_State * L)
{
// Function signature:
// cNetwork:HostnameToIP(Host, Callbacks) -> bool
cLuaState S(L);
if (
!S.CheckParamUserTable(1, "cNetwork") ||
!S.CheckParamString(2) ||
!S.CheckParamTable(3) ||
!S.CheckParamEnd(4)
)
{
return 0;
}
// Get the plugin instance:
cPluginLua * Plugin = GetLuaPlugin(L);
if (Plugin == nullptr)
{
// An error message has been already printed in GetLuaPlugin()
S.Push(false);
return 1;
}
// Read the params:
AString Host;
S.GetStackValue(2, Host);
// Try to look up:
bool res = cNetwork::HostnameToIP(Host, std::make_shared<cLuaNameLookup>(Host, *Plugin, 3));
S.Push(res);
return 1;
}
static int tolua_cNetwork_IPToHostname(lua_State * L)
{
// Function signature:
// cNetwork:IPToHostname(IP, Callbacks) -> bool
cLuaState S(L);
if (
!S.CheckParamUserTable(1, "cNetwork") ||
!S.CheckParamString(2) ||
!S.CheckParamTable(3) ||
!S.CheckParamEnd(4)
)
{
return 0;
}
// Get the plugin instance:
cPluginLua * Plugin = GetLuaPlugin(L);
if (Plugin == nullptr)
{
// An error message has been already printed in GetLuaPlugin()
S.Push(false);
return 1;
}
// Read the params:
AString Host;
S.GetStackValue(2, Host);
// Try to look up:
bool res = cNetwork::IPToHostName(Host, std::make_shared<cLuaNameLookup>(Host, *Plugin, 3));
S.Push(res);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
// cTCPLink bindings (routed through cLuaTCPLink):
@@ -258,9 +339,9 @@ void ManualBindings::BindNetwork(lua_State * tolua_S)
// Fill in the functions (alpha-sorted):
tolua_beginmodule(tolua_S, "cNetwork");
tolua_function(tolua_S, "Connect", tolua_cNetwork_Connect);
/*
tolua_function(tolua_S, "HostnameToIP", tolua_cNetwork_HostnameToIP);
tolua_function(tolua_S, "IPToHostname", tolua_cNetwork_IPToHostname);
/*
tolua_function(tolua_S, "Listen", tolua_cNetwork_Listen);
*/
tolua_endmodule(tolua_S);