setPeer method
Implementation
@override
Future<void> setPeer(PeerId peerId) async {
if (_peerScopeImpl != null) {
throw Exception('$name: connection scope already attached to a peer: ${_peerScopeImpl!.name}');
}
// 1. Get PeerScope from the ResourceManager.
// Note: _rcmgr._getPeerScope is not public, but ConnectionScopeImpl is in the same library.
// A cleaner way might be for ResourceManagerImpl to expose a method like `internalGetPeerScope`.
// For now, direct access is assumed as they are tightly coupled.
final newPeerScope = _rcmgr.getPeerScopeInternal(peerId);
// 2. Identify original transient scope and get the global system scope.
// ConnectionScopeImpl is initially parented only by the transient scope.
if (edges.isEmpty || edges[0] is! TransientScopeImpl) {
throw StateError('$name: Expected initial parent to be TransientScopeImpl.');
}
// Get the system scope from the resource manager.
// This assumes _rcmgr.systemScope provides the correct SystemScopeImpl instance.
final systemScope = _rcmgr.systemScope;
// 3. Move the connection from the transient scope to the peer scope.
// reparent() counts it in the peer scope and releases it from the
// transient scope; the system scope, an ancestor before and after,
// keeps counting it once. On a limit error nothing changes and the
// caller (the network layer) closes the connection.
reparent([newPeerScope, systemScope]);
_peerScopeImpl = newPeerScope;
}