Software /
code /
prosody
Annotate
plugins/mod_net_multiplex.lua @ 5877:615a0774e4cc
util.timer: Updated to use util.indexedbheap to provide a more complete API. Timers can now be stopped or rescheduled. Callbacks are now pcall'd. Adding/removing timers from within timer callbacks works better. Optional parameter can be passed when creating timer which gets passed to callback, eliminating the need for closures in various timer uses. Timers are now much more lightweight.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 30 Oct 2013 17:44:42 -0400 |
parent | 5120:bcabea740c00 |
child | 6380:4220ffb87b22 |
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 |
5120
bcabea740c00
mod_{admin_telnet,c2s,component,http,net_multiplex,s2s}: Use module:provides() instead of module:add_item().
Waqas Hussain <waqas20@gmail.com>
parents:
4619
diff
changeset
|
60 module:provides("net", { |
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
|
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 }); |