summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-02-20 14:28:05 +0100
committerMattes D <github@xoft.cz>2015-02-20 14:28:05 +0100
commit9c5162041e6e0699283862b87e2e424bf8e3b8b8 (patch)
tree3beed26a11a3fb35b79b444f2380f6b93dc2f3a7 /src/Bindings
parentNetworkSingleton: LibEvent thread is joined properly on server exit. (diff)
downloadcuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.gz
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.bz2
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.lz
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.xz
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.tar.zst
cuberite-9c5162041e6e0699283862b87e2e424bf8e3b8b8.zip
Diffstat (limited to '')
-rw-r--r--src/Bindings/CMakeLists.txt2
-rw-r--r--src/Bindings/LuaState.cpp12
-rw-r--r--src/Bindings/LuaState.h2
-rw-r--r--src/Bindings/LuaUDPEndpoint.cpp221
-rw-r--r--src/Bindings/LuaUDPEndpoint.h86
-rw-r--r--src/Bindings/ManualBindings_Network.cpp328
6 files changed, 620 insertions, 31 deletions
diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt
index 40b8d4bd9..4cc73b350 100644
--- a/src/Bindings/CMakeLists.txt
+++ b/src/Bindings/CMakeLists.txt
@@ -12,6 +12,7 @@ SET (SRCS
LuaServerHandle.cpp
LuaState.cpp
LuaTCPLink.cpp
+ LuaUDPEndpoint.cpp
LuaWindow.cpp
ManualBindings.cpp
ManualBindings_Network.cpp
@@ -31,6 +32,7 @@ SET (HDRS
LuaServerHandle.h
LuaState.h
LuaTCPLink.h
+ LuaUDPEndpoint.h
LuaWindow.h
ManualBindings.h
Plugin.h
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index 81770058c..25c77a652 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -692,6 +692,18 @@ void cLuaState::Push(cLuaTCPLink * a_TCPLink)
+void cLuaState::Push(cLuaUDPEndpoint * a_UDPEndpoint)
+{
+ ASSERT(IsValid());
+
+ tolua_pushusertype(m_LuaState, a_UDPEndpoint, "cUDPEndpoint");
+ m_NumCurrentFunctionArgs += 1;
+}
+
+
+
+
+
void cLuaState::Push(cMonster * a_Monster)
{
ASSERT(IsValid());
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 7fc3197eb..159483634 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -61,6 +61,7 @@ class cBlockEntity;
class cBoundingBox;
class cLuaTCPLink;
class cLuaServerHandle;
+class cLuaUDPEndpoint;
typedef cBoundingBox * pBoundingBox;
typedef cWorld * pWorld;
@@ -212,6 +213,7 @@ public:
void Push(cItems * a_Items);
void Push(cLuaServerHandle * a_ServerHandle);
void Push(cLuaTCPLink * a_TCPLink);
+ void Push(cLuaUDPEndpoint * a_UDPEndpoint);
void Push(cMonster * a_Monster);
void Push(cPickup * a_Pickup);
void Push(cPlayer * a_Player);
diff --git a/src/Bindings/LuaUDPEndpoint.cpp b/src/Bindings/LuaUDPEndpoint.cpp
new file mode 100644
index 000000000..8637eeb4e
--- /dev/null
+++ b/src/Bindings/LuaUDPEndpoint.cpp
@@ -0,0 +1,221 @@
+
+// LuaUDPEndpoint.cpp
+
+// Implements the cLuaUDPEndpoint class representing a Lua wrapper for the cUDPEndpoint class and the callbacks it needs
+
+#include "Globals.h"
+#include "LuaUDPEndpoint.h"
+
+
+
+
+
+cLuaUDPEndpoint::cLuaUDPEndpoint(cPluginLua & a_Plugin, int a_CallbacksTableStackPos):
+ m_Plugin(a_Plugin),
+ m_Callbacks(a_Plugin.GetLuaState(), a_CallbacksTableStackPos)
+{
+ // Warn if the callbacks aren't valid:
+ if (!m_Callbacks.IsValid())
+ {
+ LOGWARNING("cLuaUDPEndpoint in plugin %s: callbacks could not be retrieved", m_Plugin.GetName().c_str());
+ cPluginLua::cOperation Op(m_Plugin);
+ Op().LogStackTrace();
+ }
+}
+
+
+
+
+
+cLuaUDPEndpoint::~cLuaUDPEndpoint()
+{
+ // If the endpoint is still open, close it:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint != nullptr)
+ {
+ Endpoint->Close();
+ }
+
+ Terminated();
+}
+
+
+
+
+
+bool cLuaUDPEndpoint::Open(UInt16 a_Port, cLuaUDPEndpointPtr a_Self)
+{
+ ASSERT(m_Self == nullptr); // Must not be opened yet
+ ASSERT(m_Endpoint == nullptr);
+
+ m_Self = a_Self;
+ m_Endpoint = cNetwork::CreateUDPEndpoint(a_Port, *this);
+ return m_Endpoint->IsOpen();
+}
+
+
+
+
+
+bool cLuaUDPEndpoint::Send(const AString & a_Data, const AString & a_RemotePeer, UInt16 a_RemotePort)
+{
+ // Safely grab a copy of the endpoint:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint == nullptr)
+ {
+ return false;
+ }
+
+ // Send the data:
+ return Endpoint->Send(a_Data, a_RemotePeer, a_RemotePort);
+}
+
+
+
+
+
+UInt16 cLuaUDPEndpoint::GetPort(void) const
+{
+ // Safely grab a copy of the endpoint:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint == nullptr)
+ {
+ return 0;
+ }
+
+ // Get the port:
+ return Endpoint->GetPort();
+}
+
+
+
+
+
+bool cLuaUDPEndpoint::IsOpen(void) const
+{
+ // Safely grab a copy of the endpoint:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint == nullptr)
+ {
+ // No endpoint means that we're not open
+ return false;
+ }
+
+ // Get the state:
+ return Endpoint->IsOpen();
+}
+
+
+
+
+
+void cLuaUDPEndpoint::Close(void)
+{
+ // If the endpoint is still open, close it:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint != nullptr)
+ {
+ Endpoint->Close();
+ m_Endpoint.reset();
+ }
+
+ Terminated();
+}
+
+
+
+
+
+void cLuaUDPEndpoint::EnableBroadcasts(void)
+{
+ // Safely grab a copy of the endpoint:
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint != nullptr)
+ {
+ Endpoint->EnableBroadcasts();
+ }
+}
+
+
+
+
+
+void cLuaUDPEndpoint::Release(void)
+{
+ // Close the endpoint, if not already closed:
+ Close();
+
+ // Allow self to deallocate:
+ m_Self.reset();
+}
+
+
+
+
+
+void cLuaUDPEndpoint::Terminated(void)
+{
+ // Disable the callbacks:
+ if (m_Callbacks.IsValid())
+ {
+ m_Callbacks.UnRef();
+ }
+
+ // If the endpoint is still open, close it:
+ {
+ cUDPEndpointPtr Endpoint = m_Endpoint;
+ if (Endpoint != nullptr)
+ {
+ Endpoint->Close();
+ m_Endpoint.reset();
+ }
+ }
+}
+
+
+
+
+
+void cLuaUDPEndpoint::OnReceivedData(const char * a_Data, size_t a_NumBytes, const AString & a_RemotePeer, UInt16 a_RemotePort)
+{
+ // 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, "OnReceivedData"), this, AString(a_Data, a_NumBytes), a_RemotePeer, a_RemotePort))
+ {
+ LOGINFO("cUDPEndpoint OnReceivedData callback failed in plugin %s.", m_Plugin.GetName().c_str());
+ }
+}
+
+
+
+
+
+void cLuaUDPEndpoint::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"), a_ErrorCode, a_ErrorMsg))
+ {
+ LOGINFO("cUDPEndpoint OnError() callback failed in plugin %s; the endpoint error is %d (%s).",
+ m_Plugin.GetName().c_str(), a_ErrorCode, a_ErrorMsg.c_str()
+ );
+ }
+
+ Terminated();
+}
+
+
+
+
diff --git a/src/Bindings/LuaUDPEndpoint.h b/src/Bindings/LuaUDPEndpoint.h
new file mode 100644
index 000000000..0587491ab
--- /dev/null
+++ b/src/Bindings/LuaUDPEndpoint.h
@@ -0,0 +1,86 @@
+
+// LuaUDPEndpoint.h
+
+// Declares the cLuaUDPEndpoint class representing a Lua wrapper for the cUDPEndpoint class and the callbacks it needs
+
+
+
+
+
+#pragma once
+
+#include "../OSSupport/Network.h"
+#include "PluginLua.h"
+
+
+
+
+
+// fwd:
+class cLuaUDPEndpoint;
+typedef SharedPtr<cLuaUDPEndpoint> cLuaUDPEndpointPtr;
+
+
+
+
+
+class cLuaUDPEndpoint:
+ public cUDPEndpoint::cCallbacks
+{
+public:
+ /** Creates a new instance of the endpoint, attached to the specified plugin and wrapping the callbacks that are in a table at the specified stack pos. */
+ cLuaUDPEndpoint(cPluginLua & a_Plugin, int a_CallbacksTableStackPos);
+
+ ~cLuaUDPEndpoint();
+
+ /** Opens the endpoint so that it starts listening for incoming data on the specified port.
+ a_Self is the shared pointer to self that the object keeps to keep itself alive for as long as it needs (for Lua). */
+ bool Open(UInt16 a_Port, cLuaUDPEndpointPtr a_Self);
+
+ /** Sends the data contained in the string to the specified remote peer.
+ Returns true if successful, false on immediate failure (queueing the data failed etc.) */
+ bool Send(const AString & a_Data, const AString & a_RemotePeer, UInt16 a_RemotePort);
+
+ /** Returns the port on which endpoint is listening for incoming data. */
+ UInt16 GetPort(void) const;
+
+ /** Returns true if the endpoint is open for incoming data. */
+ bool IsOpen(void) const;
+
+ /** Closes the UDP listener. */
+ void Close(void);
+
+ /** Enables outgoing broadcasts to be made using this endpoint. */
+ void EnableBroadcasts(void);
+
+ /** Called when Lua garbage-collects the object.
+ Releases the internal SharedPtr to self, so that the instance may be deallocated. */
+ void Release(void);
+
+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;
+
+ /** SharedPtr to self, so that the object can keep itself alive for as long as it needs (for Lua). */
+ cLuaUDPEndpointPtr m_Self;
+
+ /** The underlying network endpoint.
+ May be nullptr. */
+ cUDPEndpointPtr m_Endpoint;
+
+
+ /** Common code called when the endpoint is considered as terminated.
+ Releases m_Endpoint and m_Callbacks, each when applicable. */
+ void Terminated(void);
+
+ // cUDPEndpoint::cCallbacks overrides:
+ virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override;
+ virtual void OnReceivedData(const char * a_Data, size_t a_Size, const AString & a_RemotePeer, UInt16 a_RemotePort) override;
+};
+
+
+
+
diff --git a/src/Bindings/ManualBindings_Network.cpp b/src/Bindings/ManualBindings_Network.cpp
index 30a34815c..a628eb9ca 100644
--- a/src/Bindings/ManualBindings_Network.cpp
+++ b/src/Bindings/ManualBindings_Network.cpp
@@ -11,6 +11,7 @@
#include "LuaTCPLink.h"
#include "LuaNameLookup.h"
#include "LuaServerHandle.h"
+#include "LuaUDPEndpoint.h"
@@ -73,6 +74,61 @@ static int tolua_cNetwork_Connect(lua_State * L)
+/** Binds cNetwork::CreateUDPEndpoint */
+static int tolua_cNetwork_CreateUDPEndpoint(lua_State * L)
+{
+ // Function signature:
+ // cNetwork:CreateUDPEndpoint(Port, Callbacks) -> cUDPEndpoint
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserTable(1, "cNetwork") ||
+ !S.CheckParamNumber(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:
+ int Port;
+ S.GetStackValues(2, Port);
+
+ // Check validity:
+ if ((Port < 0) || (Port > 65535))
+ {
+ LOGWARNING("cNetwork:CreateUDPEndpoint() called with invalid port (%d), failing the request.", Port);
+ S.Push(false);
+ return 1;
+ }
+
+ // Create the LuaUDPEndpoint glue class:
+ auto Endpoint = std::make_shared<cLuaUDPEndpoint>(*Plugin, 3);
+ Endpoint->Open(Port, Endpoint);
+
+ // Register the endpoint to be garbage-collected by Lua:
+ tolua_pushusertype(L, Endpoint.get(), "cUDPEndpoint");
+ tolua_register_gc(L, lua_gettop(L));
+
+ // Return the endpoint object:
+ S.Push(Endpoint.get());
+ return 1;
+}
+
+
+
+
+
/** Binds cNetwork::HostnameToIP */
static int tolua_cNetwork_HostnameToIP(lua_State * L)
{
@@ -159,7 +215,7 @@ static int tolua_cNetwork_IPToHostname(lua_State * L)
static int tolua_cNetwork_Listen(lua_State * L)
{
// Function signature:
- // cNetwork:Listen(Port, Callbacks) -> bool
+ // cNetwork:Listen(Port, Callbacks) -> cServerHandle
cLuaState S(L);
if (
@@ -212,6 +268,90 @@ static int tolua_cNetwork_Listen(lua_State * L)
////////////////////////////////////////////////////////////////////////////////
+// cServerHandle bindings (routed through cLuaServerHandle):
+
+/** Called when Lua destroys the object instance.
+Close the server and let it deallocate on its own (it's in a SharedPtr). */
+static int tolua_collect_cServerHandle(lua_State * L)
+{
+ cLuaServerHandle * Srv = static_cast<cLuaServerHandle *>(tolua_tousertype(L, 1, nullptr));
+ Srv->Release();
+ return 0;
+}
+
+
+
+
+
+/** Binds cLuaServerHandle::Close */
+static int tolua_cServerHandle_Close(lua_State * L)
+{
+ // Function signature:
+ // ServerInstance:Close()
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cServerHandle") ||
+ !S.CheckParamEnd(2)
+ )
+ {
+ return 0;
+ }
+
+ // Get the server handle:
+ cLuaServerHandle * Srv;
+ if (lua_isnil(L, 1))
+ {
+ LOGWARNING("cServerHandle:Close(): invalid server handle object. Stack trace:");
+ S.LogStackTrace();
+ return 0;
+ }
+ Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
+
+ // Close it:
+ Srv->Close();
+ return 0;
+}
+
+
+
+
+
+/** Binds cLuaServerHandle::IsListening */
+static int tolua_cServerHandle_IsListening(lua_State * L)
+{
+ // Function signature:
+ // ServerInstance:IsListening() -> bool
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cServerHandle") ||
+ !S.CheckParamEnd(2)
+ )
+ {
+ return 0;
+ }
+
+ // Get the server handle:
+ cLuaServerHandle * Srv;
+ if (lua_isnil(L, 1))
+ {
+ LOGWARNING("cServerHandle:IsListening(): invalid server handle object. Stack trace:");
+ S.LogStackTrace();
+ return 0;
+ }
+ Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
+
+ // Close it:
+ S.Push(Srv->IsListening());
+ return 1;
+}
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////////////
// cTCPLink bindings (routed through cLuaTCPLink):
/** Binds cLuaTCPLink::Close */
@@ -549,14 +689,15 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L)
////////////////////////////////////////////////////////////////////////////////
-// cServerHandle bindings (routed through cLuaServerHandle):
+// cUDPEndpoint bindings (routed through cLuaUDPEndpoint):
/** Called when Lua destroys the object instance.
-Close the server and let it deallocate on its own (it's in a SharedPtr). */
-static int tolua_collect_cServerHandle(lua_State * L)
+Close the endpoint and let it deallocate on its own (it's in a SharedPtr). */
+static int tolua_collect_cUDPEndpoint(lua_State * L)
{
- cLuaServerHandle * Srv = static_cast<cLuaServerHandle *>(tolua_tousertype(L, 1, nullptr));
- Srv->Release();
+ LOGD("Lua: Collecting cUDPEndpoint");
+ cLuaUDPEndpoint * Endpoint = static_cast<cLuaUDPEndpoint *>(tolua_tousertype(L, 1, nullptr));
+ Endpoint->Release();
return 0;
}
@@ -564,33 +705,32 @@ static int tolua_collect_cServerHandle(lua_State * L)
-/** Binds cLuaServerHandle::Close */
-static int tolua_cServerHandle_Close(lua_State * L)
+/** Binds cLuaUDPEndpoint::Close */
+static int tolua_cUDPEndpoint_Close(lua_State * L)
{
// Function signature:
- // ServerInstance:Close()
+ // EndpointInstance:Close()
cLuaState S(L);
if (
- !S.CheckParamUserType(1, "cServerHandle") ||
+ !S.CheckParamUserType(1, "cUDPEndpoint") ||
!S.CheckParamEnd(2)
)
{
return 0;
}
- // Get the server handle:
- cLuaServerHandle * Srv;
+ // Get the endpoint:
if (lua_isnil(L, 1))
{
- LOGWARNING("cServerHandle:Close(): invalid server handle object. Stack trace:");
+ LOGWARNING("cUDPEndpoint:Close(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
- Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
+ auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Close it:
- Srv->Close();
+ Endpoint->Close();
return 0;
}
@@ -598,33 +738,147 @@ static int tolua_cServerHandle_Close(lua_State * L)
-/** Binds cLuaServerHandle::IsListening */
-static int tolua_cServerHandle_IsListening(lua_State * L)
+/** Binds cLuaUDPEndpoint::EnableBroadcasts */
+static int tolua_cUDPEndpoint_EnableBroadcasts(lua_State * L)
{
// Function signature:
- // ServerInstance:IsListening() -> bool
+ // EndpointInstance:EnableBroadcasts()
cLuaState S(L);
if (
- !S.CheckParamUserType(1, "cServerHandle") ||
+ !S.CheckParamUserType(1, "cUDPEndpoint") ||
!S.CheckParamEnd(2)
)
{
return 0;
}
- // Get the server handle:
- cLuaServerHandle * Srv;
+ // Get the endpoint:
if (lua_isnil(L, 1))
{
- LOGWARNING("cServerHandle:IsListening(): invalid server handle object. Stack trace:");
+ LOGWARNING("cUDPEndpoint:EnableBroadcasts(): invalid endpoint object. Stack trace:");
S.LogStackTrace();
return 0;
}
- Srv = *static_cast<cLuaServerHandle **>(lua_touserdata(L, 1));
+ auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
+
+ // Enable the broadcasts:
+ Endpoint->EnableBroadcasts();
+ return 0;
+}
+
+
+
+
+
+/** Binds cLuaUDPEndpoint::GetPort */
+static int tolua_cUDPEndpoint_GetPort(lua_State * L)
+{
+ // Function signature:
+ // Endpoint:GetPort() -> number
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cUDPEndpoint") ||
+ !S.CheckParamEnd(2)
+ )
+ {
+ return 0;
+ }
+
+ // Get the endpoint:
+ if (lua_isnil(L, 1))
+ {
+ LOGWARNING("cUDPEndpoint:GetPort(): invalid endpoint object. Stack trace:");
+ S.LogStackTrace();
+ return 0;
+ }
+ auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
+
+ // Get the Port:
+ S.Push(Endpoint->GetPort());
+ return 1;
+}
+
+
+
+
+
+/** Binds cLuaUDPEndpoint::IsOpen */
+static int tolua_cUDPEndpoint_IsOpen(lua_State * L)
+{
+ // Function signature:
+ // Endpoint:IsOpen() -> bool
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cUDPEndpoint") ||
+ !S.CheckParamEnd(2)
+ )
+ {
+ return 0;
+ }
+
+ // Get the endpoint:
+ if (lua_isnil(L, 1))
+ {
+ LOGWARNING("cUDPEndpoint:IsListening(): invalid server handle object. Stack trace:");
+ S.LogStackTrace();
+ return 0;
+ }
+ auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
// Close it:
- S.Push(Srv->IsListening());
+ S.Push(Endpoint->IsOpen());
+ return 1;
+}
+
+
+
+
+
+/** Binds cLuaUDPEndpoint::Send */
+static int tolua_cUDPEndpoint_Send(lua_State * L)
+{
+ // Function signature:
+ // LinkInstance:Send(DataString)
+
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cUDPEndpoint") ||
+ !S.CheckParamString(2, 3) ||
+ !S.CheckParamNumber(4) ||
+ !S.CheckParamEnd(5)
+ )
+ {
+ return 0;
+ }
+
+ // Get the link:
+ if (lua_isnil(L, 1))
+ {
+ LOGWARNING("cUDPEndpoint:Send(): invalid endpoint object. Stack trace:");
+ S.LogStackTrace();
+ return 0;
+ }
+ auto Endpoint = *static_cast<cLuaUDPEndpoint **>(lua_touserdata(L, 1));
+
+ // Get the data to send:
+ AString Data, RemotePeer;
+ int RemotePort;
+ S.GetStackValues(2, Data, RemotePeer, RemotePort);
+
+ // Check the port:
+ if ((RemotePort < 0) || (RemotePort > USHRT_MAX))
+ {
+ LOGWARNING("cUDPEndpoint:Send() called with invalid port (%d), failing.", RemotePort);
+ S.LogStackTrace();
+ S.Push(false);
+ return 1;
+ }
+
+ // Send the data:
+ S.Push(Endpoint->Send(Data, RemotePeer, static_cast<UInt16>(RemotePort)));
return 1;
}
@@ -644,13 +898,21 @@ void ManualBindings::BindNetwork(lua_State * tolua_S)
tolua_cclass(tolua_S, "cTCPLink", "cTCPLink", "", nullptr);
tolua_usertype(tolua_S, "cServerHandle");
tolua_cclass(tolua_S, "cServerHandle", "cServerHandle", "", tolua_collect_cServerHandle);
+ tolua_usertype(tolua_S, "cUDPEndpoint");
+ tolua_cclass(tolua_S, "cUDPEndpoint", "cUDPEndpoint", "", tolua_collect_cUDPEndpoint);
// 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_function(tolua_S, "Connect", tolua_cNetwork_Connect);
+ tolua_function(tolua_S, "CreateUDPEndpoint", tolua_cNetwork_CreateUDPEndpoint);
+ 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);
+
+ tolua_beginmodule(tolua_S, "cServerHandle");
+ tolua_function(tolua_S, "Close", tolua_cServerHandle_Close);
+ tolua_function(tolua_S, "IsListening", tolua_cServerHandle_IsListening);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cTCPLink");
@@ -665,10 +927,14 @@ void ManualBindings::BindNetwork(lua_State * tolua_S)
tolua_function(tolua_S, "StartTLSServer", tolua_cTCPLink_StartTLSServer);
tolua_endmodule(tolua_S);
- tolua_beginmodule(tolua_S, "cServerHandle");
- tolua_function(tolua_S, "Close", tolua_cServerHandle_Close);
- tolua_function(tolua_S, "IsListening", tolua_cServerHandle_IsListening);
+ tolua_beginmodule(tolua_S, "cUDPEndpoint");
+ tolua_function(tolua_S, "Close", tolua_cUDPEndpoint_Close);
+ tolua_function(tolua_S, "EnableBroadcasts", tolua_cUDPEndpoint_EnableBroadcasts);
+ tolua_function(tolua_S, "GetPort", tolua_cUDPEndpoint_GetPort);
+ tolua_function(tolua_S, "IsOpen", tolua_cUDPEndpoint_IsOpen);
+ tolua_function(tolua_S, "Send", tolua_cUDPEndpoint_Send);
tolua_endmodule(tolua_S);
+
}