ranch_tcp.erl 6.4 KB

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