2003-11-10 22:25:36 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_receiver.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-11-10 22:25:36 +01:00
|
|
|
%%% Purpose : Socket receiver for C2S and S2S connections
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 10 Nov 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:11:32 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-11-10 22:25:36 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_receiver).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2006-09-05 06:26:28 +02:00
|
|
|
-export([start_link/4,
|
2006-01-29 05:38:31 +01:00
|
|
|
start/3,
|
2006-04-22 05:35:13 +02:00
|
|
|
start/4,
|
2003-11-10 22:25:36 +01:00
|
|
|
change_shaper/2,
|
2004-08-01 22:12:20 +02:00
|
|
|
reset_stream/1,
|
2006-01-13 02:55:20 +01:00
|
|
|
starttls/2,
|
2010-07-13 12:04:30 +02:00
|
|
|
starttls/3,
|
2006-01-19 03:17:31 +01:00
|
|
|
compress/2,
|
2010-07-13 12:04:30 +02:00
|
|
|
send/2,
|
2006-09-05 06:26:28 +02:00
|
|
|
become_controller/2,
|
2010-07-13 12:04:30 +02:00
|
|
|
change_controller/2,
|
|
|
|
setopts/2,
|
2006-01-13 02:55:20 +01:00
|
|
|
close/1]).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
-record(state, {socket,
|
|
|
|
sock_mod,
|
|
|
|
shaper_state,
|
|
|
|
c2s_pid,
|
2006-04-22 05:35:13 +02:00
|
|
|
max_stanza_size,
|
2006-01-13 02:55:20 +01:00
|
|
|
xml_stream_state,
|
2010-07-13 12:04:30 +02:00
|
|
|
tref,
|
2006-01-13 02:55:20 +01:00
|
|
|
timeout}).
|
|
|
|
|
2008-11-23 10:43:52 +01:00
|
|
|
-define(HIBERNATE_TIMEOUT, 90000).
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2006-09-05 06:26:28 +02:00
|
|
|
start_link(Socket, SockMod, Shaper, MaxStanzaSize) ->
|
2006-04-22 05:35:13 +02:00
|
|
|
gen_server:start_link(
|
2006-09-05 06:26:28 +02:00
|
|
|
?MODULE, [Socket, SockMod, Shaper, MaxStanzaSize], []).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2003-11-10 22:25:36 +01:00
|
|
|
start(Socket, SockMod, Shaper) ->
|
2006-04-22 05:35:13 +02:00
|
|
|
start(Socket, SockMod, Shaper, infinity).
|
|
|
|
|
|
|
|
start(Socket, SockMod, Shaper, MaxStanzaSize) ->
|
|
|
|
{ok, Pid} = supervisor:start_child(
|
|
|
|
ejabberd_receiver_sup,
|
2006-09-05 06:26:28 +02:00
|
|
|
[Socket, SockMod, Shaper, MaxStanzaSize]),
|
2006-01-13 02:55:20 +01:00
|
|
|
Pid.
|
|
|
|
|
|
|
|
change_shaper(Pid, Shaper) ->
|
|
|
|
gen_server:cast(Pid, {change_shaper, Shaper}).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
reset_stream(Pid) ->
|
|
|
|
gen_server:call(Pid, reset_stream).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2010-07-13 12:04:30 +02:00
|
|
|
starttls(Pid, TLSOpts) ->
|
|
|
|
starttls(Pid, TLSOpts, undefined).
|
2006-01-13 02:55:20 +01:00
|
|
|
|
2010-07-13 12:04:30 +02:00
|
|
|
starttls(Pid, TLSOpts, Data) ->
|
|
|
|
gen_server:call(Pid, {starttls, TLSOpts, Data}).
|
|
|
|
|
|
|
|
compress(Pid, Data) ->
|
|
|
|
gen_server:call(Pid, {compress, Data}).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
become_controller(Pid, C2SPid) ->
|
|
|
|
gen_server:call(Pid, {become_controller, C2SPid}).
|
2006-01-13 02:55:20 +01:00
|
|
|
|
2010-07-13 12:04:30 +02:00
|
|
|
change_controller(Pid, C2SPid) ->
|
2010-10-08 11:26:49 +02:00
|
|
|
case catch gen_server:call(Pid, {change_controller, C2SPid}) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
{error, einval};
|
|
|
|
Res ->
|
|
|
|
Res
|
|
|
|
end.
|
2010-07-13 12:04:30 +02:00
|
|
|
|
|
|
|
setopts(Pid, Opts) ->
|
|
|
|
case lists:member({active, false}, Opts) of
|
|
|
|
true ->
|
2010-10-08 11:26:49 +02:00
|
|
|
case catch gen_server:call(Pid, deactivate_socket) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
{error, einval};
|
|
|
|
Res ->
|
|
|
|
Res
|
|
|
|
end;
|
2010-07-13 12:04:30 +02:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
send(Pid, Data) ->
|
|
|
|
gen_server:call(Pid, {send, Data}).
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
close(Pid) ->
|
|
|
|
gen_server:cast(Pid, close).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
2006-09-05 06:26:28 +02:00
|
|
|
init([Socket, SockMod, Shaper, MaxStanzaSize]) ->
|
2003-11-10 22:25:36 +01:00
|
|
|
ShaperState = shaper:new(Shaper),
|
|
|
|
Timeout = case SockMod of
|
|
|
|
ssl ->
|
|
|
|
20;
|
|
|
|
_ ->
|
|
|
|
infinity
|
|
|
|
end,
|
2006-01-13 02:55:20 +01:00
|
|
|
{ok, #state{socket = Socket,
|
|
|
|
sock_mod = SockMod,
|
|
|
|
shaper_state = ShaperState,
|
2006-04-22 05:35:13 +02:00
|
|
|
max_stanza_size = MaxStanzaSize,
|
2006-01-13 02:55:20 +01:00
|
|
|
timeout = Timeout}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
2010-07-13 12:04:30 +02:00
|
|
|
handle_call({starttls, TLSOpts, Data}, _From,
|
2006-01-13 02:55:20 +01:00
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2006-04-22 05:35:13 +02:00
|
|
|
c2s_pid = C2SPid,
|
2010-07-13 12:04:30 +02:00
|
|
|
socket = Socket,
|
2006-04-22 05:35:13 +02:00
|
|
|
max_stanza_size = MaxStanzaSize} = State) ->
|
2010-07-13 12:04:30 +02:00
|
|
|
{ok, TLSSocket} = tls:tcp_to_tls(Socket, TLSOpts),
|
|
|
|
if Data /= undefined ->
|
|
|
|
do_send(State, Data);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2006-04-22 05:35:13 +02:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
|
2006-01-13 02:55:20 +01:00
|
|
|
NewState = State#state{socket = TLSSocket,
|
|
|
|
sock_mod = tls,
|
|
|
|
xml_stream_state = NewXMLStreamState},
|
|
|
|
case tls:recv_data(TLSSocket, "") of
|
|
|
|
{ok, TLSData} ->
|
2010-07-13 12:04:30 +02:00
|
|
|
{reply, {ok, TLSSocket},
|
|
|
|
process_data(TLSData, NewState), ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
{error, _Reason} ->
|
|
|
|
{stop, normal, ok, NewState}
|
|
|
|
end;
|
2010-07-13 12:04:30 +02:00
|
|
|
handle_call({compress, Data}, _From,
|
2006-01-19 03:17:31 +01:00
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2006-04-22 05:35:13 +02:00
|
|
|
c2s_pid = C2SPid,
|
2010-07-13 12:04:30 +02:00
|
|
|
socket = Socket,
|
|
|
|
sock_mod = SockMod,
|
2006-04-22 05:35:13 +02:00
|
|
|
max_stanza_size = MaxStanzaSize} = State) ->
|
2010-07-13 12:04:30 +02:00
|
|
|
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(SockMod, Socket),
|
|
|
|
if Data /= undefined ->
|
|
|
|
do_send(State, Data);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2006-04-22 05:35:13 +02:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
|
2006-01-19 03:17:31 +01:00
|
|
|
NewState = State#state{socket = ZlibSocket,
|
|
|
|
sock_mod = ejabberd_zlib,
|
|
|
|
xml_stream_state = NewXMLStreamState},
|
|
|
|
case ejabberd_zlib:recv_data(ZlibSocket, "") of
|
|
|
|
{ok, ZlibData} ->
|
2010-07-13 12:04:30 +02:00
|
|
|
{reply, {ok, ZlibSocket},
|
|
|
|
process_data(ZlibData, NewState), ?HIBERNATE_TIMEOUT};
|
2006-01-19 03:17:31 +01:00
|
|
|
{error, _Reason} ->
|
|
|
|
{stop, normal, ok, NewState}
|
|
|
|
end;
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_call(reset_stream, _From,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2006-04-22 05:35:13 +02:00
|
|
|
c2s_pid = C2SPid,
|
|
|
|
max_stanza_size = MaxStanzaSize} = State) ->
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2006-04-22 05:35:13 +02:00
|
|
|
NewXMLStreamState = xml_stream:new(C2SPid, MaxStanzaSize),
|
2006-01-13 02:55:20 +01:00
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State#state{xml_stream_state = NewXMLStreamState},
|
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-09-05 06:26:28 +02:00
|
|
|
handle_call({become_controller, C2SPid}, _From, State) ->
|
2010-07-13 12:04:30 +02:00
|
|
|
erlang:monitor(process, C2SPid),
|
2006-09-05 06:26:28 +02:00
|
|
|
XMLStreamState = xml_stream:new(C2SPid, State#state.max_stanza_size),
|
|
|
|
NewState = State#state{c2s_pid = C2SPid,
|
|
|
|
xml_stream_state = XMLStreamState},
|
|
|
|
activate_socket(NewState),
|
2006-01-13 02:55:20 +01:00
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, NewState, ?HIBERNATE_TIMEOUT};
|
2010-07-13 12:04:30 +02:00
|
|
|
handle_call({change_controller, C2SPid}, _From, State) ->
|
|
|
|
erlang:monitor(process, C2SPid),
|
|
|
|
NewXMLStreamState = xml_stream:change_callback_pid(
|
|
|
|
State#state.xml_stream_state, C2SPid),
|
|
|
|
NewState = State#state{c2s_pid = C2SPid,
|
|
|
|
xml_stream_state = NewXMLStreamState},
|
|
|
|
activate_socket(NewState),
|
|
|
|
{reply, ok, NewState, ?HIBERNATE_TIMEOUT};
|
|
|
|
handle_call({send, Data}, _From, State) ->
|
|
|
|
case do_send(State, Data) of
|
|
|
|
ok ->
|
|
|
|
{reply, ok, State, ?HIBERNATE_TIMEOUT};
|
|
|
|
{error, _Reason} = Err ->
|
|
|
|
{stop, normal, Err, State}
|
|
|
|
end;
|
|
|
|
handle_call(deactivate_socket, _From, State) ->
|
|
|
|
deactivate_socket(State),
|
|
|
|
{reply, ok, State, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast({change_shaper, Shaper}, State) ->
|
|
|
|
NewShaperState = shaper:new(Shaper),
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State#state{shaper_state = NewShaperState}, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_cast(close, State) ->
|
|
|
|
{stop, normal, State};
|
|
|
|
handle_cast(_Msg, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({Tag, _TCPSocket, Data},
|
|
|
|
#state{socket = Socket,
|
|
|
|
sock_mod = SockMod} = State)
|
2009-08-31 20:37:52 +02:00
|
|
|
when (Tag == tcp) or (Tag == ssl) or (Tag == ejabberd_xml) ->
|
2006-01-13 02:55:20 +01:00
|
|
|
case SockMod of
|
|
|
|
tls ->
|
|
|
|
case tls:recv_data(Socket, Data) of
|
|
|
|
{ok, TLSData} ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, process_data(TLSData, State),
|
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
{error, _Reason} ->
|
|
|
|
{stop, normal, State}
|
|
|
|
end;
|
2006-01-19 03:17:31 +01:00
|
|
|
ejabberd_zlib ->
|
|
|
|
case ejabberd_zlib:recv_data(Socket, Data) of
|
|
|
|
{ok, ZlibData} ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, process_data(ZlibData, State),
|
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-01-19 03:17:31 +01:00
|
|
|
{error, _Reason} ->
|
|
|
|
{stop, normal, State}
|
|
|
|
end;
|
2006-01-13 02:55:20 +01:00
|
|
|
_ ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, process_data(Data, State), ?HIBERNATE_TIMEOUT}
|
2006-01-13 02:55:20 +01:00
|
|
|
end;
|
|
|
|
handle_info({Tag, _TCPSocket}, State)
|
|
|
|
when (Tag == tcp_closed) or (Tag == ssl_closed) ->
|
|
|
|
{stop, normal, State};
|
|
|
|
handle_info({Tag, _TCPSocket, Reason}, State)
|
|
|
|
when (Tag == tcp_error) or (Tag == ssl_error) ->
|
|
|
|
case Reason of
|
|
|
|
timeout ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
_ ->
|
|
|
|
{stop, normal, State}
|
|
|
|
end;
|
2010-07-13 12:04:30 +02:00
|
|
|
handle_info({'DOWN', _MRef, process, C2SPid, _},
|
|
|
|
#state{c2s_pid = C2SPid} = State) ->
|
|
|
|
{stop, normal, State};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_info({timeout, _Ref, activate}, State) ->
|
|
|
|
activate_socket(State),
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
|
|
|
handle_info(timeout, State) ->
|
|
|
|
proc_lib:hibernate(gen_server, enter_loop, [?MODULE, [], State]),
|
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
2006-01-13 02:55:20 +01:00
|
|
|
handle_info(_Info, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-01-13 02:55:20 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, #state{xml_stream_state = XMLStreamState,
|
|
|
|
c2s_pid = C2SPid} = State) ->
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(XMLStreamState),
|
2006-12-08 21:48:26 +01:00
|
|
|
if
|
|
|
|
C2SPid /= undefined ->
|
|
|
|
gen_fsm:send_event(C2SPid, closed);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2006-01-13 02:55:20 +01:00
|
|
|
catch (State#state.sock_mod):close(State#state.socket),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
activate_socket(#state{socket = Socket,
|
2008-07-16 14:55:29 +02:00
|
|
|
sock_mod = SockMod}) ->
|
2007-05-07 19:52:35 +02:00
|
|
|
PeerName =
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:setopts(Socket, [{active, once}]),
|
|
|
|
inet:peername(Socket);
|
|
|
|
_ ->
|
|
|
|
SockMod:setopts(Socket, [{active, once}]),
|
|
|
|
SockMod:peername(Socket)
|
|
|
|
end,
|
|
|
|
case PeerName of
|
|
|
|
{error, _Reason} ->
|
|
|
|
self() ! {tcp_closed, Socket};
|
2008-07-16 14:55:29 +02:00
|
|
|
{ok, _} ->
|
2007-05-07 19:52:35 +02:00
|
|
|
ok
|
2003-11-10 22:25:36 +01:00
|
|
|
end.
|
|
|
|
|
2010-07-13 12:04:30 +02:00
|
|
|
deactivate_socket(#state{socket = Socket,
|
|
|
|
tref = TRef,
|
|
|
|
sock_mod = SockMod}) ->
|
|
|
|
cancel_timer(TRef),
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:setopts(Socket, [{active, false}]);
|
|
|
|
_ ->
|
|
|
|
SockMod:setopts(Socket, [{active, false}])
|
|
|
|
end.
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Data processing for connectors directly generating xmlelement in
|
|
|
|
%% Erlang data structure.
|
|
|
|
%% WARNING: Shaper does not work with Erlang data structure.
|
|
|
|
process_data([], State) ->
|
|
|
|
activate_socket(State),
|
|
|
|
State;
|
|
|
|
process_data([Element|Els], #state{c2s_pid = C2SPid} = State)
|
|
|
|
when element(1, Element) == xmlelement;
|
|
|
|
element(1, Element) == xmlstreamstart;
|
|
|
|
element(1, Element) == xmlstreamelement;
|
|
|
|
element(1, Element) == xmlstreamend ->
|
|
|
|
if
|
|
|
|
C2SPid == undefined ->
|
|
|
|
State;
|
|
|
|
true ->
|
|
|
|
catch gen_fsm:send_event(C2SPid, element_wrapper(Element)),
|
|
|
|
process_data(Els, State)
|
|
|
|
end;
|
|
|
|
%% Data processing for connectors receivind data as string.
|
2006-01-13 02:55:20 +01:00
|
|
|
process_data(Data,
|
|
|
|
#state{xml_stream_state = XMLStreamState,
|
2010-07-13 12:04:30 +02:00
|
|
|
tref = TRef,
|
2008-05-04 09:38:24 +02:00
|
|
|
shaper_state = ShaperState,
|
|
|
|
c2s_pid = C2SPid} = State) ->
|
2006-01-20 17:51:58 +01:00
|
|
|
?DEBUG("Received XML on stream = ~p", [binary_to_list(Data)]),
|
2006-01-13 02:55:20 +01:00
|
|
|
XMLStreamState1 = xml_stream:parse(XMLStreamState, Data),
|
|
|
|
{NewShaperState, Pause} = shaper:update(ShaperState, size(Data)),
|
2010-07-13 12:04:30 +02:00
|
|
|
NewTRef = if
|
|
|
|
C2SPid == undefined ->
|
|
|
|
TRef;
|
|
|
|
Pause > 0 ->
|
|
|
|
erlang:start_timer(Pause, self(), activate);
|
|
|
|
true ->
|
|
|
|
activate_socket(State),
|
|
|
|
TRef
|
|
|
|
end,
|
2006-01-13 02:55:20 +01:00
|
|
|
State#state{xml_stream_state = XMLStreamState1,
|
2010-07-13 12:04:30 +02:00
|
|
|
tref = NewTRef,
|
2006-01-13 02:55:20 +01:00
|
|
|
shaper_state = NewShaperState}.
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
%% Element coming from XML parser are wrapped inside xmlstreamelement
|
|
|
|
%% When we receive directly xmlelement tuple (from a socket module
|
|
|
|
%% speaking directly Erlang XML), we wrap it inside the same
|
|
|
|
%% xmlstreamelement coming from the XML parser.
|
|
|
|
element_wrapper(XMLElement)
|
|
|
|
when element(1, XMLElement) == xmlelement ->
|
|
|
|
{xmlstreamelement, XMLElement};
|
|
|
|
element_wrapper(Element) ->
|
|
|
|
Element.
|
|
|
|
|
2008-02-11 16:36:56 +01:00
|
|
|
close_stream(undefined) ->
|
|
|
|
ok;
|
|
|
|
close_stream(XMLStreamState) ->
|
|
|
|
xml_stream:close(XMLStreamState).
|
2010-07-13 12:04:30 +02:00
|
|
|
|
|
|
|
do_send(State, Data) ->
|
|
|
|
(State#state.sock_mod):send(State#state.socket, Data).
|
|
|
|
|
|
|
|
cancel_timer(TRef) when is_reference(TRef) ->
|
|
|
|
case erlang:cancel_timer(TRef) of
|
|
|
|
false ->
|
|
|
|
receive
|
|
|
|
{timeout, TRef, _} ->
|
|
|
|
ok
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
cancel_timer(_) ->
|
|
|
|
ok.
|