Home > Software engineering >  IOCP model, on the socket at the same time delivery WSARecv and WSASend problems?
IOCP model, on the socket at the same time delivery WSARecv and WSASend problems?

Time:09-15

Typedef struct _SocketState//socket state & amp; The control
{
char operation;
The SOCKET SOCKET;
DWORD length;
Char buf [MAX_BUF];
} SocketState.

The static SocketState * new_socket_state (void)
{
Return (SocketState *) calloc (1, sizeof (SocketState));
}

The static WSAOVERLAPPED * new_overlapped (void)
{
Return (WSAOVERLAPPED *) calloc (1, sizeof (WSAOVERLAPPED));
}

NewSocketState=new_socket_state ();
NewSocketState - & gt; The socket=socketState - & gt; The socket;//here socketState - & gt; The socket is already connected socket;

CreateIoCompletionPort ((HANDLE) newSocketState - & gt; Socket, cpl_port ULONG_PTR newSocketState, 0)//bind to the completion port.
==================
I want to post two IO:
SocketState - & gt; Operation=OP_READ;
WSARecv (newSocketState - & gt; The socket, & amp; Wsabuf, 1, NULL, & amp; Flags, ovf, NULL)
SocketState - & gt; Operation=OP_WRITE;
WSASend (newSocketState - & gt; The socket, & amp; Wsabuf, 1, NULL, 0, ovf, NULL)
Here I can't get socketState - & gt; The operation of OP_READ or OP_WRITE two values at the same time? Here should be how to deal with?

CodePudding user response:

With the WSAEventSelect (s, hEventObject, FD_READ | FD_WRITE | FD_CLOSE); Event-driven can, and then use WSAEnumNetworkEvents check




CodePudding user response:

reference 1st floor zgl7903 response:
with the WSAEventSelect (s, hEventObject FD_READ | FD_WRITE | FD_CLOSE); Event-driven can, and then use WSAEnumNetworkEvents check

IOCP also use events, there is no other way?

CodePudding user response:

What I mean is that, first delivery:
SocketState - & gt; Operation=OP_READ;
WSARecv (newSocketState - & gt; The socket, & amp; Wsabuf, 1, NULL, & amp; Flags, ovf, NULL)
GetQueuedCompletionStatus at this time also does not return, that is, WSARecv are not done, then delivery
SocketState - & gt; Operation=OP_WRITE;
WSASend (newSocketState - & gt; The socket, & amp; Wsabuf, 1, NULL, 0, ovf, NULL)
Thus socketState - & gt; Operation=OP_WRITE covers socketState - & gt; Operation=OP_READ, then
GetQueuedCompletionStatus returned to operation can only be socketState - & gt; Operation=OP_WRITE, and won't get a socketState - & gt; Operation=OP_READ, this how to solve?

CodePudding user response:

IOCP 2: send and receive at the same time


CodePudding user response:

The
reference 4 floor zgl7903 reply:
IOCP 2: send and receive at the same time

The key is the key is the same newSocketState binding,

CodePudding user response:

Successively delivery WSARecv and WSASend almost at the same time, delivering WSARecv, set newSocketState - & gt; Operation=OP_READ; Followed by delivering WSASend, set newSocketState - & gt; Operation=OP_WRITE;
When the GetQueuedCompletionStatus return twice (after WSARecv and WSASend), can enter OP_READ branch processing program,

CodePudding user response:

Delivery will not cover it twice, should return twice

CodePudding user response:

Roughly to see, the feeling you understand is wrong, as the server side, recv not casually delivery, recv object is created, but the accept function received a connection request, after get the client socket, can only be delivered to its recv operations,
SocketState - & gt; Operation=OP_READ; What is action? Have not seen, and in reality may not begin delivering the send and recv, like other people make a phone call to ask you questions, after the telephone connected, you can't ask others what question and answer his question, must first listen to his problems, then answer him, back to the socket, the server is accept, after the recv delivery, and see what the client request, and then complete the routine in the corresponding request, also is to deliver the send reply, of course, multiple client connection, the client recv with another client send may exist at the same time, but that's a different session,

CodePudding user response:

The analysis of the 8th floor reasonable
Delivery is dependent on the business, and is not connected directly after delivery to read and write
Server is a passive response,
Needs to receive the data read will send delivery;
If you need to send data, would like to write the delivery,

CodePudding user response:

Logic first no matter, if you want to deliver two requests, using two newSocketState is ok

CodePudding user response:

You can deliver at the same time the Send and Recv, binding two WSBuf, keep binding buffer effectively during the delivery,

CodePudding user response:

IOCP can be said to be the most convenient to use, the framework of completely don't have to make so trouble, event-driven you so to write:

iocp event definition:
Struct iocp_evt;

/* iocp event handler */
Iocp_evt_handler typedef void (*) (void * priv, struct iocp_evt * evt);

/* define the iocp events, overlapped and event binding up */
Struct iocp_evt {
Iocp_evt_handler handler.
Void * priv;
OVERLAPPED OVERLAPPED;
DOWRD numberOfBytes;
DOWRD dwError;
ULONG_PTR key;
}


/* event to initialize */
Void iocp_evt_init (struct iocp_evt * evt, iocp_evt_handler handler, void * priv)
{
Evt - & gt; Handler=handler;
Evt - & gt; Priv=priv;
Memset (& amp; Evt - & gt; Overlapped, 0, sizeof (OVARLAPPED));
}

event is triggered when the handler:
Void on_wsa_recv (void * priv, struct iocp_evt * evt)
{
/* to get the error code in the IOCP */
DOWRD dwError=evt - & gt; DwError;

The number of bytes/* to get the IOCP transmission */
DOWRD numberOfBytes=evt - & gt; NumberOfBytes;

/* to get the IOCP binding Key */
ULONG_PTR key=evt - & gt; The key;

/* */to carry their own context
Void * context=priv;

/*... */
}


delivery IOCP events in writing:

/* generate iocp events */
Struct iocp_evt * evt=(struct iocp_evt *) malloc (sizeof (struct iocp_evt));
/* the incoming event callback function and its own context pointer initialized to events */
Iocp_evt_init (evt, on_wsa_recv user_data);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related