cowboy_tcp_transport.erl 3.7 KB

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