Eine aufbereitete Darstellung der Quelle

 
     
 
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung


Impressum VoIPIceServerSource.swift   Sprache: JAVA

 

import Foundation
import ThreemaFramework

let MIN_SPARE_VALIDITY: TimeInterval = 3600

struct TurnServerInfo {
    var turnURLs: [String]
    var turnURLsDualStack: [String]
    var turnUsername: String
    var turnPassword: String
    var expirationDate: Date
}

enum TurnServerError: Error {
    case badServerData
}

enum VoIPIceServerSource {
    private static var cachedTurnServers: TurnServerInfo?
    
    static func prefetchIceServers() {
        obtainIceServers(dualStack: false) { _ in
            // ignored
        }
    }
    
    static func obtainIceServers(
        dualStack: Bool,
        completion: @escaping (Swift.Result<RTCIceServer, Error>) -> Void
    ) {
        if cachedTurnServers != nil {
            let minExpiration = Date().addingTimeInterval(MIN_SPARE_VALIDITY)
            if cachedTurnServers!.expirationDate > minExpiration {
                completion(.success(makeIceServers(dualStack: dualStack, turnServerInfo: cachedTurnServers!)))
                return
            }
        }
        
        // No unexpired TURN server info in cache; must fetch
        ServerAPIConnector()
            .obtainTurnServers(with: MyIdentityStore.shared()) { (response: [AnyHashable: Any]?) in
                guard let expiration = response?["expiration"] as? TimeInterval,
                      let turnURLs = response?["turnUrls"] as? [String],
                      let turnURLsDualStack = response?["turnUrlsDualStack"] as? [String],
                      let turnUsername = response?["turnUsername"] as? String,
                      let turnPassword = response?["turnPassword"] as? String else {
                    completion(.failure(TurnServerError.badServerData))
                    return
                }
            
                let expirationDate = Date().addingTimeInterval(expiration)
                cachedTurnServers = TurnServerInfo(
                    turnURLs: turnURLs,
                    turnURLsDualStack: turnURLsDualStack,
                    turnUsername: turnUsername,
                    turnPassword: turnPassword,
                    expirationDate: expirationDate
                )
                completion(.success(makeIceServers(dualStack: dualStack, turnServerInfo: cachedTurnServers!)))
            } onError: { (e: Error?) in
                completion(.failure(e!))
            }
    }
    
    private static func makeIceServers(dualStack: Bool, turnServerInfo: TurnServerInfo) -> RTCIceServer {
        let urls = dualStack ? turnServerInfo.turnURLsDualStack : turnServerInfo.turnURLs
        return RTCIceServer(
            urlStrings: urls,
            username: turnServerInfo.turnUsername,
            credential: turnServerInfo.turnPassword,
            tlsCertPolicy: .secure
        )
    }
}

Messung
CHZG
839310092

¤ Dauer der Verarbeitung: 0.264 Sekunden  ¤

*© Formatika GbR, Deutschland







                                                                                                                                                                                                                                                                                                                                                                                                     

 

Neuigkeiten

Aktuelles
Motto des Tages

Open Source Software


Gedichte
Musik
Bilder

Jenseits des Üblichen

Besucherstatistik

Besucherstatistik

Statistik
#Sources=1,187,826
#Domains=881,162