2016년 3월 12일 토요일

Echo Server using Asynchronous Notification IO

1. Enroll Event Kernel Object to OS using WSACreateEvent()
   WSACreateEvent() is manual-reset mode event object creation function
   WSAEventSelect() - OS observe chage of IO according to 3th parameter state and notify user. Thus, this funtion mean that WSAEventSelect() connect event object and socket
※ WSAEventSelect(SOCKET s, WSAEVENT hEventObject, long lNetworkEvents)
 (1)  's' delivered socket generate event according to 'lNetworkEvents' delivered event
 (2)  'hEventObject' delivered handle change state into 'signaled'

ex)
newEvent = WSACreateEvent();
if (WSAEventSelect(hServSock, newEvent, FD_ACCEPT) == SOCKET_ERROR)
ErrorHandling("WSAEventSelect() error");


2. Socket have to connect Event Object as matching index value.
ex)
hSockArr[numOfClntSock] = hServSock;
hEventArr[numOfClntSock] = newEvent;
numOfClntSock++;

3. Check whether or not event generation is true using WSAWaitForMultipleEvents()
ex)
posInfo = WSAWaitForMultipleEvents(numOfClntSock, hEventArr, FALSE, WSA_INFINITE, FALSE);
startIdx = posInfo - WSA_WAIT_EVENT_0;
※ 'posInfo - WSA_WAIT_EVENT_0' is event generation start index value

4. Check Event Object signaled one by one using WSAWaitForMultipleEvents()
ex)
for (i = startIdx; i<numOfClntSock; i++)
{
int sigEventIdx = WSAWaitForMultipleEvents(1, &hEventArr[i], TRUE, 0, FALSE);

5. Classify and event type using WSAEnumNetworkEvents()
ex)
WSAEnumNetworkEvents(hSockArr[sigEventIdx], hEventArr[sigEventIdx], &netEvents);

6. Process event generated
FD_ACCEPT : Do exist connect request?
FD_READ : Do exist data recevied?
FD_CLOSE : Do exist disconnect request?
ex)
if (netEvents.lNetworkEvents & FD_ACCEPT)
 // process content
if (netEvents.lNetworkEvents & FD_READ)
 // process content
if (netEvents.lNetworkEvents & FD_CLOSE)
 // process content

Source Code



댓글 없음:

댓글 쓰기