2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 if not (prosody
and prosody
.config_loaded
) then
10 -- This module only supports loading inside Prosody, outside Prosody
11 -- you should directly require net.server_select or server_event, etc.
12 error(debug
.traceback("Loading outside Prosody or Prosody not yet initialized"), 0);
15 local log = require
"util.logger".init("net.server");
16 local server_type
= require
"core.configmanager".get("*", "network_backend") or "select";
18 if require
"core.configmanager".get("*", "use_libevent") then
19 server_type
= "event";
22 if server_type
== "event" then
23 if not pcall(require
, "luaevent.core") then
24 log("error", "libevent not found, falling back to select()");
25 server_type
= "select"
31 if server_type
== "event" then
32 server
= require
"net.server_event";
35 for k
,v
in pairs(server
.cfg
) do
38 function set_config(settings
)
39 local event_settings
= {
40 ACCEPT_DELAY
= settings
.accept_retry_interval
;
41 ACCEPT_QUEUE
= settings
.tcp_backlog
;
42 CLEAR_DELAY
= settings
.event_clear_interval
;
43 CONNECT_TIMEOUT
= settings
.connect_timeout
;
44 DEBUG
= settings
.debug
;
45 HANDSHAKE_TIMEOUT
= settings
.ssl_handshake_timeout
;
46 MAX_CONNECTIONS
= settings
.max_connections
;
47 MAX_HANDSHAKE_ATTEMPTS
= settings
.max_ssl_handshake_roundtrips
;
48 MAX_READ_LENGTH
= settings
.max_receive_buffer_size
;
49 MAX_SEND_LENGTH
= settings
.max_send_buffer_size
;
50 READ_TIMEOUT
= settings
.read_timeout
;
51 WRITE_TIMEOUT
= settings
.send_timeout
;
54 for k
,default
in pairs(defaults
) do
55 server
.cfg
[k
] = event_settings
[k
] or default
;
58 elseif server_type
== "select" then
59 server
= require
"net.server_select";
62 for k
,v
in pairs(server
.getsettings()) do
65 function set_config(settings
)
66 local select_settings
= {};
67 for k
,default
in pairs(defaults
) do
68 select_settings
[k
] = settings
[k
] or default
;
70 server
.changesettings(select_settings
);
73 server
= require("net.server_"..server_type
);
74 set_config
= server
.set_config
;
75 if not server
.get_backend
then
76 function server
.get_backend()
82 -- If server.hook_signal exists, replace signal.signal()
83 local has_signal
, signal
= pcall(require
, "util.signal");
85 if server
.hook_signal
then
86 function signal
.signal(signal_id
, handler
)
87 if type(signal_id
) == "string" then
88 signal_id
= signal
[signal_id
:upper()];
90 if type(signal_id
) ~= "number" then
91 return false, "invalid-signal";
93 return server
.hook_signal(signal_id
, handler
);
96 server
.hook_signal
= signal
.signal
;
99 if not server
.hook_signal
then
100 server
.hook_signal
= function()
101 return false, "signal hooking not supported"
106 if prosody
and set_config
then
107 local config_get
= require
"core.configmanager".get
;
108 local function load_config()
109 local settings
= config_get("*", "network_settings") or {};
110 return set_config(settings
);
113 prosody
.events
.add_handler("config-reloaded", load_config
);
116 -- require "net.server" shall now forever return this,
117 -- ie. server_select or server_event as chosen above.