Convert the patch server to a supervisor-compatible module.

This commit is contained in:
Loïc Hoguin 2010-07-18 10:51:08 +02:00
parent f46ba0e1f9
commit 37a5811135
10 changed files with 72 additions and 71 deletions

View File

@ -23,7 +23,7 @@
{'src/egs_db.erl', [{outdir, "ebin"}]}. {'src/egs_db.erl', [{outdir, "ebin"}]}.
{'src/egs_game.erl', [{outdir, "ebin"}]}. {'src/egs_game.erl', [{outdir, "ebin"}]}.
{'src/egs_login.erl', [{outdir, "ebin"}]}. {'src/egs_login.erl', [{outdir, "ebin"}]}.
{'src/egs_patch.erl', [{outdir, "ebin"}]}. {'src/psu/psu_patch.erl', [{outdir, "ebin"}]}.
{'src/egs_proto.erl', [{outdir, "ebin"}]}. {'src/egs_proto.erl', [{outdir, "ebin"}]}.
{'src/psu_appearance.erl', [{outdir, "ebin"}]}. {'src/psu_appearance.erl', [{outdir, "ebin"}]}.
{'src/psu_characters.erl', [{outdir, "ebin"}]}. {'src/psu_characters.erl', [{outdir, "ebin"}]}.

View File

@ -10,7 +10,7 @@
egs_db, egs_db,
egs_game, egs_game,
egs_login, egs_login,
egs_patch, psu_patch,
egs_proto, egs_proto,
psu_appearance, psu_appearance,
psu_characters, psu_characters,

View File

@ -1,68 +0,0 @@
% 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 <http://www.gnu.org/licenses/>.
-module(egs_patch).
-export([start/0]). % external
-export([listen/1, accept/1, process/1]). % internal
-include("include/network.hrl").
%% @doc Start the patch server. Currently supports AOTI US and JP.
start() ->
spawn_link(?MODULE, listen, [?PATCH_PORT_JP]),
USPid = spawn_link(?MODULE, listen, [?PATCH_PORT_US]),
{ok, USPid}.
%% @doc Listen for connections.
listen(Port) ->
process_flag(trap_exit, true),
{ok, LSocket} = gen_tcp:listen(Port, ?PATCH_LISTEN_OPTIONS),
?MODULE:accept(LSocket).
%% @doc Accept connections.
accept(LSocket) ->
case gen_tcp:accept(LSocket, 5000) of
{ok, CSocket} ->
spawn_link(?MODULE, process, [CSocket]);
{error, timeout} ->
reload
end,
?MODULE:accept(LSocket).
%% @doc Fake the patch server by sending what the game wants to hear: no updates available.
%% Ignore all the return values.
process(CSocket) ->
io:format("faking patch server: no updates~n"),
_ = send_packet(CSocket, "p/patch-0.bin"),
_ = gen_tcp:recv(CSocket, 0, 5000),
_ = send_packet(CSocket, "p/patch-1.bin"),
_ = send_packet(CSocket, "p/patch-2.bin"),
_ = gen_tcp:recv(CSocket, 0, 5000),
_ = send_packet(CSocket, "p/patch-3.bin"),
_ = send_packet(CSocket, "p/patch-4.bin"),
_ = gen_tcp:close(CSocket).
%% @doc Send a packet from a file.
send_packet(CSocket, PacketFilename) ->
{ok, Packet} = file:read_file(PacketFilename),
gen_tcp:send(CSocket, Packet).

View File

@ -22,6 +22,8 @@
-export([init/1]). %% Supervisor callbacks. -export([init/1]). %% Supervisor callbacks.
-export([start_link/0, upgrade/0]). %% Other functions. -export([start_link/0, upgrade/0]). %% Other functions.
-include("include/network.hrl").
%% @spec start_link() -> ServerRet %% @spec start_link() -> ServerRet
%% @doc API for starting the supervisor. %% @doc API for starting the supervisor.
start_link() -> start_link() ->
@ -50,5 +52,6 @@ init([]) ->
Processes = [{egs_cron, {egs_cron, start, []}, permanent, 5000, worker, dynamic}, Processes = [{egs_cron, {egs_cron, start, []}, permanent, 5000, worker, dynamic},
{egs_game, {egs_game, start, []}, permanent, 5000, worker, dynamic}, {egs_game, {egs_game, start, []}, permanent, 5000, worker, dynamic},
{egs_login, {egs_login, start, []}, permanent, 5000, worker, dynamic}, {egs_login, {egs_login, start, []}, permanent, 5000, worker, dynamic},
{egs_patch, {egs_patch, start, []}, permanent, 5000, worker, dynamic}], {psu_patch_jp, {psu_patch, start_link, [?PATCH_PORT_JP]}, permanent, 5000, worker, dynamic},
{psu_patch_us, {psu_patch, start_link, [?PATCH_PORT_US]}, permanent, 5000, worker, dynamic}],
{ok, {{one_for_one, 10, 10}, Processes}}. {ok, {{one_for_one, 10, 10}, Processes}}.

66
src/psu/psu_patch.erl Normal file
View File

@ -0,0 +1,66 @@
%% @author Loïc Hoguin <essen@dev-extend.eu>
%% @copyright 2010 Loïc Hoguin.
%% @doc Process patch requests.
%%
%% 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 <http://www.gnu.org/licenses/>.
-module(psu_patch).
-export([start_link/1]). %% External.
-export([listen/1, accept/1, process/1]). %% Internal
-define(OPTIONS, [binary, {send_timeout, 5000}, {packet, 0}, {active, false}, {reuseaddr, true}]).
%% @spec start_link(Port) -> {ok,Pid::pid()}
%% @doc Starts the patch server for inclusion in a supervisor tree.
start_link(Port) ->
Pid = spawn(?MODULE, listen, [Port]),
{ok, Pid}.
%% @spec listen(Port) -> ok
%% @doc Listen for connections.
listen(Port) ->
{ok, LSocket} = gen_tcp:listen(Port, ?OPTIONS),
?MODULE:accept(LSocket).
%% @spec accept(LSocket) -> ok
%% @doc Accept connections.
accept(LSocket) ->
case gen_tcp:accept(LSocket, 5000) of
{ok, CSocket} ->
spawn(?MODULE, process, [CSocket]);
{error, timeout} ->
reload
end,
?MODULE:accept(LSocket).
%% @spec process(CSocket) -> ok
%% @doc Fake the patch server by sending what the game wants to hear: no updates available.
process(CSocket) ->
io:format("faking patch server: no updates~n"),
send_packet(CSocket, "priv/psu_patch/patch-0.bin"),
gen_tcp:recv(CSocket, 0, 5000),
send_packet(CSocket, "priv/psu_patch/patch-1.bin"),
send_packet(CSocket, "priv/psu_patch/patch-2.bin"),
gen_tcp:recv(CSocket, 0, 5000),
send_packet(CSocket, "priv/psu_patch/patch-3.bin"),
send_packet(CSocket, "priv/psu_patch/patch-4.bin"),
gen_tcp:close(CSocket).
%% @spec send_packet(CSocket, PacketFilename) -> ok
%% @doc Send a packet from a file.
send_packet(CSocket, PacketFilename) ->
{ok, Packet} = file:read_file(PacketFilename),
gen_tcp:send(CSocket, Packet).