• Stars
    star
    11
  • Rank 1,694,829 (Top 34 %)
  • Language
    Erlang
  • License
    Apache License 2.0
  • Created over 12 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generic TCP Server for Erlang applications

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?

  • Run make to build.
  • Run make test to run tests.
  • Add as a dependency to your rebar.config:
{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/1 - called on accepting a new connection
handle_accept(Socket :: socket()) -> {ok, State :: term()} | {stop, Reason :: term()}.
  • handle_tcp/3 - for handling incoming TCP data
handle_tcp(Socket :: socket(), Data :: binary(), State :: term()) -> {ok, State :: term()} |
                                                                     {stop, Reason :: term()}.
  • handle_close/3 - called when socket is closed
-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.