Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- oohh = oohh or {}
- oohh.Clients = oohh.Clients or {}
- do -- meta
- local META = {}
- function META:SetName(name)
- self.name = name
- end
- function META:GetName()
- return self.name or "[no name]"
- end
- local function encode(id, ...)
- return sigh.Encode(id, ...)
- end
- function META:Send(name, ...)
- self:SendRaw(encode(name, ...))
- end
- function META:Close(reason)
- self:Send("close", reason or "Dropped by Server")
- self.sock:Close()
- end
- function META:SendRaw(data)
- local buf = GLSockBuffer()
- buf:Write(data .. "\n")
- self.sock:Send(buf, function(self, sent, error)
- if error ~= GLSOCK_ERROR_SUCCESS then
- ErrorNoHalt("[oohh] Failure sending data to "..self:Name().."! Error code "..error.."\n")
- end
- end)
- end
- oohh.ObjectMeta = META
- end
- do -- server
- local function OnRead(self, buffer, err)
- local obj = self.oohh_obj
- if not obj then
- ErrorNoHalt("unidentified client!\n")
- obj:Close()
- return
- end
- if err == GLSOCK_ERROR_SUCCESS then
- print(sigh.decode(buffer:Read(buffer:Size())))
- else
- ErrorNoHalt("failed to read data from oohh client: "..err.." ("..obj:GetName()..")\n")
- end
- end
- local function OnAccept(self, client, err)
- if err == GLSOCK_ERROR_SUCCESS then
- local obj = setmetatable({sock = client}, oohh.ObjectMeta)
- client.oohh_obj = obj
- client:Read(1500, OnRead)
- table.insert(oohh.Clients, obj)
- else
- ErrorNoHalt("failed to accept new oohh client: "..err.."\n")
- end
- end
- local function OnListen(self, err)
- if errno == GLSOCK_ERROR_SUCCESS then
- self:Accept(OnAccept)
- else
- print("Listen Error: "..tonumber(errno))
- end
- end
- local function OnBind(self, errno)
- if errno == GLSOCK_ERROR_SUCCESS then
- self:Listen(10, OnListen)
- else
- print("Bind Error: "..tonumber(errno))
- end
- end
- function oohh.StartServer(port)
- for _, obj in pairs(oohh.Clients) do
- obj:Close("Server Restarting")
- end
- local socket = oohh.Server
- if socket then
- socket:Cancel()
- socket:Close()
- socket:Destroy()
- end
- socket = GLSock(GLSOCK_TYPE_ACCEPTOR)
- socket:Bind("", port or oohh.Port, OnBind)
- oohh.Server = socket
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment