TM-SGNL-iOS/SignalServiceKit/Network/SgxWebsocketConnection/SgxWebsocketConnectionFactory.swift
TeleMessage developers dde0620daf initial commit
2025-05-03 12:28:28 -07:00

51 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import LibSignalClient
// MARK: -
public protocol SgxWebsocketConnectionFactory {
/// Connect to an SgxClient-conformant server via websocket and perform the initial handshake.
///
/// - Parameters:
/// - queue: The queue to use.
/// - Returns:
/// A Promise for an established connection. If the Promise doesnt
/// resolve to an error, the caller is responsible for ensuring the
/// returned connection is properly disconnected.
func connectAndPerformHandshake<Configurator: SgxWebsocketConfigurator>(
configurator: Configurator,
on scheduler: Scheduler
) -> Promise<SgxWebsocketConnection<Configurator>>
}
final class SgxWebsocketConnectionFactoryImpl: SgxWebsocketConnectionFactory {
private let websocketFactory: WebSocketFactory
public init(websocketFactory: WebSocketFactory) {
self.websocketFactory = websocketFactory
}
func connectAndPerformHandshake<Configurator: SgxWebsocketConfigurator>(
configurator: Configurator,
on scheduler: Scheduler
) -> Promise<SgxWebsocketConnection<Configurator>> {
let websocketFactory = self.websocketFactory
return firstly {
return configurator.fetchAuth()
}.then(on: scheduler) { (auth) -> Promise<SgxWebsocketConnection<Configurator>> in
return try SgxWebsocketConnectionImpl<Configurator>.connectAndPerformHandshake(
configurator: configurator,
auth: auth,
websocketFactory: websocketFactory,
scheduler: scheduler
)
}
}
}