cowboy_tcp_transport.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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([listen/1, accept/1, recv/3, send/2, setopts/2,
  16. controlling_process/2, peername/1, close/1]).
  17. -include("include/types.hrl").
  18. -spec listen([{port, Port::port_number()}])
  19. -> {ok, LSocket::socket()} | {error, Reason::posix()}.
  20. listen(Opts) ->
  21. {port, Port} = lists:keyfind(port, 1, Opts),
  22. gen_tcp:listen(Port, [binary, {active, false},
  23. {packet, raw}, {reuseaddr, true}]).
  24. -spec accept(LSocket::socket())
  25. -> {ok, Socket::socket()} | {error, Reason::closed | timeout | posix()}.
  26. accept(LSocket) ->
  27. gen_tcp:accept(LSocket).
  28. -spec recv(Socket::socket(), Length::integer(), Timeout::timeout())
  29. -> {ok, Packet::term()} | {error, Reason::closed | posix()}.
  30. recv(Socket, Length, Timeout) ->
  31. gen_tcp:recv(Socket, Length, Timeout).
  32. -spec send(Socket::socket(), Packet::iolist())
  33. -> ok | {error, Reason::posix()}.
  34. send(Socket, Packet) ->
  35. gen_tcp:send(Socket, Packet).
  36. -spec setopts(Socket::socket(), Opts::list(term()))
  37. -> ok | {error, Reason::posix()}.
  38. setopts(Socket, Opts) ->
  39. inet:setopts(Socket, Opts).
  40. -spec controlling_process(Socket::socket(), Pid::pid())
  41. -> ok | {error, Reason::closed | not_owner | posix()}.
  42. controlling_process(Socket, Pid) ->
  43. gen_tcp:controlling_process(Socket, Pid).
  44. -spec peername(Socket::socket())
  45. -> {ok, {Address::ip_address(), Port::port_number()}} | {error, posix()}.
  46. peername(Socket) ->
  47. inet:peername(Socket).
  48. -spec close(Socket::socket()) -> ok | {error, Reason::posix}.
  49. close(Socket) ->
  50. gen_tcp:close(Socket).