ranch_tcp.erl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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([connect/3]).
  24. -export([connect/4]).
  25. -export([recv/3]).
  26. -export([send/2]).
  27. -export([sendfile/2]).
  28. -export([sendfile/4]).
  29. -export([sendfile/5]).
  30. -export([setopts/2]).
  31. -export([getopts/2]).
  32. -export([getstat/1]).
  33. -export([getstat/2]).
  34. -export([controlling_process/2]).
  35. -export([peername/1]).
  36. -export([sockname/1]).
  37. -export([shutdown/2]).
  38. -export([close/1]).
  39. -type opt() :: {backlog, non_neg_integer()}
  40. | {buffer, non_neg_integer()}
  41. | {delay_send, boolean()}
  42. | {dontroute, boolean()}
  43. | {exit_on_close, boolean()}
  44. | {fd, non_neg_integer()}
  45. | {high_msgq_watermark, non_neg_integer()}
  46. | {high_watermark, non_neg_integer()}
  47. | inet
  48. | inet6
  49. | {ip, inet:ip_address()}
  50. | {ipv6_v6only, boolean()}
  51. | {keepalive, boolean()}
  52. | {linger, {boolean(), non_neg_integer()}}
  53. | {low_msgq_watermark, non_neg_integer()}
  54. | {low_watermark, non_neg_integer()}
  55. | {nodelay, boolean()}
  56. | {port, inet:port_number()}
  57. | {priority, integer()}
  58. | {raw, non_neg_integer(), non_neg_integer(), binary()}
  59. | {recbuf, non_neg_integer()}
  60. | {send_timeout, timeout()}
  61. | {send_timeout_close, boolean()}
  62. | {sndbuf, non_neg_integer()}
  63. | {tos, integer()}.
  64. -export_type([opt/0]).
  65. -type opts() :: [opt()].
  66. -export_type([opts/0]).
  67. name() -> tcp.
  68. -spec secure() -> boolean().
  69. secure() ->
  70. false.
  71. messages() -> {tcp, tcp_closed, tcp_error}.
  72. -spec listen(opts()) -> {ok, inet:socket()} | {error, atom()}.
  73. listen(Opts) ->
  74. Opts2 = ranch:set_option_default(Opts, backlog, 1024),
  75. Opts3 = ranch:set_option_default(Opts2, nodelay, true),
  76. Opts4 = ranch:set_option_default(Opts3, send_timeout, 30000),
  77. Opts5 = ranch:set_option_default(Opts4, send_timeout_close, true),
  78. %% We set the port to 0 because it is given in the Opts directly.
  79. %% The port in the options takes precedence over the one in the
  80. %% first argument.
  81. gen_tcp:listen(0, ranch:filter_options(Opts5, disallowed_listen_options(),
  82. [binary, {active, false}, {packet, raw}, {reuseaddr, true}])).
  83. %% 'binary' and 'list' are disallowed but they are handled
  84. %% specifically as they do not have 2-tuple equivalents.
  85. disallowed_listen_options() ->
  86. [active, header, mode, packet, packet_size, line_delimiter, reuseaddr].
  87. -spec accept(inet:socket(), timeout())
  88. -> {ok, inet:socket()} | {error, closed | timeout | atom()}.
  89. accept(LSocket, Timeout) ->
  90. gen_tcp:accept(LSocket, Timeout).
  91. -spec accept_ack(inet:socket(), timeout()) -> ok.
  92. accept_ack(_, _) ->
  93. ok.
  94. %% @todo Probably filter Opts?
  95. -spec connect(inet:ip_address() | inet:hostname(),
  96. inet:port_number(), any())
  97. -> {ok, inet:socket()} | {error, atom()}.
  98. connect(Host, Port, Opts) when is_integer(Port) ->
  99. gen_tcp:connect(Host, Port,
  100. Opts ++ [binary, {active, false}, {packet, raw}]).
  101. %% @todo Probably filter Opts?
  102. -spec connect(inet:ip_address() | inet:hostname(),
  103. inet:port_number(), any(), timeout())
  104. -> {ok, inet:socket()} | {error, atom()}.
  105. connect(Host, Port, Opts, Timeout) when is_integer(Port) ->
  106. gen_tcp:connect(Host, Port,
  107. Opts ++ [binary, {active, false}, {packet, raw}],
  108. Timeout).
  109. -spec recv(inet:socket(), non_neg_integer(), timeout())
  110. -> {ok, any()} | {error, closed | atom()}.
  111. recv(Socket, Length, Timeout) ->
  112. gen_tcp:recv(Socket, Length, Timeout).
  113. -spec send(inet:socket(), iodata()) -> ok | {error, atom()}.
  114. send(Socket, Packet) ->
  115. gen_tcp:send(Socket, Packet).
  116. -spec sendfile(inet:socket(), file:name_all() | file:fd())
  117. -> {ok, non_neg_integer()} | {error, atom()}.
  118. sendfile(Socket, Filename) ->
  119. sendfile(Socket, Filename, 0, 0, []).
  120. -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(),
  121. non_neg_integer())
  122. -> {ok, non_neg_integer()} | {error, atom()}.
  123. sendfile(Socket, File, Offset, Bytes) ->
  124. sendfile(Socket, File, Offset, Bytes, []).
  125. -spec sendfile(inet:socket(), file:name_all() | file:fd(), non_neg_integer(),
  126. non_neg_integer(), [{chunk_size, non_neg_integer()}])
  127. -> {ok, non_neg_integer()} | {error, atom()}.
  128. sendfile(Socket, Filename, Offset, Bytes, Opts)
  129. when is_list(Filename) orelse is_atom(Filename)
  130. orelse is_binary(Filename) ->
  131. case file:open(Filename, [read, raw, binary]) of
  132. {ok, RawFile} ->
  133. try sendfile(Socket, RawFile, Offset, Bytes, Opts) of
  134. Result -> Result
  135. after
  136. ok = file:close(RawFile)
  137. end;
  138. {error, _} = Error ->
  139. Error
  140. end;
  141. sendfile(Socket, RawFile, Offset, Bytes, Opts) ->
  142. Opts2 = case Opts of
  143. [] -> [{chunk_size, 16#1FFF}];
  144. _ -> Opts
  145. end,
  146. try file:sendfile(RawFile, Socket, Offset, Bytes, Opts2) of
  147. Result -> Result
  148. catch
  149. error:{badmatch, {error, enotconn}} ->
  150. %% file:sendfile/5 might fail by throwing a
  151. %% {badmatch, {error, enotconn}}. This is because its
  152. %% implementation fails with a badmatch in
  153. %% prim_file:sendfile/10 if the socket is not connected.
  154. {error, closed}
  155. end.
  156. %% @todo Probably filter Opts?
  157. -spec setopts(inet:socket(), list()) -> ok | {error, atom()}.
  158. setopts(Socket, Opts) ->
  159. inet:setopts(Socket, Opts).
  160. -spec getopts(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}.
  161. getopts(Socket, Opts) ->
  162. inet:getopts(Socket, Opts).
  163. -spec getstat(inet:socket()) -> {ok, list()} | {error, atom()}.
  164. getstat(Socket) ->
  165. inet:getstat(Socket).
  166. -spec getstat(inet:socket(), [atom()]) -> {ok, list()} | {error, atom()}.
  167. getstat(Socket, OptionNames) ->
  168. inet:getstat(Socket, OptionNames).
  169. -spec controlling_process(inet:socket(), pid())
  170. -> ok | {error, closed | not_owner | atom()}.
  171. controlling_process(Socket, Pid) ->
  172. gen_tcp:controlling_process(Socket, Pid).
  173. -spec peername(inet:socket())
  174. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  175. peername(Socket) ->
  176. inet:peername(Socket).
  177. -spec sockname(inet:socket())
  178. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  179. sockname(Socket) ->
  180. inet:sockname(Socket).
  181. -spec shutdown(inet:socket(), read | write | read_write)
  182. -> ok | {error, atom()}.
  183. shutdown(Socket, How) ->
  184. gen_tcp:shutdown(Socket, How).
  185. -spec close(inet:socket()) -> ok.
  186. close(Socket) ->
  187. gen_tcp:close(Socket).