Annotate

plugins/mod_net_multiplex.lua @ 4842:88101f652174

Backed out changeset 9f1fb34cd7f8 - obsoleted by 5ef05f32bc42.
author Matthew Wild <mwild1@gmail.com>
date Fri, 11 May 2012 01:39:52 +0100
parent 4619:d5739b8b7161
child 5120:bcabea740c00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4619
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:set_global();
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local max_buffer_len = module:get_option_number("multiplex_buffer_size", 1024);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local portmanager = require "core.portmanager";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local available_services = {};
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local function add_service(service)
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local multiplex_pattern = service.multiplex and service.multiplex.pattern;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if multiplex_pattern then
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 module:log("debug", "Adding multiplex service %q with pattern %q", service.name, multiplex_pattern);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 available_services[service] = multiplex_pattern;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 else
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 module:log("debug", "Service %q is not multiplex-capable", service.name);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 module:hook("service-added", function (event) add_service(event.service); end);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 module:hook("service-removed", function (event) available_services[event.service] = nil; end);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 for service_name, services in pairs(portmanager.get_registered_services()) do
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 for i, service in ipairs(services) do
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 add_service(service);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local buffers = {};
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local listener = { default_mode = "*a" };
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function listener.onconnect()
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function listener.onincoming(conn, data)
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if not data then return; end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local buf = buffers[conn];
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 buffers[conn] = nil;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 buf = buf and buf..data or data;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 for service, multiplex_pattern in pairs(available_services) do
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if buf:match(multiplex_pattern) then
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 module:log("debug", "Routing incoming connection to %s", service.name);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local listener = service.listener;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 conn:setlistener(listener);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local onconnect = listener.onconnect;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if onconnect then onconnect(conn) end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return listener.onincoming(conn, buf);
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if #buf > max_buffer_len then -- Give up
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 conn:close();
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 else
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 buffers[conn] = buf;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 function listener.ondisconnect(conn, err)
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 buffers[conn] = nil; -- warn if no buffer?
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 module:add_item("net-provider", {
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 name = "multiplex";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 config_prefix = "";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 listener = listener;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 });
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 module:provides("net", {
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 name = "multiplex_ssl";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 config_prefix = "ssl";
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 listener = listener;
d5739b8b7161 mod_net_multiplex: Port multiplexing (run multiple different services on a the same port(s))... now pluggable for use with any net plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 });