2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local listeners_dir = (CFG_SOURCEDIR or ".").."/net/";
12 local server = require "net.server";
13 local log = require "util.logger".init("connlisteners");
14 local tostring = tostring;
18 local dofile, xpcall, error =
21 local debug_traceback = debug.traceback;
23 module "connlisteners"
27 function register(name, listener)
28 if listeners[name] and listeners[name] ~= listener then
29 log("debug", "Listener %s is already registered, not registering any more", name);
32 listeners[name] = listener;
33 log("debug", "Registered connection listener %s", name);
37 function deregister(name)
38 listeners[name] = nil;
42 local h = listeners[name];
44 local ok, ret = xpcall(function() dofile(listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua") end, debug_traceback);
46 log("error", "Error while loading listener '%s': %s", tostring(name), tostring(ret));
54 function start(name, udata)
55 local h, err = get(name);
57 error("No such connection module: "..name.. (err and (" ("..err..")") or ""), 0);
60 local interfaces = (udata and udata.interface) or h.default_interface or "*";
61 if type(interfaces) == "string" then interfaces = {interfaces}; end
62 local port = (udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0);
63 local mode = (udata and udata.mode) or h.default_mode or 1;
64 local ssl = (udata and udata.ssl) or nil;
65 local autossl = udata and udata.type == "ssl";
67 if autossl and not ssl then
68 return nil, "no ssl context";
72 for _, interface in ipairs(interfaces) do
74 handler, err[interface] = server.addserver(interface, port, h, mode, autossl and ssl or nil);