Created
June 25, 2017 08:27
-
-
Save yurapyon/e152d331ef2969033bc716833ead2e1c to your computer and use it in GitHub Desktop.
luasend
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
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