CapsAdmin

Untitled

Mar 10th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. oohh = oohh or {}
  2. oohh.Clients = oohh.Clients or {}
  3.  
  4. do -- meta
  5.     local META = {}
  6.  
  7.     function META:SetName(name)
  8.         self.name = name
  9.     end
  10.  
  11.     function META:GetName()
  12.         return self.name or "[no name]"
  13.     end
  14.  
  15.     local function encode(id, ...)
  16.         return sigh.Encode(id, ...)
  17.     end
  18.  
  19.     function META:Send(name, ...)
  20.         self:SendRaw(encode(name, ...))
  21.     end
  22.  
  23.     function META:Close(reason)
  24.         self:Send("close", reason or "Dropped by Server")
  25.         self.sock:Close()
  26.     end
  27.  
  28.     function META:SendRaw(data)
  29.         local buf = GLSockBuffer()
  30.         buf:Write(data .. "\n")
  31.         self.sock:Send(buf, function(self, sent, error)
  32.             if error ~= GLSOCK_ERROR_SUCCESS then
  33.                 ErrorNoHalt("[oohh] Failure sending data to "..self:Name().."! Error code "..error.."\n")
  34.             end
  35.         end)
  36.     end
  37.    
  38.     oohh.ObjectMeta = META
  39. end
  40.  
  41. do -- server
  42.  
  43.     local function OnRead(self, buffer, err)
  44.         local obj = self.oohh_obj
  45.  
  46.         if not obj then
  47.             ErrorNoHalt("unidentified client!\n")
  48.             obj:Close()
  49.             return
  50.         end
  51.  
  52.         if err == GLSOCK_ERROR_SUCCESS then
  53.             print(sigh.decode(buffer:Read(buffer:Size())))
  54.         else
  55.             ErrorNoHalt("failed to read data from oohh client: "..err.." ("..obj:GetName()..")\n")
  56.         end
  57.     end
  58.  
  59.     local function OnAccept(self, client, err)
  60.         if err == GLSOCK_ERROR_SUCCESS then
  61.             local obj = setmetatable({sock = client}, oohh.ObjectMeta)
  62.  
  63.             client.oohh_obj = obj
  64.             client:Read(1500, OnRead)
  65.  
  66.             table.insert(oohh.Clients, obj)
  67.         else
  68.             ErrorNoHalt("failed to accept new oohh client: "..err.."\n")
  69.         end
  70.     end
  71.  
  72.     local function OnListen(self, err)
  73.         if errno == GLSOCK_ERROR_SUCCESS then
  74.             self:Accept(OnAccept)
  75.         else
  76.             print("Listen Error: "..tonumber(errno))
  77.         end
  78.     end
  79.  
  80.     local function OnBind(self, errno)
  81.         if errno == GLSOCK_ERROR_SUCCESS then
  82.             self:Listen(10, OnListen)
  83.         else
  84.             print("Bind Error: "..tonumber(errno))
  85.         end
  86.     end
  87.  
  88.     function oohh.StartServer(port)
  89.         for _, obj in pairs(oohh.Clients) do
  90.             obj:Close("Server Restarting")
  91.         end
  92.  
  93.         local socket = oohh.Server
  94.  
  95.         if socket then
  96.             socket:Cancel()
  97.             socket:Close()
  98.             socket:Destroy()
  99.         end
  100.  
  101.         socket = GLSock(GLSOCK_TYPE_ACCEPTOR)
  102.         socket:Bind("", port or oohh.Port, OnBind)
  103.  
  104.         oohh.Server = socket
  105.     end
  106.  
  107. end
Advertisement
Add Comment
Please, Sign In to add comment