aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/ranch_transport.asciidoc
blob: 6545720662c4a996ef4099402f8b129c564bd511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
= ranch_transport(3)

== Name

ranch_transport - behaviour for transport modules

== Description

The `ranch_transport` behaviour defines the interface used
by Ranch transports.

== Types

=== sendfile_opts() = [{chunk_size, non_neg_integer()}]

Options used by the sendfile function and callbacks.

Allows configuring the chunk size, in bytes. Defaults to 8191 bytes.

== Callbacks

=== accept(LSocket, Timeout) -> {ok, CSocket} | {error, closed | timeout | atom()}

LSocket = CSocket = any():: Listening socket.
Timeout = timeout():: Accept timeout.

Accept a connection on the given listening socket.

The `handshake` callback will be used to initialize the socket
after accepting the connection. This is most useful when the
transport is not raw TCP, like with SSL for example.

=== close(Socket) -> ok

Socket = any():: Socket opened with listen/1 or accept/2.

Close the given socket.

=== controlling_process(Socket, Pid) -> ok | {error, closed | not_owner | atom()}

Socket = any():: Socket opened with listen/1 or accept/2.
Pid = pid():: Pid of the new owner of the socket.

Change the controlling process for the given socket.

The controlling process is the process that is allowed to
perform operations on the socket, and that will receive
messages from the socket when active mode is used. When
the controlling process dies, the socket is closed.

=== handshake(CSocket0, Options, Timeout) -> {ok, CSocket1}

CSocket0 = any():: Uninitialized socket for this connection.
Options = any():: Options for initialization.
Timeout = timeout():: Handshake timeout.
CSocket1 = any():: Initialized socket for this connection.

Perform post-accept initialization of the connection.

This function will be called by connection processes
before performing any socket operation. It allows
transports that require extra initialization to perform
their task and return a socket that is ready to use.

=== listen(TransOpts) -> {ok, LSocket} | {error, atom()}

TransOpts = any():: Transport options.
LSocket = any():: Listening socket.

Listen for connections on the given port.

The port is given as part of the transport options under
the key `port`. Any other option is transport dependent.

The socket returned by this call can then be used to
accept connections. It is not possible to send or receive
data from the listening socket.

=== messages() -> {OK, Closed, Error}

OK = Closed = Error = atom():: Tuple names.

Return the atoms used to identify messages sent in active mode.

=== name() -> Name

Name = atom():: Transport module name.

Return the name of the transport.

=== peername(CSocket) -> {ok, {IP, Port}} | {error, atom()}

CSocket = any():: Socket for this connection.
IP = inet:ip_address():: IP of the remote endpoint.
Port = inet:port_number():: Port of the remote endpoint.

Return the IP and port of the remote endpoint.

=== recv(CSocket, Length, Timeout) -> {ok, Packet} | {error, closed | timeout | atom()}

CSocket = any():: Socket for this connection.
Length = non_neg_integer():: Requested length.
Timeout = timeout():: Receive timeout.
Packet = iodata() | any():: Data received.

Receive data from the given socket when in passive mode.

Trying to receive data from a socket that is in active mode
will return an error.

A length of 0 will return any data available on the socket.

While it is possible to use the timeout value `infinity`,
this is highly discouraged as this could cause your process
to get stuck waiting for data that will never come. This may
happen when a socket becomes half-open due to a crash of the
remote endpoint. Wi-Fi going down is another common culprit
of this issue.

=== send(CSocket, Packet) -> ok | {error, atom()}

CSocket = any():: Socket for this connection.
Packet = iodata():: Data to be sent.

Send data to the given socket.

=== sendfile(CSocket, File) -> sendfile(CSocket, File, 0, 0, [])

Alias of `ranch_transport:sendfile/5`.

=== sendfile(CSocket, File, Offset, Bytes) -> sendfile(CSocket, File, Offset, Bytes, [])

Alias of `ranch_transport:sendfile/5`.

=== sendfile(CSocket, File, Offset, Bytes, SfOpts) -> {ok, SentBytes} | {error, atom()}

CSocket = any():: Socket for this connection.
File = file:filename_all() | file:fd():: Filename or file descriptor for the file to be sent.
Offset = non_neg_integer():: Begin sending at this position in the file.
Bytes = non_neg_integer():: Send this many bytes.
SentBytes = non_neg_integer():: This many bytes were sent.
SfOpts = sendfile_opts():: Sendfile options.

Send data from a file to the given socket.

The file may be sent full or in parts, and may be specified
by its filename or by an already open file descriptor.

Transports that manipulate TCP directly may use the
`file:sendfile/{2,4,5}` function, which calls the sendfile
syscall where applicable (on Linux, for example). Other
transports can use the `sendfile/6` function exported from
this module.

=== setopts(CSocket, SockOpts) -> ok | {error, atom()}

CSocket = any():: Socket for this connection.
SockOpts = any():: Socket options.

Change options for the given socket.

This is mainly useful for switching to active or passive mode
or to set protocol-specific options.

=== getopts(CSocket, SockOpts) -> {ok, SockOptValues} | {error, atom()}

CSocket = any():: Socket for this connection.
SockOpts = [atom()]:: Socket option names.
SockOptValues = list():: Socket options.

Get options for the given socket.

=== getstat(CSocket) -> {ok, SockStatValues} | {error, atom()}

CSocket = any():: Socket for this connection.
SockStatValues = list():: Socket statistics.

Get statistics for the given socket.

=== getstat(CSocket, SockStats) -> {ok, SockStatValues} | {error, atom()}

CSocket = any():: Socket for this connection.
SockStats = [atom()]:: Socket statistic names.
SockStatValues = list():: Socket statistics.

Get statistics for the given socket.

=== shutdown(CSocket, How) -> ok | {error, atom()}

CSocket = any():: Socket for this connection.
How = read | write | read_write:: Which side(s) of the socket to close.

Immediately close the socket in one or two directions.

=== sockname(Socket) -> {ok, {IP, Port}} | {error, atom()}

Socket = any():: Socket opened with listen/1 or accept/2.
IP = inet:ip_address():: IP of the local endpoint.
Port = inet:port_number():: Port of the local endpoint.

Return the IP and port of the local endpoint.

== Exports

=== sendfile(Transport, CSocket, File, Offset, Bytes, SfOpts) -> {ok, SentBytes} | {error, atom()}

Transport = module():: Transport module for this socket.
CSocket = any():: Socket for this connection.
File = file:filename_all() | file:fd():: Filename or file descriptor for the file to be sent.
Offset = non_neg_integer():: Begin sending at this position in the file.
Bytes = non_neg_integer():: Send this many bytes.
SentBytes = non_neg_integer():: This many bytes were sent.
SfOpts = sendfile_opts():: Sendfile options.

Send data from a file to the given socket.

This function emulates the function `file:sendfile/{2,4,5}`
and may be used when transports are not manipulating TCP
directly.