Three. Event selection (WSAEventSelect) model for the Windows I/O model

Source: Internet
Author: User

1. Event selection Model: Similar to the asynchronous selection model, it also allows applications to receive event-based network event notifications on one or more sockets. For the network events used by the asynchronous selection model, they can be migrated intact to the event selection model. The main difference between the event selection model and the asynchronous selection model is that the network event is posted to an event object handle instead of being posted to a window routine.
2. Create Event object: The event selection model requires the application to create an event object first for each socket that intends to use the event selection model. The creation method is called the Wsacreateevent function, which is defined as follows: Wsaevent wsacreateevent (void);
3. Bind the event object to the socket:
int WSAEventSelect (
SOCKET S,
Wsaevent Heventobject,
Long lnetworkevents//network event, which is the network event in asynchronous selection, the usage is exactly the same
);
For events, he has two types, automatic events and manual events. There are two states, the state is not triggered, and the trigger state. Events created by using the Wsacreateevent function default to human events and are not in a triggered state. As network events trigger an event object that is associated with a socket, the state of work transitions to a trigger state without triggering the state. Since the event object was created in a manual reset mode, after the processing of an I/O request, our application will be responsible for working
State-triggered state changes are not triggered.
4. Reset event is not triggered state:
BOOL wsaresetevent (wsaevent hevent);
The function is to reset the time from the trigger state to the non-triggering state
5. Close the event object and release the kernel resources it occupies:
BOOL wsacloseevent (wsaevent hevent);
6. Monitor the status of the event object:
DWORD WSAWaitForMultipleEvents (
The number of events in the DWORD cevents;//event object array
Const Wsaevent far* lphevents,//Event Object array
BOOL fwaitall,//This parameter indicates whether you want to wait until all event objects become trigger state functions to return
The DWORD dwtimeout,//timeout, in milliseconds exceeding the specified time, the function returns immediately, even if the conditions specified by the fWaitAll parameter have not been met
BOOL falertable//ignored, set to False
);
Parameters:
Note that wsawaitformultipleevents can only support a maximum value set by the Wsa_maximum_wait_events object, defined here as 6 4. Therefore, for each thread that makes a wsawaitformultipleevents call, the I/O model can support up to 6 4 sockets at a time. If you want this model to manage more than 64 sockets at the same time, you must create additional worker threads to wait for more event objects. The fWaitAll parameter specifies whether to wait until all event objects become trigger state functions to return. If set to True, the function will return only if all event objects contained within the lphevents array have entered the trigger state, but if set to False, any event object enters the trigger state and the function returns. In the latter case, the return value indicates which event object is responsible for the return of the function. Typically, the application should set this parameter to False, and typically, dwTimeOut is placed as 0.
Servicing only one socket event at a time
Function Explanation:
Once a socket is associated with the same event object handle, the application can begin I/O processing by waiting for the network event to trigger the event object handle's working state. The WSAWaitForMultipleEvents function is designed to wait for one or more event object handles, and after one or all of the previously specified event objects enter the triggering state,
or returns immediately after a specified period of time has passed.
7. Determine the socket for network events: if WSAWaitForMultipleEvents receives a network event notification for an event object, it returns a value indicating the event object that caused the function to return. In this way, our application can reference the event that has been sent in the event array and retrieve the socket corresponding to that event, and determine what type of network event occurred on which socket. When referencing events in an event array, you should use the return value of WSAWaitForMultipleEvents, minus the predefined value wsa_wait_event_0, to get a specific reference value (that is, the index position).
Index=wsawaitformultipleevents (...);
MYEVENT=EVENTARRAY[INDEX-WSA_WAIT_EVENT_0];
7. Investigate the types of network events that occur:
int Wsaenumnetworkevents (
SOCKET S,
The wsaevent heventobjects,//parameter is optional and corresponds to the event object that you intend to reset, that is, the set event is not triggered. Same function as Wsatresetevent function
Lpwsanetworkevents lpnetworkevents//is used to accept the type of network event that occurs and any error codes that may occur
);
The fourth parameter in the function is used to receive the type of network event that occurred
8.WSANETWORKEVENTS structure:
tydef struct _wsanetworkevents
{
Long lnetworkevents;//Network Event type
Long ierrorcode[fd_max_events];//error code
}wsanetworkevents,far* lpwsanetworkevents;
The Ierrorcode parameter specifies an array of error codes that are associated with events in Lnetworkevents. Ierrorcode for each network event type, there is a special event index, similar to the name of the event type, just to add a "_bit" suffix string after the event name.

Example code:

1SOCKET Socket[wsa_maximum_wait_events];// -2WsaeventEvent[wsa_maximum_wait_events];3 SOCKET Accept,listen;4DWORD eventtotal=0;5 DWORD index;6 7 //Creating Sockets8listen=socket (...);9 Ten //bind to address One bind (...); A  - //Creating an Event object - wsaevent newevent; theNewevent=wsacreateevent (); -  - //Registering Network Events -WSAEventSelect (listen,newevent,fd_accept|fd_close); +  -socket[eventtotal]=Listen; + Event[eventtotal]=Neweventl; Aeventtotal++; at  -  -  while(1) - { -     //wait for Event trigger status -Index=wsawaitformultipleevents (Eventtotal,Event, false,wsa_infinite,false); in  -     //view the types of network events that occur and identify the sockets that have network events to wsanetworkevents networkevents; +Wsaenumnetworkevents (Socket[index-wsa_wait_event_0],Event[index-wsa_wait_event_0],&networkevents); -  the     //determine the type of network event that occurs *     if(networkevents.lnetworkevents&fd_accept) $     {Panax Notoginseng         if(networkevents.ierrorcode[fd_accept_bit]!=0) -         { theprintf"fd_accept failed with error%d\n", Networkevents.ierrorcode[fd_accept_bit]); +              Break; A         } the          +         //after the Fd_accept event occurs, the subsequent processing -accept=Accept (socket[index_wsa_wait_event_0],null,null); $          $         //to view the number of event objects -         if(eventtotal>wsa_maximum_wait_events) -         { theprintf"Too many connections\n"); - closesocket (accept);Wuyi              Break; the         } -          Wu         //Create the event again and do the above again to cycle -Newevent=wsacreateevent (); About          $WSAEventSelect (Listen, newevent, fd_read| Fd_write |fd_close); -          -         Event[eventtotal]=newevent; -socket[eventtotal]=Accept; Aeventtotal++; +printf"Socket%d connected\n", accept); the     } -  $     //handling of Fd_read events the     //handling similar to fd_accept network events the     if(networkevents.lnetworkevents&fd_read) the     { the         if(networkevents.ierrorcode[fd_read_bit]!=0) -         { inprintf"Fd_read failed with error%d\n", Networkevents.ierrorcode[fd_read_bit]); the              Break; the         } About         //reading Data theRecv (Socket[index-wsa_wait_event_0],buf,sizeof(BUF),0); the     } the  +     //Next, the other network events are handled as above, but it is important to note that the event selection model is based on the window program and requires a message to be sent, except that this part of the code is given. -      the }Bayi  the  the  -     
View Code

Three. Event selection (WSAEventSelect) model for the Windows I/O model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.