Guest User

knorket socket bla

a guest
Apr 23rd, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. if not Spring.GetConfigInt("LuaSocketEnabled", 0) == 1 then
  2.     Spring.Echo("LuaSocketEnabled is disabled")
  3.     return false
  4. end
  5.  
  6. function widget:GetInfo()
  7. return {
  8.     name    = "Test-Widget for luasocket",
  9.     desc    = "a simple test widget to show capabilities of luasocket",
  10.     author  = "knorke loling with abmas socket thing",
  11.     date    = "Jul. 2011",
  12.     license = "GNU GPL, v2 or later",
  13.     layer   = 0,
  14.     enabled = true,
  15. }
  16. end
  17.  
  18. include "tp_clickbuttons.lua"
  19.  
  20. --VFS.Include(LUAUI_DIRNAME .. "Widgets/socket/socket.lua")
  21.  
  22. local socket=require("socket")
  23.  
  24. local client
  25. local set
  26.  
  27. local host = "localhost"
  28. local port = 8200
  29. --local file = "/"
  30.  
  31. local function dumpConfig()
  32.     -- dump all luasocket related config settings to console
  33.     for _, conf in ipairs({"TCPAllowConnect", "TCPAllowListen", "UDPAllowConnect", "UDPAllowListen"  }) do
  34.         Spring.Echo(conf .. " = " .. Spring.GetConfigString(conf, ""))
  35.     end
  36. end
  37.  
  38. local chatTextBox =
  39. {
  40.     x = 0.5,
  41.     y = 0.95,
  42.     w = 0.4,
  43.     h = 0.2,
  44.     textsize = 0.02,
  45.     color = 1,
  46.     messages = {}
  47. }
  48.  
  49.  
  50.  
  51. function widget:Initialize()
  52.     addmessage (chatTextBox, "HEY!")
  53.     addmessage (chatTextBox, "1")
  54.     addmessage (chatTextBox, "2")
  55.     addmessage (chatTextBox, "3")
  56.     addmessage (chatTextBox, "4A\n4B\r\n4c")
  57.     addmessage (chatTextBox, "5")
  58.    
  59.    
  60.     dumpConfig()
  61.     client=socket.tcp()
  62.     client:settimeout(0)
  63.     --Spring.Echo(socket.dns.toip("localhost"))
  64.     --FIXME dns-request seems to block
  65.     res, err = client:connect(host, port)
  66.     if not res and not res=="timeout" then
  67.         Spring.Echo("Error in connect: "..err)
  68.         return
  69.     end
  70.     set = newset()
  71.     set:insert(client)
  72. end
  73.  
  74.  
  75. function newset()
  76.     local reverse = {}
  77.     local set = {}
  78.     return setmetatable(set, {__index = {
  79.         insert = function(set, value)
  80.             if not reverse[value] then
  81.                 table.insert(set, value)
  82.                 reverse[value] = table.getn(set)
  83.             end
  84.         end,
  85.         remove = function(set, value)
  86.             local index = reverse[value]
  87.             if index then
  88.                 reverse[value] = nil
  89.                 local top = table.remove(set)
  90.                 if top ~= value then
  91.                     reverse[top] = index
  92.                     set[index] = top
  93.                 end
  94.             end
  95.         end
  96.     }})
  97. end
  98.  
  99.  
  100. local sendLogin = true
  101.  
  102.  
  103. local t = 0
  104. function widget:Update(dt) 
  105.     t = t +dt
  106.     if (t>3) then
  107.         sendPing ()
  108.         t = 0
  109.     end
  110.     local readable, writeable, error = socket.select(set, set, 0)
  111.  
  112.     --receive
  113.     for __, input in ipairs(readable) do
  114.         local s, status, partial = input:receive('*a') --try to read all data
  115.         if status == "timeout" then
  116.             Spring.Echo(s or partial)
  117.             addmessage (chatTextBox, "<" .. (s or partial or "nil"))           
  118.         elseif status == "closed" then
  119.             Spring.Echo("closed connection")
  120.             input:close()
  121.             set:remove(input)
  122.         end
  123.     end
  124.  
  125.     --send
  126.     if sendBuffer and #sendBuffer > 1 then     
  127.         if error~=nil then -- some error happened in select        
  128.             if error=="timeout" then return end -- nothing to do, return       
  129.             Spring.Echo("Error in select: " .. error)
  130.         end
  131.         for __, output in ipairs(writeable) do
  132.             for i=1,#sendBuffer, 1 do
  133.                 output:send (sendBuffer[i])
  134.                 addmessage (chatTextBox,  ">" .. (sendBuffer[i] or "nil"))             
  135.             end
  136.             sendBuffer = {}
  137.         end
  138.     end
  139.  
  140. end
  141.  
  142.  
  143. function sendPing ()
  144.     sendSocketMsg ("PING")
  145. end
  146.  
  147. ---------
  148. sendBuffer = {}
  149.  
  150. --add a message to sendBuffer that will be send next Update()
  151. function sendSocketMsg (msg)
  152.     sendBuffer[#sendBuffer+1] = msg .. "\r\n"
  153. end
  154.  
  155. --return table of received messages
  156. msgCounter = 0
  157. function receivedSocketMsg (msg)
  158.     Spring.Echo (msgCounter ..") receivedSocketMsg:".. msg)
  159.     msgCounter=msgCounter+1
  160. end
  161. ---------
  162.    
  163. function widget:TextCommand(command)
  164.     if (command=="join") then      
  165.         sendSocketMsg ("LOGIN knorkebot MD5_PASSWORD_HASH_REMOVED 6666 * knorkeluasocket")
  166.         sendSocketMsg ("JOIN knorkesbot")
  167.     else
  168.         sendSocketMsg ("SAY knorkesbot " ..command)
  169.     end
  170. end
  171.  
  172. -------------------------------------------------------
  173. -----------drawing
  174.  
  175. function widget:DrawScreen()   
  176.     drawmessagebox(chatTextBox, 20)
  177. end
Advertisement
Add Comment
Please, Sign In to add comment