Software /
code /
prosody
Annotate
net/multiplex_listener.lua @ 4211:9a12fc2baa37
tools/migration/*.lua: Rename config to migrator.cfg.lua, add error handling for config and command-line parameters
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 25 Feb 2011 03:32:44 +0000 |
parent | 3138:d6151c9a6f7e |
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); |
3138
d6151c9a6f7e
net.multiplex_listener: Test to make sure listener.onconnect isn't nil.
Waqas Hussain <waqas20@gmail.com>
parents:
3137
diff
changeset
|
22 local onconnect = listener.onconnect; |
d6151c9a6f7e
net.multiplex_listener: Test to make sure listener.onconnect isn't nil.
Waqas Hussain <waqas20@gmail.com>
parents:
3137
diff
changeset
|
23 if onconnect then onconnect(conn) end |
2351
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 listener.onincoming(conn, buf); |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 elseif buf:match(">") then |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local listener; |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 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
|
28 if xmlns == "jabber:server" then |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 listener = xmppserver_listener; |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 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
|
31 listener = xmppcomponent_listener; |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 else |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 listener = xmppclient_listener; |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 end |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 conn:setlistener(listener); |
3138
d6151c9a6f7e
net.multiplex_listener: Test to make sure listener.onconnect isn't nil.
Waqas Hussain <waqas20@gmail.com>
parents:
3137
diff
changeset
|
36 local onconnect = listener.onconnect; |
d6151c9a6f7e
net.multiplex_listener: Test to make sure listener.onconnect isn't nil.
Waqas Hussain <waqas20@gmail.com>
parents:
3137
diff
changeset
|
37 if onconnect then onconnect(conn) end |
2351
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 listener.onincoming(conn, buf); |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 elseif #buf > 1024 then |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 conn:close(); |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 else |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 buffer[conn] = buf; |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 end |
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 function server.ondisconnect(conn, err) |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 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
|
48 end |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
f8d002090cea
multiplex_listener: Initial commit. Multiple protocols on a single port.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 connlisteners_register("multiplex", server); |