Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit ddf3d5e

Browse files
author
Mathieu Borderé
committed
uv_recv: Only use first 2 bytes of preamble to determine message type.
1 parent 26f4af5 commit ddf3d5e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/uv_encoding.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,14 @@ static void decodeTimeoutNow(const uv_buf_t *buf, struct raft_timeout_now *p)
484484
p->last_log_term = byteGet64(&cursor);
485485
}
486486

487-
int uvDecodeMessage(const unsigned long type,
487+
int uvDecodeMessage(uint16_t type,
488488
const uv_buf_t *header,
489489
struct raft_message *message,
490490
size_t *payload_len)
491491
{
492492
unsigned i;
493493
int rv = 0;
494494

495-
if (type > USHRT_MAX) {
496-
return RAFT_INVALID;
497-
}
498-
499495
memset(message, 0, sizeof(*message));
500496

501497
message->type = (unsigned short)type;

src/uv_encoding.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int uvEncodeMessage(const struct raft_message *message,
1414
uv_buf_t **bufs,
1515
unsigned *n_bufs);
1616

17-
int uvDecodeMessage(unsigned long type,
17+
int uvDecodeMessage(uint16_t type,
1818
const uv_buf_t *header,
1919
struct raft_message *message,
2020
size_t *payload_len);

src/uv_recv.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ static void uvServerReadCb(uv_stream_t *stream,
253253
type = byteFlip64(s->preamble[0]);
254254
assert(type > 0);
255255

256-
rv = uvDecodeMessage((unsigned long)type, &s->header, &s->message,
256+
/* Only use first 2 bytes of the type, cast is safe as raft has far
257+
* less 2^16 - 1 message types.
258+
* TODO: This is preparation to add the version of the message
259+
* in the raft preamble. Once this change has been active for
260+
* sufficiently long time, we can start encoding the version in some
261+
* of the remaining bytes of s->preamble[0]. */
262+
rv = uvDecodeMessage((uint16_t)type, &s->header, &s->message,
257263
&s->payload.len);
258264
if (rv != 0) {
259265
Tracef(s->uv->tracer, "decode message: %s",

0 commit comments

Comments
 (0)