Snippets
Created by
Mikael Eiman
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | //
// main.swift
// SyncThing-Protocol
//
// Created by Mikael Eiman on 2023-07-10.
//
import Foundation
import Network
print("Hello, World!")
enum SyncThingError : Error {
case badMagic
}
var listener: NWListener?
var connections: [NWConnection] = []
do {
// 21027
let udp = NWParameters.udp
udp.allowLocalEndpointReuse = true
udp.allowFastOpen = true
listener = try NWListener(using: udp, on: 21027)
} catch {
print("exception upon creating listener")
exit(1)
}
listener?.stateUpdateHandler = {(newState) in
switch newState {
case .ready:
print("Listener ready")
default:
print("Something else")
break
}
}
func receive(on connection: NWConnection) {
print("receive(): preparing callback")
connection.receiveMessage { (data, context, isComplete, error) in
print("receive callback")
if !isComplete {
print("Not Complete")
}
if let error = error {
print("Error in receive: - ",error)
return
}
if let data = data, !data.isEmpty {
print("!!! Data received: \(data.count) bytes")
do {
// verify magic bytes
let rawInt = data[0..<4]
let magic = rawInt.reduce(0) { Int($0) << 8 + Int($1) }
print("Magic is \(magic)")
if magic != 0x2EA7D90B {
throw SyncThingError.badMagic
}
// parse protobuf
let decoded = try Announce(serializedData: data.dropFirst(4))
print("Announce:")
print("Instance ID: \(decoded.instanceID)")
print("ID as base64: \(decoded.id.base64EncodedString())")
print("Addresses: \(decoded.addresses)")
}
catch let err {
print("An error: \(err)")
}
}
// prepare another receive
receive(on: connection)
}
}
listener?.newConnectionHandler = {(newConnection) in
print("Got a new connection, preparing...")
connections.append(newConnection)
newConnection.stateUpdateHandler = {newState in
print("Connection has new state: \(newState)")
switch newState {
case .ready:
print("Connection ready")
receive(on: newConnection)
return
default:
break
}
}
//newConnection.start(queue: DispatchQueue(label: "newconn"))
newConnection.start(queue: .main)
print("Started new connection")
listener?.cancel()
print("Stopped listening for further connections")
}
listener?.start(queue: .main)
//
/*
let connection = NWConnection(host: "255.255.255.255", port: 21027, using: .udp)
connection.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
print("ready")
//self.send()
//self.receive()
connection.receiveMessage { (data, context, isComplete, error) in
print("Got it")
}
case .setup:
print("setup")
case .cancelled:
print("cancelled")
case .preparing:
print("Preparing")
default:
print("waiting or failed")
}
}
connection.start(queue: .global())
*/
/*
import Foundation
import Network
import Combine
class UDPListener: ObservableObject {
var listener: NWListener?
var connection: NWConnection?
var queue = DispatchQueue.global(qos: .userInitiated)
/// New data will be place in this variable to be received by observers
@Published private(set) public var messageReceived: Data?
/// When there is an active listening NWConnection this will be `true`
@Published private(set) public var isReady: Bool = false
/// Default value `true`, this will become false if the UDPListener ceases listening for any reason
@Published public var listening: Bool = true
/// A convenience init using Int instead of NWEndpoint.Port
convenience init(on port: Int) {
self.init(on: NWEndpoint.Port(integerLiteral: NWEndpoint.Port.IntegerLiteralType(port)))
}
/// Use this init or the one that takes an Int to start the listener
init(on port: NWEndpoint.Port) {
let params = NWParameters.udp
params.allowFastOpen = true
self.listener = try? NWListener(using: params, on: port)
self.listener?.stateUpdateHandler = { update in
switch update {
case .ready:
self.isReady = true
case .failed, .cancelled:
// Announce we are no longer able to listen
self.listening = false
self.isReady = false
default:
()
}
}
self.listener?.newConnectionHandler = { connection in
self.createConnection(connection: connection)
}
self.listener?.start(queue: self.queue)
}
private func createConnection(connection: NWConnection) {
self.connection = connection
self.connection?.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
self.receive()
case .cancelled, .failed:
// Cancel the listener, something went wrong
self.listener?.cancel()
// Announce we are no longer able to listen
self.listening = false
default:
()
}
}
self.connection?.start(queue: .global())
}
func receive() {
self.connection?.receiveMessage { data, context, isComplete, error in
if error != nil {
return
}
guard isComplete, let data = data else {
return
}
self.messageReceived = data
print("Rx data size = \(data.count)")
if self.listening {
self.receive()
}
}
}
func cancel() {
self.listening = false
self.connection?.cancel()
}
}
let listener = UDPListener(on: 21027)
*/
// Do nothing for a while to see if we get any data
try await Task.sleep(nanoseconds: 63_000_000_000)
print("End of line")
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.