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

Should i pass unique OVERLAPPED structure for each WSASend call , in this case?

本文关键字:唯一 重叠 结构 调用 我应该 这种情况下 WSASend      更新时间:2023-10-16

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

我有 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;
}
}

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

谢谢!

您必须为每个调用传递不同的OVERLAPPED缓冲区,因为您将进行多个挂起的调用。 这在OVERLAPPED结构的文档中有明确说明。