ranch_tcp.erl 5.0 KB

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