Skip to content

Commit

Permalink
Pix bosh given port to use the listerer's one
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Mar 7, 2023
1 parent 7f70812 commit 1ffe149
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
34 changes: 17 additions & 17 deletions src/mod_bosh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
-type headers_list() :: [{binary(), binary()}].

%% Request State
-record(rstate, {req_sid}).
-record(rstate, {req_sid, opts}).
-type rstate() :: #rstate{}.
-type req() :: cowboy_req:req().

Expand Down Expand Up @@ -141,12 +141,12 @@ node_cleanup(Acc, #{node := Node}, _) ->
%%--------------------------------------------------------------------

-spec init(req(), mongoose_http_handler:options()) -> {cowboy_loop, req(), rstate()}.
init(Req, _Opts) ->
init(Req, Opts) ->
?LOG_DEBUG(#{what => bosh_init, req => Req}),
Msg = init_msg(Req),
self() ! Msg,
%% Upgrade to cowboy_loop behaviour to enable long polling
{cowboy_loop, Req, #rstate{}}.
{cowboy_loop, Req, #rstate{opts = Opts}}.


%% ok return keep the handler looping.
Expand Down Expand Up @@ -275,11 +275,11 @@ check_event_type_streamstrart(Body) ->

-spec forward_body(req(), exml:element(), rstate())
-> {ok, req(), rstate()} | {stop, req(), rstate()}.
forward_body(Req, #xmlel{} = Body, S) ->
forward_body(Req, #xmlel{} = Body, #rstate{opts = Opts} = S) ->
Type = to_event_type(Body),
case Type of
streamstart ->
{SessionStarted, Req1} = maybe_start_session(Req, Body),
{SessionStarted, Req1} = maybe_start_session(Req, Body, Opts),
case SessionStarted of
true ->
{ok, Req1, S};
Expand Down Expand Up @@ -319,27 +319,27 @@ get_session_socket(Sid) ->
end.


-spec maybe_start_session(req(), exml:element()) ->
-spec maybe_start_session(req(), exml:element(), map()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session(Req, Body) ->
maybe_start_session(Req, Body, Opts) ->
Domain = exml_query:attr(Body, <<"to">>),
case mongoose_domain_api:get_domain_host_type(Domain) of
{ok, HostType} ->
case gen_mod:is_loaded(HostType, ?MODULE) of
true ->
maybe_start_session_on_known_host(HostType, Req, Body);
maybe_start_session_on_known_host(HostType, Req, Body, Opts);
false ->
{false, terminal_condition(<<"host-unknown">>, Req)}
end;
{error, not_found} ->
{false, terminal_condition(<<"host-unknown">>, Req)}
end.

-spec maybe_start_session_on_known_host(mongooseim:host_type(), req(), exml:element()) ->
-spec maybe_start_session_on_known_host(mongooseim:host_type(), req(), exml:element(), map()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session_on_known_host(HostType, Req, Body) ->
maybe_start_session_on_known_host(HostType, Req, Body, Opts) ->
try
maybe_start_session_on_known_host_unsafe(HostType, Req, Body)
maybe_start_session_on_known_host_unsafe(HostType, Req, Body, Opts)
catch
error:Reason ->
%% It's here because something catch-y was here before
Expand All @@ -349,22 +349,22 @@ maybe_start_session_on_known_host(HostType, Req, Body) ->
{false, Req1}
end.

-spec maybe_start_session_on_known_host_unsafe(mongooseim:host_type(), req(), exml:element()) ->
-spec maybe_start_session_on_known_host_unsafe(mongooseim:host_type(), req(), exml:element(), map()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session_on_known_host_unsafe(HostType, Req, Body) ->
maybe_start_session_on_known_host_unsafe(HostType, Req, Body, Opts) ->
%% Version isn't checked as it would be meaningless when supporting
%% only a subset of the specification.
{ok, NewBody} = set_max_hold(Body),
Peer = cowboy_req:peer(Req),
PeerCert = cowboy_req:cert(Req),
start_session(HostType, Peer, PeerCert, NewBody),
start_session(HostType, Peer, PeerCert, NewBody, Opts),
{true, Req}.

-spec start_session(mongooseim:host_type(), mongoose_transport:peer(),
binary() | undefined, exml:element()) -> any().
start_session(HostType, Peer, PeerCert, Body) ->
binary() | undefined, exml:element(), map()) -> any().
start_session(HostType, Peer, PeerCert, Body, Opts) ->
Sid = make_sid(),
{ok, Socket} = mod_bosh_socket:start(HostType, Sid, Peer, PeerCert),
{ok, Socket} = mod_bosh_socket:start(HostType, Sid, Peer, PeerCert, Opts),
store_session(Sid, Socket),
handle_request(Socket, streamstart, Body),
?LOG_DEBUG(#{what => bosh_start_session, sid => Sid}).
Expand Down
39 changes: 20 additions & 19 deletions src/mod_bosh_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
-behaviour(mongoose_c2s_socket).

%% API
-export([start/4,
start_link/4,
-export([start/5,
start_link/5,
start_supervisor/0,
is_supervisor_started/0,
handle_request/2,
Expand Down Expand Up @@ -49,7 +49,7 @@
closing/3, compress/1, compress/3, get_cached_responses/1,
get_client_acks/1, get_handlers/1, get_peer_certificate/1,
get_pending/1, get_pending/1, get_sockmod/1, normal/2, normal/3,
pause/2, send/2, set_client_acks/2, start_link/4]).
pause/2, send/2, set_client_acks/2, start_link/5]).

-include("mongoose.hrl").
-include("jlib.hrl").
Expand Down Expand Up @@ -101,16 +101,16 @@
%%--------------------------------------------------------------------

-spec start(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(),
binary() | undefined) ->
binary() | undefined, map()) ->
{'error', _} | {'ok', 'undefined' | pid()} | {'ok', 'undefined' | pid(), _}.
start(HostType, Sid, Peer, PeerCert) ->
supervisor:start_child(?BOSH_SOCKET_SUP, [HostType, Sid, Peer, PeerCert]).
start(HostType, Sid, Peer, PeerCert, Opts) ->
supervisor:start_child(?BOSH_SOCKET_SUP, [HostType, Sid, Peer, PeerCert, Opts]).

-spec start_link(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(),
binary() | undefined) ->
binary() | undefined, map()) ->
'ignore' | {'error', _} | {'ok', pid()}.
start_link(HostType, Sid, Peer, PeerCert) ->
gen_fsm_compat:start_link(?MODULE, [{HostType, Sid, Peer, PeerCert}], []).
start_link(HostType, Sid, Peer, PeerCert, Opts) ->
gen_fsm_compat:start_link(?MODULE, [{HostType, Sid, Peer, PeerCert, Opts}], []).

-spec start_supervisor() -> {ok, pid()} | {error, any()}.
start_supervisor() ->
Expand Down Expand Up @@ -192,18 +192,19 @@ get_cached_responses(Pid) ->
%% {stop, StopReason}
%% @end
%%--------------------------------------------------------------------
-spec init([{mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), undefined | binary()}]) ->
-spec init([{mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(), undefined |
binary(), map()}]) ->
{ok, accumulate, state()}.
init([{HostType, Sid, Peer = {IPTuple, Port}, PeerCert}]) ->
init([{HostType, Sid, Peer, PeerCert, ListenerOpts}]) ->
BoshSocket = #bosh_socket{sid = Sid, pid = self(), peer = Peer, peercert = PeerCert},
C2SOpts = #{access => all,
shaper => none,
xml_socket => true,
max_stanza_size => 0,
hibernate_after => 0,
c2s_state_timeout => 5000,
backwards_compatible_session => true,
port => Port, ip_tuple => IPTuple, proto => tcp},
C2SOpts = ListenerOpts#{access => all,
shaper => none,
xml_socket => true,
max_stanza_size => 0,
hibernate_after => 0,
c2s_state_timeout => 5000,
backwards_compatible_session => true,
proto => tcp},
{ok, C2SPid} = mongoose_c2s:start({?MODULE, BoshSocket, C2SOpts}, []),
Opts = gen_mod:get_loaded_module_opts(HostType, mod_bosh),
State = new_state(Sid, C2SPid, Opts),
Expand Down

0 comments on commit 1ffe149

Please sign in to comment.