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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
= ranch_transport(3)
== Name
ranch_transport - Transport modules
== Description
The module `ranch_transport` defines the interface used
by Ranch transports.
== Callbacks
Ranch transports implement the following interface:
=== accept
[source,erlang]
----
accept(LSocket :: socket(), Timeout :: timeout())
-> {ok, Socket :: socket()}
| {error, closed | timeout | atom()}
----
Use the listening socket returned by `listen/1`
to accept a new connection. The timeout is specified
in milliseconds.
=== close
[source,erlang]
----
close(Socket :: socket()) -> ok
----
Close the socket.
=== controlling_process
[source,erlang]
----
controlling_process(Socket :: socket(), Pid :: pid())
-> ok | {error, closed | not_owner | atom()}
----
Assign a new controlling process to the socket. The
controlling process is the process that is linked to
and receives messages from the socket.
=== getopts
[source,erlang]
----
getopts(Socket :: socket(), SockOpts :: [atom()])
-> {ok, any()} | {error, atom()}
----
Get one or more options for the socket.
=== getstat
[source,erlang]
----
getstat(Socket :: socket())
-> {ok, SockStatValues :: any()} | {error, atom()}
----
Get all statistics for the socket.
[source,erlang]
----
getstat(Socket :: socket(), SockStats :: [atom()])
-> {ok, SockStatValues :: any()} | {error, atom()}
----
Get one or more statistic options for the socket.
=== handshake
[source,erlang]
----
handshake(Socket0 :: socket(),
SockOpts :: any(),
Timeout :: timeout())
-> {ok, Socket}
----
Perform the transport-level handshake.
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.
This function may also be used to upgrade a connection
from a transport to another depending on the capabilities
of the transports. For example a `ranch_tcp` socket may
be upgraded to a `ranch_ssl` one using this function.
=== listen
[source,erlang]
----
listen(SockOpts :: any())
-> {ok, LSocket :: socket()} | {error, atom()}
----
Create a socket that listens on the given port.
The port may not be specified or may be set to 0, which
means a random available port number will be chosen.
=== messages
[source,erlang]
----
messages()
-> {OK :: atom(),
Closed :: atom(),
Error :: atom()}
----
Return the tuple keys for the messages sent by the socket.
=== name
[source,erlang]
----
name() -> Name :: atom()
----
Return the name of the transport.
=== peername
[source,erlang]
----
peername(Socket :: socket())
-> {ok, {inet:ip_address(), inet:port_number()}}
| {error, atom()}.
----
Return the address and port number for the other end of
the connection.
=== recv
[source,erlang]
----
recv(Socket :: socket(),
Length :: non_neg_integer(),
Timeout :: timeout())
-> {ok, Packet :: any()}
| {error, closed | timeout | atom()}
----
Receive a packet from the socket in passive mode.
Attempting to receive data from a socket that is
in active mode will return an error.
A length of 0 will return the data available on
the socket as soon as possible, regardless of length.
While it is possible to use the timeout value `infinity`,
it is highly discouraged as it 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.
=== secure
[source,erlang]
----
secure() -> boolean()
----
Return whether the transport can be used for secure connections.
=== send
[source,erlang]
----
send(Socket :: socket(), Packet :: iodata())
-> ok | {error, atom()}
----
Send a packet on the socket.
=== sendfile
[source,erlang]
----
sendfile(Socket, File)
-> sendfile(Socket, File, 0, 0, [])
sendfile(Socket, File, Offset, Bytes)
-> sendfile(Socket, File, Offset, Bytes, [])
sendfile(Socket :: socket(),
File :: file:name_all() | file:fd(),
Offset :: non_neg_integer(),
Bytes :: non_neg_integer(),
Opts :: sendfile_opts())
-> {ok, SentBytes :: non_neg_integer()} | {error, atom()}
----
Send a file on the 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
[source,erlang]
----
setopts(Socket :: socket(), SockOpts :: any())
-> ok | {error, atom()}
----
Set one or more options for the socket.
=== shutdown
[source,erlang]
----
shutdown(Socket :: socket(),
How :: read | write | read_write)
-> ok | {error, atom()}
----
Close the socket for reading and/or writing.
=== sockname
[source,erlang]
----
sockname(Socket :: socket())
-> {ok, {inet:ip_address(), inet:port_number()}}
| {error, atom()}.
----
Return the address and port number for the local end
of the connection.
== Exports
The following function can be used when implementing
transport modules:
* link:man:ranch_transport:sendfile(3)[ranch_transport:sendfile(3)] - Send a file on the socket
== Types
=== sendfile_opts()
[source,erlang]
----
sendfile_opts() :: [{chunk_size, non_neg_integer()}]
----
Options accepted by the sendfile function and callbacks:
chunk_size (8191)::
The chunk size, in bytes.
=== socket()
[source,erlang]
----
socket() :: any()
----
The socket.
The exact type will vary depending on the transport module.
== Changelog
* *1.6*: The `socket()` type was added for documentation purposes.
* *1.6*: The type of the sendfile filename was extended.
== See also
link:man:ranch(7)[ranch(7)],
link:man:ranch_tcp(3)[ranch_tcp(3)],
link:man:ranch_ssl(3)[ranch_ssl(3)]
|