Home

Awesome

Generic TCP Server Build Status

Generic TCP Server (gen_tcp_server) is an Erlang behaviour providing quick and easy way to add TCP server functionality to you application. It's implemented as a supervisor managing TCP connections as it's children.

How to use it?

{gen_tcp_server, "", {git, "git://github.com/rpt/gen_tcp_server.git", {tag, "1.0.1"}}}

Callbacks

The gen_tcp_server behaviour specifies three callbacks:

handle_accept(Socket :: socket()) -> {ok, State :: term()} | {stop, Reason :: term()}.
handle_tcp(Socket :: socket(), Data :: binary(), State :: term()) -> {ok, State :: term()} |
                                                                     {stop, Reason :: term()}.
-type reason :: normal | {tcp_error, term()} |
                {handle_accept_error, term()} | {handle_tcp_error, term()}.

handle_close(Socket :: socket(), Reason :: reason(), State :: term()) -> ok.

Pool of acceptors

To use a pool of acceptors use gen_tcp_server:start_link/3 and specify a pool option. For example:

gen_tcp_server:start_link(handler_module, 1234, [{pool, 10}]).

Examples

Simple echo_server example showing how to use gen_tcp_server can be found here.