CapsAdmin

Untitled

Mar 10th, 2012
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. gmod = gmod or {}
  2.  
  3. gmod.Ip = "88.191.111.120"
  4. gmod.ServerPort = 27005
  5. gmod.ClientPort = 5556
  6.  
  7. gmod.Hooks = gmod.Hooks or {}
  8.  
  9. function gmod.Hook(tag, callback)
  10.     gmod.Hooks[tag] = callback
  11. end
  12.  
  13. local function decode(str)
  14.     local args = sigh.Decode(str)
  15.  
  16.     local id = args[1]
  17.     table.remove(args, 1)
  18.  
  19.     return id, args
  20. end
  21.  
  22. local function encode(id, ...)
  23.     return sigh.Encode(id, ...)
  24. end
  25.  
  26. function gmod.Send(id, ...)
  27.     local str = encode(id, ...)
  28.  
  29.     gmod.RawSend(str)
  30. end
  31.  
  32. function gmod.ReceiveFromGmod(str)
  33.     local id, args = decode(str)
  34.  
  35.     if gmod.Hooks[id] then
  36.         gmod.Hooks[id](unpack(args))
  37.     end
  38. end
  39. hook.Add("GmodMessage", "gmod", gmod.ReceiveFromGmod, print)
  40.  
  41. -- socket specific code
  42.  
  43. function gmod.RawSend(str)
  44.     if gmod_client then
  45.         gmod_client:close()
  46.     end
  47.     gmod_client = socket.connect(gmod.Ip, gmod.ClientPort)
  48.     gmod_client:settimeout(0)
  49.  
  50.     gmod_client:send(str)
  51. end
  52.  
  53. function gmod.StartServer()
  54.     if gmod_server then
  55.         gmod_server:close()
  56.     end
  57.  
  58.     gmod_server = assert(luasocket.socket.bind("*", gmod.ServerPort))
  59.     gmod_server:settimeout(0)
  60.  
  61.     timer.Create("gmod_server", 0, 0, function()
  62.         local client = gmod_server:accept()
  63.  
  64.         if client then
  65.             client:settimeout(0)
  66.             local tries = 0
  67.  
  68.             timer.Create("gmod_server_send", 0, 0, function()
  69.                 local str, err = client:receive()
  70.  
  71.                 if str then
  72.                     hook.Call("GmodMessage", str)
  73.                     tries = math.huge
  74.                 elseif err ~= "timeout" then
  75.                     print(err)
  76.                 end
  77.  
  78.                 if tries > 20 then
  79.                     client:close()
  80.                     timer.Remove("gmod_server_send")
  81.                 end
  82.  
  83.                 tries = tries + 1
  84.             end)
  85.         end
  86.     end)
  87. end
  88.  
  89. gmod.StartServer()
  90.  
  91. gmod.Hook("hello", function(...)
  92.     print(...)
  93. end)
  94.  
  95. gmod.Send("hello", "hello from oohh!")
  96.  
  97. --
  98.  
  99. gmod.Hook("l", function(str)
  100.     easylua.RunString(entities.GetLocalPlayer(), str)
  101. end)
  102.  
  103. hook.Add("ConsolePrint",1,function(str)
  104.     gmod.Send("console", str)
  105. end)
Advertisement
Add Comment
Please, Sign In to add comment