I am trying to make http3 client with Network.framework
on Apple platforms.
Codes that implement NWConnectionGroup.start
with NWListener
don't always work with warning below.
I assume NWConnectionGroup.newConnectionHandler
or NWListener.newConnectionHandler
will be called to start connection from the server if it works.
nw_protocol_instance_add_new_flow [C1.1.1:2] No listener registered, cannot accept new flow
quic_stream_add_new_flow [C1.1.1:2] [-fde1594b83caa9b7] failed to create new stream for received stream id 3
so I tried:
- create the
NWListener
-> not work - check whether
NWConnectionGroup
has a member to register or notNWListener
-> not work (it doesn't have). - use
NWConnection
instead ofNWConnectionGroup
-> not work
Is my understanding correct?
How should I do to set or associate listener with NWConnection/Group
for newConnectionHandler
is called and to delete wanings?
What is the best practice in the case?
Sample codes are below.
Thanks in advance.
// http3 needs unidirectional stream by the server and client.
// listener
private let _listener: NWListener
let option: NWProtocolQUIC.Options = .init(alpn:["h3"])
let param: NWParameters = .init(quic: option)
_listener = try! .init(using: param)
_listener.stateUpdateHandler = { state in
print("listener state: \(state)")
}
_listener.newConnectionHandler = { newConnection in
print("new connection added")
}
_listener.serviceRegistrationUpdateHandler = { registrationState in
print("connection registrationstate")
}
// create connection
private let _group: NWConnectionGroup
let options: NWProtocolQUIC.Options = .init(alpn: ["h3"])
options.direction = .unidirectional
options.isDatagram = false
options.maxDatagramFrameSize = 65535
sec_protocol_options_set_verify_block(options.securityProtocolOptions, {(_: sec_protocol_metadata_t, _: sec_trust_t, completion: @escaping sec_protocol_verify_complete_t) in
print("cert completion.")
completion(true)
}, .global())
let params: NWParameters = .init(quic: options)
let group: NWMultiplexGroup = .init(
to: .hostPort(host: NWEndpoint.Host("google.com"),
port: NWEndpoint.Port(String(443))!))
_group = .init(with: group, using: params)
_group.setReceiveHandler {message,content,isComplete in
print("receive: \(message)")
}
_group.newConnectionHandler = {newConnection in
print("newConnectionHandler: \(newConnection.state)")
}
_group.stateUpdateHandler = { state in
print("state: \(state)")
}
_group.start(queue: .global())
_listener.start(queue: .global())
if let conn = _group.extract() {
let data: Data = .init()
let _ = _group.reinsert(connection: conn)
conn.send(content: data, completion: .idempotent)
}
I think the key point you’re missing here is newConnectionGroupHandler
. If you working with QUIC, you accept connection groups rather than connections, and for that you need to set this callback rather than newConnectionHandler
.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"