summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/NetworkTest
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 /MCServer/Plugins/NetworkTest
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 'MCServer/Plugins/NetworkTest')
-rw-r--r--MCServer/Plugins/NetworkTest/Info.lua42
-rw-r--r--MCServer/Plugins/NetworkTest/NetworkTest.lua93
2 files changed, 134 insertions, 1 deletions
diff --git a/MCServer/Plugins/NetworkTest/Info.lua b/MCServer/Plugins/NetworkTest/Info.lua
index c3c2ea8fc..52422d427 100644
--- a/MCServer/Plugins/NetworkTest/Info.lua
+++ b/MCServer/Plugins/NetworkTest/Info.lua
@@ -84,6 +84,48 @@ g_PluginInfo =
},
}, -- lookup
+ udp =
+ {
+ Subcommands =
+ {
+ close =
+ {
+ Handler = HandleConsoleNetUdpClose,
+ ParameterCombinations =
+ {
+ {
+ Params = "[Port]",
+ Help = "Closes the UDP endpoint on the specified port [1024].",
+ }
+ },
+ }, -- close
+
+ listen =
+ {
+ Handler = HandleConsoleNetUdpListen,
+ ParameterCombinations =
+ {
+ {
+ Params = "[Port]",
+ Help = "Listens on the specified UDP port [1024], dumping the incoming datagrams into log",
+ },
+ },
+ }, -- listen
+
+ send =
+ {
+ Handler = HandleConsoleNetUdpSend,
+ ParameterCombinations =
+ {
+ {
+ Params = "[Host] [Port] [Message]",
+ Help = "Sends the message [\"hello\"] through UDP to the specified host [localhost] and port [1024]",
+ },
+ },
+ } -- send
+ }, -- Subcommands ("net udp")
+ }, -- udp
+
wasc =
{
HelpString = "Requests the webadmin homepage using https",
diff --git a/MCServer/Plugins/NetworkTest/NetworkTest.lua b/MCServer/Plugins/NetworkTest/NetworkTest.lua
index 39774f403..daab0a4bf 100644
--- a/MCServer/Plugins/NetworkTest/NetworkTest.lua
+++ b/MCServer/Plugins/NetworkTest/NetworkTest.lua
@@ -11,6 +11,10 @@
-- g_Servers[PortNum] = cServerHandle
local g_Servers = {}
+--- Map of all UDP endpoints currently open
+-- g_UDPEndpoints[PortNum] = cUDPEndpoint
+local g_UDPEndpoints = {}
+
--- List of fortune messages for the fortune server
-- A random message is chosen for each incoming connection
-- The contents are loaded from the splashes.txt file on plugin startup
@@ -268,7 +272,7 @@ function HandleConsoleNetClose(a_Split)
-- Get the port to close:
local Port = tonumber(a_Split[3] or 1024)
if not(Port) then
- return true, "Bad port number: \"" .. Port .. "\"."
+ return true, "Bad port number: \"" .. a_Split[3] .. "\"."
end
-- Close the server, if there is one:
@@ -354,6 +358,93 @@ end
+function HandleConsoleNetUdpClose(a_Split)
+ -- Get the port to close:
+ local Port = tonumber(a_Split[4] or 1024)
+ if not(Port) then
+ return true, "Bad port number: \"" .. a_Split[4] .. "\"."
+ end
+
+ -- Close the server, if there is one:
+ if not(g_UDPEndpoints[Port]) then
+ return true, "There is no UDP endpoint currently listening on port " .. Port .. "."
+ end
+ g_UDPEndpoints[Port]:Close()
+ g_UDPEndpoints[Port] = nil
+ return true, "UDP Port " .. Port .. " closed."
+end
+
+
+
+
+
+function HandleConsoleNetUdpListen(a_Split)
+ -- Get the params:
+ local Port = tonumber(a_Split[4] or 1024)
+ if not(Port) then
+ return true, "Invalid port: \"" .. a_Split[4] .. "\"."
+ end
+
+ local Callbacks =
+ {
+ OnReceivedData = function (a_Endpoint, a_Data, a_RemotePeer, a_RemotePort)
+ LOG("Incoming UDP datagram from " .. a_RemotePeer .. " port " .. a_RemotePort .. ":\r\n" .. a_Data)
+ end,
+
+ OnError = function (a_Endpoint, a_ErrorCode, a_ErrorMsg)
+ LOG("Error in UDP endpoint: " .. a_ErrorCode .. " (" .. a_ErrorMsg .. ")")
+ end,
+ }
+
+ g_UDPEndpoints[Port] = cNetwork:CreateUDPEndpoint(Port, Callbacks)
+ return true, "UDP listener on port " .. Port .. " started."
+end
+
+
+
+
+
+function HandleConsoleNetUdpSend(a_Split)
+ -- Get the params:
+ local Host = a_Split[4] or "localhost"
+ local Port = tonumber(a_Split[5] or 1024)
+ if not(Port) then
+ return true, "Invalid port: \"" .. a_Split[5] .. "\"."
+ end
+ local Message
+ if (a_Split[6]) then
+ Message = table.concat(a_Split, " ", 6)
+ else
+ Message = "hello"
+ end
+
+ -- Define minimum callbacks for the UDP endpoint:
+ local Callbacks =
+ {
+ OnError = function (a_Endpoint, a_ErrorCode, a_ErrorMsg)
+ LOG("Error in UDP datagram sending: " .. a_ErrorCode .. " (" .. a_ErrorMsg .. ")")
+ end,
+
+ OnReceivedData = function ()
+ -- ignore
+ end,
+ }
+
+ -- Send the data:
+ local Endpoint = cNetwork:CreateUDPEndpoint(0, Callbacks)
+ Endpoint:EnableBroadcasts()
+ if not(Endpoint:Send(Message, Host, Port)) then
+ Endpoint:Close()
+ return true, "Sending UDP datagram failed"
+ end
+ Endpoint:Close()
+ return true, "UDP datagram sent"
+end
+
+
+
+
+
function HandleConsoleNetWasc(a_Split)
local Callbacks =
{