157 lines
3.1 KiB
Swift
157 lines
3.1 KiB
Swift
//
|
||
// SDLPacketType.swift
|
||
// Tun
|
||
//
|
||
// Created by 安礼成 on 2024/4/10.
|
||
//
|
||
|
||
import Foundation
|
||
import NIOCore
|
||
|
||
// 消息类型定义
|
||
enum SDLPacketType: UInt8 {
|
||
case empty = 0x00
|
||
|
||
case registerSuper = 0x01
|
||
case registerSuperAck = 0x02
|
||
case registerSuperNak = 0x04
|
||
|
||
case unregisterSuper = 0x05
|
||
|
||
case queryInfo = 0x06
|
||
case peerInfo = 0x07
|
||
|
||
case ping = 0x08
|
||
case pong = 0x09
|
||
|
||
// 事件类型
|
||
case event = 0x10
|
||
|
||
// 推送命令消息, 需要返回值
|
||
case command = 0x11
|
||
case commandAck = 0x12
|
||
|
||
// 流量统计
|
||
case flowTracer = 0x15
|
||
|
||
case register = 0x20
|
||
case registerAck = 0x21
|
||
|
||
case stunRequest = 0x30
|
||
case stunReply = 0x31
|
||
|
||
case stunProbe = 0x32
|
||
case stunProbeReply = 0x33
|
||
|
||
case data = 0xFF
|
||
}
|
||
|
||
// 升级策略
|
||
enum SDLUpgradeType: UInt32 {
|
||
case none = 0
|
||
case normal = 1
|
||
case force = 2
|
||
}
|
||
|
||
// Id生成器
|
||
struct SDLIdGenerator {
|
||
// 消息体id
|
||
private var packetId: UInt32
|
||
|
||
init(seed packetId: UInt32) {
|
||
self.packetId = packetId
|
||
}
|
||
|
||
mutating func nextId() -> UInt32 {
|
||
let packetId = self.packetId
|
||
self.packetId = packetId + 1
|
||
return packetId
|
||
}
|
||
}
|
||
|
||
// 定义事件类型
|
||
|
||
// 命令类型
|
||
enum SDLEventType: UInt8 {
|
||
case natChanged = 0x03
|
||
case sendRegister = 0x04
|
||
case networkShutdown = 0xFF
|
||
}
|
||
|
||
enum SDLEvent {
|
||
case natChanged(SDLNatChangedEvent)
|
||
case sendRegister(SDLSendRegisterEvent)
|
||
case networkShutdown(SDLNetworkShutdownEvent)
|
||
}
|
||
|
||
// --MARK: 定义命令类型
|
||
|
||
enum SDLCommandType: UInt8 {
|
||
case changeNetwork = 0x01
|
||
}
|
||
|
||
enum SDLCommand {
|
||
case changeNetwork(SDLChangeNetworkCommand)
|
||
}
|
||
|
||
// --MARK: 网络类型探测
|
||
// 探测的Attr属性
|
||
enum SDLProbeAttr: UInt8 {
|
||
case none = 0
|
||
case port = 1
|
||
case peer = 2
|
||
}
|
||
|
||
// Nak的错误类型,不同的错误客户端的处理逻辑不一样
|
||
enum SDLNAKErrorCode: UInt8 {
|
||
case invalidToken = 1
|
||
case nodeDisabled = 2
|
||
case noIpAddress = 3
|
||
case networkFault = 4
|
||
case internalFault = 5
|
||
}
|
||
|
||
extension SDLV4Info {
|
||
func socketAddress() -> SocketAddress? {
|
||
let address = "\(v4[0]).\(v4[1]).\(v4[2]).\(v4[3])"
|
||
|
||
return try? SocketAddress.makeAddressResolvingHost(address, port: Int(port))
|
||
}
|
||
}
|
||
|
||
extension SDLStunProbeReply {
|
||
func socketAddress() -> SocketAddress? {
|
||
let address = SDLUtil.int32ToIp(self.ip)
|
||
|
||
return try? SocketAddress.makeAddressResolvingHost(address, port: Int(port))
|
||
}
|
||
}
|
||
|
||
// --MARK: 进来的消息, 这里需要采用代数类型来表示
|
||
|
||
enum SDLHoleInboundMessage {
|
||
case stunReply(SDLStunReply)
|
||
case stunProbeReply(SDLStunProbeReply)
|
||
|
||
case data(SDLData)
|
||
case register(SDLRegister)
|
||
case registerAck(SDLRegisterAck)
|
||
}
|
||
|
||
// --MARK: 定义消息类型
|
||
|
||
struct SDLSuperInboundMessage {
|
||
let msgId: UInt32
|
||
let packet: InboundPacket
|
||
|
||
enum InboundPacket {
|
||
case empty
|
||
case registerSuperAck(SDLRegisterSuperAck)
|
||
case registerSuperNak(SDLRegisterSuperNak)
|
||
case peerInfo(SDLPeerInfo)
|
||
case pong
|
||
case event(SDLEvent)
|
||
case command(SDLCommand)
|
||
}
|
||
}
|