Skip to content

Commit

Permalink
fix: unacked stanza reading and writing problem (unhandled exception)…
Browse files Browse the repository at this point in the history
… when there is a `resource` change(s)
  • Loading branch information
vsevex committed Aug 23, 2024
1 parent 58b750f commit a8cbf74
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
18 changes: 7 additions & 11 deletions lib/src/database/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ class HiveController {
/// path.
static Future<void> initialize([String? path]) async {
if (_smBox != null) {
Log.instance.warning(
'Tried to reinitialize sm box, but it is already initialized',
);
Log.instance.warning('Tried to reinitialize sm box');
}
if (_unackeds != null) {
Log.instance.warning(
'Tried to reinitialize unacked stanzas box, but it is already initialized',
);
Log.instance.warning('Tried to reinitialize unacked stanzas box');
}
_smBox = await Hive.openBox<Map<dynamic, dynamic>>('sm', path: path);
_unackeds = await Hive.openBox<String>('unacked', path: path);
Expand All @@ -38,17 +34,17 @@ class HiveController {
/// Writes a key-value pair to the storage box.
///
/// [jid] is the key associated with the provided [state].
static Future<void> writeToSMBox(String jid, Map<String, dynamic> state) =>
_smBox!.put(jid, state);
static Future<void>? writeToSMBox(String jid, Map<String, dynamic> state) =>
_smBox?.put(jid, state);

/// Writes a key-value pair to the storage box.
///
/// [sequence] is the key associated with the provided unacked [stanza].
static Future<void> writeUnackeds(int sequence, Stanza stanza) =>
_unackeds!.put(sequence, stanza.toXMLString());
static Future<void>? writeUnackeds(int sequence, Stanza stanza) =>
_unackeds?.put(sequence, stanza.toXMLString());

/// All available key-value pairs for lcaolly saved unacked stanzas.
static Map<dynamic, String> get unackeds => _unackeds!.toMap();
static Map<dynamic, String>? get unackeds => _unackeds?.toMap();

/// Pops from the unacked stanzas list.
static String? popUnacked() {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/plugins/mechanisms/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class FeatureMechanisms {
late final bool _unencryptedPlain;
late final bool _unencryptedScram;

final mechanisms = <String>[];
final mechanisms = <String>{};

late final attemptedMechanisms = <String>[];
final attemptedMechanisms = <String>{};
late Mechanism _mech;

void pluginInitialize() {
Expand Down Expand Up @@ -134,7 +134,7 @@ class FeatureMechanisms {
bool handleSASLAuth(Packet features) {
if (StreamFeatures.supported.contains('mechanisms')) return false;
if (features is! StreamFeatures) return false;
mechanisms.addAll(features.mechanisms?.list ?? <String>[]);
mechanisms.addAll(features.mechanisms?.list ?? <String>{});

return _sendAuthentication();
}
Expand Down
10 changes: 8 additions & 2 deletions lib/src/plugins/mechanisms/mechanisms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SASLMechanisms {
SASLMechanisms();

/// List of supported mechanisms.
final list = <String>[];
final list = <String>{};

/// Constructs a [SASLMechanisms] instance from XML.
factory SASLMechanisms.fromXML(xml.XmlElement node) {
Expand All @@ -37,7 +37,13 @@ class SASLMechanisms {
);

for (final mech in list) {
element.children.add(xml.XmlText(mech).copy());
element.children.add(
xml.XmlElement(
xml.XmlName('mechanism'),
[],
[xml.XmlText(mech).copy()],
),
);
}

return element;
Expand Down
7 changes: 3 additions & 4 deletions lib/src/plugins/sm/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ class StreamManagement {
return await HiveController.readFromSMBox(jid!);
}

static Future<void> saveUnackedToLocal(int sequence, Stanza stanza) {
return HiveController.writeUnackeds(sequence, stanza);
}
static Future<void>? saveUnackedToLocal(int sequence, Stanza stanza) =>
HiveController.writeUnackeds(sequence, stanza);

static Map<dynamic, String> get unackeds => HiveController.unackeds;
static Map<dynamic, String>? get unackeds => HiveController.unackeds;

static String? popFromUnackeds() => HiveController.popUnacked();

Expand Down
4 changes: 4 additions & 0 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class Session {

int numAcked = ((packet.h ?? 0) - (state?.lastAck ?? 0)) % _seq;
final unackeds = StreamManagement.unackeds;
if (unackeds == null) {
state = state?.copyWith(lastAck: packet.h);
return saveSMState(full, state);
}

Log.instance.warning(
'Acked: ${packet.h}, Last acked: ${state?.lastAck}, Unacked: ${unackeds.length}, Num acked: $numAcked, Remaining: ${unackeds.length - numAcked}',
Expand Down

0 comments on commit a8cbf74

Please sign in to comment.