CapsAdmin

Untitled

Mar 10th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. require("glsock")
  2.  
  3. oohh = oohh or {}
  4.  
  5. oohh.Ip = me:IPAddress():match("(.-):")
  6. oohh.ServerPort = 5556
  7. oohh.ClientPort = 27005
  8.  
  9. oohh.Hooks = oohh.Hooks or {}
  10.  
  11. function oohh.Hook(tag, callback)
  12.     oohh.Hooks[tag] = callback
  13. end
  14.  
  15. local function decode(str)
  16.     local args = sigh.Decode(str)
  17.  
  18.     local id = args[1]
  19.     table.remove(args, 1)
  20.  
  21.     return id, args
  22. end
  23.  
  24. local function encode(id, ...)
  25.     return sigh.Encode(id, ...)
  26. end
  27.  
  28. function oohh.Send(id, ...)
  29.     local str = encode(id, ...)
  30.  
  31.     oohh.RawSend(str)
  32. end
  33.  
  34. function oohh.ReceiveFromoohh(str)
  35.     local id, args = decode(str)
  36.  
  37.     if oohh.Hooks[id] then
  38.         oohh.Hooks[id](unpack(args))
  39.     end
  40. end
  41. hook.Add("OohhMessage", "oohh", oohh.ReceiveFromoohh)
  42.  
  43. -- socket specific code
  44.  
  45. function oohh.RawSend(str)
  46.     if oohh_client then
  47.         oohh_client:Cancel()
  48.         oohh_client:Close()
  49.     end
  50.  
  51.     oohh_client = GLSock(GLSOCK_TYPE_TCP)
  52.     oohh_client:Connect(oohh.Ip, oohh.ClientPort, function(sck, error)
  53.  
  54.         if error == GLSOCK_ERROR_SUCCESS then
  55.             local buffer = GLSockBuffer()
  56.                 buffer:Write(str)
  57.                 buffer:Write("\n")
  58.             sck:Send(buffer, function() end)
  59.         else
  60.             print("Failed to connect to capsadmin, error: " .. error)
  61.         end
  62.     end)
  63. end
  64.  
  65. function oohh.StartServer()
  66.     local bind_socket
  67.  
  68.     local function OnRead(sck, buffer, error)
  69.         if error == GLSOCK_ERROR_SUCCESS then
  70.             local count, data = buffer:Read(buffer:Size())
  71.             if count > 0 then
  72.                 hook.Call("OohhMessage", GAMEMODE, data)
  73.             end
  74.         end
  75.  
  76.         if error == GLSOCK_ERROR_SUCCESS then
  77.             bind_socket()
  78.         else
  79.             sck:Read(100, OnRead)
  80.         end
  81.     end
  82.  
  83.     local function OnAccept(sck, client, errno)
  84.         if errno == GLSOCK_ERROR_SUCCESS then
  85.             client:Read(1500, OnRead)
  86.         else
  87.             print("Accept Error: "..tonumber(errno))
  88.         end
  89.     end
  90.  
  91.     local function OnListen(sck, errno)
  92.         if errno == GLSOCK_ERROR_SUCCESS then
  93.             sck:Accept(OnAccept)
  94.         else
  95.             print("Listen Error: "..tonumber(errno))
  96.         end
  97.     end
  98.  
  99.     local function OnBind(sck, errno)
  100.         print(sck, errno)
  101.         if errno == GLSOCK_ERROR_SUCCESS then
  102.             sck:Listen(10, OnListen)
  103.             print("bound to port " .. oohh.ServerPort)
  104.         else
  105.             print("Bind Error: "..tonumber(errno))
  106.         end
  107.     end
  108.  
  109.     bind_socket = function()
  110.         if oohh_server then
  111.             oohh_server:Cancel()
  112.             oohh_server:Close()
  113.             oohh_server:Destroy()
  114.         end
  115.  
  116.         oohh_server = GLSock(GLSOCK_TYPE_ACCEPTOR)
  117.         oohh_server:Bind("", oohh.ServerPort, OnBind)
  118.     end
  119.  
  120.     bind_socket()
  121. end
  122.  
  123. oohh.StartServer()
  124.  
  125. oohh.Hook("hello", function(...)
  126.     print(...)
  127. end)
  128.  
  129. oohh.Send("hello", "hello from gmod!")
Advertisement
Add Comment
Please, Sign In to add comment