Skip to content

Instantly share code, notes, and snippets.

@yurapyon
Created June 25, 2017 08:27
Show Gist options
  • Save yurapyon/e152d331ef2969033bc716833ead2e1c to your computer and use it in GitHub Desktop.
Save yurapyon/e152d331ef2969033bc716833ead2e1c to your computer and use it in GitHub Desktop.
luasend
local string = require "string"
local fmt = string.format
local socket = require "socket"
local udp = socket.udp()
local lua_send = {}
local MLEN = 4096
local function usend(it)
udp:sendto(it, "127.0.0.1", 33333)
end
local function urecv()
return udp:receive(MLEN)
end
function lua_send.send(str)
local len = #str
local rem = len % MLEN
local ct = (len - rem) / MLEN
if rem > 0 then
ct = ct + 1
end
local msg = fmt("%08d", ct)
usend(msg)
for i = 1, ct do
msg = str:sub((i - 1) * 4096 + 1, i * 4096)
usend(msg)
end
end
function lua_send.recv_init()
udp:setsockname("127.0.0.1", 33333)
udp:settimeout(0.005)
end
function lua_send.recv()
local msg = urecv()
if not msg then
return nil
end
local ct = tonumber(msg)
local msg = ""
for i = 1, ct do
msg = msg .. urecv()
end
return msg
end
return lua_send
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment