| 1 |
== Protocols |
| 2 |
|
| 3 |
A protocol handler starts a connection process and defines the |
| 4 |
protocol logic executed in this process. |
| 5 |
|
| 6 |
=== Writing a protocol handler |
| 7 |
|
| 8 |
All protocol handlers must implement the `ranch_protocol` behavior |
| 9 |
which defines a single callback, `start_link/3`. This callback is |
| 10 |
responsible for spawning a new process for handling the connection. |
| 11 |
It receives three arguments: the name of the listener, the |
| 12 |
transport handler being used and the protocol options defined in |
| 13 |
the call to `ranch:start_listener/5`. This callback must |
| 14 |
return `{ok, Pid}`, with `Pid` the pid of the new process. |
| 15 |
|
| 16 |
The newly started process can then freely initialize itself. However, |
| 17 |
it must call `ranch:handshake/1,2` before doing any socket operation. |
| 18 |
This will ensure the connection process is the owner of the socket. |
| 19 |
It expects the listener's name as argument. |
| 20 |
|
| 21 |
.Perform the socket handshake |
| 22 |
|
| 23 |
[source,erlang] |
| 24 |
{ok, Socket} = ranch:handshake(Ref). |
| 25 |
|
| 26 |
If your protocol code requires specific socket options, you should |
| 27 |
set them while initializing your connection process, after |
| 28 |
calling `ranch:handshake/1,2`. You can use `Transport:setopts/2` |
| 29 |
for that purpose. |
| 30 |
|
| 31 |
Following is the complete protocol code for the example found |
| 32 |
in `examples/tcp_echo/`. |
| 33 |
|
| 34 |
.Protocol module that echoes everything it receives |
| 35 |
|
| 36 |
[source,erlang] |
| 37 |
---- |
| 38 |
-module(echo_protocol). |
| 39 |
-behaviour(ranch_protocol). |
| 40 |
|
| 41 |
-export([start_link/3]). |
| 42 |
-export([init/3]). |
| 43 |
|
| 44 |
start_link(Ref, Transport, Opts) -> |
| 45 |
Pid = spawn_link(?MODULE, init, [Ref, Transport, Opts]), |
| 46 |
{ok, Pid}. |
| 47 |
|
| 48 |
init(Ref, Transport, _Opts = []) -> |
| 49 |
{ok, Socket} = ranch:handshake(Ref), |
| 50 |
loop(Socket, Transport). |
| 51 |
|
| 52 |
loop(Socket, Transport) -> |
| 53 |
case Transport:recv(Socket, 0, 5000) of |
| 54 |
{ok, Data} -> |
| 55 |
Transport:send(Socket, Data), |
| 56 |
loop(Socket, Transport); |
| 57 |
_ -> |
| 58 |
ok = Transport:close(Socket) |
| 59 |
end. |
| 60 |
---- |
| 61 |
|
| 62 |
=== Using gen_statem and gen_server |
| 63 |
|
| 64 |
Special processes like the ones that use the `gen_statem` or `gen_server` |
| 65 |
behaviours have the particularity of having their `start_link` call not |
| 66 |
return until the `init` function returns. This is problematic, because |
| 67 |
you won't be able to call `ranch:handshake/1,2` from the `init` callback |
| 68 |
as this would cause a deadlock to happen. |
| 69 |
|
| 70 |
This problem can be addressed in several ways. |
| 71 |
|
| 72 |
==== gen_statem |
| 73 |
|
| 74 |
* Use state enter calls and place the `ranch:handshake/1,2` call in the enter |
| 75 |
clause of the initial state. Check the `tcp_reverse` example for a complete |
| 76 |
example. |
| 77 |
* Use a `next_event` action in the return from `init/1` and place the |
| 78 |
`ranch:handshake/1,2` call in the clause handling the event in the initial |
| 79 |
state. |
| 80 |
* Use the `gen_statem:enter_loop/4` function and start your process with |
| 81 |
`proc_lib:spawn_link/3` or `proc_lib:start_link/3,4,5`. See below for an |
| 82 |
example. |
| 83 |
|
| 84 |
.Using gen_statem:enter_loop/4 to start a protocol |
| 85 |
|
| 86 |
[source,erlang] |
| 87 |
---- |
| 88 |
-module(my_protocol). |
| 89 |
-behaviour(gen_statem). |
| 90 |
-behaviour(ranch_protocol). |
| 91 |
|
| 92 |
-export([start_link/3]). |
| 93 |
-export([init/1]). |
| 94 |
%% Exports of other gen_statem callbacks here. |
| 95 |
|
| 96 |
start_link(Ref, Transport, Opts) -> |
| 97 |
{ok, proc_lib:spawn_link(?MODULE, init, [{Ref, Transport, Opts}])}. |
| 98 |
|
| 99 |
init({Ref, Transport, _Opts}) -> |
| 100 |
%% Perform any required state initialization here. |
| 101 |
{ok, Socket} = ranch:handshake(Ref), |
| 102 |
ok = Transport:setopts(Socket, [{active, once}]), |
| 103 |
gen_statem:enter_loop(?MODULE, [], state_name, {state_data, Socket, Transport}). |
| 104 |
|
| 105 |
%% Other gen_statem callbacks here. |
| 106 |
---- |
| 107 |
|
| 108 |
==== gen_server |
| 109 |
|
| 110 |
* Use `{continue, Continue}` in the return from `init/1` and place the |
| 111 |
`ranch:handshake/1,2` call in a corresponding `handle_continue/2` clause. |
| 112 |
* Use the `gen_server:enter_loop/3` function and start your process with |
| 113 |
`proc_lib:spawn_link/3` or `proc_lib:start_link/3,4,5`. |