ranch_tcp.erl 5.6 KB

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