diff options
author | Christian Pointner <equinox@anylike.org> | 2009-12-26 17:30:47 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anylike.org> | 2009-12-26 17:30:47 +0000 |
commit | eb54dea987d187a9e5dcf3135caefb837d4b7cc2 (patch) | |
tree | 105fb87dd54ba1c71ce3104dfbe1c6085f92a359 | |
parent | removed useless ; in main_loop.lua (diff) |
added simple udp echo server using luasocket
-rw-r--r-- | src/anylike.c | 1 | ||||
-rw-r--r-- | src/main_loop.lua | 42 |
2 files changed, 41 insertions, 2 deletions
diff --git a/src/anylike.c b/src/anylike.c index 1fd8442..91088b8 100644 --- a/src/anylike.c +++ b/src/anylike.c @@ -82,6 +82,7 @@ static const luaL_Reg anylike_lualibs[] = { {LUA_LOADLIBNAME, luaopen_package}, {LUA_TABLIBNAME, luaopen_table}, {LUA_STRLIBNAME, luaopen_string}, + {LUA_MATHLIBNAME, luaopen_math}, {LUA_LOGLIBNAME, luaopen_log}, {LUA_CRYPTLIBNAME, luaopen_crypt}, {NULL, NULL} diff --git a/src/main_loop.lua b/src/main_loop.lua index 785f7b2..5de973a 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -23,10 +23,48 @@ -- along with anylike. If not, see <http://www.gnu.org/licenses/>. -- +socket = require("socket") + function main_loop(opt) log.print("NOTICE", "main_loop started") - - -- add main loop + local host, port = "localhost", 4500 + local ip, err = socket.dns.toip(host) + if(ip == nil) then + log.print("ERROR", "can't resolve " .. host .. ": " .. err) + return -1 + end + + local udp, err = socket.udp() + if(udp == nil) then + log.print("ERROR", "can't create udp socket") + return -1 + end + + local ret, err = udp:setsockname(ip, port) + if(ret == nil) then + log.print("ERROR", "sendto(" .. ip .. "," .. port .. " failed: " .. err) + return -1 + end + + while true do + local dgrm, from_ip, from_port = udp:receivefrom() + if(ret == nil) then + log.print("ERROR", "receivefrom(" .. ip .. "," .. port .. " failed: " .. from_ip) + return -1 + end + + local ret, err = udp:sendto(dgrm, from_ip, from_port) + if(ret == nil) then + log.print("ERROR", "sendto(" .. ip .. "," .. port .. " failed: " .. err) + return -1 + end + + if(string.gsub(dgrm, "^(%w+)%s*%c$", "%1") == "quit") then + return 0 + end + + end + return 0 -- =0 -> OK, <0 -> error, >0 -> signal end |