Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Shutdown network in Session.dispose
Browse files Browse the repository at this point in the history
And rename the dispose function to asyncClose. Also rename the
shutdownNetworkAndClose function to syncClose.
  • Loading branch information
inetic committed Sep 20, 2023
1 parent 2241975 commit 7b4b371
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
12 changes: 6 additions & 6 deletions lib/bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ class Bindings {
/// # Safety
///
/// `session` must be a valid session handle.
void session_destroy(
void session_close(
int session,
) {
return _session_destroy(
return _session_close(
session,
);
}

late final _session_destroyPtr =
late final _session_closePtr =
_lookup<ffi.NativeFunction<ffi.Void Function(SessionHandle)>>(
'session_destroy');
late final _session_destroy =
_session_destroyPtr.asFunction<void Function(int)>();
'session_close');
late final _session_close =
_session_closePtr.asFunction<void Function(int)>();

/// # Safety
///
Expand Down
2 changes: 1 addition & 1 deletion lib/native_channels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class NativeChannels {
session = null;

if (s != null) {
s.shutdownNetworkAndClose();
s.syncClose();
}

return;
Expand Down
56 changes: 39 additions & 17 deletions lib/ouisync_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ typedef ErrorCode = b.ErrorCode;
/// Entry point to the ouisync bindings. A session should be opened at the start of the application
/// and closed at the end. There can be only one session at the time.
class Session {
final int handle;
int _handle;
final Client client;
final Subscription _networkSubscription;
String? _mountPoint;

Session._(this.handle, this.client)
int get handle => _handle;

Session._(this._handle, this.client)
: _networkSubscription = Subscription(client, "network", null) {
NativeChannels.session = this;
}
Expand Down Expand Up @@ -164,30 +166,50 @@ class Session {
Future<void> addStorageServer(String host) =>
client.invoke<void>('network_add_storage_server', host);

/// Destroys the session.
Future<void> dispose() async {
if (debugTrace) {
print("Session.dispose");
/// Try to gracefully close connections to peers then close the session.
///
/// Note that this function is idempotent with itself as well as with the
/// `syncClose` function.
Future<void> asyncClose() async {
if (_handle == 0) {
return;
}

final h = _handle;
_handle = 0;

await _networkSubscription.close();

if (handle != 0) {
b.bindings.session_destroy(handle);
NativeChannels.session = null;
}
}
NativeChannels.session = null;

/// Try to gracefully close connections to peers.
Future<void> shutdownNetwork() async {
await client.invoke<void>('network_shutdown');
await _shutdownNetwork();
b.bindings.session_close(h);
}

/// Try to gracefully close connections to peers then close the session.
void shutdownNetworkAndClose() {
if (handle != 0) {
b.bindings.session_shutdown_network_and_close(handle);
/// Async functions don't work reliably when the dart engine is being
/// shutdown (on app exit). In those situations the network needs to be shut
/// down using a blocking call.
///
/// Note that this function is idempotent with itself as well as with the
/// `asyncClose` function.
void syncClose() {
if (_handle == 0) {
return;
}

final h = _handle;
_handle = 0;

await _networkSubscription.close();

NativeChannels.session = null;
b.bindings.session_shutdown_network_and_close(h);
}

/// Try to gracefully close connections to peers.
Future<void> _shutdownNetwork() async {
await client.invoke<void>('network_shutdown');
}
}

Expand Down
2 changes: 1 addition & 1 deletion ouisync
Submodule ouisync updated 1 files
+2 −2 ffi/src/lib.rs

0 comments on commit 7b4b371

Please sign in to comment.