2017-08-06 17 views
0

我有一個套接字列表(已打開的連接)在這種情況下,我是否應該爲每個WSASend調用傳遞唯一的OVERLAPPED結構?

我有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文檔每WSASend讀取調用重疊有前人的精力自己的OVERLAPPED結構過去了,但就是有效的,當我WSASend相同緩衝?

謝謝!

+0

只有當您不關心返回狀態和numberOfBytesTransfered(並且完全知道哪個套接字io完成)時,您可以在多個請求中使用相同的「OVERLAPPED」緩衝區)。如果在這個結構上正確實現addref/release邏輯 – RbMm

+0

Duude,謝謝,我不在乎thouse的東西。和它的decr_ref只,當我張貼發送我設置裁判號。的連接 –

+0

請將它作爲Answare發佈,以便我可以接受它! –

回答

2

您必須爲每個呼叫傳遞一個不同的OVERLAPPED緩衝區,因爲您將進行多個掛起的呼叫。這在OVERLAPPED結構的文檔中有明確的說明。

相關問題