diff options
author | Mattes D <github@xoft.cz> | 2015-01-29 11:09:56 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2015-02-04 08:40:52 +0100 |
commit | 17498a97a289119debdb651ab898ddea99e86ff9 (patch) | |
tree | 04f103b0c0f5d120cec59756618bcafeafb53cb2 /src | |
parent | NetworkTest plugin: Added cNetwork:Connect test code. (diff) | |
download | cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar.gz cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar.bz2 cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar.lz cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar.xz cuberite-17498a97a289119debdb651ab898ddea99e86ff9.tar.zst cuberite-17498a97a289119debdb651ab898ddea99e86ff9.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Bindings/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Bindings/LuaNameLookup.cpp | 88 | ||||
-rw-r--r-- | src/Bindings/LuaNameLookup.h | 46 | ||||
-rw-r--r-- | src/Bindings/LuaTCPLink.h | 3 | ||||
-rw-r--r-- | src/Bindings/ManualBindings_Network.cpp | 83 |
5 files changed, 219 insertions, 3 deletions
diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt index a72611f76..13428b5c6 100644 --- a/src/Bindings/CMakeLists.txt +++ b/src/Bindings/CMakeLists.txt @@ -8,6 +8,7 @@ SET (SRCS Bindings.cpp DeprecatedBindings.cpp LuaChunkStay.cpp + LuaNameLookup.cpp LuaState.cpp LuaTCPLink.cpp LuaWindow.cpp @@ -25,6 +26,7 @@ SET (HDRS DeprecatedBindings.h LuaChunkStay.h LuaFunctions.h + LuaNameLookup.h LuaState.h LuaTCPLink.h LuaWindow.h diff --git a/src/Bindings/LuaNameLookup.cpp b/src/Bindings/LuaNameLookup.cpp new file mode 100644 index 000000000..e52d8dbdc --- /dev/null +++ b/src/Bindings/LuaNameLookup.cpp @@ -0,0 +1,88 @@ + +// LuaNameLookup.cpp + +// Implements the cLuaNameLookup class used as the cNetwork API callbacks for name and IP lookups from Lua + +#include "Globals.h" +#include "LuaNameLookup.h" + + + + + +cLuaNameLookup::cLuaNameLookup(const AString & a_Query, cPluginLua & a_Plugin, int a_CallbacksTableStackPos): + m_Plugin(a_Plugin), + m_Callbacks(a_Plugin.GetLuaState(), a_CallbacksTableStackPos), + m_Query(a_Query) +{ +} + + + + + +void cLuaNameLookup::OnNameResolved(const AString & a_Name, const AString & a_IP) +{ + // Check if we're still valid: + if (!m_Callbacks.IsValid()) + { + return; + } + + // Call the callback: + cPluginLua::cOperation Op(m_Plugin); + if (!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnNameResolved"), a_Name, a_IP)) + { + LOGINFO("cNetwork name lookup OnNameResolved callback failed in plugin %s looking up %s. %s resolves to %s.", + m_Plugin.GetName().c_str(), m_Query.c_str(), a_Name.c_str(), a_IP.c_str() + ); + } +} + + + + + +void cLuaNameLookup::OnError(int a_ErrorCode, const AString & a_ErrorMsg) +{ + // Check if we're still valid: + if (!m_Callbacks.IsValid()) + { + return; + } + + // Call the callback: + cPluginLua::cOperation Op(m_Plugin); + if (!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnError"), m_Query, a_ErrorCode, a_ErrorMsg)) + { + LOGINFO("cNetwork name lookup OnError callback failed in plugin %s looking up %s. The error is %d (%s)", + m_Plugin.GetName().c_str(), m_Query.c_str(), a_ErrorCode, a_ErrorMsg.c_str() + ); + } +} + + + + + +void cLuaNameLookup::OnFinished(void) +{ + // Check if we're still valid: + if (!m_Callbacks.IsValid()) + { + return; + } + + // Call the callback: + cPluginLua::cOperation Op(m_Plugin); + if (!Op().Call(cLuaState::cTableRef(m_Callbacks, "OnFinished"), m_Query)) + { + LOGINFO("cNetwork name lookup OnFinished callback failed in plugin %s, looking up %s.", + m_Plugin.GetName().c_str(), m_Query.c_str() + ); + } +} + + + + diff --git a/src/Bindings/LuaNameLookup.h b/src/Bindings/LuaNameLookup.h new file mode 100644 index 000000000..8b456ad90 --- /dev/null +++ b/src/Bindings/LuaNameLookup.h @@ -0,0 +1,46 @@ + +// LuaNameLookup.h + +// Declares the cLuaNameLookup class used as the cNetwork API callbacks for name and IP lookups from Lua + + + + + +#pragma once + +#include "../OSSupport/Network.h" +#include "PluginLua.h" + + + + + +class cLuaNameLookup: + public cNetwork::cResolveNameCallbacks +{ +public: + /** Creates a new instance of the lookup callbacks for the specified query, + attached to the specified lua plugin and wrapping the callbacks that are in a table at the specified stack pos. */ + cLuaNameLookup(const AString & a_Query, cPluginLua & a_Plugin, int a_CallbacksTableStackPos); + +protected: + /** The plugin for which the link is created. */ + cPluginLua & m_Plugin; + + /** The Lua table that holds the callbacks to be invoked. */ + cLuaState::cRef m_Callbacks; + + /** The query used to start the lookup (either hostname or IP). */ + AString m_Query; + + + // cNetwork::cResolveNameCallbacks overrides: + virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) override; + virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override; + virtual void OnFinished(void) override; +}; + + + + diff --git a/src/Bindings/LuaTCPLink.h b/src/Bindings/LuaTCPLink.h index 54c1d2dbf..125cc1b31 100644 --- a/src/Bindings/LuaTCPLink.h +++ b/src/Bindings/LuaTCPLink.h @@ -10,7 +10,6 @@ #pragma once #include "../OSSupport/Network.h" -#include "LuaState.h" #include "PluginLua.h" @@ -22,7 +21,7 @@ class cLuaTCPLink: public cTCPLink::cCallbacks { public: - /** Creates a new instance of the link, attached to the specified lua state and wrapping the callbacks that are in a table at the specified stack pos. */ + /** Creates a new instance of the link, attached to the specified plugin and wrapping the callbacks that are in a table at the specified stack pos. */ cLuaTCPLink(cPluginLua & a_Plugin, int a_CallbacksTableStackPos); /** Sends the data contained in the string to the remote peer. diff --git a/src/Bindings/ManualBindings_Network.cpp b/src/Bindings/ManualBindings_Network.cpp index 882af4e9e..3123ef885 100644 --- a/src/Bindings/ManualBindings_Network.cpp +++ b/src/Bindings/ManualBindings_Network.cpp @@ -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); |