mirror of https://github.com/status-im/nim-eth.git
Improve serialization implementation (#403)
This commit is contained in:
parent
1babe38226
commit
32ef1b7f4f
|
@ -69,33 +69,29 @@ proc encodeTypeVer(h: PacketHeaderV1): uint8 =
|
||||||
typeVer = (typeVer and 0xf) or (typeOrd shl 4)
|
typeVer = (typeVer and 0xf) or (typeOrd shl 4)
|
||||||
typeVer
|
typeVer
|
||||||
|
|
||||||
proc encodeHeader*(h: PacketHeaderV1): seq[byte] =
|
proc encodeHeaderStream(s: var OutputStream, h: PacketHeaderV1) =
|
||||||
var mem = memoryOutput().s
|
|
||||||
try:
|
try:
|
||||||
mem.write(encodeTypeVer(h))
|
s.write(encodeTypeVer(h))
|
||||||
mem.write(h.extension)
|
s.write(h.extension)
|
||||||
mem.write(h.connectionId.toBytesBE())
|
s.write(h.connectionId.toBytesBE())
|
||||||
mem.write(h.timestamp.toBytesBE())
|
s.write(h.timestamp.toBytesBE())
|
||||||
mem.write(h.timestampDiff.toBytesBE())
|
s.write(h.timestampDiff.toBytesBE())
|
||||||
mem.write(h.wndSize.toBytesBE())
|
s.write(h.wndSize.toBytesBE())
|
||||||
mem.write(h.seqNr.toBytesBE())
|
s.write(h.seqNr.toBytesBE())
|
||||||
mem.write(h.ackNr.toBytesBE())
|
s.write(h.ackNr.toBytesBE())
|
||||||
return mem.getOutput()
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
# TODO not sure how writing to memory buffer could throw. Raise assertion error if
|
# This should not happen in case of in-memory streams
|
||||||
# its happen for now
|
|
||||||
raiseAssert e.msg
|
raiseAssert e.msg
|
||||||
|
|
||||||
proc encodePacket*(p: Packet): seq[byte] =
|
proc encodePacket*(p: Packet): seq[byte] =
|
||||||
var mem = memoryOutput().s
|
var s = memoryOutput().s
|
||||||
try:
|
try:
|
||||||
mem.write(encodeHeader(p.header))
|
encodeHeaderStream(s, p.header)
|
||||||
if (len(p.payload) > 0):
|
if (len(p.payload) > 0):
|
||||||
mem.write(p.payload)
|
s.write(p.payload)
|
||||||
mem.getOutput()
|
s.getOutput()
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
# TODO not sure how writing to memory buffer could throw. Raise assertion error if
|
# This should not happen in case of in-memory streams
|
||||||
# its happen for now
|
|
||||||
raiseAssert e.msg
|
raiseAssert e.msg
|
||||||
|
|
||||||
# TODO for now we do not handle extensions
|
# TODO for now we do not handle extensions
|
||||||
|
@ -116,12 +112,12 @@ proc decodePacket*(bytes: openArray[byte]): Result[Packet, string] =
|
||||||
pType: kind,
|
pType: kind,
|
||||||
version: version,
|
version: version,
|
||||||
extension: bytes[1],
|
extension: bytes[1],
|
||||||
connection_id: fromBytesBE(uint16, [bytes[2], bytes[3]]),
|
connection_id: fromBytesBE(uint16, bytes.toOpenArray(2, 3)),
|
||||||
timestamp: fromBytesBE(uint32, [bytes[4], bytes[5], bytes[6], bytes[7]]),
|
timestamp: fromBytesBE(uint32, bytes.toOpenArray(4, 7)),
|
||||||
timestamp_diff: fromBytesBE(uint32, [bytes[8], bytes[9], bytes[10], bytes[11]]),
|
timestamp_diff: fromBytesBE(uint32, bytes.toOpenArray(8, 11)),
|
||||||
wnd_size: fromBytesBE(uint32, [bytes[12], bytes[13], bytes[14], bytes[15]]),
|
wnd_size: fromBytesBE(uint32, bytes.toOpenArray(12, 15)),
|
||||||
seq_nr: fromBytesBE(uint16, [bytes[16], bytes[17]]),
|
seq_nr: fromBytesBE(uint16, bytes.toOpenArray(16, 17)),
|
||||||
ack_nr: fromBytesBE(uint16, [bytes[18], bytes[19]]),
|
ack_nr: fromBytesBE(uint16, bytes.toOpenArray(18, 19)),
|
||||||
)
|
)
|
||||||
|
|
||||||
let payload =
|
let payload =
|
||||||
|
|
Loading…
Reference in New Issue