ReactOS  0.4.13-dev-99-g7e18b6d
event.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS Ancillary Function Driver DLL
4  * FILE: dll/win32/msafd/misc/event.c
5  * PURPOSE: Event handling
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * Alex Ionescu (alex@relsoft.net)
8  * REVISIONS:
9  * CSH 15/06/2001 - Created
10  * Alex 16/07/2004 - Complete Rewrite
11  */
12 
13 #include <msafd.h>
14 
15 int
16 WSPAPI
19  IN WSAEVENT hEventObject,
20  IN long lNetworkEvents,
21  OUT LPINT lpErrno)
22 {
23  IO_STATUS_BLOCK IOSB;
24  AFD_EVENT_SELECT_INFO EventSelectInfo;
25  PSOCKET_INFORMATION Socket = NULL;
27  BOOLEAN BlockMode;
29 
30  TRACE("WSPEventSelect (%lx) %lx %lx\n", Handle, hEventObject, lNetworkEvents);
31 
32  /* Get the Socket Structure associate to this Socket*/
33  Socket = GetSocketStructure(Handle);
34  if (!Socket)
35  {
36  if (lpErrno) *lpErrno = WSAENOTSOCK;
37  return SOCKET_ERROR;
38  }
39 
42 
43  if (!NT_SUCCESS(Status)) return SOCKET_ERROR;
44 
45  /* Set Socket to Non-Blocking */
46  BlockMode = TRUE;
48  Socket->SharedData->NonBlocking = TRUE;
49 
50  /* Deactivate Async Select if there is one */
51  if (Socket->EventObject) {
52  Socket->SharedData->hWnd = NULL;
53  Socket->SharedData->wMsg = 0;
54  Socket->SharedData->AsyncEvents = 0;
55  Socket->SharedData->SequenceNumber++; // This will kill Async Select after the next completion
56  }
57 
58  /* Set Structure Info */
59  EventSelectInfo.EventObject = hEventObject;
60  EventSelectInfo.Events = 0;
61 
62  /* Set Events to wait for */
63  if (lNetworkEvents & FD_READ) {
64  EventSelectInfo.Events |= AFD_EVENT_RECEIVE;
65  }
66 
67  if (lNetworkEvents & FD_WRITE) {
68  EventSelectInfo.Events |= AFD_EVENT_SEND;
69  }
70 
71  if (lNetworkEvents & FD_OOB) {
72  EventSelectInfo.Events |= AFD_EVENT_OOB_RECEIVE;
73  }
74 
75  if (lNetworkEvents & FD_ACCEPT) {
76  EventSelectInfo.Events |= AFD_EVENT_ACCEPT;
77  }
78 
79  if (lNetworkEvents & FD_CONNECT) {
80  EventSelectInfo.Events |= AFD_EVENT_CONNECT | AFD_EVENT_CONNECT_FAIL;
81  }
82 
83  if (lNetworkEvents & FD_CLOSE) {
85  }
86 
87  if (lNetworkEvents & FD_QOS) {
88  EventSelectInfo.Events |= AFD_EVENT_QOS;
89  }
90 
91  if (lNetworkEvents & FD_GROUP_QOS) {
92  EventSelectInfo.Events |= AFD_EVENT_GROUP_QOS;
93  }
94 
95  /* Send IOCTL */
97  SockEvent,
98  NULL,
99  NULL,
100  &IOSB,
102  &EventSelectInfo,
103  sizeof(EventSelectInfo),
104  NULL,
105  0);
106 
107  /* Wait for return */
108  if (Status == STATUS_PENDING) {
110  Status = IOSB.Status;
111  }
112 
113  NtClose (SockEvent);
114 
115  if (Status != STATUS_SUCCESS)
116  {
117  ERR("Got status 0x%08x.\n", Status);
118  return MsafdReturnWithErrno(Status, lpErrno, 0, NULL);
119  }
120 
121  /* Set Socket Data*/
122  Socket->EventObject = hEventObject;
123  Socket->NetworkEvents = lNetworkEvents;
124 
125  TRACE("Leaving\n");
126 
127  return 0;
128 }
129 
130 
131 INT
132 WSPAPI
134  IN SOCKET Handle,
135  IN WSAEVENT hEventObject,
136  OUT LPWSANETWORKEVENTS lpNetworkEvents,
137  OUT LPINT lpErrno)
138 {
140  IO_STATUS_BLOCK IOSB;
141  PSOCKET_INFORMATION Socket = NULL;
144 
145  TRACE("Called (lpNetworkEvents %x)\n", lpNetworkEvents);
146 
147  /* Get the Socket Structure associate to this Socket*/
148  Socket = GetSocketStructure(Handle);
149  if (!Socket)
150  {
151  if (lpErrno) *lpErrno = WSAENOTSOCK;
152  return SOCKET_ERROR;
153  }
154  if (!lpNetworkEvents)
155  {
156  if (lpErrno) *lpErrno = WSAEFAULT;
157  return SOCKET_ERROR;
158  }
159 
162 
163  if( !NT_SUCCESS(Status) ) {
164  ERR("Could not make an event %x\n", Status);
165  return SOCKET_ERROR;
166  }
167 
168  EnumReq.Event = hEventObject;
169 
170  /* Send IOCTL */
172  SockEvent,
173  NULL,
174  NULL,
175  &IOSB,
177  &EnumReq,
178  sizeof(EnumReq),
179  NULL,
180  0);
181 
182  /* Wait for return */
183  if (Status == STATUS_PENDING) {
185  Status = IOSB.Status;
186  }
187 
188  NtClose (SockEvent);
189 
190  if (Status != STATUS_SUCCESS)
191  {
192  ERR("Status 0x%08x", Status);
193  return MsafdReturnWithErrno(Status, lpErrno, 0, NULL);
194  }
195 
196  lpNetworkEvents->lNetworkEvents = 0;
197 
198  /* Set Events to wait for */
199  if (EnumReq.PollEvents & AFD_EVENT_RECEIVE) {
200  lpNetworkEvents->lNetworkEvents |= FD_READ;
201  lpNetworkEvents->iErrorCode[FD_READ_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_READ_BIT]);
202  }
203 
204  if (EnumReq.PollEvents & AFD_EVENT_SEND) {
205  lpNetworkEvents->lNetworkEvents |= FD_WRITE;
206  lpNetworkEvents->iErrorCode[FD_WRITE_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_WRITE_BIT]);
207  }
208 
209  if (EnumReq.PollEvents & AFD_EVENT_OOB_RECEIVE) {
210  lpNetworkEvents->lNetworkEvents |= FD_OOB;
211  lpNetworkEvents->iErrorCode[FD_OOB_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_OOB_BIT]);
212  }
213 
214  if (EnumReq.PollEvents & AFD_EVENT_ACCEPT) {
215  lpNetworkEvents->lNetworkEvents |= FD_ACCEPT;
216  lpNetworkEvents->iErrorCode[FD_ACCEPT_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_ACCEPT_BIT]);
217  }
218 
219  if (EnumReq.PollEvents &
221  lpNetworkEvents->lNetworkEvents |= FD_CONNECT;
222  lpNetworkEvents->iErrorCode[FD_CONNECT_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_CONNECT_BIT]);
223  }
224 
225  if (EnumReq.PollEvents &
227  lpNetworkEvents->lNetworkEvents |= FD_CLOSE;
228  lpNetworkEvents->iErrorCode[FD_CLOSE_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_CLOSE_BIT]);
229  }
230 
231  if (EnumReq.PollEvents & AFD_EVENT_QOS) {
232  lpNetworkEvents->lNetworkEvents |= FD_QOS;
233  lpNetworkEvents->iErrorCode[FD_QOS_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_QOS_BIT]);
234  }
235 
236  if (EnumReq.PollEvents & AFD_EVENT_GROUP_QOS) {
237  lpNetworkEvents->lNetworkEvents |= FD_GROUP_QOS;
238  lpNetworkEvents->iErrorCode[FD_GROUP_QOS_BIT] = TranslateNtStatusError(EnumReq.EventStatus[FD_GROUP_QOS_BIT]);
239  }
240 
241  TRACE("Leaving\n");
242 
243  return MsafdReturnWithErrno(STATUS_SUCCESS, lpErrno, 0, NULL);
244 }
245 
246 /* EOF */
#define WSAENOTSOCK
Definition: winerror.h:1951
#define SOCKET_ERROR
Definition: winsock.h:333
HANDLE EventObject
Definition: msafd.h:110
#define IN
Definition: typedefs.h:38
HANDLE SockEvent
#define TRUE
Definition: types.h:120
#define WSAEFAULT
Definition: winerror.h:1945
#define AFD_EVENT_CONNECT_FAIL
Definition: shared.h:211
INT WSPAPI WSPEnumNetworkEvents(IN SOCKET Handle, IN WSAEVENT hEventObject, OUT LPWSANETWORKEVENTS lpNetworkEvents, OUT LPINT lpErrno)
Definition: event.c:133
#define FD_ACCEPT
Definition: winsock.h:408
#define FD_CONNECT
Definition: winsock.h:409
#define AFD_EVENT_DISCONNECT
Definition: shared.h:206
#define WSAEVENT
Definition: winsock2.h:585
PSOCKET_INFORMATION GetSocketStructure(SOCKET Handle)
Definition: dllmain.c:3507
LONG NTSTATUS
Definition: precomp.h:26
#define AFD_EVENT_ABORT
Definition: shared.h:207
NTSTATUS NTAPI NtCreateEvent(OUT PHANDLE EventHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN EVENT_TYPE EventType, IN BOOLEAN InitialState)
Definition: event.c:100
#define FD_QOS_BIT
Definition: winsock2.h:305
#define FD_CONNECT_BIT
Definition: winsock2.h:301
DWORD SequenceNumber
Definition: msafd.h:90
#define FD_READ
Definition: winsock.h:405
NTSYSAPI NTSTATUS NTAPI NtDeviceIoControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
#define FD_OOB_BIT
Definition: winsock2.h:297
int32_t INT
Definition: typedefs.h:56
#define FD_OOB
Definition: winsock.h:407
#define FD_WRITE
Definition: winsock.h:406
#define AFD_EVENT_CLOSE
Definition: shared.h:208
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define FD_GROUP_QOS
Definition: winsock2.h:308
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
PSOCK_SHARED_INFO SharedData
Definition: msafd.h:100
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define AFD_EVENT_RECEIVE
Definition: shared.h:203
#define FD_GROUP_QOS_BIT
Definition: winsock2.h:307
#define AFD_EVENT_CONNECT
Definition: shared.h:209
#define FD_READ_BIT
Definition: winsock2.h:293
#define IOCTL_AFD_ENUM_NETWORK_EVENTS
Definition: shared.h:335
#define AFD_INFO_BLOCKING_MODE
Definition: shared.h:183
#define AFD_EVENT_SEND
Definition: shared.h:205
BOOLEAN NonBlocking
Definition: msafd.h:74
_In_ HANDLE Handle
Definition: extypes.h:390
#define FD_CLOSE_BIT
Definition: winsock2.h:303
#define AFD_EVENT_ACCEPT
Definition: shared.h:210
#define TRACE(s)
Definition: solgame.cpp:4
#define FD_CLOSE
Definition: winsock.h:410
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
FORCEINLINE DWORD MsafdReturnWithErrno(NTSTATUS Status, LPINT Errno, DWORD Received, LPDWORD ReturnedBytes)
Definition: msafd.h:546
int WSPAPI WSPEventSelect(IN SOCKET Handle, IN WSAEVENT hEventObject, IN long lNetworkEvents, OUT LPINT lpErrno)
Definition: event.c:17
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define AFD_EVENT_OOB_RECEIVE
Definition: shared.h:204
Status
Definition: gdiplustypes.h:24
#define ERR(fmt,...)
Definition: debug.h:109
#define FD_ACCEPT_BIT
Definition: winsock2.h:299
int SetSocketInformation(PSOCKET_INFORMATION Socket, ULONG AfdInformationClass, PBOOLEAN Boolean OPTIONAL, PULONG Ulong OPTIONAL, PLARGE_INTEGER LargeInteger OPTIONAL, LPWSAOVERLAPPED Overlapped OPTIONAL, LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine OPTIONAL)
Definition: dllmain.c:3384
#define AFD_EVENT_QOS
Definition: shared.h:212
INT TranslateNtStatusError(NTSTATUS Status)
Definition: dllmain.c:537
#define OUT
Definition: typedefs.h:39
UINT_PTR SOCKET
Definition: winsock.h:47
#define IOCTL_AFD_EVENT_SELECT
Definition: shared.h:329
LONG NetworkEvents
Definition: msafd.h:111
#define INFINITE
Definition: serial.h:102
return STATUS_SUCCESS
Definition: btrfs.c:2725
LONG AsyncEvents
Definition: msafd.h:92
#define FD_WRITE_BIT
Definition: winsock2.h:295
NTSTATUS EventStatus[AFD_MAX_EVENTS]
Definition: shared.h:132
#define AFD_EVENT_GROUP_QOS
Definition: shared.h:213
#define FD_QOS
Definition: winsock2.h:306
int * LPINT
Definition: windef.h:178
#define WSPAPI
Definition: ws2spi.h:39