- 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
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
import PlacCore
import SwiftUI
enum DiscoveryResult {
case loading
case loaded([UnsafeMutablePointer<PlacCore.plac_server>])
case failed(PlacCore.plac_scan_result_code)
case nilscanner
}
struct ServerDiscoveryScene: Scene {
@Environment(\.scenePhase) private var scenePhase
@State private var discovery: DiscoveryResult = .loading
var body: some Scene {
WindowGroup {
VStack {
switch discovery {
case .loading:
Text("Scanning Roon Server on network...")
case .loaded(let serverPtrs):
List {
ForEach(serverPtrs, id: \.pointee.id) { ptr in
Text(String.init(cString: ptr.pointee.name))
}
}
case .failed(let plac_scan_result_code):
switch plac_scan_result_code {
case PlacCore.PLAC_SCAN_NETWORK_UNAVAILABLE:
Text("Network unavailable")
case PlacCore.PLAC_SCAN_UNKNOWN_ERROR:
Text("Unknown error")
case PlacCore.PLAC_SCAN_SOCKET_PERMISSION_ERROR:
Text("Failed to open socket (permission error)")
case PlacCore.PLAC_SCAN_SOCKET_SETUP_ERROR:
Text("Failed to open socket")
case PlacCore.PLAC_SCAN_INVALID_RECEIVE_WINDOW:
Text("Invalid receiving window duration")
case PlacCore.PLAC_SCAN_OK:
Text("OK (unreachable)")
case PlacCore.PLAC_SCAN_OUT_OF_MEMORY:
Text("Out of memory")
case PlacCore.PLAC_SCAN_UDP_RECV_ERROR:
Text("Unabled to receive UDP message")
case PlacCore.PLAC_SCAN_UDP_SEND_ERROR:
Text("Unable to send UDP message")
case PlacCore.PLAC_SCAN_TOO_MANY_SOCKET_ERROR:
Text("Failed to open socket (too many socket)")
case PlacCore.PLAC_SCAN_NULL_POINTER_ARGS:
Text("Failed to setup scanner or options")
default:
Text("Unexpected error (platform misbehaving)")
}
case .nilscanner:
Text("Failed to scan")
}
}
}
.onChange(of: scenePhase, initial: true) {
if scenePhase == .active {
let thread = Thread {
switch discovery {
case .loaded(let prevPointers):
for pointer in prevPointers {
PlacCore.plac_server_free(pointer)
}
default:
break
}
discovery = .loading
var opts = PlacCore.plac_server_scan_options()
PlacCore.plac_server_scan_options_init(&opts)
let scanner = PlacCore.plac_server_scanner_make()
if let result = PlacCore.plac_server_scanner_scan(
scanner, &opts)
{
var servers:
[UnsafeMutablePointer<PlacCore.plac_server>] =
[]
while true {
if let next =
PlacCore.plac_scan_result_next(
result)
{
servers.append(next)
} else {
break
}
}
if result.pointee.code == PlacCore.PLAC_SCAN_OK {
discovery = .loaded(servers)
} else {
discovery = .failed(result.pointee.code)
}
PlacCore.plac_scan_result_free(result)
} else {
discovery = .nilscanner
}
PlacCore.plac_server_scanner_free(scanner)
}
thread.start()
}
}
}
}