Then you need a reference count for number of requests outstanding.
When you want to close the socket, set a flag, then cancel the outstanding requests.
If I understand right, you mean wait operations to be completed and don't post it again. When all is completed, close socket.
If so, I don't care operations for complete gracefully because its abortive close.
I don't want to call closesocket function simultaneously with any wsa operation(but any operation should be called simultaneously except closesocket, maybe socket handle going to reused), so I want to do something like this:
WSABegin() // will check for socket is closing, if so reads ref count if zero, closes socket here and sets invalid handle else adds ref
WSASend(), WSARecv(), ... any socket required operation
WSAEnd() // decrements ref and checks for ref is zero, if so checks for closing flag, sets state closed and calls closesocket
void close()
{
InterlockedExc(&m_state, closing)
if (m_ref == 0)
{
closesocket(socket)
socket = INVALID_SOCKET
}
}
some complex to explain for me
not sure how to implement these operations in atomic way
is that way good?
The structure you want to use is not great threaded/asynchronous design.
sounds like I dont know what im doing actually
How about you just use one thread, that does both WSASend() and WSARecv() and GetQueuedCompletionStatus()?
Then you don't have this problem. The amount of I/O done on the network card on a single socket will not be such that the single thread will be a problem.
I'll do another operations when send is completed rather than calling socket functions
on send completed, ill read packet from circular queue, format it and send again