Annotate

net/multiplex_listener.lua @ 2628:04958fb28c44

certmanager, hostmanager: Rename get_context() to create_context() to be more explicit about what it does
author Matthew Wild <mwild1@gmail.com>
date Sat, 13 Feb 2010 16:08:43 +0000
parent 2351:f8d002090cea
child 3137:463d51ee9a2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2351
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local connlisteners_register = require "net.connlisteners".register;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local connlisteners_get = require "net.connlisteners".get;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local httpserver_listener = connlisteners_get("httpserver");
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local xmppserver_listener = connlisteners_get("xmppserver");
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local xmppclient_listener = connlisteners_get("xmppclient");
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local xmppcomponent_listener = connlisteners_get("xmppcomponent");
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local server = { default_mode = "*a" };
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local buffer = {};
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 function server.onincoming(conn, data)
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 if not data then return; end
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local buf = buffer[conn];
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 buffer[conn] = nil;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 buf = buf and buf..data or data;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 if buf:match("^[a-zA-Z]") then
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local listener = httpserver_listener;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 conn:setlistener(listener);
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 listener.onincoming(conn, buf);
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 elseif buf:match(">") then
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local listener;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local xmlns = buf:match("%sxmlns%s*=%s*['\"]([^'\"]*)");
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 if xmlns == "jabber:server" then
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 listener = xmppserver_listener;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 elseif xmlns == "jabber:component:accept" then
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 listener = xmppcomponent_listener;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 else
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 listener = xmppclient_listener;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 end
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 conn:setlistener(listener);
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 listener.onincoming(conn, buf);
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 elseif #buf > 1024 then
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 conn:close();
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 else
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 buffer[conn] = buf;
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 end
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 function server.ondisconnect(conn, err)
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 buffer[conn] = nil; -- warn if no buffer?
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
f8d002090cea multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 connlisteners_register("multiplex", server);