3 local max_buffer_len
= module
:get_option_number("multiplex_buffer_size", 1024);
5 local portmanager
= require
"core.portmanager";
7 local available_services
= {};
9 local function add_service(service
)
10 local multiplex_pattern
= service
.multiplex
and service
.multiplex
.pattern
;
11 if multiplex_pattern
then
12 module
:log("debug", "Adding multiplex service %q with pattern %q", service
.name
, multiplex_pattern
);
13 available_services
[service
] = multiplex_pattern
;
15 module
:log("debug", "Service %q is not multiplex-capable", service
.name
);
18 module
:hook("service-added", function (event
) add_service(event
.service
); end);
19 module
:hook("service-removed", function (event
) available_services
[event
.service
] = nil; end);
21 for _
, services
in pairs(portmanager
.get_registered_services()) do
22 for _
, service
in ipairs(services
) do
29 local listener
= { default_mode
= "*a" };
31 function listener
.onconnect()
34 function listener
.onincoming(conn
, data
)
35 if not data
then return; end
36 local buf
= buffers
[conn
];
37 buf
= buf
and buf
..data
or data
;
38 for service
, multiplex_pattern
in pairs(available_services
) do
39 if buf
:match(multiplex_pattern
) then
40 module
:log("debug", "Routing incoming connection to %s", service
.name
);
41 local next_listener
= service
.listener
;
42 conn
:setlistener(next_listener
);
43 local onconnect
= next_listener
.onconnect
;
44 if onconnect
then onconnect(conn
) end
45 return next_listener
.onincoming(conn
, buf
);
48 if #buf
> max_buffer_len
then -- Give up
55 function listener
.ondisconnect(conn
)
56 buffers
[conn
] = nil; -- warn if no buffer?
59 listener
.ondetach
= listener
.ondisconnect
;
61 module
:provides("net", {
67 module
:provides("net", {
68 name
= "multiplex_ssl";
69 config_prefix
= "ssl";