Bring code/key deserialization into serializer

Duplicates first byte parsing but makes testing more meaningful,
because there's no need to provide a bogus code/key pair.
This commit is contained in:
Davide De Rosa 2018-09-18 18:04:43 +02:00
parent 11cb312c02
commit ce94a594f9
3 changed files with 22 additions and 11 deletions

View File

@ -82,9 +82,8 @@ class ControlChannel {
serializer.reset() serializer.reset()
} }
func readInboundPacket(withCode code: PacketCode, key: UInt8, data: Data, offset: Int) throws -> ControlPacket { func readInboundPacket(withData data: Data, offset: Int) throws -> ControlPacket {
log.debug("Control: Try read packet with code \(code) and key \(key)") let packet = try serializer.deserialize(data: data, start: offset, end: nil)
let packet = try serializer.deserialize(code: code, key: key, data: data, start: offset, end: nil)
log.debug("Control: Read packet \(packet)") log.debug("Control: Read packet \(packet)")
if let ackIds = packet.ackIds as? [UInt32], let ackRemoteSessionId = packet.ackRemoteSessionId { if let ackIds = packet.ackIds as? [UInt32], let ackRemoteSessionId = packet.ackRemoteSessionId {
try readAcks(ackIds, acksRemoteSessionId: ackRemoteSessionId) try readAcks(ackIds, acksRemoteSessionId: ackRemoteSessionId)

View File

@ -34,7 +34,7 @@ protocol ControlChannelSerializer {
func serialize(packet: ControlPacket) throws -> Data func serialize(packet: ControlPacket) throws -> Data
func deserialize(code: PacketCode, key: UInt8, data: Data, start: Int, end: Int?) throws -> ControlPacket func deserialize(data: Data, start: Int, end: Int?) throws -> ControlPacket
} }
extension ControlChannel { extension ControlChannel {
@ -46,10 +46,22 @@ extension ControlChannel {
return packet.serialized() return packet.serialized()
} }
func deserialize(code: PacketCode, key: UInt8, data packet: Data, start: Int, end: Int?) throws -> ControlPacket { func deserialize(data packet: Data, start: Int, end: Int?) throws -> ControlPacket {
var offset = start var offset = start
let end = end ?? packet.count let end = end ?? packet.count
guard end >= offset + PacketHeaderLength else {
throw ControlChannelError("Missing header")
}
let codeValue = packet[offset] >> 3
guard let code = PacketCode(rawValue: codeValue) else {
throw ControlChannelError("Unknown code: \(codeValue))")
}
let key = packet[offset] & 0b111
offset += PacketHeaderLength
log.debug("Control: Try read packet with code \(code) and key \(key)")
guard end >= offset + PacketSessionIdLength else { guard end >= offset + PacketSessionIdLength else {
throw ControlChannelError("Missing sessionId") throw ControlChannelError("Missing sessionId")
} }

View File

@ -418,7 +418,7 @@ public class SessionProxy {
// log.verbose("Received data from LINK (\(packet.count) bytes): \(packet.toHex())") // log.verbose("Received data from LINK (\(packet.count) bytes): \(packet.toHex())")
guard let firstByte = packet.first else { guard let firstByte = packet.first else {
log.warning("Dropped malformed packet (missing header)") log.warning("Dropped malformed packet (missing opcode)")
continue continue
} }
let codeValue = firstByte >> 3 let codeValue = firstByte >> 3
@ -426,9 +426,8 @@ public class SessionProxy {
log.warning("Dropped malformed packet (unknown code: \(codeValue))") log.warning("Dropped malformed packet (unknown code: \(codeValue))")
continue continue
} }
let key = firstByte & 0b111
// log.verbose("Parsed packet with (code, key) = (\(code.rawValue), \(key))") // log.verbose("Parsed packet with code \(code)")
var offset = 1 var offset = 1
if (code == .dataV2) { if (code == .dataV2) {
@ -440,6 +439,7 @@ public class SessionProxy {
} }
if (code == .dataV1) || (code == .dataV2) { if (code == .dataV1) || (code == .dataV2) {
let key = firstByte & 0b111
guard let _ = keys[key] else { guard let _ = keys[key] else {
log.error("Key with id \(key) not found") log.error("Key with id \(key) not found")
deferStop(.shutdown, SessionError.badKey) deferStop(.shutdown, SessionError.badKey)
@ -454,10 +454,10 @@ public class SessionProxy {
continue continue
} }
log.debug("Packet has code \(code.rawValue), key \(key)") log.debug("Packet has code \(code.rawValue)")
let controlPacket: ControlPacket let controlPacket: ControlPacket
do { do {
let parsedPacket = try controlChannel.readInboundPacket(withCode: code, key: key, data: packet, offset: offset) let parsedPacket = try controlChannel.readInboundPacket(withData: packet, offset: 0)
handleAcks() handleAcks()
if parsedPacket.code == .ackV1 { if parsedPacket.code == .ackV1 {
continue continue