80 lines
2.7 KiB
Swift
80 lines
2.7 KiB
Swift
//
|
|
// GeminiConnection.swift
|
|
// Gemini
|
|
//
|
|
// Created by Shadowfacts on 7/12/20.
|
|
//
|
|
|
|
import Foundation
|
|
import Network
|
|
|
|
public protocol GeminiConnectionDelegate: class {
|
|
func connectionReady(_ connection: GeminiConnection)
|
|
func connection(_ connection: GeminiConnection, receivedData data: Data?, header: GeminiResponseHeader)
|
|
func connection(_ connection: GeminiConnection, handleError error: GeminiConnection.Error)
|
|
func connectionCompleted(_ connection: GeminiConnection)
|
|
}
|
|
|
|
public class GeminiConnection {
|
|
|
|
public typealias Error = NWError
|
|
|
|
public weak var delegate: GeminiConnectionDelegate?
|
|
|
|
private let connection: NWConnection
|
|
|
|
public init(endpoint: NWEndpoint, delegate: GeminiConnectionDelegate? = nil) {
|
|
self.connection = NWConnection(to: endpoint, using: .gemini)
|
|
self.delegate = delegate
|
|
}
|
|
|
|
public func startConnection() {
|
|
connection.stateUpdateHandler = { (newState) in
|
|
switch newState {
|
|
case .ready:
|
|
print("\(self.connection) established")
|
|
self.delegate?.connectionReady(self)
|
|
case let .failed(error):
|
|
print("\(self.connection) failed: \(error)")
|
|
self.connection.cancel()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
// todo: should this be on a background queue?
|
|
connection.start(queue: .main)
|
|
}
|
|
|
|
public func cancelConnection() {
|
|
if connection.state != .cancelled {
|
|
connection.cancel()
|
|
}
|
|
}
|
|
|
|
public func sendRequest(_ request: GeminiRequest) {
|
|
let message = NWProtocolFramer.Message(geminiRequest: request)
|
|
let context = NWConnection.ContentContext(identifier: "GeminiRequest", metadata: [message])
|
|
// todo: should this really always be idempotent?
|
|
connection.send(content: nil, contentContext: context, isComplete: true, completion: .contentProcessed({ (_) in }))
|
|
receiveNextMessage()
|
|
}
|
|
|
|
private func receiveNextMessage() {
|
|
connection.receiveMessage { (data, context, isComplete, error) in
|
|
if let error = error {
|
|
self.delegate?.connection(self, handleError: error)
|
|
} else if let message = context?.protocolMetadata(definition: GeminiProtocol.definition) as? NWProtocolFramer.Message,
|
|
let header = message.geminiResponseHeader,
|
|
let delegate = self.delegate {
|
|
delegate.connection(self, receivedData: data, header: header)
|
|
}
|
|
|
|
guard isComplete else { fatalError("Connection should complete immediately") }
|
|
self.delegate?.connectionCompleted(self)
|
|
self.connection.cancel()
|
|
}
|
|
}
|
|
|
|
}
|