mod_s2s: Handle authentication of s2sin and s2sout the same way
[prosody.git] / doc / net.server.lua
blobaa9a4a9d172aa8153325ba81cac6d0cbe64ad064
1 -- Prosody IM
2 -- Copyright (C) 2014,2016 Daurnimator
3 --
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
7 --luacheck: ignore
9 --[[
10 This file is a template for writing a net.server compatible backend.
13 --[[
14 Read patterns (also called modes) can be one of:
15 - "*a": Read as much as possible
16 - "*l": Read until end of line
19 --- Handle API
20 local handle_mt = {};
21 local handle_methods = {};
22 handle_mt.__index = handle_methods;
24 function handle_methods:set_mode(new_pattern)
25 end
27 function handle_methods:setlistener(listeners)
28 end
30 function handle_methods:setoption(option, value)
31 end
33 function handle_methods:ip()
34 end
36 function handle_methods:starttls(sslctx)
37 end
39 function handle_methods:write(data)
40 end
42 function handle_methods:close()
43 end
45 function handle_methods:pause()
46 end
48 function handle_methods:resume()
49 end
51 --[[
52 Returns
53 - socket: the socket object underlying this handle
55 function handle_methods:socket()
56 end
58 --[[
59 Returns
60 - boolean: if an ssl context has been set on this handle
62 function handle_methods:ssl()
63 end
66 --- Listeners API
67 local listeners = {}
69 --[[ connect
70 Called when a client socket has established a connection with it's peer
72 function listeners.onconnect(handle)
73 end
75 --[[ incoming
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)
80 end
82 --[[ status
83 Known statuses:
84 - "ssl-handshake-complete"
86 function listeners.onstatus(handle, status)
87 end
89 --[[ disconnect
90 Called when the peer has closed the connection
92 function listeners.ondisconnect(handle)
93 end
95 --[[ drain
96 Called when the handle's write buffer is empty
98 function listeners.ondrain(handle)
99 end
101 --[[ readtimeout
102 Called when a socket inactivity timeout occurs
104 function listeners.onreadtimeout(handle)
107 --[[ detach: Called when other listeners are going to be removed
108 Allows for clean-up
110 function listeners.ondetach(handle)
113 --- Top level functions
115 --[[ Returns the syscall level event mechanism in use.
117 Returns:
118 - backend: e.g. "select", "epoll"
120 local function get_backend()
123 --[[ Starts the event loop.
125 Returns:
126 - "quitting"
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
139 Arguments:
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
149 Arguments:
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
156 Returns:
157 - handle
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
166 Arguments:
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
175 Returns:
176 - handle
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.
187 Arguments:
188 - socket: the lua-socket object to wrap
189 - ip: returned by `handle:ip()`
190 - port:
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:
195 - "tcp"
196 - "tcp6"
197 - "udp"
199 Returns:
200 - handle, socket
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.
209 Arguments:
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:
216 - "tcp"
217 - "tcp6"
218 - "udp"
220 Returns:
221 - handle
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.
236 Arguments:
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
247 Arguments:
248 - signal_id: the signal id (as number) to listen for
249 - handler: callback
251 local function hook_signal(signal_id, handler)
254 --[[ Adds a low-level FD watcher
255 Arguments:
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
261 Returns:
262 - net.server handle
264 local function watchfd(fd_number, on_readable, on_writable)
267 return {
268 get_backend = get_backend;
269 loop = loop;
270 setquitting = setquitting;
271 link = link;
272 addserver = addserver;
273 wrapclient = wrapclient;
274 addclient = addclient;
275 closeall = closeall;
276 hook_signal = hook_signal;
277 watchfd = watchfd;
278 listen = listen;