ranch_tcp.erl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. %% Copyright (c) 2011-2012, 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. %% @doc TCP transport API.
  15. %%
  16. %% Wrapper around <em>gen_tcp</em> implementing the Ranch transport API.
  17. %%
  18. %% @see gen_tcp
  19. -module(ranch_tcp).
  20. -behaviour(ranch_transport).
  21. -export([name/0]).
  22. -export([messages/0]).
  23. -export([listen/1]).
  24. -export([accept/2]).
  25. -export([connect/3]).
  26. -export([recv/3]).
  27. -export([send/2]).
  28. -export([sendfile/2]).
  29. -export([setopts/2]).
  30. -export([controlling_process/2]).
  31. -export([peername/1]).
  32. -export([sockname/1]).
  33. -export([close/1]).
  34. %% @doc Name of this transport, <em>tcp</em>.
  35. name() -> tcp.
  36. %% @doc Atoms used to identify messages in {active, once | true} mode.
  37. messages() -> {tcp, tcp_closed, tcp_error}.
  38. %% @doc Listen for connections on the given port number.
  39. %%
  40. %% Calling this function returns a listening socket that can then
  41. %% be passed to accept/2 to accept connections.
  42. %%
  43. %% The available options are:
  44. %% <dl>
  45. %% <dt>backlog</dt><dd>Maximum length of the pending connections queue.
  46. %% Defaults to 1024.</dd>
  47. %% <dt>ip</dt><dd>Interface to listen on. Listen on all interfaces
  48. %% by default.</dd>
  49. %% <dt>nodelay</dt><dd>Optional. Enable TCP_NODELAY. Enabled by default.</dd>
  50. %% <dt>port</dt><dd>TCP port number to open. Defaults to 0 (see below).</dd>
  51. %% </dl>
  52. %%
  53. %% You can listen to a random port by setting the port option to 0.
  54. %% It is then possible to retrieve this port number by calling
  55. %% sockname/1 on the listening socket. If you are using Ranch's
  56. %% listener API, then this port number can obtained through
  57. %% ranch:get_port/1 instead.
  58. %%
  59. %% @see gen_tcp:listen/2
  60. -spec listen([{backlog, non_neg_integer()} | {ip, inet:ip_address()}
  61. | {nodelay, boolean()} | {port, inet:port_number()}])
  62. -> {ok, inet:socket()} | {error, atom()}.
  63. listen(Opts) ->
  64. Opts2 = ranch:set_option_default(Opts, backlog, 1024),
  65. %% We set the port to 0 because it is given in the Opts directly.
  66. %% The port in the options takes precedence over the one in the
  67. %% first argument.
  68. gen_tcp:listen(0, ranch:filter_options(Opts2, [backlog, ip, nodelay, port],
  69. [binary, {active, false}, {packet, raw},
  70. {reuseaddr, true}, {nodelay, true}])).
  71. %% @doc Accept connections with the given listening socket.
  72. %% @see gen_tcp:accept/2
  73. -spec accept(inet:socket(), timeout())
  74. -> {ok, inet:socket()} | {error, closed | timeout | atom()}.
  75. accept(LSocket, Timeout) ->
  76. gen_tcp:accept(LSocket, Timeout).
  77. %% @private Experimental. Open a connection to the given host and port number.
  78. %% @see gen_tcp:connect/3
  79. %% @todo Probably filter Opts?
  80. -spec connect(inet:ip_address() | inet:hostname(),
  81. inet:port_number(), any())
  82. -> {ok, inet:socket()} | {error, atom()}.
  83. connect(Host, Port, Opts) when is_integer(Port) ->
  84. gen_tcp:connect(Host, Port,
  85. Opts ++ [binary, {active, false}, {packet, raw}]).
  86. %% @doc Receive data from a socket in passive mode.
  87. %% @see gen_tcp:recv/3
  88. -spec recv(inet:socket(), non_neg_integer(), timeout())
  89. -> {ok, any()} | {error, closed | atom()}.
  90. recv(Socket, Length, Timeout) ->
  91. gen_tcp:recv(Socket, Length, Timeout).
  92. %% @doc Send data on a socket.
  93. %% @see gen_tcp:send/2
  94. -spec send(inet:socket(), iodata()) -> ok | {error, atom()}.
  95. send(Socket, Packet) ->
  96. gen_tcp:send(Socket, Packet).
  97. %% @doc Send a file on a socket.
  98. %%
  99. %% This is the optimal way to send files using TCP. It uses a syscall
  100. %% which means there is no context switch between opening the file
  101. %% and writing its contents on the socket.
  102. %%
  103. %% @see file:sendfile/2
  104. -spec sendfile(inet:socket(), file:name())
  105. -> {ok, non_neg_integer()} | {error, atom()}.
  106. sendfile(Socket, Filename) ->
  107. file:sendfile(Filename, Socket).
  108. %% @doc Set options on the given socket.
  109. %% @see inet:setopts/2
  110. %% @todo Probably filter Opts?
  111. -spec setopts(inet:socket(), list()) -> ok | {error, atom()}.
  112. setopts(Socket, Opts) ->
  113. inet:setopts(Socket, Opts).
  114. %% @doc Give control of the socket to a new process.
  115. %%
  116. %% Must be called from the process currently controlling the socket,
  117. %% otherwise an {error, not_owner} tuple will be returned.
  118. %%
  119. %% @see gen_tcp:controlling_process/2
  120. -spec controlling_process(inet:socket(), pid())
  121. -> ok | {error, closed | not_owner | atom()}.
  122. controlling_process(Socket, Pid) ->
  123. gen_tcp:controlling_process(Socket, Pid).
  124. %% @doc Return the remote address and port of the connection.
  125. %% @see inet:peername/1
  126. -spec peername(inet:socket())
  127. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  128. peername(Socket) ->
  129. inet:peername(Socket).
  130. %% @doc Return the local address and port of the connection.
  131. %% @see inet:sockname/1
  132. -spec sockname(inet:socket())
  133. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  134. sockname(Socket) ->
  135. inet:sockname(Socket).
  136. %% @doc Close the given socket.
  137. %% @see gen_tcp:close/1
  138. -spec close(inet:socket()) -> ok.
  139. close(Socket) ->
  140. gen_tcp:close(Socket).