forked from xHasKx/luamqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luasocket.lua
52 lines (43 loc) · 1.36 KB
/
luasocket.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- DOC: http://w3.impa.br/~diego/software/luasocket/tcp.html
-- module table
local luasocket = {}
local socket = require("socket")
-- Open network connection to .host and .port in conn table
-- Store opened socket to conn table
-- Returns true on success, or false and error text on failure
function luasocket.connect(conn)
local sock, err = socket.connect(conn.host, conn.port)
if not sock then
return false, "socket.connect failed: "..err
end
conn.sock = sock
return true
end
-- Shutdown network connection
function luasocket.shutdown(conn)
conn.sock:shutdown()
end
-- Send data to network connection
function luasocket.send(conn, data, i, j)
local ok, err = conn.sock:send(data, i, j)
-- print(" luasocket.send:", ok, err, require("mqtt.tools").hex(data))
return ok, err
end
-- Receive given amount of data from network connection
function luasocket.receive(conn, size)
local ok, err = conn.sock:receive(size)
-- if ok then
-- print(" luasocket.receive:", size, require("mqtt.tools").hex(ok))
-- elseif err ~= "timeout" then
-- print(" luasocket.receive:", ok, err)
-- end
return ok, err
end
-- Set connection's socket to non-blocking mode and set a timeout for it
function luasocket.settimeout(conn, timeout)
conn.timeout = timeout
conn.sock:settimeout(timeout, "t")
end
-- export module table
return luasocket
-- vim: ts=4 sts=4 sw=4 noet ft=lua