Compare commits

..

No commits in common. "85995d51e4d9e1518ee8647ea684631a5d927129" and "4321573dfb9c58ad05f5fdec8fa89859dadeb602" have entirely different histories.

9 changed files with 97 additions and 98 deletions

View File

@ -77,9 +77,7 @@ class _MyHomePageState extends State<MyHomePage> {
StreamManagementManager(),
DiscoManager([]),
RosterManager(TestingRosterStateManager("", [])),
PingManager(
const Duration(minutes: 3),
),
PingManager(),
MessageManager(),
PresenceManager(),
])

View File

@ -83,6 +83,7 @@ class XmppConnection {
ReconnectionPolicy reconnectionPolicy,
ConnectivityManager connectivityManager,
this._socket, {
this.connectionPingDuration = const Duration(minutes: 3),
this.connectingTimeout = const Duration(minutes: 2),
}) : _reconnectionPolicy = reconnectionPolicy,
_connectivityManager = connectivityManager {
@ -142,6 +143,10 @@ class XmppConnection {
/// UUID object to generate stanza and origin IDs
final Uuid _uuid = const Uuid();
/// The time between sending a ping to keep the connection open
// TODO(Unknown): Only start the timer if we did not send a stanza after n seconds
final Duration connectionPingDuration;
/// The time that we may spent in the "connecting" state
final Duration connectingTimeout;
@ -154,6 +159,9 @@ class XmppConnection {
/// True if we are authenticated. False if not.
bool _isAuthenticated = false;
/// Timer for the keep-alive ping.
Timer? _connectionPingTimer;
/// Timer for the connecting timeout
Timer? _connectingTimeoutTimer;
@ -619,7 +627,22 @@ class XmppConnection {
final oldState = _connectionState;
_connectionState = state;
final sm = getNegotiatorById<StreamManagementNegotiator>(
streamManagementNegotiator,
);
await _sendEvent(
ConnectionStateChangedEvent(
state,
oldState,
sm?.isResumed ?? false,
),
);
if (state == XmppConnectionState.connected) {
_log.finest('Starting _pingConnectionTimer');
_connectionPingTimer =
Timer.periodic(connectionPingDuration, _pingConnectionOpen);
// We are connected, so the timer can stop.
_destroyConnectingTimer();
} else if (state == XmppConnectionState.connecting) {
@ -632,18 +655,14 @@ class XmppConnection {
} else {
// Just make sure the connecting timeout timer is not running
_destroyConnectingTimer();
}
final sm = getNegotiatorById<StreamManagementNegotiator>(
streamManagementNegotiator,
);
await _sendEvent(
ConnectionStateChangedEvent(
state,
oldState,
sm?.isResumed ?? false,
),
);
// The ping timer makes no sense if we are not connected
if (_connectionPingTimer != null) {
_log.finest('Destroying _pingConnectionTimer');
_connectionPingTimer!.cancel();
_connectionPingTimer = null;
}
}
}
/// Sets the routing state and logs the change
@ -663,6 +682,22 @@ class XmppConnection {
return _eventStreamController.stream.asBroadcastStream();
}
/// Timer callback to prevent the connection from timing out.
Future<void> _pingConnectionOpen(Timer timer) async {
// Follow the recommendation of XEP-0198 and just request an ack. If SM is not enabled,
// send a whitespace ping
_log.finest('_pingConnectionTimer: Callback called.');
if (_connectionState == XmppConnectionState.connected) {
_log.finest('_pingConnectionTimer: Connected. Triggering a ping event.');
unawaited(_sendEvent(SendPingEvent()));
} else {
_log.finest(
'_pingConnectionTimer: Not connected. Not triggering an event.',
);
}
}
/// Iterate over [handlers] and check if the handler matches [stanza]. If it does,
/// call its callback and end the processing if the callback returned true; continue
/// if it returned false.
@ -1095,6 +1130,10 @@ class XmppConnection {
_xmppManagers.containsKey(discoManager),
'A DiscoManager is mandatory',
);
assert(
_xmppManagers.containsKey(pingManager),
'A PingManager is mandatory',
);
}
/// The private implementation for [XmppConnection.connect]. The parameters have

View File

@ -26,12 +26,6 @@ class ConnectionStateChangedEvent extends XmppEvent {
final XmppConnectionState before;
final XmppConnectionState state;
final bool resumed;
/// Indicates whether the connection state switched from a not connected state to a
/// connected state.
bool get connectionEstablished =>
before != XmppConnectionState.connected &&
state == XmppConnectionState.connected;
}
/// Triggered when we encounter a stream error.
@ -49,6 +43,9 @@ class AuthenticationFailedEvent extends XmppEvent {
/// Triggered after the SASL authentication has succeeded.
class AuthenticationSuccessEvent extends XmppEvent {}
/// Triggered when we want to ping the connection open
class SendPingEvent extends XmppEvent {}
/// Triggered when the stream resumption was successful
class StreamResumedEvent extends XmppEvent {
StreamResumedEvent({required this.h});

View File

@ -1,23 +1,10 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:moxxmpp/src/events.dart';
import 'package:moxxmpp/src/managers/base.dart';
import 'package:moxxmpp/src/managers/namespaces.dart';
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
import 'package:synchronized/synchronized.dart';
/// This manager class is responsible to sending periodic pings, if required, using
/// either whitespaces or Stream Management. Keep in mind, that without
/// Stream Management, a stale connection cannot be detected.
class PingManager extends XmppManagerBase {
PingManager(this._pingDuration) : super(pingManager);
/// The time between pings, when connected.
final Duration _pingDuration;
/// The actual timer.
Timer? _pingTimer;
final Lock _timerLock = Lock();
PingManager() : super(pingManager);
@override
Future<bool> isSupported() async => true;
@ -28,73 +15,39 @@ class PingManager extends XmppManagerBase {
);
}
/// Cancel a potentially scheduled ping timer. Can be overriden to cancel a custom timing mechanism.
/// By default, cancels a [Timer.periodic] that was set up prior.
@visibleForOverriding
Future<void> cancelPing() async {
await _timerLock.synchronized(() {
logger.finest('Cancelling timer');
_pingTimer?.cancel();
_pingTimer = null;
});
}
/// Schedule a ping to be sent after a given amount of time. Can be overriden for custom timing mechanisms.
/// By default, uses a [Timer.periodic] timer to trigger a ping.
/// NOTE: This function is called whenever the connection is re-established. Custom
/// implementations should thus guard against multiple timers being started.
@visibleForOverriding
Future<void> schedulePing() async {
await _timerLock.synchronized(() {
logger.finest('Scheduling new timer? ${_pingTimer != null}');
_pingTimer ??= Timer.periodic(
_pingDuration,
_sendPing,
);
});
}
Future<void> _sendPing(Timer _) async {
logger.finest('Attempting to send ping');
final attrs = getAttributes();
final socket = attrs.getSocket();
if (socket.managesKeepalives()) {
logger.finest('Not sending ping as the socket manages it.');
return;
}
final stream = attrs.getManagerById(smManager) as StreamManagementManager?;
if (stream != null) {
if (stream
.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
logger.finest('Sending an ack ping as Stream Management is enabled');
stream.sendAckRequestPing();
} else if (attrs.getSocket().whitespacePingAllowed()) {
logger.finest(
'Sending a whitespace ping as Stream Management is not enabled',
);
attrs.getConnection().sendWhitespacePing();
} else {
_logWarning();
}
} else {
if (attrs.getSocket().whitespacePingAllowed()) {
attrs.getConnection().sendWhitespacePing();
} else {
_logWarning();
}
}
}
@override
Future<void> onXmppEvent(XmppEvent event) async {
if (event is ConnectionStateChangedEvent) {
if (event.connectionEstablished) {
await schedulePing();
if (event is SendPingEvent) {
logger.finest('Received ping event.');
final attrs = getAttributes();
final socket = attrs.getSocket();
if (socket.managesKeepalives()) {
logger.finest('Not sending ping as the socket manages it.');
return;
}
final stream =
attrs.getManagerById(smManager) as StreamManagementManager?;
if (stream != null) {
if (stream
.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
logger.finest('Sending an ack ping as Stream Management is enabled');
stream.sendAckRequestPing();
} else if (attrs.getSocket().whitespacePingAllowed()) {
logger.finest(
'Sending a whitespace ping as Stream Management is not enabled',
);
attrs.getConnection().sendWhitespacePing();
} else {
_logWarning();
}
} else {
await cancelPing();
if (attrs.getSocket().whitespacePingAllowed()) {
attrs.getConnection().sendWhitespacePing();
} else {
_logWarning();
}
}
}
}

View File

@ -75,6 +75,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
EntityCapabilitiesManager('http://moxxmpp.example'),
])
..setConnectionSettings(

View File

@ -82,6 +82,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager(null, [])),
DiscoManager([]),
PingManager(),
EntityCapabilitiesManager('http://moxxmpp.example'),
]);
conn.registerFeatureNegotiators([

View File

@ -168,6 +168,7 @@ void main() {
PresenceManager(),
MessageManager(),
RosterManager(TestingRosterStateManager(null, [])),
PingManager(),
]);
connection
..registerFeatureNegotiators([

View File

@ -294,6 +294,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
sm,
CarbonsManager()..forceEnable(),
EntityCapabilitiesManager('http://moxxmpp.example'),
@ -419,6 +420,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
sm,
CarbonsManager()..forceEnable(),
//EntityCapabilitiesManager('http://moxxmpp.example'),
@ -578,6 +580,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
StreamManagementManager(),
]);
conn.registerFeatureNegotiators([
@ -672,6 +675,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
StreamManagementManager(),
]);
conn.registerFeatureNegotiators([
@ -763,6 +767,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
StreamManagementManager(),
]);
conn.registerFeatureNegotiators([

View File

@ -137,6 +137,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
StreamManagementManager(),
EntityCapabilitiesManager('http://moxxmpp.example'),
]);
@ -193,6 +194,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
EntityCapabilitiesManager('http://moxxmpp.example'),
]);
conn.registerFeatureNegotiators([
@ -252,6 +254,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
EntityCapabilitiesManager('http://moxxmpp.example'),
]);
conn.registerFeatureNegotiators([SaslPlainNegotiator()]);
@ -406,6 +409,7 @@ void main() {
PresenceManager(),
RosterManager(TestingRosterStateManager('', [])),
DiscoManager([]),
PingManager(),
]);
conn
..registerFeatureNegotiators([