newStream method
NewStream opens a new stream to given peer p, and writes a p2p/protocol header with given ProtocolID. If there is no connection to p, attempts to create one. If ProtocolID is "", writes no header. (Thread-safe)
If context
is not provided, a new Context will be created.
Implementation
@override
Future<P2PStream> newStream(PeerId p, List<ProtocolID> pids, Context context) async {
final effectiveCtx = context; // context is now non-nullable due to interface
// Use specific getter from Context
bool noDial = effectiveCtx.getNoDial().$1;
if (!noDial) {
// Ensure we have a connection, with peer addresses resolved by the routing system.
// It is not sufficient to let the underlying host connect, it will most likely not have
// any addresses for the peer without any prior connections.
try {
// We only need the ID for connect, addresses will be resolved.
await connect(AddrInfo(p, []), context: effectiveCtx);
} catch (e) {
// _log.warning('Failed to connect to peer $p before opening stream: $e');
rethrow; // Propagate connection error
}
}
// Call with positional context argument
return _host.newStream(p, pids, effectiveCtx);
}