diff --git a/Emakefile b/Emakefile index a4c9601..7ae4f97 100644 --- a/Emakefile +++ b/Emakefile @@ -17,6 +17,7 @@ % along with EGS. If not, see . {'src/egs.erl', [{outdir, "ebin"}]}. +{'src/egs_cron.erl', [{outdir, "ebin"}]}. {'src/egs_db.erl', [{outdir, "ebin"}]}. {'src/egs_game.erl', [{outdir, "ebin"}]}. {'src/egs_login.erl', [{outdir, "ebin"}]}. diff --git a/src/egs.erl b/src/egs.erl index f9612f4..8918b35 100644 --- a/src/egs.erl +++ b/src/egs.erl @@ -21,7 +21,7 @@ -include("include/records.hrl"). --define(MODULES, [egs, egs_db, egs_game, egs_login, egs_patch, egs_proto]). +-define(MODULES, [egs, egs_cron, egs_db, egs_game, egs_login, egs_patch, egs_proto]). %% @doc Start all the application servers. Return the PIDs of the listening processes. @@ -30,10 +30,11 @@ start() -> application:start(ssl), ssl:seed(crypto:rand_bytes(256)), egs_db:create(), + Cron = egs_cron:start(), Game = egs_game:start(), Login = egs_login:start(), Patch = egs_patch:start(), - [{patch, Patch}, {login, Login}, {game, Game}]. + [{patch, Patch}, {login, Login}, {game, Game}, {cron, Cron}]. %% @doc Reload all the modules. diff --git a/src/egs_cron.erl b/src/egs_cron.erl new file mode 100644 index 0000000..30097e8 --- /dev/null +++ b/src/egs_cron.erl @@ -0,0 +1,42 @@ +% EGS: Erlang Game Server +% Copyright (C) 2010 Loic Hoguin +% +% This file is part of EGS. +% +% EGS is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% EGS is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with EGS. If not, see . + +-module(egs_cron). +-export([start/0]). % external +-export([cleanup/0, keepalive/0]). % internal + +-include("include/records.hrl"). + +%% @doc Start the cron processes. + +start() -> + KeepAlivePid = spawn_link(?MODULE, keepalive, []), + [{keepalive, KeepAlivePid}]. + +%% @doc Keep connected players alive. +%% @todo Don't even need to send a keepalive packet if we sent a packet in the last Timeout milliseconds. + +keepalive() -> + receive + _ -> + ?MODULE:keepalive() + after 5000 -> + lists:foreach(fun(User) -> User#users.pid ! {psu_keepalive} end, egs_db:users_select_all()), + reload, + ?MODULE:keepalive() + end. diff --git a/src/egs_game.erl b/src/egs_game.erl index 03b6516..f74b8b2 100644 --- a/src/egs_game.erl +++ b/src/egs_game.erl @@ -235,6 +235,9 @@ loop(CSocket, GID, Version, SoFar) -> {psu_chat, ChatGID, ChatName, ChatModifiers, ChatMessage} -> egs_proto:send_chat(CSocket, Version, ChatGID, ChatName, ChatModifiers, ChatMessage), ?MODULE:loop(CSocket, GID, Version, SoFar); + {psu_keepalive} -> + egs_proto:send_keepalive(CSocket, GID), + ?MODULE:loop(CSocket, GID, Version, SoFar); {psu_player_spawn, SpawnPlayer} -> send_spawn(CSocket, GID, SpawnPlayer), ?MODULE:loop(CSocket, GID, Version, SoFar); @@ -253,7 +256,6 @@ loop(CSocket, GID, Version, SoFar) -> _ -> ?MODULE:loop(CSocket, GID, Version, SoFar) after 1000 -> - egs_proto:send_keepalive(CSocket, GID), reload, ?MODULE:loop(CSocket, GID, Version, SoFar) end.