Cauldron2D.Link (Cauldron2D v0.3.3)

Copy Markdown View Source

Two Cauldron2D.Worlds on two machines playing with rollback over one TCP connection, without BEAM distribution: nothing a peer sends can run code in this VM.

{:ok, listener} = Cauldron2D.Link.listen(host_world, port: 0)
Cauldron2D.Link.port(listener)

{:ok, link} = Cauldron2D.Link.connect({192, 168, 1, 5}, port)
Cauldron2D.World.start_link(game: MyGame, follow: link, rollback: true)

listen/2 accepts connections on :port (default any free port) for the world world, one link process for each. connect/3 makes a link process on this machine that stands for that world: a world started with follow: link, rollback: ... plays with it as if it were the host world itself.

A link carries only what two worlds playing with rollback send each other: the rollback messages (Cauldron2D.World.Rollback) and the calls a world makes of its host (its tick rate, starting to play, joins and leaves). Each message is a 4-byte length and an Erlang term, read with :erlang.binary_to_term/2's :safe; a message of any other shape closes the connection. Pids never cross: a link puts itself in the place of every pid in what it passes on, so the world on the other side sees the link as the peer, and as every player of the other machine. When the connection closes the link stops, so the world sees its peer, and those players, gone.

ping/3 measures the round trip to a listener by connecting and closing again. drop/1 closes every connection a listener has accepted, so the worlds on the other side see their peer gone.

Summary

Functions

Returns a specification to start this module under a supervisor.

Stop a listener accepting connections, links already made going on; or close a link's connection.

A link to the world behind the listener at address and port, for a world of this machine to follow. {:error, reason} when it cannot connect within timeout milliseconds (default 5_000).

Close every connection listener has accepted that is still open.

Accept connections on :port (default 0, any free port) for world. Returns the listener; port/1 gives the port it took.

The milliseconds a TCP connection to address and port takes to open, or {:error, reason}.

The port listener accepts connections on.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close(link)

@spec close(pid()) :: :ok

Stop a listener accepting connections, links already made going on; or close a link's connection.

connect(address, port, timeout \\ 5000)

@spec connect(:inet.ip_address() | String.t(), :inet.port_number(), timeout()) ::
  {:ok, pid()} | {:error, term()}

A link to the world behind the listener at address and port, for a world of this machine to follow. {:error, reason} when it cannot connect within timeout milliseconds (default 5_000).

drop(listener)

@spec drop(pid()) :: :ok

Close every connection listener has accepted that is still open.

listen(world, opts \\ [])

@spec listen(GenServer.server(), keyword()) :: {:ok, pid()} | {:error, term()}

Accept connections on :port (default 0, any free port) for world. Returns the listener; port/1 gives the port it took.

ping(address, port, timeout \\ 2000)

@spec ping(:inet.ip_address() | String.t(), :inet.port_number(), timeout()) ::
  {:ok, float()} | {:error, term()}

The milliseconds a TCP connection to address and port takes to open, or {:error, reason}.

port(listener)

@spec port(pid()) :: :inet.port_number()

The port listener accepts connections on.