update for HEAD-2003091401
[reactos.git] / lib / ws2_32 / misc / event.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS WinSock 2 DLL
4  * FILE:        misc/event.c
5  * PURPOSE:     Event handling
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * REVISIONS:
8  *   CSH 01/09-2000 Created
9  */
10 #include <ws2_32.h>
11 #include <handle.h>
12
13
14 /*
15  * @implemented
16  */
17 BOOL
18 EXPORT
19 WSACloseEvent(
20   IN  WSAEVENT hEvent)
21 {
22   BOOL Success;
23
24   if (!WSAINITIALIZED) {
25     WSASetLastError(WSANOTINITIALISED);
26     return FALSE;
27   }
28
29   Success = CloseHandle((HANDLE)hEvent);
30
31   if (!Success)
32     WSASetLastError(WSA_INVALID_HANDLE);
33
34   return Success;
35 }
36
37
38 /*
39  * @implemented
40  */
41 WSAEVENT
42 EXPORT
43 WSACreateEvent(VOID)
44 {
45   HANDLE Event;
46
47   if (!WSAINITIALIZED) {
48     WSASetLastError(WSANOTINITIALISED);
49     return FALSE;
50   }
51
52   Event = CreateEventW(NULL, TRUE, FALSE, NULL);
53
54   if (Event == INVALID_HANDLE_VALUE)
55     WSASetLastError(WSA_INVALID_HANDLE);
56
57   return (WSAEVENT)Event;
58 }
59
60
61 /*
62  * @implemented
63  */
64 BOOL
65 EXPORT
66 WSAResetEvent(
67   IN  WSAEVENT hEvent)
68 {
69   BOOL Success;
70
71   if (!WSAINITIALIZED) {
72     WSASetLastError(WSANOTINITIALISED);
73     return FALSE;
74   }
75
76   Success = ResetEvent((HANDLE)hEvent);
77
78   if (!Success)
79     WSASetLastError(WSA_INVALID_HANDLE);
80
81   return Success;
82 }
83
84
85 /*
86  * @implemented
87  */
88 BOOL
89 EXPORT
90 WSASetEvent(
91   IN  WSAEVENT hEvent)
92 {
93   BOOL Success;
94
95   if (!WSAINITIALIZED) {
96     WSASetLastError(WSANOTINITIALISED);
97     return FALSE;
98   }
99
100   Success = SetEvent((HANDLE)hEvent);
101
102   if (!Success)
103     WSASetLastError(WSA_INVALID_HANDLE);
104
105   return Success;
106 }
107
108
109 /*
110  * @implemented
111  */
112 DWORD
113 EXPORT
114 WSAWaitForMultipleEvents(
115   IN  DWORD cEvents,
116   IN  CONST WSAEVENT FAR* lphEvents,
117   IN  BOOL fWaitAll,
118   IN  DWORD dwTimeout,
119   IN  BOOL fAlertable)
120 {
121   DWORD Status;
122
123   if (!WSAINITIALIZED) {
124     WSASetLastError(WSANOTINITIALISED);
125     return FALSE;
126   }
127
128   Status = WaitForMultipleObjectsEx(cEvents, lphEvents, fWaitAll, dwTimeout, fAlertable);
129   if (Status == WAIT_FAILED) {
130     Status = GetLastError();
131
132     if (Status == ERROR_NOT_ENOUGH_MEMORY)
133       WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
134     else if (Status == ERROR_INVALID_HANDLE)
135       WSASetLastError(WSA_INVALID_HANDLE);
136     else
137       WSASetLastError(WSA_INVALID_PARAMETER);
138
139     return WSA_WAIT_FAILED;
140   }
141
142   return Status;
143 }
144
145
146 /*
147  * @implemented
148  */
149 INT
150 EXPORT
151 WSAEnumNetworkEvents(
152   IN  SOCKET s,
153   IN  WSAEVENT hEventObject,
154   OUT LPWSANETWORKEVENTS lpNetworkEvents)
155 {
156   PCATALOG_ENTRY Provider;
157   INT Status;
158   INT Errno;
159
160   if (!lpNetworkEvents) {
161     WSASetLastError(WSAEINVAL);
162     return SOCKET_ERROR;
163   }
164
165   if (!WSAINITIALIZED) {
166     WSASetLastError(WSANOTINITIALISED);
167     return SOCKET_ERROR;
168   }
169
170   if (!ReferenceProviderByHandle((HANDLE)s, &Provider)) {
171     WSASetLastError(WSAENOTSOCK);
172     return SOCKET_ERROR;
173   }
174
175   Status = Provider->ProcTable.lpWSPEnumNetworkEvents(
176     s, hEventObject, lpNetworkEvents, &Errno);
177
178   DereferenceProviderByPointer(Provider);
179
180   if (Status == SOCKET_ERROR)
181     WSASetLastError(Errno);
182
183   return Status;
184 }
185
186
187 /*
188  * @implemented
189  */
190 INT
191 EXPORT
192 WSAEventSelect(
193     IN  SOCKET s,
194     IN  WSAEVENT hEventObject,
195     IN  LONG lNetworkEvents)
196 {
197   PCATALOG_ENTRY Provider;
198   INT Status;
199   INT Errno;
200
201   if (!WSAINITIALIZED) {
202     WSASetLastError(WSANOTINITIALISED);
203     return SOCKET_ERROR;
204   }
205
206   if (!ReferenceProviderByHandle((HANDLE)s, &Provider)) {
207     WSASetLastError(WSAENOTSOCK);
208     return SOCKET_ERROR;
209   }
210
211   Status = Provider->ProcTable.lpWSPEventSelect(
212     s, hEventObject, lNetworkEvents, &Errno);
213
214   DereferenceProviderByPointer(Provider);
215
216   if (Status == SOCKET_ERROR)
217     WSASetLastError(Errno);
218
219   return Status;
220 }
221
222 /* EOF */