cowboy_acceptor.erl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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_acceptor).
  15. -export([start_link/5]). %% API.
  16. -export([acceptor/5]). %% Internal.
  17. -include("include/types.hrl").
  18. %% API.
  19. -spec start_link(LSocket::socket(), Transport::module(),
  20. Protocol::module(), Opts::term(), ReqsSup::pid()) -> {ok, Pid::pid()}.
  21. start_link(LSocket, Transport, Protocol, Opts, ReqsSup) ->
  22. Pid = spawn_link(?MODULE, acceptor,
  23. [LSocket, Transport, Protocol, Opts, ReqsSup]),
  24. {ok, Pid}.
  25. %% Internal.
  26. -spec acceptor(LSocket::socket(), Transport::module(),
  27. Protocol::module(), Opts::term(), ReqsSup::pid()) -> no_return().
  28. acceptor(LSocket, Transport, Protocol, Opts, ReqsSup) ->
  29. case Transport:accept(LSocket) of
  30. {ok, CSocket} ->
  31. {ok, Pid} = supervisor:start_child(ReqsSup,
  32. [CSocket, Transport, Protocol, Opts]),
  33. Transport:controlling_process(CSocket, Pid);
  34. {error, _Reason} ->
  35. %% @todo Probably do something here. If the socket was closed,
  36. %% we may want to try and listen again on the port?
  37. ignore
  38. end,
  39. ?MODULE:acceptor(LSocket, Transport, Protocol, Opts, ReqsSup).