gpt4 book ai didi

c++ - boost::asio::async_write 混合来自两条消息的数据(错误)

转载 作者:行者123 更新时间:2023-11-27 23:59:18 30 4
gpt4 key购买 nike

<分区>

我将 boost::asio 用于异步客户端和服务器。在工作过程中,客户端向服务器发送不同类型的数据:小型服务消息(5-50 B)和带有原始图像数据的最大消息(40-200 KB)。当我按顺序调用 Client::send 时(在一个线程中,连续):

  1. 发送“小服务消息”;
  2. 发送“大图消息”;

我在服务器湖上得到混合数据(错误):

|大消息开始||小消息||大消息结束|

void Client::send(MessageType type, const void* data, int size, bool read_header_after) {
assert(cstatus.is_connected());

header.type = type;
size_t buf_size = sizeof(ProtocolHeader) + size;
Bytes *p = new Bytes();
p->resize(buf_size);
std::memcpy(&p->front(), &header, sizeof(ProtocolHeader));
if (size) {
std::memcpy(&p->at(sizeof(ProtocolHeader)), data, size);
}


std::cout << "***** SEND start: " << p->size() << " bytes *****" << std::endl;


ba::async_write(*socket, ba::buffer(&p->front(), buf_size),
ba::transfer_exactly(buf_size),
[this, p, read_header_after](const boost::system::error_code& ec, std::size_t length) {

std::cout << "***** SEND complete: "
<< p->size() << " bytes; ec="
<< ec.value() << " (" << ec.message() << ") bufsize="
<< p->size()
<< " *****"
<< std::endl;

size_t buf_size = p->size();
delete p; // remove sent data

if (ec) {
cstatus.set_last_network_error("Client::send " + ec.message());
connection_failed("send - ec");
} else if (length < buf_size) {
connection_failed("send - len");
} else {
if (read_header_after) {
read_header();
}
start_check_timer(NORMAL_INTERVAL_DATA_SEND_MILLISEC);
}
});
}

输出显示小消息作为第二个发送到 async_write 但在大消息之前作为第一个执行(完成)。

***** SEND start: 53147 bytes *****
***** SEND start: 5 bytes *****
***** SEND complete: 5 bytes; ec=0 (Success) bufsize=5 *****
***** SEND complete: 53147 bytes; ec=0 (Success) bufsize=53147 *****

这怎么可能以及如何同步?谢谢!

更新

我不需要同步任务队列。我需要同步两个具有不同缓冲区大小的 async_write 操作。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com