玩家进程用gen_server来建模,我不直接使用 use GenServer, 而是使用exactor,该库可以去掉反锁的接口定义。
我们新建一个 player_server_manager app吧, 使用 mix new player_server_manager --sup, 会给我们增加sup。然后在mix.exs里增加exactor的依赖如下:
defp deps do [{:exactor, "~> 2.2"}] end
跑 mix deps.get,成功了依赖就准备好了。
默认生成的player_server_manager.ex
defmodule PlayerServerManager do use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Define workers and child supervisors to be supervised # worker(PlayerServerManager.Worker, [arg1, arg2, arg3]), ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: PlayerServerManager.Supervisor] Supervisor.start_link(children, opts) end end
玩家进程就叫player_server吧。把它改成我需要的。
defmodule PlayerServerManager do use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do import Supervisor.Spec, warn: false children = [ # Define workers and child supervisors to be supervised worker(PlayerServer, [], restart: :temporary), ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :simple_one_for_one, name: PlayerServerManager.Supervisor] Supervisor.start_link(children, opts) end end
因为玩家进程是动态创建的,因此采用simple_one_for_one, 同时我不需要默认传参数,还有我不需要重启。
好了现在让我们编写PlayerServer模块(现在是简单的,而且我还未知有什么需要调整的,后续随着服务器设计而演化吧)。
让我们先增加个查询钻石的接口吧。
defmodule PlayerServer do use ExActor.GenServer defstart start_link(player), do: initial_state(%{player: player, socket: nil}) defcall gem, state: state, do: reply(state.player.base_info.gem) end
我们在PlayerServerManager里增加启动接口如下
def start_player_server(%Player{} = player) do Supervisor.start_child(PlayerServerManager.Supervisor, [player]) end
在player_server_manager_test.exs里增加测试代码
defmodule PlayerServerManagerTest do use ExUnit.Case doctest PlayerServerManager setup do Application.stop(:player_server_manager) :ok = Application.start(:player_server_manager) end setup do player = Player.new(0) {:ok, player: player} end test "start player_server", %{player: player} do assert {:ok, pid} = PlayerServerManager.start_player_server(player) assert PlayerServer.gem(pid) == 0 end end
测试通过。
写到这里发现之前Player和BaseInfo @behavior 实际上应该为@hehaviour, 既然之前没报错,我就把它去掉了,测试依然通过。说明其实可能只需要
defdelegate。
好了,这一章就到这。遗留的问题有,我们通常需要给玩家进程一个名字,而不是通过pid,如果是单节点的话, local 注册已经足够,如果是跨服访问,我们需要一个分布式的注册机制(实际上分布式注册机制容易有坑,如无必要,千万别)。比如gporc,比如syn。 前者比较有名,我以前在erlang用过,之前有碰到莫名奇妙的问题。所以下章试试syn吧。
时间: 2024-10-09 22:05:24