// // NoticeMessage.swift // sdlan // // Created by 安礼成 on 2024/6/3. // import Foundation import NIOCore struct NoticeMessage { enum InboundMessage { case none case alertMessage(alert: String) } static func decodeMessage(buffer: inout ByteBuffer) -> InboundMessage { guard let type = buffer.readInteger(as: UInt8.self) else { return .none } switch type { case 0x01: if let len0 = buffer.readInteger(as: UInt16.self), let alert = buffer.readString(length: Int(len0)) { return .alertMessage(alert: alert) } default: return .none } return .none } static func alert(alert: String) -> Data { var data = Data() data.append(contentsOf: [0x01]) data.append(contentsOf: lenBytes(UInt16(alert.count))) data.append(alert.data(using: .utf8)!) return data } private static func lenBytes(_ value: UInt16) -> [UInt8] { let byte1 = UInt8((value >> 8) & 0xFF) let bytes2 = UInt8(value & 0xFF) return [byte1, bytes2] } }