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

时间:2017-08-06 21:17:17

标签: c++ winapi

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

我有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调用返回重叠结果后在worker循环中执行此操作;

    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相同的缓冲区时它是否有效?

谢谢!

1 个答案:

答案 0 :(得分:2)

您必须为每个呼叫传递不同的OVERLAPPED缓冲区,因为您将进行多个待处理呼叫。这在OVERLAPPED结构的文档中清楚地说明了。