hlfw.ca

NineSwift

Download patch

ref: 44ac90449fa714efd31996e60d7e578e02ac91e5
parent: d2a973b953f6ccc33e493c3aa8eeeb4241f6dcc0
author: halfwit <michaelmisch1985@gmail.com>
date: Sat May 18 15:42:52 PDT 2024

Set to public

--- a/Sources/NineSwift/Connection.swift
+++ b/Sources/NineSwift/Connection.swift
@@ -37,7 +37,7 @@
 }
 
 @available(macOS 10.15, *)
-protocol PeerConnectionDelegate: AnyObject {
+public protocol PeerConnectionDelegate: AnyObject {
     func connectionReady()
     func connectionFailed()
     func displayAdvertiseError(_ error: NWError)
@@ -44,10 +44,11 @@
 }
 
 @available(macOS 10.15, *)
-class PeerConnection {
+public class PeerConnection {
     weak var delegate: PeerConnectionDelegate?
     var connection: NWConnection?
     let name: String
+    let endpoint: NWEndpoint
     let initiatedConnection: Bool
     var sendQueue = Queue<Enqueued>()
     var handles: [Handle] = [Handle]()
@@ -55,23 +56,19 @@
     
     /* Connect to a service */
     @available(macOS 10.15, *)
-    init(name: String, delegate: PeerConnectionDelegate) {
+    public init(name: String, delegate: PeerConnectionDelegate, host: NWEndpoint.Host, port: NWEndpoint.Port) {
         self.delegate = delegate
         self.name = name
         self.initiatedConnection = true
-        
-        guard let endpointPort = NWEndpoint.Port("12345") else { return }
-        let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host("localhost"), port: endpointPort)
-        //let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host("127.0.0.1"), port: endpointPort)
-        //let endpoint = NWEndpoint.service(name: name, type: "_altid._tcp.", domain: "local.", interface: nil)
+        self.endpoint = NWEndpoint.hostPort(host: host, port: port)
         connection = NWConnection(to: endpoint, using: applicationServiceParameters())
     }
     
-    func addHandle(handle: Handle) {
+    public func addHandle(handle: Handle) {
         handles.append(handle)
     }
     
-    func cancel() {
+    public func cancel() {
         if let connection = self.connection {
             connection.cancel()
             self.connection = nil
@@ -80,7 +77,7 @@
     
     // Handle starting the peer-to-peer connection for both inbound and outbound connections.
     @available(macOS 10.15, *)
-    func startConnection() {
+    public func startConnection() {
         guard let connection = self.connection else {
             return
         }
@@ -98,10 +95,7 @@
                 if let initiated = self?.initiatedConnection,
                    initiated && error == NWError.posix(.ECONNABORTED) {
                     // Reconnect if the user suspends the app on the nearby device.
-                    guard let endpointPort = NWEndpoint.Port("12345") else { return }
-                    let endpoint = NWEndpoint.hostPort(host: NWEndpoint.Host("192.168.0.248"), port: endpointPort)
-                    //let endpoint = NWEndpoint.service(name: self!.name, type: "_altid._tcp", domain: "local", interface: nil)
-                    let connection = NWConnection(to: endpoint, using: applicationServiceParameters())
+                    let connection = NWConnection(to: self!.endpoint, using: applicationServiceParameters())
                     self?.connection = connection
                     self?.startConnection()
                 } else if let delegate = self?.delegate {
@@ -118,7 +112,7 @@
 
 /* Utility functions */
 @available(macOS 10.15, *)
-extension PeerConnection {
+public extension PeerConnection {
     func connect(uname: String = "") {
         send(Tversion())
         send(Tattach(fid: 0, afid: 0, uname: uname, aname: ""))
--- a/Sources/NineSwift/Handle.swift
+++ b/Sources/NineSwift/Handle.swift
@@ -8,7 +8,7 @@
 import Foundation
 
 /* Handle to an open nine file */
-struct Handle {
+public struct Handle {
     let name: String
     var fid: UInt32
     var iounit: UInt32
--- a/Sources/NineSwift/NineProtocol.swift
+++ b/Sources/NineSwift/NineProtocol.swift
@@ -8,10 +8,10 @@
 import Foundation
 import Network
 
-var MSIZE: UInt32 = 8192
+public var MSIZE: UInt32 = 8192
 let version = "9P2000 ".data(using: .utf8)!
 
-enum NineErrors: Error {
+public enum NineErrors: Error {
     case decodeError
     case unknownType
     case connectError
@@ -18,7 +18,7 @@
     case success
 }
 
-enum nineType: UInt8 {
+public enum nineType: UInt8 {
     case Tversion = 100
     case Tauth = 102
     case Tattach = 104
@@ -49,7 +49,7 @@
     case invalid = 0
 }
 
-enum fileType: UInt8, Codable {
+public enum fileType: UInt8, Codable {
     case dir = 128
     case append = 64
     case excl = 32
@@ -59,7 +59,7 @@
     case file = 0
 }
 
-enum nineMode: UInt8, Codable {
+public enum nineMode: UInt8, Codable {
     case read = 0
     case write = 1
     case rdwr = 2
@@ -68,13 +68,13 @@
     case rclose = 0x40
 }
 
-struct nineQid: Codable {
+public struct nineQid: Codable {
     var type: fileType
     var version: UInt32
     var path: UInt64
 }
 
-struct nineStat: Codable {
+public struct nineStat: Codable {
     var size: UInt16
     var type: UInt16
     var dev: UInt32
@@ -91,17 +91,17 @@
 
 // Main framing protocol
 @available(macOS 10.15, *)
-class NineProtocol: NWProtocolFramerImplementation {
+public class NineProtocol: NWProtocolFramerImplementation {
     static let definition = NWProtocolFramer.Definition(implementation: NineProtocol.self)
-    static var label: String { return "9p" }
+    public static var label: String { return "9p" }
     
-    required init(framer: NWProtocolFramer.Instance) {}
-    func start(framer: NWProtocolFramer.Instance) -> NWProtocolFramer.StartResult { return .ready }
-    func wakeup(framer: NWProtocolFramer.Instance) { print("In wakeup")}
-    func stop(framer: NWProtocolFramer.Instance) -> Bool { print("In stop"); return true }
-    func cleanup(framer: NWProtocolFramer.Instance) { print("In cleanup")}
+    required public init(framer: NWProtocolFramer.Instance) {}
+    public func start(framer: NWProtocolFramer.Instance) -> NWProtocolFramer.StartResult { return .ready }
+    public func wakeup(framer: NWProtocolFramer.Instance) { print("In wakeup")}
+    public func stop(framer: NWProtocolFramer.Instance) -> Bool { print("In stop"); return true }
+    public func cleanup(framer: NWProtocolFramer.Instance) { print("In cleanup")}
     
-    func handleOutput(framer: NWProtocolFramer.Instance, message: NWProtocolFramer.Message, messageLength: Int, isComplete: Bool) {
+    public func handleOutput(framer: NWProtocolFramer.Instance, message: NWProtocolFramer.Message, messageLength: Int, isComplete: Bool) {
         do {
             try framer.writeOutputNoCopy(length: messageLength)
         } catch {
@@ -109,7 +109,7 @@
         }
     }
     
-    func handleInput(framer: NWProtocolFramer.Instance) -> Int {
+    public func handleInput(framer: NWProtocolFramer.Instance) -> Int {
         let headerSize = 7
         var count: UInt32 = 0
         var type: UInt8 = 0
@@ -301,7 +301,7 @@
 }
 
 @available(macOS 10.15, *)
-extension NWProtocolFramer.Message {
+public extension NWProtocolFramer.Message {
     /* Set completions here */
     convenience init(count: UInt32, type: UInt8, tag: UInt16, fid: UInt32 = 0, iounit: UInt32 = 0, qids: [nineQid]? = nil, stat: nineStat? = nil) {
         self.init(definition: NineProtocol.definition)
@@ -379,10 +379,10 @@
 }
 
 @available(macOS 10.15, *)
-struct Tversion: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 13
+public struct Tversion: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 13
     
-    var encodedData: Data {
+    public var encodedData: Data {
         let count: UInt32 = UInt32(13 + version.count)
         var data = Data(count: 0)
         w32(&data, input: count) // length
@@ -393,14 +393,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tversion")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tauth: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 20
+public struct Tauth: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 20
     
     let length: UInt32
     let afid: UInt32
@@ -416,7 +416,7 @@
         self.aname = aname.data(using: .utf8)!
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: length)
         w8(&data, input: nineType.Tauth.rawValue)
@@ -427,14 +427,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tauth")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tattach: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 20
+public struct Tattach: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 20
     
     let length: UInt32
     let fid: UInt32
@@ -452,7 +452,7 @@
         self.aname = aname.data(using: .utf8)!
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: length)
         w8(&data, input: nineType.Tattach.rawValue)
@@ -464,14 +464,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tattach")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tflush: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 7
+public struct Tflush: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 7
     
     let tag: UInt16
     let oldtag: UInt16
@@ -480,7 +480,7 @@
         self.oldtag = oldtag
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 11)
         w8(&data, input: nineType.Tflush.rawValue)
@@ -489,14 +489,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tflush")
     }
 }
 
 @available(macOS 10.15, *)
-struct Twalk: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 22
+public struct Twalk: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 22
     
     let length: UInt32
     let tag: UInt16
@@ -521,7 +521,7 @@
         self.wnames = tmpwnames
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: length)
         w8(&data, input: nineType.Twalk.rawValue)
@@ -535,14 +535,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Twalk")
     }
 }
 
 @available(macOS 10.15, *)
-struct Topen: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 24
+public struct Topen: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 24
     
     let tag: UInt16
     let fid: UInt32
@@ -554,7 +554,7 @@
         self.mode = mode
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 4+1+2+4+1)
         w8(&data, input: nineType.Topen.rawValue)
@@ -564,14 +564,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Topen")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tcreate: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 24
+public struct Tcreate: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 24
     
     let tag: UInt16
     let fid: UInt32
@@ -587,7 +587,7 @@
         self.mode = mode
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: UInt32(name.count + 16))
         w8(&data, input: nineType.Tcreate.rawValue)
@@ -598,14 +598,14 @@
         w8(&data, input: mode)
         return data
     }
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tcreate")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tread: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 13
+public struct Tread: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 13
     
     let tag: UInt16
     let fid: UInt32
@@ -619,7 +619,7 @@
         self.count = count
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 4+1+2+4+8+4)
         w8(&data, input: nineType.Tread.rawValue)
@@ -630,14 +630,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tread")
     }
 }
 
 @available(macOS 10.15, *)
-struct Twrite: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 11
+public struct Twrite: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 11
     
     let tag: UInt16
     let fid: UInt32
@@ -653,7 +653,7 @@
         self.bytes = bytes
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: UInt32(bytes.count + 23))
         w8(&data, input: nineType.Twrite.rawValue)
@@ -664,14 +664,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Twrite")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tclunk: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 7
+public struct Tclunk: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 7
     
     let tag: UInt16
     let fid: UInt32
@@ -680,7 +680,7 @@
         self.fid = fid
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 11)
         w8(&data, input: nineType.Tclunk.rawValue)
@@ -689,14 +689,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tclunk")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tremove: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 7
+public struct Tremove: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 7
     
     let tag: UInt16
     let fid: UInt32
@@ -705,7 +705,7 @@
         self.fid = fid
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 11)
         w8(&data, input: nineType.Tremove.rawValue)
@@ -714,14 +714,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tremove")
     }
 }
 
 @available(macOS 10.15, *)
-struct Tstat: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 52
+public struct Tstat: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 52
     
     let tag: UInt16
     let fid: UInt32
@@ -730,7 +730,7 @@
         self.fid = fid
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         w32(&data, input: 11)
         w8(&data, input: nineType.Tstat.rawValue)
@@ -739,14 +739,14 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Tstat")
     }
 }
 
 @available(macOS 10.15, *)
-struct Twstat: QueueableMessage, Encodable {
-    var minReceiveLength: Int = 7
+public struct Twstat: QueueableMessage, Encodable {
+    public var minReceiveLength: Int = 7
     
     let tag: UInt16
     let fid: UInt32
@@ -758,7 +758,7 @@
         self.stat = stat
     }
     
-    var encodedData: Data {
+    public var encodedData: Data {
         var data = Data(count: 0)
         let length = 52 + stat.name.count + stat.uid.count + stat.gid.count + stat.muid.count
         w32(&data, input: UInt32(length))
@@ -783,7 +783,7 @@
         return data
     }
     
-    var context: NWConnection.ContentContext {
+    public var context: NWConnection.ContentContext {
         return NWConnection.ContentContext(identifier: "Twstat")
     }
 }
@@ -917,7 +917,7 @@
     return nineQid(type: fileType(rawValue: type) ?? .invalid, version: vers, path: path)
 }
 
-extension Data {
+private extension Data {
     public var bytes: [UInt8]
     {
         return [UInt8](self)
@@ -924,7 +924,7 @@
     }
 }
 
-extension UInt8 {
+private extension UInt8 {
     var char: Character {
         return Character(UnicodeScalar(self))
     }
--- a/Sources/NineSwift/Queue.swift
+++ b/Sources/NineSwift/Queue.swift
@@ -7,7 +7,7 @@
 import Foundation
 import Network
 
-struct Queue<T> {
+public struct Queue<T> {
     private var elements: [T] = []
     
     mutating func enqueue(_ value: T) {
@@ -29,7 +29,7 @@
 }
 
 @available(macOS 10.15, *)
-protocol QueueableMessage {
+public protocol QueueableMessage {
     var encodedData: Data {get}
     var minReceiveLength: Int {get}
     var context: NWConnection.ContentContext {get}
@@ -36,7 +36,7 @@
 }
 
 @available(macOS 10.15, *)
-struct Enqueued {
+public struct Enqueued {
     let message: QueueableMessage
     let action: (NWProtocolFramer.Message, Data?, NWError?) -> Void
     init(message: QueueableMessage, action: @escaping (NWProtocolFramer.Message, Data?, NWError?) -> Void) {