11 C
New York
Monday, April 7, 2025

ios – find out how to register listener to `NWConnectionGroup` for QUIC


I’m making an attempt to make http3 consumer with Community.framework on Apple platforms.

Codes that implement NWConnectionGroup.begin with NWListener do not all the time work with warning beneath.
I assume NWConnectionGroup.newConnectionHandler or NWListener.newConnectionHandler will likely be known as to start out connection from the server if it really works.

nw_protocol_instance_add_new_flow [C1.1.1:2] No listener registered, can not settle for new circulation
quic_stream_add_new_flow [C1.1.1:2] [-fde1594b83caa9b7] didn't create new stream for obtained stream id 3

so I attempted:

  • create the NWListener -> not work
  • examine whether or not NWConnectionGroup has a member to register or not NWListener -> not work (it would not have).
  • use NWConnection as an alternative of NWConnectionGroup -> not work

How do I do to set or affiliate listener with NWConnection/Group for newConnectionHandler known as and to delete wanings?

What’s the finest follow within the case?

Pattern codes are beneath.

Thanks prematurely.

// http3 wants unidirectional stream by the server and consumer. 

// listener
personal let _listener: NWListener

let choice: NWProtocolQUIC.Choices = .init(alpn:["h3"])
let param: NWParameters = .init(quic: choice)

_listener = attempt! .init(utilizing: 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
personal let _group: NWConnectionGroup

let choices: NWProtocolQUIC.Choices = .init(alpn: ["h3"])
choices.route = .unidirectional
choices.isDatagram = false
choices.maxDatagramFrameSize = 65535

sec_protocol_options_set_verify_block(choices.securityProtocolOptions, {(_: sec_protocol_metadata_t, _: sec_trust_t, completion: @escaping sec_protocol_verify_complete_t) in
    print("cert completion.")
    completion(true)
}, .world())

let params: NWParameters = .init(quic: choices)

let group: NWMultiplexGroup = .init(
    to: .hostPort(host: NWEndpoint.Host("google.com"),
                  port: NWEndpoint.Port(String(443))!))

_group = .init(with: group, utilizing: params)

_group.setReceiveHandler {message,content material,isComplete in
    print("obtain: (message)")
}
_group.newConnectionHandler = {newConnection in
    print("newConnectionHandler: (newConnection.state)")
}
_group.stateUpdateHandler = { state in
    print("state: (state)")
}
_group.begin(queue: .world())

_listener.begin(queue: .world())

if let conn = _group.extract() {
    let knowledge: Knowledge = .init()
    let _ = _group.reinsert(connection: conn)
    conn.ship(content material: knowledge, completion: .idempotent)
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles