gpt4 book ai didi

c++ - 在这种情况下,我应该为每个 WSASend 调用传递唯一的 OVERLAPPED 结构吗?

转载 作者:行者123 更新时间:2023-11-28 01:44:58 25 4
gpt4 key购买 nike

我有一个套接字列表。(打开的连接)

我有 n 个工作线程。

线程循环:

while (1)
{
_this.result = GetQueuedCompletionStatus(a_server.server_iocp, &_this.numberOfBytesTransfered,
&_this.completionKey, (OVERLAPPED**)&_this.iocp_task, INFINITE);
...
}

我有这个简单的结构:

struct iocp_send_global :public iocp_task<IOCP_SEND_GLOBAL> {
OVERLLAPED ov; //overlapped struct at top
std::atomic_uint32_t ref;

bool decr_ref(){ return ref.fetch_sub(1, std::memory_order_acq_rel) == 1;}


//packet data here
}

...

这是“广播”功能:

iocp_send_global * packet = new iocp_send_global;

[set packet data here]

for(int i=0;i<connectionsCount;++i){
WSASend(connections[i],...,&packet,...); //posting same packet to all connections
}

我想在 GetQueuedCompletionStatus 调用返回重叠结果后在工作循环中执行此操作;

    if (_this.iocp_task->type == IOCP_SEND_GLOBAL) {
auto* task = (iocp_send_global*)_this.iocp_task;

if (!task->decr_ref()) {
_this.iocp_task = nullptr;
//dont delete the task yet,
//all send post must finish first
//[all posts share the same buffer]
}
else {
//delete the task containing the send data after all send posts finished
delete _this.iocp_task;
_this.iocp_task = nullptr;
}

}

根据我在 Microsoft WSASend 文档中读到的内容,每个 WSASend 重叠调用都应该传递自己的 OVERLAPPED 结构,但是当我 WSASend 相同的缓冲区时,这是否有效?

谢谢!

最佳答案

您必须为每个调用传递不同的 OVERLAPPED 缓冲区,因为您将进行多个挂起的调用。 OVERLAPPED 结构的文档中清楚地说明了这一点。

关于c++ - 在这种情况下,我应该为每个 WSASend 调用传递唯一的 OVERLAPPED 结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45536733/

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