net/server.lua
author Tobias Markmann <tm@ayena.de>
Sun, 29 Nov 2009 21:33:37 +0100
changeset 2281 27441b099984
parent 2059 d4fb80b60c65
parent 2162 22b6b1899a55
child 2435 1ab73691b58e
permissions -rw-r--r--
Merge with tip.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     1
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local use_luaevent = require "core.configmanager".get("*", "core", "use_libevent");
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     4
if use_luaevent then
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     5
	use_luaevent = pcall(require, "luaevent.core");
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     6
	if not use_luaevent then
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     7
		log("error", "libevent not found, falling back to select()");
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     8
	end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
     9
end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
    10
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
    13
if use_luaevent then
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
	server = require "net.server_event";
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    15
	-- util.timer requires "net.server", so instead of having
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    16
	-- Lua look for, and load us again (causing a loop) - set this here
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    17
	-- (usually it isn't set until we return, look down there...)
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
	package.loaded["net.server"] = server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	-- Backwards compatibility for timers, addtimer
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
	-- called a function roughly every second
2105
fecf33cb2913 net.server: Small fix for addtimer() compatibility code
Matthew Wild <mwild1@gmail.com>
parents: 2094
diff changeset
    22
	local add_task = require "util.timer".add_task;
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	function server.addtimer(f)
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		return add_task(1, function (...) f(...); return 1; end);
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	end
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
else
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	server = require "net.server_select";
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	package.loaded["net.server"] = server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
end
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    31
-- require "net.server" shall now forever return this,
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    32
-- ie. server_select or server_event as chosen above.
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
    33
return server;