cowboy_protocol.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %% Copyright (c) 2011, Michiel Hakvoort <michiel@hakvoort.it>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. %% @doc Cowboy protocol.
  16. %%
  17. %% A Cowboy protocol must implement one callback: <em>start_link/4</em>.
  18. %%
  19. %% <em>start_link/4</em> is meant for the initialization of the
  20. %% protocol process.
  21. %% It receives the pid to the listener's gen_server, the client socket,
  22. %% the module name of the chosen transport and the options defined when
  23. %% starting the listener. The <em>start_link/4</em> function must follow
  24. %% the supervisor start function specification.
  25. %%
  26. %% After initializing your protocol, it is recommended to wait to
  27. %% receive a message containing the atom 'shoot', as it will ensure
  28. %% Cowboy has been able to fully initialize the socket.
  29. %% Anything you do past this point is up to you!
  30. %%
  31. %% If you need to change some socket options, like enabling raw mode
  32. %% for example, you can call the <em>Transport:setopts/2</em> function.
  33. %% It is the protocol's responsability to manage the socket usage,
  34. %% there should be no need for an user to specify that kind of options
  35. %% while starting a listener.
  36. %%
  37. %% You should definitely look at the cowboy_http_protocol module for
  38. %% a great example of fast request handling if you need to.
  39. %% Otherwise it's probably safe to use <code>{active, once}</code> mode
  40. %% and handle everything as it comes.
  41. %%
  42. %% Note that while you technically can run a protocol handler directly
  43. %% as a gen_server or a gen_fsm, it's probably not a good idea,
  44. %% as the only call you'll ever receive from Cowboy is the
  45. %% <em>start_link/4</em> call. On the other hand, feel free to write
  46. %% a very basic protocol handler which then forwards requests to a
  47. %% gen_server or gen_fsm. By doing so however you must take care to
  48. %% supervise their processes as Cowboy only knows about the protocol
  49. %% handler itself.
  50. -module(cowboy_protocol).
  51. -export([behaviour_info/1]).
  52. %% @private
  53. -spec behaviour_info(_)
  54. -> undefined | [{start_link, 4}, ...].
  55. behaviour_info(callbacks) ->
  56. [{start_link, 4}];
  57. behaviour_info(_Other) ->
  58. undefined.