Balan Narcis
Balan Narcis

Reputation: 139

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

I have a list of sockets.(Opened connections)

I have n worker threads.

Thread loop:

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

I have this simple struct:

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
}

...

This is the 'Broadcast' function:

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
}

I want to do this in the worker loop after GetQueuedCompletionStatus call returns with the overlapped result;

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

}

From what i read on Microsoft WSASend documentation each WSASend overlapped call sould have its own OVERLAPPED structure passed, but is that valid when i WSASend the same buffer?

Thank you!

Upvotes: 0

Views: 97

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

You must pass a different OVERLAPPED buffer for each call since you'll be making multiple pending calls. This is clearly spelled out in the documentation for the OVERLAPPED structure.

Upvotes: 2

Related Questions