cowboy_acceptor.erl 1.7 KB

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