| 1 |
== Transports |
| 2 |
|
| 3 |
A transport defines the interface to interact with a socket. |
| 4 |
|
| 5 |
Transports can be used for connecting, listening and accepting |
| 6 |
connections, but also for receiving and sending data. Both |
| 7 |
passive and active mode are supported, although all sockets |
| 8 |
are initialized as passive. |
| 9 |
|
| 10 |
=== TCP transport |
| 11 |
|
| 12 |
The TCP transport is a thin wrapper around `gen_tcp`. |
| 13 |
|
| 14 |
=== SSL transport |
| 15 |
|
| 16 |
The SSL transport is a thin wrapper around `ssl`. |
| 17 |
|
| 18 |
Ranch depends on `ssl` by default so any necessary |
| 19 |
dependencies will start when Ranch is started. It is |
| 20 |
possible to remove the dependency when the SSL transport |
| 21 |
will not be used. Refer to your release build tool's |
| 22 |
documentation for more information. |
| 23 |
|
| 24 |
When embedding Ranch listeners that have an SSL transport, |
| 25 |
your application must depend on the `ssl` application for |
| 26 |
proper behavior. |
| 27 |
|
| 28 |
=== Sending and receiving data |
| 29 |
|
| 30 |
This section assumes that `Transport` is a valid transport handler |
| 31 |
(like `ranch_tcp` or `ranch_ssl`) and `Socket` is a connected |
| 32 |
socket obtained through the listener. |
| 33 |
|
| 34 |
You can send data to a socket by calling the `Transport:send/2` |
| 35 |
function. The data can be given as `iodata()`, which is defined as |
| 36 |
`binary() | iolist()`. All the following calls will work: |
| 37 |
|
| 38 |
.Sending data to the socket |
| 39 |
|
| 40 |
[source,erlang] |
| 41 |
---- |
| 42 |
Transport:send(Socket, <<"Ranch is cool!">>). |
| 43 |
Transport:send(Socket, "Ranch is cool!"). |
| 44 |
Transport:send(Socket, ["Ranch", ["is", "cool!"]]). |
| 45 |
Transport:send(Socket, ["Ranch", [<<"is">>, "cool!"]]). |
| 46 |
---- |
| 47 |
|
| 48 |
You can receive data either in passive or in active mode. Passive mode |
| 49 |
means that you will perform a blocking `Transport:recv/3` call, while |
| 50 |
active mode means that you will receive the data as a message. |
| 51 |
|
| 52 |
By default, all data will be received as binary. It is possible to |
| 53 |
receive data as strings, although this is not recommended as binaries |
| 54 |
are a more efficient construct, especially for binary protocols. |
| 55 |
|
| 56 |
Receiving data using passive mode requires a single function call. The |
| 57 |
first argument is the socket, and the third argument is a timeout duration |
| 58 |
before the call returns with `{error, timeout}`. |
| 59 |
|
| 60 |
The second argument is the amount of data in bytes that we want to receive. |
| 61 |
The function will wait for data until it has received exactly this amount. |
| 62 |
If you are not expecting a precise size, you can specify 0 which will make |
| 63 |
this call return as soon as data was read, regardless of its size. |
| 64 |
|
| 65 |
.Receiving data from the socket in passive mode |
| 66 |
|
| 67 |
[source,erlang] |
| 68 |
{ok, Data} = Transport:recv(Socket, 0, 5000). |
| 69 |
|
| 70 |
Active mode requires you to inform the socket that you want to receive |
| 71 |
data as a message and to write the code to actually receive it. |
| 72 |
|
| 73 |
There are three kinds of active modes: `{active, once}`, `{active, N}` |
| 74 |
and `{active, true}`. The first will send a single message before going |
| 75 |
back to passive mode; the second will send `N` messages followed by |
| 76 |
a `Passive` message when switching back to passive mode; the third |
| 77 |
will send messages indefinitely. We recommend not using the `{active, true}` |
| 78 |
mode as it could quickly flood your process mailbox. It's better to keep |
| 79 |
the data in the socket and read it only when required. |
| 80 |
|
| 81 |
Four different messages can be received: |
| 82 |
|
| 83 |
* Incoming data: `{OK, Socket, Data}` |
| 84 |
* Socket closed: `{Closed, Socket}` |
| 85 |
* Socket error: `{Error, Socket, Reason}` |
| 86 |
* Switch to passive mode: `{Passive, Socket}` |
| 87 |
|
| 88 |
The value of `OK`, `Closed`, `Error` and `Passive` can be different |
| 89 |
depending on the transport being used. To be able to properly match |
| 90 |
on them you must first call the `Transport:messages/0` function. |
| 91 |
|
| 92 |
.Retrieving the transport's active message identifiers |
| 93 |
|
| 94 |
[source,erlang] |
| 95 |
{OK, Closed, Error, Passive} = Transport:messages(). |
| 96 |
|
| 97 |
To start receiving messages you will need to call the `Transport:setopts/2` |
| 98 |
function, and do so every time you want to receive data. |
| 99 |
|
| 100 |
.Receiving messages from the socket in active mode |
| 101 |
|
| 102 |
[source,erlang] |
| 103 |
---- |
| 104 |
{OK, Closed, Error, Passive} = Transport:messages(), |
| 105 |
Transport:setopts(Socket, [{active, once}]), |
| 106 |
receive |
| 107 |
{OK, Socket, Data} -> |
| 108 |
io:format("data received: ~p~n", [Data]); |
| 109 |
{Closed, Socket} -> |
| 110 |
io:format("socket got closed!~n"); |
| 111 |
{Error, Socket, Reason} -> |
| 112 |
io:format("error happened: ~p~n", [Reason]) |
| 113 |
end. |
| 114 |
---- |
| 115 |
|
| 116 |
You can easily integrate active sockets with existing Erlang code as all |
| 117 |
you really need is just a few more clauses when receiving messages. |
| 118 |
|
| 119 |
=== Sending files |
| 120 |
|
| 121 |
As in the previous section it is assumed `Transport` is a valid transport |
| 122 |
handler and `Socket` is a connected socket obtained through the listener. |
| 123 |
|
| 124 |
To send a whole file, with name `Filename`, over a socket: |
| 125 |
|
| 126 |
.Sending a file by filename |
| 127 |
|
| 128 |
[source,erlang] |
| 129 |
{ok, SentBytes} = Transport:sendfile(Socket, Filename). |
| 130 |
|
| 131 |
Or part of a file, with `Offset` greater than or equal to 0, `Bytes` number of |
| 132 |
bytes and chunks of size `ChunkSize`: |
| 133 |
|
| 134 |
.Sending part of a file by filename in chunks |
| 135 |
|
| 136 |
[source,erlang] |
| 137 |
Opts = [{chunk_size, ChunkSize}], |
| 138 |
{ok, SentBytes} = Transport:sendfile(Socket, Filename, Offset, Bytes, Opts). |
| 139 |
|
| 140 |
To improve efficiency when sending multiple parts of the same file it is also |
| 141 |
possible to use a file descriptor opened in raw mode: |
| 142 |
|
| 143 |
.Sending a file opened in raw mode |
| 144 |
|
| 145 |
[source,erlang] |
| 146 |
{ok, RawFile} = file:open(Filename, [raw, read, binary]), |
| 147 |
{ok, SentBytes} = Transport:sendfile(Socket, RawFile, Offset, Bytes, Opts). |
| 148 |
|
| 149 |
=== Upgrading a TCP socket to SSL |
| 150 |
|
| 151 |
A connected TCP socket can be upgraded to a SSL socket via the function |
| 152 |
`ranch_ssl:handshake/3`. The socket *must* be in `{active, false}` mode |
| 153 |
before telling the client that the server is ready to upgrade in order |
| 154 |
to avoid race conditions. |
| 155 |
|
| 156 |
IMPORTANT: The new socket received from `ranch_ssl:handshake/3` must be |
| 157 |
used via the `ranch_ssl` transport. |
| 158 |
|
| 159 |
.Performing a TLS handshake on a TCP socket |
| 160 |
[source,erlang] |
| 161 |
{ok, SslSocket} = ranch_ssl:handshake(TcpSocket, SslOpts, 5000). |
| 162 |
|
| 163 |
=== Writing a transport handler |
| 164 |
|
| 165 |
A transport handler is a module implementing the `ranch_transport` behavior. |
| 166 |
It defines a certain number of callbacks that must be written in order to |
| 167 |
allow transparent usage of the transport handler. |
| 168 |
|
| 169 |
The behavior doesn't define the socket options available when opening a |
| 170 |
socket. These do not need to be common to all transports as it's easy enough |
| 171 |
to write different initialization functions for the different transports that |
| 172 |
will be used. With one exception though. The `setopts/2` function *must* |
| 173 |
implement the `{active, once}` and the `{active, true}` options. |
| 174 |
|
| 175 |
If the transport handler doesn't have a native implementation of `sendfile/5` a |
| 176 |
fallback is available, `ranch_transport:sendfile/6`. The extra first argument |
| 177 |
is the transport's module. See `ranch_ssl` for an example. |
| 178 |
|
| 179 |
It is highly recommended for a custom transport handler to implement the |
| 180 |
optional `format_error/1` callback, in order to provide a human-readable |
| 181 |
diagnostic string. Implementing this callback will become mandatory in |
| 182 |
Ranch 3.0. |