Created
April 23, 2014 17:05
-
-
Save CapsAdmin/11223745 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gmod = gmod or {} | |
| gmod.Ip = "88.191.111.120" | |
| gmod.ServerPort = 27005 | |
| gmod.ClientPort = 5556 | |
| gmod.Hooks = gmod.Hooks or {} | |
| function gmod.Hook(tag, callback) | |
| gmod.Hooks[tag] = callback | |
| end | |
| local function decode(str) | |
| local args = sigh.Decode(str) | |
| local id = args[1] | |
| table.remove(args, 1) | |
| return id, args | |
| end | |
| local function encode(id, ...) | |
| return sigh.Encode(id, ...) | |
| end | |
| function gmod.Send(id, ...) | |
| local str = encode(id, ...) | |
| gmod.RawSend(str) | |
| end | |
| function gmod.ReceiveFromGmod(str) | |
| local id, args = decode(str) | |
| if gmod.Hooks[id] then | |
| gmod.Hooks[id](unpack(args)) | |
| end | |
| end | |
| hook.Add("GmodMessage", "gmod", gmod.ReceiveFromGmod, print) | |
| -- socket specific code | |
| function gmod.RawSend(str) | |
| if gmod_client then | |
| gmod_client:close() | |
| end | |
| gmod_client = socket.connect(gmod.Ip, gmod.ClientPort) | |
| gmod_client:settimeout(0) | |
| gmod_client:send(str) | |
| end | |
| function gmod.StartServer() | |
| if gmod_server then | |
| gmod_server:close() | |
| end | |
| gmod_server = assert(luasocket.socket.bind("*", gmod.ServerPort)) | |
| gmod_server:settimeout(0) | |
| timer.Create("gmod_server", 0, 0, function() | |
| local client = gmod_server:accept() | |
| if client then | |
| client:settimeout(0) | |
| local tries = 0 | |
| timer.Create("gmod_server_send", 0, 0, function() | |
| local str, err = client:receive() | |
| if str then | |
| hook.Call("GmodMessage", str) | |
| tries = math.huge | |
| elseif err ~= "timeout" then | |
| print(err) | |
| end | |
| if tries > 20 then | |
| client:close() | |
| timer.Remove("gmod_server_send") | |
| end | |
| tries = tries + 1 | |
| end) | |
| end | |
| end) | |
| end | |
| gmod.StartServer() | |
| gmod.Hook("hello", function(...) | |
| print(...) | |
| end) | |
| gmod.Send("hello", "hello from oohh!") | |
| -- | |
| gmod.Hook("l", function(str) | |
| easylua.RunString(entities.GetLocalPlayer(), str) | |
| end) | |
| hook.Add("ConsolePrint",1,function(str) | |
| gmod.Send("console", str) | |
| end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment