iOS 15, macOS 12부터 QUIC이 사용 가능하다
Evolution of HTTP
HTTP/1
HTTP/2
HTTP/3
커넥션 설정 시간 감소 → QUIC으로 달성
각 스트림이 독립적으로 동작 → 한 스트림의 패킷 유실이 다른 스트림에 영향을 미치지 않는다.
QUIC: IETF에 의해 표준화 된 전송 프로토콜
Using HTTP/3
Using QUIC(커스텀 네트워크 프로토콜이 QUIC을 사용하도록 하는 것)
let connection = NWConnection(host: "example.com", port: 443, using: .quic(alpn: ["myproto"]))
connection.stateUpdateHandler = { newState in
switch newState {
case .ready:
print("Connected using QUIC!")
default:
break
}
}
// 상태 업데이트가 실행될 디스패치큐
connection.start(queue: queue)
connection.send(content: data, completion: .contentProcessed { (error) in
// error가 있다면 처리
// 다음번 send를 스케쥴
})
connection.receive(minimumIncompleteLength: 1, maximumlength: desiredLength) {
(receivedContent, context, isComplete, receivedError) in
// 데이터 및 에러 처리
// 다음번 receive를 스케쥴
}
NWMultiplexGroup 추가 - 스트림을 그룹화 하기 위함
let descriptor = NWMultiplexGroup(to: .hostPort(host: "example.com", port: 443)
let group = NWConnectionGroup(with: descriptor, using: .quic(alpn: ["myproto"]))
group.stateUpdateHandler = { newState in
switch newState {
case .ready:
print("Connected using QUIC!")
default:
break
}
}
group.start(queue: queue)
/// outgoing stream 생성
let connection = NWConnection(from: group)
// 새로운 커넥션이 생겼을 때의 처리
group.newConnectionHandler = { newConneciton in
newConnection.stateUpdateHandler = { newState in
}
newConnection.start(queue: queue)
}
let options = NWProtocolQUIC.Options()
options.initialMaxStreamsBidirectional = 100
let options = NWProtocolQUIC.Options()
options.direction = .unidirectional
let connection = NWConnection(from: group, using: options)
listener.newConnectionGroupHandler = { group in
group.stateUpdateHandler = { newState in
}
group.newConnectionHandler = { stream in
}
group.start(queue: queue)
}
if let metadata = connection.metadata(definition: NWProtocolQUIC.definition)
as? NWProtocolQUIC.Metadata {
print("QUIC Stream ID is \\(metadata.streamIdentifier)")
// 시간이 지난 후
metadata.applicationError = 0x100
connection.cancel()
}