Compare commits
No commits in common. "85995d51e4d9e1518ee8647ea684631a5d927129" and "4321573dfb9c58ad05f5fdec8fa89859dadeb602" have entirely different histories.
85995d51e4
...
4321573dfb
@ -77,9 +77,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
RosterManager(TestingRosterStateManager("", [])),
|
RosterManager(TestingRosterStateManager("", [])),
|
||||||
PingManager(
|
PingManager(),
|
||||||
const Duration(minutes: 3),
|
|
||||||
),
|
|
||||||
MessageManager(),
|
MessageManager(),
|
||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
])
|
])
|
||||||
|
@ -83,6 +83,7 @@ class XmppConnection {
|
|||||||
ReconnectionPolicy reconnectionPolicy,
|
ReconnectionPolicy reconnectionPolicy,
|
||||||
ConnectivityManager connectivityManager,
|
ConnectivityManager connectivityManager,
|
||||||
this._socket, {
|
this._socket, {
|
||||||
|
this.connectionPingDuration = const Duration(minutes: 3),
|
||||||
this.connectingTimeout = const Duration(minutes: 2),
|
this.connectingTimeout = const Duration(minutes: 2),
|
||||||
}) : _reconnectionPolicy = reconnectionPolicy,
|
}) : _reconnectionPolicy = reconnectionPolicy,
|
||||||
_connectivityManager = connectivityManager {
|
_connectivityManager = connectivityManager {
|
||||||
@ -142,6 +143,10 @@ class XmppConnection {
|
|||||||
/// UUID object to generate stanza and origin IDs
|
/// UUID object to generate stanza and origin IDs
|
||||||
final Uuid _uuid = const Uuid();
|
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
|
/// The time that we may spent in the "connecting" state
|
||||||
final Duration connectingTimeout;
|
final Duration connectingTimeout;
|
||||||
|
|
||||||
@ -154,6 +159,9 @@ class XmppConnection {
|
|||||||
/// True if we are authenticated. False if not.
|
/// True if we are authenticated. False if not.
|
||||||
bool _isAuthenticated = false;
|
bool _isAuthenticated = false;
|
||||||
|
|
||||||
|
/// Timer for the keep-alive ping.
|
||||||
|
Timer? _connectionPingTimer;
|
||||||
|
|
||||||
/// Timer for the connecting timeout
|
/// Timer for the connecting timeout
|
||||||
Timer? _connectingTimeoutTimer;
|
Timer? _connectingTimeoutTimer;
|
||||||
|
|
||||||
@ -619,7 +627,22 @@ class XmppConnection {
|
|||||||
final oldState = _connectionState;
|
final oldState = _connectionState;
|
||||||
_connectionState = state;
|
_connectionState = state;
|
||||||
|
|
||||||
|
final sm = getNegotiatorById<StreamManagementNegotiator>(
|
||||||
|
streamManagementNegotiator,
|
||||||
|
);
|
||||||
|
await _sendEvent(
|
||||||
|
ConnectionStateChangedEvent(
|
||||||
|
state,
|
||||||
|
oldState,
|
||||||
|
sm?.isResumed ?? false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
if (state == XmppConnectionState.connected) {
|
if (state == XmppConnectionState.connected) {
|
||||||
|
_log.finest('Starting _pingConnectionTimer');
|
||||||
|
_connectionPingTimer =
|
||||||
|
Timer.periodic(connectionPingDuration, _pingConnectionOpen);
|
||||||
|
|
||||||
// We are connected, so the timer can stop.
|
// We are connected, so the timer can stop.
|
||||||
_destroyConnectingTimer();
|
_destroyConnectingTimer();
|
||||||
} else if (state == XmppConnectionState.connecting) {
|
} else if (state == XmppConnectionState.connecting) {
|
||||||
@ -632,18 +655,14 @@ class XmppConnection {
|
|||||||
} else {
|
} else {
|
||||||
// Just make sure the connecting timeout timer is not running
|
// Just make sure the connecting timeout timer is not running
|
||||||
_destroyConnectingTimer();
|
_destroyConnectingTimer();
|
||||||
}
|
|
||||||
|
|
||||||
final sm = getNegotiatorById<StreamManagementNegotiator>(
|
// The ping timer makes no sense if we are not connected
|
||||||
streamManagementNegotiator,
|
if (_connectionPingTimer != null) {
|
||||||
);
|
_log.finest('Destroying _pingConnectionTimer');
|
||||||
await _sendEvent(
|
_connectionPingTimer!.cancel();
|
||||||
ConnectionStateChangedEvent(
|
_connectionPingTimer = null;
|
||||||
state,
|
}
|
||||||
oldState,
|
}
|
||||||
sm?.isResumed ?? false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the routing state and logs the change
|
/// Sets the routing state and logs the change
|
||||||
@ -663,6 +682,22 @@ class XmppConnection {
|
|||||||
return _eventStreamController.stream.asBroadcastStream();
|
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,
|
/// 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
|
/// call its callback and end the processing if the callback returned true; continue
|
||||||
/// if it returned false.
|
/// if it returned false.
|
||||||
@ -1095,6 +1130,10 @@ class XmppConnection {
|
|||||||
_xmppManagers.containsKey(discoManager),
|
_xmppManagers.containsKey(discoManager),
|
||||||
'A DiscoManager is mandatory',
|
'A DiscoManager is mandatory',
|
||||||
);
|
);
|
||||||
|
assert(
|
||||||
|
_xmppManagers.containsKey(pingManager),
|
||||||
|
'A PingManager is mandatory',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The private implementation for [XmppConnection.connect]. The parameters have
|
/// The private implementation for [XmppConnection.connect]. The parameters have
|
||||||
|
@ -26,12 +26,6 @@ class ConnectionStateChangedEvent extends XmppEvent {
|
|||||||
final XmppConnectionState before;
|
final XmppConnectionState before;
|
||||||
final XmppConnectionState state;
|
final XmppConnectionState state;
|
||||||
final bool resumed;
|
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.
|
/// Triggered when we encounter a stream error.
|
||||||
@ -49,6 +43,9 @@ class AuthenticationFailedEvent extends XmppEvent {
|
|||||||
/// Triggered after the SASL authentication has succeeded.
|
/// Triggered after the SASL authentication has succeeded.
|
||||||
class AuthenticationSuccessEvent extends XmppEvent {}
|
class AuthenticationSuccessEvent extends XmppEvent {}
|
||||||
|
|
||||||
|
/// Triggered when we want to ping the connection open
|
||||||
|
class SendPingEvent extends XmppEvent {}
|
||||||
|
|
||||||
/// Triggered when the stream resumption was successful
|
/// Triggered when the stream resumption was successful
|
||||||
class StreamResumedEvent extends XmppEvent {
|
class StreamResumedEvent extends XmppEvent {
|
||||||
StreamResumedEvent({required this.h});
|
StreamResumedEvent({required this.h});
|
||||||
|
@ -1,23 +1,10 @@
|
|||||||
import 'dart:async';
|
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:moxxmpp/src/events.dart';
|
import 'package:moxxmpp/src/events.dart';
|
||||||
import 'package:moxxmpp/src/managers/base.dart';
|
import 'package:moxxmpp/src/managers/base.dart';
|
||||||
import 'package:moxxmpp/src/managers/namespaces.dart';
|
import 'package:moxxmpp/src/managers/namespaces.dart';
|
||||||
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.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 {
|
class PingManager extends XmppManagerBase {
|
||||||
PingManager(this._pingDuration) : super(pingManager);
|
PingManager() : super(pingManager);
|
||||||
|
|
||||||
/// The time between pings, when connected.
|
|
||||||
final Duration _pingDuration;
|
|
||||||
|
|
||||||
/// The actual timer.
|
|
||||||
Timer? _pingTimer;
|
|
||||||
final Lock _timerLock = Lock();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> isSupported() async => true;
|
Future<bool> isSupported() async => true;
|
||||||
@ -28,35 +15,10 @@ class PingManager extends XmppManagerBase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cancel a potentially scheduled ping timer. Can be overriden to cancel a custom timing mechanism.
|
@override
|
||||||
/// By default, cancels a [Timer.periodic] that was set up prior.
|
Future<void> onXmppEvent(XmppEvent event) async {
|
||||||
@visibleForOverriding
|
if (event is SendPingEvent) {
|
||||||
Future<void> cancelPing() async {
|
logger.finest('Received ping event.');
|
||||||
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 attrs = getAttributes();
|
||||||
final socket = attrs.getSocket();
|
final socket = attrs.getSocket();
|
||||||
|
|
||||||
@ -65,7 +27,8 @@ class PingManager extends XmppManagerBase {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final stream = attrs.getManagerById(smManager) as StreamManagementManager?;
|
final stream =
|
||||||
|
attrs.getManagerById(smManager) as StreamManagementManager?;
|
||||||
if (stream != null) {
|
if (stream != null) {
|
||||||
if (stream
|
if (stream
|
||||||
.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
|
.isStreamManagementEnabled() /*&& stream.getUnackedStanzaCount() > 0*/) {
|
||||||
@ -87,15 +50,5 @@ class PingManager extends XmppManagerBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onXmppEvent(XmppEvent event) async {
|
|
||||||
if (event is ConnectionStateChangedEvent) {
|
|
||||||
if (event.connectionEstablished) {
|
|
||||||
await schedulePing();
|
|
||||||
} else {
|
|
||||||
await cancelPing();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
])
|
])
|
||||||
..setConnectionSettings(
|
..setConnectionSettings(
|
||||||
|
@ -82,6 +82,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager(null, [])),
|
RosterManager(TestingRosterStateManager(null, [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
|
@ -168,6 +168,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
MessageManager(),
|
MessageManager(),
|
||||||
RosterManager(TestingRosterStateManager(null, [])),
|
RosterManager(TestingRosterStateManager(null, [])),
|
||||||
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
connection
|
connection
|
||||||
..registerFeatureNegotiators([
|
..registerFeatureNegotiators([
|
||||||
|
@ -294,6 +294,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
CarbonsManager()..forceEnable(),
|
CarbonsManager()..forceEnable(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
@ -419,6 +420,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
sm,
|
sm,
|
||||||
CarbonsManager()..forceEnable(),
|
CarbonsManager()..forceEnable(),
|
||||||
//EntityCapabilitiesManager('http://moxxmpp.example'),
|
//EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
@ -578,6 +580,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
@ -672,6 +675,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
@ -763,6 +767,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
|
@ -137,6 +137,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
StreamManagementManager(),
|
StreamManagementManager(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
@ -193,6 +194,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([
|
conn.registerFeatureNegotiators([
|
||||||
@ -252,6 +254,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
EntityCapabilitiesManager('http://moxxmpp.example'),
|
EntityCapabilitiesManager('http://moxxmpp.example'),
|
||||||
]);
|
]);
|
||||||
conn.registerFeatureNegotiators([SaslPlainNegotiator()]);
|
conn.registerFeatureNegotiators([SaslPlainNegotiator()]);
|
||||||
@ -406,6 +409,7 @@ void main() {
|
|||||||
PresenceManager(),
|
PresenceManager(),
|
||||||
RosterManager(TestingRosterStateManager('', [])),
|
RosterManager(TestingRosterStateManager('', [])),
|
||||||
DiscoManager([]),
|
DiscoManager([]),
|
||||||
|
PingManager(),
|
||||||
]);
|
]);
|
||||||
conn
|
conn
|
||||||
..registerFeatureNegotiators([
|
..registerFeatureNegotiators([
|
||||||
|
Loading…
Reference in New Issue
Block a user