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
237
238
239
240
241
|
import Foundation
import Network
enum HotlineClientStatus: Int {
case disconnected
case connecting
case connected
}
class HotlineClient : ObservableObject {
static let shared = HotlineClient()
static let handshakePacket = Data([
0x54, 0x52, 0x54, 0x50, // 'TRTP' protocol ID
0x48, 0x4F, 0x54, 0x4C, // Sub-protocol ID
0x00, 0x01, // Version
0x00, 0x02, // Sub-version
])
@Published var connectionStatus: HotlineClientStatus = .disconnected
var server: HotlineServer?
var connection: NWConnection?
var bytes = Data()
var handshakeComplete = false
init() {
}
func connect(to server: HotlineServer) {
self.server = server
let serverAddress = NWEndpoint.Host(server.address)
let serverPort = NWEndpoint.Port(rawValue: server.port)!
self.connection = NWConnection(host: serverAddress, port: serverPort, using: .tcp)
self.connection?.stateUpdateHandler = { [weak self] (newState: NWConnection.State) in
switch newState {
case .ready:
print("HotlineClient: connection ready!")
DispatchQueue.main.async {
self?.connectionStatus = .connected
}
self?.sendHandshake()
case .cancelled:
print("HotlineClient: connection cancelled")
DispatchQueue.main.async {
self?.connectionStatus = .disconnected
}
case .failed(let err):
print("HotlineClient: connection error \(err)")
DispatchQueue.main.async {
self?.connectionStatus = .disconnected
}
default:
print("HotlineClient: unhandled connection state \(newState)")
}
}
DispatchQueue.main.async {
self.connectionStatus = .connecting
}
self.connection?.start(queue: .global())
}
private func disconnect() {
guard let c = connection else {
print("HotlineClient: already disconnected")
return
}
c.cancel()
self.connection = nil
}
private func sendHandshake() {
guard let c = connection else {
print("HotlineClient: invalid connection to send handshake.")
return
}
c.send(content: HotlineClient.handshakePacket, completion: .contentProcessed { [weak self] (error) in
if let err = error {
print("HotlineClient: sending magic failed \(err)")
return
}
print("HotlineClient: sent handshake packet!")
self?.receiveHandshake()
})
}
private func receiveHandshake() {
guard let c = connection else {
print("HotlineTracker: invalid connection to receive magic.")
return
}
print("HotlineClient: receiving handshake...")
c.receive(minimumIncompleteLength: 8, maximumLength: 8) { [weak self] (data, context, isComplete, error) in
guard let self = self, let data = data else {
return
}
if data.isEmpty {
print("HotlineClient: empty handshake response")
self.disconnect()
return
}
let protocolID = data.readUInt32(at: 0)!
if protocolID != 0x54525450 { // 'TRTP'
print("HotlineClient: invalid handshake protocol ID \(protocolID)")
self.disconnect()
return
}
let errorCode = data.readUInt32(at: 4)!
if errorCode != 0 { // 0 == no error
print("HotlineClient: handshake error", errorCode)
self.disconnect()
return
}
print("HotlineClient: completed handshake")
self.sendLogin()
}
}
private func sendLogin() {
guard let c = connection else {
print("HotlineClient: no connection for transaction.")
return
}
c.send(content: HotlineClient.handshakePacket, completion: .contentProcessed { [weak self] (error) in
if let err = error {
print("HotlineClient: sending login failed \(err)")
return
}
print("HotlineClient: sent handshake packet!")
})
}
private func receiveTransaction() {
guard let c = connection else {
print("HotlineClient: no connection for transaction.")
return
}
print("HotlineClient: waiting on transaction header...")
c.receive(minimumIncompleteLength: HotlineTransaction.headerSize, maximumLength: HotlineTransaction.headerSize) { [weak self] (data, context, isComplete, error) in
guard let self = self else {
return
}
if let error = error {
print("HotlineClient: receive error \(error)")
self.disconnect()
return
}
if let data = data, !data.isEmpty {
print("HotlineClient: received \(data.count) header bytes")
let transaction = self.parseTransaction(data: data)
if var t = transaction {
if t.dataSize > 0 {
c.receive(minimumIncompleteLength: Int(t.dataSize), maximumLength: Int(t.dataSize)) { [weak self] (data, context, isComplete, error) in
guard let self = self else {
return
}
if let data = data, !data.isEmpty {
t.parameterCount = data.readUInt16(at: 0)!
if t.parameterCount > 0 {
t.parameters = []
var dataCursor = 2
for _ in 0..<t.parameterCount {
if
let fieldID = data.readUInt16(at: dataCursor),
let fieldSize = data.readUInt16(at: dataCursor + 2),
let fieldData = data.readData(at: dataCursor + 4, length: Int(fieldSize)) {
t.parameters?.append(HotlineTransactionParameter(id: fieldID, dataSize: fieldSize, data: fieldData))
dataCursor += 4 + Int(fieldSize)
}
}
}
}
self.processTransaction(t)
}
}
else {
self.processTransaction(t)
}
}
// print("HotlineTracker: server count = \(self.serverCount)")
}
// self.receiveListing()
// }
}
}
private func processTransaction(_ transaction: HotlineTransaction) {
print("HotlineClient processing transaction \(transaction.type) with \(transaction.parameterCount) parameters")
}
private func parseTransaction(data: Data) -> HotlineTransaction? {
if
let flags = data.readUInt8(at: 0),
let isReply = data.readUInt8(at: 1),
let type = data.readUInt16(at: 2),
let id = data.readUInt32(at: 4),
let errorCode = data.readUInt32(at: 8),
let transactionSize = data.readUInt32(at: 12),
let dataSize = data.readUInt32(at: 16) {
return HotlineTransaction(
flags: flags,
isReply: isReply,
type: HotlineTransactionType(rawValue: type) ?? HotlineTransactionType.unknown,
id: id,
errorCode: errorCode,
totalSize: transactionSize,
dataSize: dataSize
)
}
return nil
}
}
|