cowboy_tcp_transport.erl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Cowboy transport API.
  17. %%
  18. %% @see gen_tcp
  19. -module(cowboy_tcp_transport).
  20. -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
  21. controlling_process/2, peername/1, close/1, sockname/1]).
  22. -export([connect/3]).
  23. %% @doc Name of this transport API, <em>tcp</em>.
  24. -spec name() -> tcp.
  25. name() -> tcp.
  26. %% @doc Atoms used in the process messages sent by this API.
  27. %%
  28. %% They identify incoming data, closed connection and errors when receiving
  29. %% data in active mode.
  30. -spec messages() -> {tcp, tcp_closed, tcp_error}.
  31. messages() -> {tcp, tcp_closed, tcp_error}.
  32. %% @private
  33. connect(Host, Port, Opts) when is_list(Host), is_integer(Port) ->
  34. gen_tcp:connect(Host, Port,
  35. Opts ++ [binary, {active, false}, {packet, raw}]).
  36. %% @doc Setup a socket to listen on the given port on the local host.
  37. %%
  38. %% The available options are:
  39. %% <dl>
  40. %% <dt>port</dt><dd>Mandatory. TCP port number to open.</dd>
  41. %% <dt>backlog</dt><dd>Maximum length of the pending connections queue.
  42. %% Defaults to 1024.</dd>
  43. %% <dt>ip</dt><dd>Interface to listen on. Listen on all interfaces
  44. %% by default.</dd>
  45. %% </dl>
  46. %%
  47. %% @see gen_tcp:listen/2
  48. -spec listen([{port, inet:port_number()} | {ip, inet:ip_address()}])
  49. -> {ok, inet:socket()} | {error, atom()}.
  50. listen(Opts) ->
  51. {port, Port} = lists:keyfind(port, 1, Opts),
  52. Backlog = proplists:get_value(backlog, Opts, 1024),
  53. ListenOpts0 = [binary, {active, false},
  54. {backlog, Backlog}, {packet, raw}, {reuseaddr, true}],
  55. ListenOpts =
  56. case lists:keyfind(ip, 1, Opts) of
  57. false -> ListenOpts0;
  58. Ip -> [Ip|ListenOpts0]
  59. end,
  60. gen_tcp:listen(Port, ListenOpts).
  61. %% @doc Accept an incoming connection on a listen socket.
  62. %% @see gen_tcp:accept/2
  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. %% @doc Receive a packet from a socket in passive mode.
  68. %% @see gen_tcp:recv/3
  69. -spec recv(inet:socket(), non_neg_integer(), timeout())
  70. -> {ok, any()} | {error, closed | atom()}.
  71. recv(Socket, Length, Timeout) ->
  72. gen_tcp:recv(Socket, Length, Timeout).
  73. %% @doc Send a packet on a socket.
  74. %% @see gen_tcp:send/2
  75. -spec send(inet:socket(), iolist()) -> ok | {error, atom()}.
  76. send(Socket, Packet) ->
  77. gen_tcp:send(Socket, Packet).
  78. %% @doc Set one or more options for a socket.
  79. %% @see inet:setopts/2
  80. -spec setopts(inet:socket(), list()) -> ok | {error, atom()}.
  81. setopts(Socket, Opts) ->
  82. inet:setopts(Socket, Opts).
  83. %% @doc Assign a new controlling process <em>Pid</em> to <em>Socket</em>.
  84. %% @see gen_tcp:controlling_process/2
  85. -spec controlling_process(inet:socket(), pid())
  86. -> ok | {error, closed | not_owner | atom()}.
  87. controlling_process(Socket, Pid) ->
  88. gen_tcp:controlling_process(Socket, Pid).
  89. %% @doc Return the address and port for the other end of a connection.
  90. %% @see inet:peername/1
  91. -spec peername(inet:socket())
  92. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  93. peername(Socket) ->
  94. inet:peername(Socket).
  95. %% @doc Close a TCP socket.
  96. %% @see gen_tcp:close/1
  97. -spec close(inet:socket()) -> ok.
  98. close(Socket) ->
  99. gen_tcp:close(Socket).
  100. %% @doc Get the local address and port of a socket
  101. %% @see inet:sockname/1
  102. -spec sockname(inet:socket())
  103. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  104. sockname(Socket) ->
  105. inet:sockname(Socket).