feat: Do initialization inline
This commit is contained in:
parent
d7723615fe
commit
890fcfb506
@ -29,22 +29,33 @@ import 'package:moxxmpp/src/xeps/xep_0352.dart';
|
|||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
/// The states the XmppConnection can be in
|
||||||
enum XmppConnectionState {
|
enum XmppConnectionState {
|
||||||
|
/// The XmppConnection instance is not connected to the server. This is either the
|
||||||
|
/// case before connecting or after disconnecting.
|
||||||
notConnected,
|
notConnected,
|
||||||
|
|
||||||
|
/// We are currently trying to connect to the server.
|
||||||
connecting,
|
connecting,
|
||||||
|
|
||||||
|
/// We are currently connected to the server.
|
||||||
connected,
|
connected,
|
||||||
|
|
||||||
|
/// We have received an unrecoverable error and the server killed the connection
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Metadata for [XmppConnection.sendStanza].
|
||||||
enum StanzaFromType {
|
enum StanzaFromType {
|
||||||
// Add the full JID to the stanza as the from attribute
|
/// Add the full JID to the stanza as the from attribute
|
||||||
full,
|
full,
|
||||||
// Add the bare JID to the stanza as the from attribute
|
/// Add the bare JID to the stanza as the from attribute
|
||||||
bare,
|
bare,
|
||||||
// Add no JID as the from attribute
|
/// Add no JID as the from attribute
|
||||||
none
|
none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Nonza describing the XMPP stream header.
|
||||||
class StreamHeaderNonza extends XMLNode {
|
class StreamHeaderNonza extends XMLNode {
|
||||||
StreamHeaderNonza(String serverDomain) : super(
|
StreamHeaderNonza(String serverDomain) : super(
|
||||||
tag: 'stream:stream',
|
tag: 'stream:stream',
|
||||||
@ -59,6 +70,7 @@ class StreamHeaderNonza extends XMLNode {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The result of an awaited connection.
|
||||||
class XmppConnectionResult {
|
class XmppConnectionResult {
|
||||||
const XmppConnectionResult(
|
const XmppConnectionResult(
|
||||||
this.success,
|
this.success,
|
||||||
@ -67,16 +79,24 @@ class XmppConnectionResult {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// True if the connection was successful. False if it failed for any reason.
|
||||||
final bool success;
|
final bool success;
|
||||||
|
|
||||||
// If a connection attempt fails, i.e. success is false, then this indicates the
|
// If a connection attempt fails, i.e. success is false, then this indicates the
|
||||||
// reason the connection failed.
|
// reason the connection failed.
|
||||||
final XmppError? error;
|
final XmppError? error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A surrogate key for awaiting stanzas.
|
||||||
@immutable
|
@immutable
|
||||||
class _StanzaAwaitableData {
|
class _StanzaAwaitableData {
|
||||||
const _StanzaAwaitableData(this.sentTo, this.id);
|
const _StanzaAwaitableData(this.sentTo, this.id);
|
||||||
|
|
||||||
|
/// The JID the original stanza was sent to. We expect the result to come from the
|
||||||
|
/// same JID.
|
||||||
final String sentTo;
|
final String sentTo;
|
||||||
|
|
||||||
|
/// The ID of the original stanza. We expect the result to have the same ID.
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -90,10 +110,8 @@ class _StanzaAwaitableData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This class is a connection to the server.
|
||||||
class XmppConnection {
|
class XmppConnection {
|
||||||
/// [_socket] is for debugging purposes.
|
|
||||||
/// [connectionPingDuration] is the duration after which a ping will be sent to keep
|
|
||||||
/// the connection open. Defaults to 15 minutes.
|
|
||||||
XmppConnection(
|
XmppConnection(
|
||||||
ReconnectionPolicy reconnectionPolicy,
|
ReconnectionPolicy reconnectionPolicy,
|
||||||
this._socket,
|
this._socket,
|
||||||
@ -101,26 +119,7 @@ class XmppConnection {
|
|||||||
this.connectionPingDuration = const Duration(minutes: 3),
|
this.connectionPingDuration = const Duration(minutes: 3),
|
||||||
this.connectingTimeout = const Duration(minutes: 2),
|
this.connectingTimeout = const Duration(minutes: 2),
|
||||||
}
|
}
|
||||||
) :
|
) : _reconnectionPolicy = reconnectionPolicy {
|
||||||
_connectionState = XmppConnectionState.notConnected,
|
|
||||||
_routingState = RoutingState.preConnection,
|
|
||||||
_eventStreamController = StreamController.broadcast(),
|
|
||||||
_resource = '',
|
|
||||||
_streamBuffer = XmlStreamBuffer(),
|
|
||||||
_uuid = const Uuid(),
|
|
||||||
_awaitingResponse = {},
|
|
||||||
_awaitingResponseLock = Lock(),
|
|
||||||
_xmppManagers = {},
|
|
||||||
_incomingStanzaHandlers = List.empty(growable: true),
|
|
||||||
_incomingPreStanzaHandlers = List.empty(growable: true),
|
|
||||||
_outgoingPreStanzaHandlers = List.empty(growable: true),
|
|
||||||
_outgoingPostStanzaHandlers = List.empty(growable: true),
|
|
||||||
_reconnectionPolicy = reconnectionPolicy,
|
|
||||||
_featureNegotiators = {},
|
|
||||||
_streamFeatures = List.empty(growable: true),
|
|
||||||
_negotiationLock = Lock(),
|
|
||||||
_isAuthenticated = false,
|
|
||||||
_log = Logger('XmppConnection') {
|
|
||||||
// Allow the reconnection policy to perform reconnections by itself
|
// Allow the reconnection policy to perform reconnections by itself
|
||||||
_reconnectionPolicy.register(
|
_reconnectionPolicy.register(
|
||||||
_attemptReconnection,
|
_attemptReconnection,
|
||||||
@ -134,69 +133,82 @@ class XmppConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Connection properties
|
/// The state that the connection is currently in
|
||||||
///
|
XmppConnectionState _connectionState = XmppConnectionState.notConnected;
|
||||||
/// The state that the connection currently is in
|
|
||||||
XmppConnectionState _connectionState;
|
|
||||||
/// The socket that we are using for the connection and its data stream
|
/// The socket that we are using for the connection and its data stream
|
||||||
final BaseSocketWrapper _socket;
|
final BaseSocketWrapper _socket;
|
||||||
|
|
||||||
|
/// The data stream of the socket
|
||||||
late final Stream<String> _socketStream;
|
late final Stream<String> _socketStream;
|
||||||
/// Account settings
|
|
||||||
|
|
||||||
|
/// Connection settings
|
||||||
late ConnectionSettings _connectionSettings;
|
late ConnectionSettings _connectionSettings;
|
||||||
|
|
||||||
/// A policy on how to reconnect
|
/// A policy on how to reconnect
|
||||||
final ReconnectionPolicy _reconnectionPolicy;
|
final ReconnectionPolicy _reconnectionPolicy;
|
||||||
/// A list of stanzas we are tracking with its corresponding critical section
|
|
||||||
final Map<_StanzaAwaitableData, Completer<XMLNode>> _awaitingResponse;
|
/// A list of stanzas we are tracking with its corresponding critical section lock
|
||||||
final Lock _awaitingResponseLock;
|
final Map<_StanzaAwaitableData, Completer<XMLNode>> _awaitingResponse = {};
|
||||||
|
final Lock _awaitingResponseLock = Lock();
|
||||||
|
|
||||||
/// Helpers
|
/// Sorted list of handlers that we call or incoming and outgoing stanzas
|
||||||
///
|
final List<StanzaHandler> _incomingStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _incomingStanzaHandlers;
|
final List<StanzaHandler> _incomingPreStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _incomingPreStanzaHandlers;
|
final List<StanzaHandler> _outgoingPreStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _outgoingPreStanzaHandlers;
|
final List<StanzaHandler> _outgoingPostStanzaHandlers = List.empty(growable: true);
|
||||||
final List<StanzaHandler> _outgoingPostStanzaHandlers;
|
final StreamController<XmppEvent> _eventStreamController = StreamController.broadcast();
|
||||||
final StreamController<XmppEvent> _eventStreamController;
|
final Map<String, XmppManagerBase> _xmppManagers = {};
|
||||||
final Map<String, XmppManagerBase> _xmppManagers;
|
|
||||||
|
|
||||||
/// Stream properties
|
|
||||||
///
|
|
||||||
/// Disco info we got after binding a resource (xmlns)
|
/// Disco info we got after binding a resource (xmlns)
|
||||||
final List<String> _serverFeatures = List.empty(growable: true);
|
final List<String> _serverFeatures = List.empty(growable: true);
|
||||||
|
|
||||||
/// The buffer object to keep split up stanzas together
|
/// The buffer object to keep split up stanzas together
|
||||||
final XmlStreamBuffer _streamBuffer;
|
final XmlStreamBuffer _streamBuffer = XmlStreamBuffer();
|
||||||
|
|
||||||
/// UUID object to generate stanza and origin IDs
|
/// UUID object to generate stanza and origin IDs
|
||||||
final Uuid _uuid;
|
final Uuid _uuid = const Uuid();
|
||||||
|
|
||||||
/// The time between sending a ping to keep the connection open
|
/// 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
|
// TODO(Unknown): Only start the timer if we did not send a stanza after n seconds
|
||||||
final Duration connectionPingDuration;
|
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;
|
||||||
|
|
||||||
/// The current state of the connection handling state machine.
|
/// The current state of the connection handling state machine.
|
||||||
RoutingState _routingState;
|
RoutingState _routingState = RoutingState.preConnection;
|
||||||
|
|
||||||
/// The currently bound resource or '' if none has been bound yet.
|
/// The currently bound resource or '' if none has been bound yet.
|
||||||
String _resource;
|
String _resource = '';
|
||||||
|
|
||||||
/// True if we are authenticated. False if not.
|
/// True if we are authenticated. False if not.
|
||||||
bool _isAuthenticated;
|
bool _isAuthenticated = false;
|
||||||
|
|
||||||
/// Timer for the keep-alive ping.
|
/// Timer for the keep-alive ping.
|
||||||
Timer? _connectionPingTimer;
|
Timer? _connectionPingTimer;
|
||||||
|
|
||||||
/// Timer for the connecting timeout
|
/// Timer for the connecting timeout
|
||||||
Timer? _connectingTimeoutTimer;
|
Timer? _connectingTimeoutTimer;
|
||||||
|
|
||||||
/// Completers for certain actions
|
/// Completers for certain actions
|
||||||
// ignore: use_late_for_private_fields_and_variables
|
// ignore: use_late_for_private_fields_and_variables
|
||||||
Completer<XmppConnectionResult>? _connectionCompleter;
|
Completer<XmppConnectionResult>? _connectionCompleter;
|
||||||
|
|
||||||
/// Controls whether an XmppSocketClosureEvent triggers a reconnection.
|
/// Controls whether an XmppSocketClosureEvent triggers a reconnection.
|
||||||
bool _socketClosureTriggersReconnect = true;
|
bool _socketClosureTriggersReconnect = true;
|
||||||
|
|
||||||
/// Negotiators
|
/// Negotiators
|
||||||
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators;
|
final Map<String, XmppFeatureNegotiatorBase> _featureNegotiators = {};
|
||||||
XmppFeatureNegotiatorBase? _currentNegotiator;
|
XmppFeatureNegotiatorBase? _currentNegotiator;
|
||||||
final List<XMLNode> _streamFeatures;
|
final List<XMLNode> _streamFeatures = List.empty(growable: true);
|
||||||
/// Prevent data from being passed to _currentNegotiator.negotiator while the negotiator
|
/// Prevent data from being passed to _currentNegotiator.negotiator while the negotiator
|
||||||
/// is still running.
|
/// is still running.
|
||||||
final Lock _negotiationLock;
|
final Lock _negotiationLock = Lock();
|
||||||
|
|
||||||
/// Misc
|
/// Misc
|
||||||
final Logger _log;
|
final Logger _log = Logger('XmppConnection');
|
||||||
|
|
||||||
ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy;
|
ReconnectionPolicy get reconnectionPolicy => _reconnectionPolicy;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user