blob: 065bf919822963c0e52089a00eda5f92f3cf6aa0 (
plain) (
blame)
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
|
//
// NetADSBDecoder.swift
// Net1090
//
// Created by Jacky Jack on 19/07/2024.
//
import Foundation
import Network
import NIO
class ADSBHandlder: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
typealias OutboundOut = ByteBuffer
func channelActive(context: ChannelHandlerContext) {
print("Channel is active")
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buffer = unwrapInboundIn(data)
let readableBytes = buffer.readableBytes
if let received = buffer.readString(length: readableBytes) {
print(received,terminator: "")
}
}
func errorCaught(context: ChannelHandlerContext, error: any Error) {
print("error: \(error.localizedDescription)")
context.close(promise: nil)
}
}
class NetADSBDecoder {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var host: String
var port: Int
init(host: String, port: Int) {
self.host = host
self.port = port
}
func start() throws {
do {
let channel = try ClientBootstrap(group: group)
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer{channel in
//channel.pipeline.add(handler: ADSBHandlder())
channel.pipeline.addHandlers([ADSBHandlder()])
}.connect(host: self.host, port: self.port)
.wait()
try channel.closeFuture.wait()
} catch let error {
print(error)
throw error
}
}
func stop() {
do {
try group.syncShutdownGracefully()
} catch let error {
print("Error shutting down \(error.localizedDescription)")
exit(0)
}
print("Connection closed")
}
}
|