Skip to content

Commit

Permalink
feat: introduce possibility to write headers to the binary form
Browse files Browse the repository at this point in the history
Mainly useful for websocket testing
  • Loading branch information
jimirocks committed Mar 10, 2024
1 parent 7cf6b85 commit 4a95565
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/Codec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cz.smarteon.loxone

import com.ditchoom.buffer.ByteOrder
import com.ditchoom.buffer.PlatformBuffer
import com.ditchoom.buffer.allocate
import com.ditchoom.buffer.wrap
import cz.smarteon.loxone.message.MessageHeader
import cz.smarteon.loxone.message.MessageHeader.Companion.FIRST_BYTE
Expand Down Expand Up @@ -56,4 +57,13 @@ object Codec {
)
}
}

internal fun writeHeader(header: MessageHeader): ByteArray {
val buffer = PlatformBuffer.allocate(PAYLOAD_LENGTH, byteOrder = ByteOrder.LITTLE_ENDIAN)
buffer[0] = FIRST_BYTE
buffer[1] = header.kind.ordinal.toByte()
buffer[2] = (if (header.sizeEstimated) 1 else 0).toByte()
buffer[MSG_SIZE_POSITION] = header.messageSize.toUInt()
return buffer.readByteArray(PAYLOAD_LENGTH)
}
}
5 changes: 5 additions & 0 deletions src/commonTest/kotlin/CodecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ class CodecTest : StringSpec({
val header = Codec.readHeader(Codec.hexToBytes("03000000ab000000"))
header shouldBe MessageHeader(MessageKind.TEXT, false, 171)
}

"should write header" {
val header = MessageHeader(MessageKind.TEXT, false, 171)
Codec.writeHeader(header) shouldBe Codec.hexToBytes("03000000ab000000")
}
})

0 comments on commit 4a95565

Please sign in to comment.