cowboy_tcp_transport.erl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. -module(cowboy_tcp_transport).
  15. -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
  16. controlling_process/2, peername/1, close/1]). %% API.
  17. -include_lib("kernel/include/inet.hrl").
  18. %% API.
  19. -spec name() -> tcp.
  20. name() -> tcp.
  21. -spec messages() -> {tcp, tcp_closed, tcp_error}.
  22. messages() -> {tcp, tcp_closed, tcp_error}.
  23. -spec listen([{port, Port::ip_port()}])
  24. -> {ok, LSocket::inet:socket()} | {error, Reason::atom()}.
  25. listen(Opts) ->
  26. {port, Port} = lists:keyfind(port, 1, Opts),
  27. Backlog = proplists:get_value(backlog, Opts, 128),
  28. gen_tcp:listen(Port, [binary, {active, false},
  29. {backlog, Backlog}, {packet, raw}, {reuseaddr, true}]).
  30. -spec accept(LSocket::inet:socket(), Timeout::timeout())
  31. -> {ok, Socket::inet:socket()}
  32. | {error, Reason::closed | timeout | atom()}.
  33. accept(LSocket, Timeout) ->
  34. gen_tcp:accept(LSocket, Timeout).
  35. -spec recv(Socket::inet:socket(), Length::integer(), Timeout::timeout())
  36. -> {ok, Packet::term()} | {error, Reason::closed | atom()}.
  37. recv(Socket, Length, Timeout) ->
  38. gen_tcp:recv(Socket, Length, Timeout).
  39. -spec send(Socket::inet:socket(), Packet::iolist())
  40. -> ok | {error, Reason::atom()}.
  41. send(Socket, Packet) ->
  42. gen_tcp:send(Socket, Packet).
  43. -spec setopts(Socket::inet:socket(), Opts::list(term()))
  44. -> ok | {error, Reason::atom()}.
  45. setopts(Socket, Opts) ->
  46. inet:setopts(Socket, Opts).
  47. -spec controlling_process(Socket::inet:socket(), Pid::pid())
  48. -> ok | {error, Reason::closed | not_owner | atom()}.
  49. controlling_process(Socket, Pid) ->
  50. gen_tcp:controlling_process(Socket, Pid).
  51. -spec peername(Socket::inet:socket())
  52. -> {ok, {Address::ip_address(), Port::ip_port()}} | {error, atom()}.
  53. peername(Socket) ->
  54. inet:peername(Socket).
  55. -spec close(Socket::inet:socket()) -> ok.
  56. close(Socket) ->
  57. gen_tcp:close(Socket).