-
Notifications
You must be signed in to change notification settings - Fork 42
/
ngxsocket.lua
55 lines (48 loc) · 1.33 KB
/
ngxsocket.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
53
54
55
-- module table
-- thanks to @irimiab: https://github.com/xHasKx/luamqtt/issues/13
local ngxsocket = {}
-- load required stuff
local string_sub = string.sub
local ngx_socket_tcp = ngx.socket.tcp -- luacheck: ignore
-- 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 ngxsocket.connect(conn)
local socket = ngx_socket_tcp()
socket:settimeout(0x7FFFFFFF)
local sock, err = socket:connect(conn.host, conn.port)
if not sock then
return false, "socket:connect failed: "..err
end
if conn.secure then
socket:sslhandshake()
end
conn.sock = socket
return true
end
-- Shutdown network connection
function ngxsocket.shutdown(conn)
conn.sock:close()
end
-- Send data to network connection
function ngxsocket.send(conn, data, i, j)
if i then
return conn.sock:send(string_sub(data, i, j))
else
return conn.sock:send(data)
end
end
-- Receive given amount of data from network connection
function ngxsocket.receive(conn, size)
return conn.sock:receive(size)
end
-- Set connection's socket to non-blocking mode and set a timeout for it
function ngxsocket.settimeout(conn, timeout)
if not timeout then
conn.sock:settimeout(0x7FFFFFFF)
else
conn.sock:settimeout(timeout * 1000)
end
end
-- export module table
return ngxsocket