ranch_tcp.erl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. %% Copyright (c) 2011-2018, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(ranch_tcp).
  15. -behaviour(ranch_transport).
  16. -export([name/0]).
  17. -export([secure/0]).
  18. -export([messages/0]).
  19. -export([listen/1]).
  20. -export([disallowed_listen_options/0]).
  21. -export([accept/2]).
  22. -export([accept_ack/2]).
  23. -export([handshake/3]).
  24. -export([connect/3]).
  25. -export([connect/4]).
  26. -export([recv/3]).
  27. -export([send/2]).
  28. -export([sendfile/2]).
  29. -export([sendfile/4]).
  30. -export([sendfile/5]).
  31. -export([setopts/2]).
  32. -export([getopts/2]).
  33. -export([getstat/1]).
  34. -export([getstat/2]).
  35. -export([controlling_process/2]).
  36. -export([peername/1]).
  37. -export([sockname/1]).
  38. -export([shutdown/2]).
  39. -export([close/1]).
  40. -type opt() :: {backlog, non_neg_integer()}
  41. | {buffer, non_neg_integer()}
  42. | {delay_send, boolean()}
  43. | {dontroute, boolean()}
  44. | {exit_on_close, boolean()}
  45. | {fd, non_neg_integer()}
  46. | {high_msgq_watermark, non_neg_integer()}
  47. | {high_watermark, non_neg_integer()}
  48. | inet
  49. | inet6
  50. | {ip, inet:ip_address()}
  51. | {ipv6_v6only, boolean()}
  52. | {keepalive, boolean()}
  53. | {linger, {boolean(), non_neg_integer()}}
  54. | {low_msgq_watermark, non_neg_integer()}
  55. | {low_watermark, non_neg_integer()}
  56. | {nodelay, boolean()}
  57. | {port, inet:port_number()}
  58. | {priority, integer()}
  59. | {raw, non_neg_integer(), non_neg_integer(), binary()}
  60. | {recbuf, non_neg_integer()}
  61. | {send_timeout, timeout()}
  62. | {send_timeout_close, boolean()}
  63. | {sndbuf, non_neg_integer()}
  64. | {tos, integer()}.
  65. -export_type([opt/0]).
  66. -type opts() :: [opt()].
  67. -export_type([opts/0]).
  68. name() -> tcp.
  69. -spec secure() -> boolean().
  70. secure() ->
  71. false.
  72. messages() -> {tcp, tcp_closed, tcp_error}.
  73. -spec listen(opts()) -> {ok, inet:socket()} | {error, atom()}.
  74. listen(Opts) ->
  75. Opts2 = ranch:set_option_default(Opts, backlog, 1024),
  76. Opts3 = ranch:set_option_default(Opts2, nodelay, true),
  77. Opts4 = ranch:set_option_default(Opts3, send_timeout, 30000),
  78. Opts5 = ranch:set_option_default(Opts4, send_timeout_close, true),
  79. %% We set the port to 0 because it is given in the Opts directly.
  80. %% The port in the options takes precedence over the one in the
  81. %% first argument.
  82. gen_tcp:listen(0, ranch:filter_options(Opts5, disallowed_listen_options(),
  83. [binary, {active, false}, {packet, raw}, {reuseaddr, true}])).
  84. %% 'binary' and 'list' are disallowed but they are handled
  85. %% specifically as they do not have 2-tuple equivalents.
  86. disallowed_listen_options() ->
  87. [active, header, mode, packet, packet_size, line_delimiter, reuseaddr].
  88. -spec accept(inet:socket(), timeout())
  89. -> {ok, inet:socket()} | {error, closed | timeout | atom()}.
  90. accept(LSocket, Timeout) ->
  91. gen_tcp:accept(LSocket, Timeout).
  92. -spec accept_ack(inet:socket(), timeout()) -> ok.
  93. accept_ack(CSocket, Timeout) ->
  94. {ok, _} = handshake(CSocket, [], Timeout),
  95. ok.
  96. -spec handshake(inet:socket(), opts(), timeout()) -> {ok, inet:socket()}.
  97. handshake(CSocket, _, _) ->
  98. {ok, CSocket}.
  99. %% @todo Probably filter Opts?
  100. -spec connect(inet:ip_address() | inet:hostname(),
  101. inet:port_number(), any())
  102. -> {ok, inet:socket()} | {error, atom()}.
  103. connect(Host, Port, Opts) when is_integer(Port) ->
  104. gen_tcp:connect(Host, Port,
  105. Opts ++ [binary, {active, false}, {packet, raw}]).
  106. %% @todo Probably filter Opts?
  107. -spec connect(inet:ip_address() | inet:hostname(),
  108. inet:port_number(), any(), timeout())
  109. -> {ok, inet:socket()} | {error, atom()}.
  110. connect(Host, Port, Opts, Timeout) when is_integer(Port) ->
  111. gen_tcp:connect(Host, Port,
  112. Opts ++ [binary, {active, false}, {packet, raw}],
  113. Timeout).
  114. -spec recv(inet:socket(), non_neg_integer(), timeout())
  115. -> {ok, any()} | {error, closed | atom()}.
  116. recv(Socket, Length, Timeout) ->
  117. gen_tcp:recv(Socket, Length, Timeout).
  118. -spec send(inet:socket(), iodata()) -> ok | {error, atom()}.
  119. send(Socket, Packet) ->
  120. gen_tcp:send(Socket, Packet).
  121. -spec sendfile(inet:socket(), file:name_all() | file:fd())
  122. -> {ok, non_neg_integer()} | {error, atom()}.
  123. sendfile(Socket, Filename) ->
  124. sendfile(Socket, Filename, 0, 0, []).
  125. -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(),
  126. non_neg_integer())
  127. -> {ok, non_neg_integer()} | {error, atom()}.
  128. sendfile(Socket, File, Offset, Bytes) ->
  129. sendfile(Socket, File, Offset, Bytes, []).
  130. -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(),
  131. non_neg_integer(), [{chunk_size, non_neg_integer()}])
  132. -> {ok, non_neg_integer()} | {error, atom()}.
  133. sendfile(Socket, Filename, Offset, Bytes, Opts)
  134. when is_list(Filename) orelse is_atom(Filename)
  135. orelse is_binary(Filename) ->
  136. case file:open(Filename, [read, raw, binary]) of
  137. {ok, RawFile} ->
  138. try sendfile(Socket, RawFile, Offset, Bytes, Opts) of
  139. Result -> Result
  140. after
  141. ok = file:close(RawFile)
  142. end;
  143. {error, _} = Error ->
  144. Error
  145. end;
  146. sendfile(Socket, RawFile, Offset, Bytes, Opts) ->
  147. Opts2 = case Opts of
  148. [] -> [{chunk_size, 16#1FFF}];
  149. _ -> Opts
  150. end,
  151. try file:sendfile(RawFile, Socket, Offset, Bytes, Opts2) of
  152. Result -> Result
  153. catch
  154. error:{badmatch, {error, enotconn}} ->
  155. %% file:sendfile/5 might fail by throwing a
  156. %% {badmatch, {error, enotconn}}. This is because its
  157. %% implementation fails with a badmatch in
  158. %% prim_file:sendfile/10 if the socket is not connected.
  159. {error, closed}
  160. end.
  161. %% @todo Probably filter Opts?
  162. -spec setopts(inet:socket(), list()) -> ok | {error, atom()}.
  163. setopts(Socket, Opts) ->
  164. inet:setopts(Socket, Opts).
  165. -spec getopts(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}.
  166. getopts(Socket, Opts) ->
  167. inet:getopts(Socket, Opts).
  168. -spec getstat(inet:socket()) -> {ok, list()} | {error, atom()}.
  169. getstat(Socket) ->
  170. inet:getstat(Socket).
  171. -spec getstat(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}.
  172. getstat(Socket, OptionNames) ->
  173. inet:getstat(Socket, OptionNames).
  174. -spec controlling_process(inet:socket(), pid())
  175. -> ok | {error, closed | not_owner | atom()}.
  176. controlling_process(Socket, Pid) ->
  177. gen_tcp:controlling_process(Socket, Pid).
  178. -spec peername(inet:socket())
  179. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  180. peername(Socket) ->
  181. inet:peername(Socket).
  182. -spec sockname(inet:socket())
  183. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  184. sockname(Socket) ->
  185. inet:sockname(Socket).
  186. -spec shutdown(inet:socket(), read | write | read_write)
  187. -> ok | {error, atom()}.
  188. shutdown(Socket, How) ->
  189. gen_tcp:shutdown(Socket, How).
  190. -spec close(inet:socket()) -> ok.
  191. close(Socket) ->
  192. gen_tcp:close(Socket).