ranch_erlang_transport.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. %% Copyright (c) 2020-2021, Loïc Hoguin <essen@ninenines.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(ranch_erlang_transport).
  15. -behaviour(ranch_transport).
  16. -export([name/0]).
  17. -export([secure/0]).
  18. -export([messages/0]).
  19. -export([listen/1]).
  20. -export([accept/2]).
  21. -export([handshake/2]).
  22. -export([handshake/3]).
  23. -export([handshake_continue/2]).
  24. -export([handshake_continue/3]).
  25. -export([handshake_cancel/1]).
  26. -export([connect/3]).
  27. -export([connect/4]).
  28. -export([recv/3]).
  29. -export([recv_proxy_header/2]).
  30. -export([send/2]).
  31. -export([sendfile/2]).
  32. -export([sendfile/4]).
  33. -export([sendfile/5]).
  34. -export([setopts/2]).
  35. -export([getopts/2]).
  36. -export([getstat/1]).
  37. -export([getstat/2]).
  38. -export([controlling_process/2]).
  39. -export([peername/1]).
  40. -export([sockname/1]).
  41. -export([shutdown/2]).
  42. -export([close/1]).
  43. -export([cleanup/1]).
  44. -type opts() :: [].
  45. -export_type([opts/0]).
  46. -spec name() -> erlang.
  47. name() -> erlang.
  48. -spec secure() -> boolean().
  49. secure() ->
  50. false.
  51. -spec messages() -> {erlang, erlang_closed, erlang_error, erlang_passive}.
  52. messages() -> {erlang, erlang_closed, erlang_error, erlang_passive}.
  53. -spec listen(ranch:transport_opts(opts())) -> {ok, reference()}.
  54. listen(_TransOpts) ->
  55. {ok, make_ref()}.
  56. -spec accept(reference(), timeout()) -> no_return(). % {ok, reference()}.
  57. accept(_LSocket, _Timeout) ->
  58. receive after infinity -> {ok, make_ref()} end.
  59. -spec handshake(reference(), timeout()) -> {ok, reference()}.
  60. handshake(CSocket, Timeout) ->
  61. handshake(CSocket, [], Timeout).
  62. -spec handshake(reference(), opts(), timeout()) -> {ok, reference()}.
  63. handshake(CSocket, _, _) ->
  64. {ok, CSocket}.
  65. -spec handshake_continue(reference(), timeout()) -> no_return().
  66. handshake_continue(CSocket, Timeout) ->
  67. handshake_continue(CSocket, [], Timeout).
  68. -spec handshake_continue(reference(), opts(), timeout()) -> no_return().
  69. handshake_continue(_, _, _) ->
  70. error(not_supported).
  71. -spec handshake_cancel(reference()) -> no_return().
  72. handshake_cancel(_) ->
  73. error(not_supported).
  74. -spec connect(inet:ip_address() | inet:hostname(),
  75. inet:port_number(), any())
  76. -> {ok, reference()}.
  77. connect(_Host, Port, _Opts) when is_integer(Port) ->
  78. {ok, make_ref()}.
  79. -spec connect(inet:ip_address() | inet:hostname(),
  80. inet:port_number(), any(), timeout())
  81. -> {ok, reference()}.
  82. connect(_Host, Port, _Opts, _Timeout) when is_integer(Port) ->
  83. {ok, make_ref()}.
  84. -spec recv(reference(), non_neg_integer(), timeout())
  85. -> {ok, any()} | {error, closed | atom()}.
  86. recv(_Socket, _Length, _Timeout) ->
  87. {ok, <<>>}.
  88. -spec recv_proxy_header(reference(), timeout()) -> no_return().
  89. recv_proxy_header(_Socket, _Timeout) ->
  90. error(not_supported).
  91. -spec send(reference(), iodata()) -> ok | {error, atom()}.
  92. send(_Socket, _Packet) ->
  93. ok.
  94. -spec sendfile(reference(), file:name_all() | file:fd())
  95. -> no_return().
  96. sendfile(Socket, Filename) ->
  97. sendfile(Socket, Filename, 0, 0, []).
  98. -spec sendfile(reference(), file:name_all() | file:fd(), non_neg_integer(),
  99. non_neg_integer())
  100. -> no_return().
  101. sendfile(Socket, File, Offset, Bytes) ->
  102. sendfile(Socket, File, Offset, Bytes, []).
  103. -spec sendfile(reference(), file:name_all() | file:fd(), non_neg_integer(),
  104. non_neg_integer(), [{chunk_size, non_neg_integer()}])
  105. -> no_return().
  106. sendfile(_Socket, Filename, _Offset, _Bytes, _Opts)
  107. when is_list(Filename) orelse is_atom(Filename)
  108. orelse is_binary(Filename) ->
  109. error(not_supported).
  110. -spec setopts(reference(), list()) -> ok.
  111. setopts(_Socket, _Opts) ->
  112. ok.
  113. -spec getopts(reference(), [atom()]) -> {ok, list()} | {error, atom()}.
  114. getopts(_Socket, _Opts) ->
  115. {ok, []}.
  116. -spec getstat(reference()) -> {ok, list()} | {error, atom()}.
  117. getstat(_Socket) ->
  118. {ok, []}.
  119. -spec getstat(reference(), [atom()]) -> {ok, list()} | {error, atom()}.
  120. getstat(_Socket, _OptionNames) ->
  121. {ok, []}.
  122. -spec controlling_process(reference(), pid())
  123. -> ok | {error, closed | not_owner | atom()}.
  124. controlling_process(_Socket, _Pid) ->
  125. ok.
  126. -spec peername(reference())
  127. -> {ok, {inet:ip_address(), inet:port_number()} | {local, binary()}} | {error, atom()}.
  128. peername(_Socket) ->
  129. {ok, {{127, 0, 0, 1}, 12701}}.
  130. -spec sockname(reference())
  131. -> {ok, {inet:ip_address(), inet:port_number()} | {local, binary()}} | {error, atom()}.
  132. sockname(_Socket) ->
  133. {ok, {{127, 0, 0, 1}, 12710}}.
  134. -spec shutdown(reference(), read | write | read_write)
  135. -> ok | {error, atom()}.
  136. shutdown(_Socket, _How) ->
  137. ok.
  138. -spec close(reference()) -> ok.
  139. close(_Socket) ->
  140. ok.
  141. -spec cleanup(ranch:transport_opts(opts())) -> ok.
  142. cleanup(_) ->
  143. ok.