ace v0.9.2 Ace.Application behaviour

Behaviour module for implementing a server to handle tcp/tls connections.

See Ace.Server to start a server with an application

Example

defmodule CounterServer do
  use Ace.Application

  def handle_connect(_, num) do
    {:nosend, num}
  end

  def handle_packet(_, last) do
    count = last + 1
    {:send, "#{count}
", count}
  end

  def handle_info(_, last) do
    {:nosend, last}
  end
end

Link to this section Summary

Types

The current state of a server

Functions

Check if a module is an implementation of Ace.Application

Callbacks

Invoked when a new client connects to the server

Invoked when connection to the client is lost, or client disconnects

Every message recieved by the server, that was not sent from the client, invokes this callback

Every packet recieved from the client invokes this callback

Link to this section Types

Link to this type state()
state() :: term

The current state of a server.

Link to this section Functions

Link to this function is_implemented?(module)

Check if a module is an implementation of Ace.Application.

iex> is_implemented?(EchoServer)
true

iex> is_implemented?(IO)
false

Link to this section Callbacks

Link to this callback handle_connect(information, state)
handle_connect(information, state) ::
  {:send, iodata, state} |
  {:send, iodata, state, timeout} |
  {:nosend, state} |
  {:nosend, state, timeout} |
  {:close, state} when information: Ace.Connection.information

Invoked when a new client connects to the server.

The state is the second element in the app tuple that was used to start the endpoint.

Returning {:nosend, state} will setup a new server with internal state. The state is perserved in the process loop and passed as the second option to subsequent callbacks.

Returning {:nosend, state, timeout} is the same as {:send, state}. In addition handle_info(:timeout, state) will be called after timeout milliseconds, if no messages are received in that interval.

Returning {:send, message, state} or {:send, message, state, timeout} is similar to their :nosend counterparts, except the message is sent as the first communication to the client.

Returning {:close, state} will shutdown the server without any messages being sent or recieved

Link to this callback handle_disconnect(reason, state)
handle_disconnect(reason, state) :: term when reason: term

Invoked when connection to the client is lost, or client disconnects.

Note a server will not always call handle_disconnect on exiting.

Link to this callback handle_info(term, state)
handle_info(term, state) ::
  {:send, term, state} |
  {:send, term, state, timeout} |
  {:nosend, state} |
  {:nosend, state, timeout} |
  {:close, state}

Every message recieved by the server, that was not sent from the client, invokes this callback.

The return actions are the same as for the handle_disconnect/2 callback

Link to this callback handle_packet(binary, state)
handle_packet(binary, state) ::
  {:send, term, state} |
  {:send, term, state, timeout} |
  {:nosend, state} |
  {:nosend, state, timeout} |
  {:close, state}

Every packet recieved from the client invokes this callback.

The return actions are the same as for the handle_disconnect/2 callback

No additional packets will be taken from the socket until this callback returns