Skip to content

表示msg长度的类型使用 uint32_t 比 int 更好 #30

Open
@dyx2025

Description

@dyx2025

paxoskv/msg_svr/msg_comm.cc

	int total_len = *(reinterpret_cast<const int*>(buffer));
	total_len = ntohl(total_len);
        // ......
	msg_size = total_len + sizeof(int);
	const char* data = buffer + sizeof(int);
	if (false == msg.ParseFromArray(data, msg_size - sizeof(int))) {
		return BROKEN_PAXOS_MESSAGE;
	}

在不同平台,int 所占的字节数不一定是4字节,使用 uint32_t 比 int 更好。改后代码。

	uint32_t total_len = *(reinterpret_cast<const uint32_t*>(buffer));
	total_len = ntohl(total_len);
        // ......
	msg_size = total_len + sizeof(uint32_t);
	const char* data = buffer + sizeof(uint32_t);
	if (false == msg.ParseFromArray(data, msg_size - sizeof(uint32_t))) {
		return BROKEN_PAXOS_MESSAGE;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions