Files
umbrix/ios/Runner/Handlers/StatsEventHandler.swift

67 lines
2.3 KiB
Swift
Raw Normal View History

2023-12-27 01:01:37 +03:30
//
// StatsEventHandler.swift
// Runner
//
// Created by Hiddify on 12/27/23.
//
import Foundation
2023-12-27 01:01:56 +03:30
import Flutter
import Libcore
public class StatsEventHandler: NSObject, FlutterPlugin, FlutterStreamHandler, LibboxCommandClientHandlerProtocol {
static let name = "\(Bundle.main.serviceIdentifier)/stats"
private var channel: FlutterEventChannel?
private var commandClient: LibboxCommandClient?
private var events: FlutterEventSink?
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = StatsEventHandler()
2024-01-15 16:48:21 +01:00
instance.channel = FlutterEventChannel(name: Self.name, binaryMessenger: registrar.messenger(), codec: FlutterJSONMethodCodec())
2023-12-27 01:01:56 +03:30
instance.channel?.setStreamHandler(instance)
}
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
FileManager.default.changeCurrentDirectoryPath(FilePath.sharedDirectory.path)
self.events = events
let opts = LibboxCommandClientOptions()
opts.command = LibboxCommandStatus
2024-01-18 23:07:29 +03:30
opts.statusInterval = Int64(NSEC_PER_SEC)
2023-12-27 01:01:56 +03:30
commandClient = LibboxCommandClient(self, options: opts)
try? commandClient?.connect()
return nil
}
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
try? commandClient?.disconnect()
return nil
}
public func writeStatus(_ message: LibboxStatusMessage?) {
2024-01-18 23:07:29 +03:30
guard
2023-12-27 01:01:56 +03:30
let message
else { return }
let data = [
"connections-in": message.connectionsIn,
"connections-out": message.connectionsOut,
"uplink": message.uplink,
"downlink": message.downlink,
"uplink-total": message.uplinkTotal,
"downlink-total": message.downlinkTotal
] as [String:Any]
2024-01-18 23:07:29 +03:30
events?(data)
2023-12-27 01:01:56 +03:30
}
}
extension StatsEventHandler {
public func clearLog() {}
public func connected() {}
public func disconnected(_ message: String?) {}
public func initializeClashMode(_ modeList: LibboxStringIteratorProtocol?, currentMode: String?) {}
public func updateClashMode(_ newMode: String?) {}
public func writeGroups(_ message: LibboxOutboundGroupIteratorProtocol?) {}
public func writeLog(_ message: String?) {}
}