2 -- Copyright (C) 2014,2016 Daurnimator
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
10 This file is a template for writing a net.server compatible backend.
14 Read patterns (also called modes) can be one of:
15 - "*a": Read as much as possible
16 - "*l": Read until end of line
21 local handle_methods
= {};
22 handle_mt
.__index
= handle_methods
;
24 function handle_methods
:set_mode(new_pattern
)
27 function handle_methods
:setlistener(listeners
)
30 function handle_methods
:setoption(option
, value
)
33 function handle_methods
:ip()
36 function handle_methods
:starttls(sslctx
)
39 function handle_methods
:write(data
)
42 function handle_methods
:close()
45 function handle_methods
:pause()
48 function handle_methods
:resume()
53 - socket: the socket object underlying this handle
55 function handle_methods
:socket()
60 - boolean: if an ssl context has been set on this handle
62 function handle_methods
:ssl()
70 Called when a client socket has established a connection with it's peer
72 function listeners
.onconnect(handle
)
76 Called when data is received
77 If reading data failed this will be called with `nil, "error message"`
79 function listeners
.onincoming(handle
, buff
, err
)
84 - "ssl-handshake-complete"
86 function listeners
.onstatus(handle
, status
)
90 Called when the peer has closed the connection
92 function listeners
.ondisconnect(handle
)
96 Called when the handle's write buffer is empty
98 function listeners
.ondrain(handle
)
102 Called when a socket inactivity timeout occurs
104 function listeners
.onreadtimeout(handle
)
107 --[[ detach: Called when other listeners are going to be removed
110 function listeners
.ondetach(handle
)
113 --- Top level functions
115 --[[ Returns the syscall level event mechanism in use.
118 - backend: e.g. "select", "epoll"
120 local function get_backend()
123 --[[ Starts the event loop.
128 local function loop()
131 --[[ Stop a running loop()
133 local function setquitting(quit
)
137 --[[ Links to two handles together, so anything written to one is piped to the other
140 - sender, receiver: handles to link
141 - buffersize: maximum #bytes until sender will be locked
143 local function link(sender
, receiver
, buffersize
)
146 --[[ Binds and listens on the given address and port
147 If `sslctx` is given, the connecting clients will have to negotiate an SSL session
150 - address: address to bind to, may be "*" to bind all addresses. will be resolved if it is a string.
151 - port: port to bind (as number)
152 - listeners: a table of listeners
153 - pattern: the read pattern
154 - sslctx: is a valid luasec constructor
158 - nil, "an error message": on failure (e.g. out of file descriptors)
160 local function addserver(address
, port
, listeners
, pattern
, sslctx
)
163 --[[ Binds and listens on the given address and port
164 Mostly the same as addserver but with all optional arguments in a table
167 - address: address to bind to, may be "*" to bind all addresses. will be resolved if it is a string.
168 - port: port to bind (as number)
169 - listeners: a table of listeners
170 - config: table of extra settings
171 - read_size: the amount of bytes to read or a read pattern
172 - tls_ctx: is a valid luasec constructor
173 - tls_direct: boolean true for direct TLS, false (or nil) for starttls
177 - nil, "an error message": on failure (e.g. out of file descriptors)
179 local function listen(address
, port
, listeners
, config
)
183 --[[ Wraps a lua-socket socket client socket in a handle.
184 The socket must be already connected to the remote end.
185 If `sslctx` is given, a SSL session will be negotiated before listeners are called.
188 - socket: the lua-socket object to wrap
189 - ip: returned by `handle:ip()`
191 - listeners: a table of listeners
192 - pattern: the read pattern
193 - sslctx: is a valid luasec constructor
194 - typ: the socket type, one of:
201 - nil, "an error message": on failure (e.g. )
203 local function wrapclient(socket
, ip
, serverport
, listeners
, pattern
, sslctx
)
206 --[[ Connects to the given address and port
207 If `sslctx` is given, a SSL session will be negotiated before listeners are called.
210 - address: address to connect to. will be resolved if it is a string.
211 - port: port to connect to (as number)
212 - listeners: a table of listeners
213 - pattern: the read pattern
214 - sslctx: is a valid luasec constructor
215 - typ: the socket type, one of:
222 - nil, "an error message": on failure (e.g. out of file descriptors)
224 local function addclient(address
, port
, listeners
, pattern
, sslctx
, typ
)
227 --[[ Close all handles
229 local function closeall()
232 --[[ The callback should be called after `delay` seconds.
233 The callback should be called with the time at the point of firing.
234 If the callback returns a number, it should be called again after that many seconds.
237 - delay: number of seconds to wait
238 - callback: function to call.
240 local function add_task(delay
, callback
)
243 --[[ Adds a handler for when a signal is fired.
244 Optional to implement
245 callback does not take any arguments
248 - signal_id: the signal id (as number) to listen for
251 local function hook_signal(signal_id
, handler
)
254 --[[ Adds a low-level FD watcher
256 - fd_number: A non-negative integer representing a file descriptor or
257 object with a :getfd() method returning one
258 - on_readable: Optional callback for when the FD is readable
259 - on_writable: Optional callback for when the FD is writable
264 local function watchfd(fd_number
, on_readable
, on_writable
)
268 get_backend
= get_backend
;
270 setquitting
= setquitting
;
272 addserver
= addserver
;
273 wrapclient
= wrapclient
;
274 addclient
= addclient
;
276 hook_signal
= hook_signal
;