summaryrefslogtreecommitdiff
path: root/win32/select.c
blob: 54840ac0f7fc7765719f6e11e6069d10e64c7f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2003 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 of the PHP license,       |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_0.txt.                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: Wez Furlong <wez@thebrainroom.com>                           |
  +----------------------------------------------------------------------+
*/

#include "php.h"
#include "php_network.h"

/* $Id$ */

/* Win32 select() will only work with sockets, so we roll our own implementation that will
 * get the OS file handle from regular fd's and sockets and then use WaitForMultipleObjects().
 * This implementation is not as feature-full as posix select, but it works for our purposes
 */
PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
{
	HANDLE *handles;
	DWORD waitret;
	DWORD ms_total;
	int f, s, fd_count = 0, sock_count = 0;
	int retval;
	php_socket_t i;
	fd_set ard, awr, aex; /* active fd sets */

	for (i = 0; i < max_fd; i++) {
		if (FD_ISSET(i, rfds) || FD_ISSET(i, wfds) || FD_ISSET(i, efds)) {
			if (_get_osfhandle(i) == 0xffffffff) {
				/* it is a socket */
				sock_count++;
			} else {
				fd_count++;
			}
		}
	}

	if (fd_count + sock_count == 0) {
		return 0;
	}

	handles = (HANDLE*)safe_emalloc((fd_count + sock_count), sizeof(HANDLE), 0);

	/* populate the events and handles arrays */
	f = 0;
	s = 0;
	for (i = 0; i < max_fd; i++) {
		if (FD_ISSET(i, rfds) || FD_ISSET(i, wfds) || FD_ISSET(i, efds)) {
			long h = _get_osfhandle(i);
			if (h == 0xFFFFFFFF) {
				HANDLE evt;
				long evt_flags = 0;

				if (FD_ISSET(i, rfds)) {
					evt_flags |= FD_READ|FD_CONNECT|FD_ACCEPT|FD_CLOSE;
				}
				if (FD_ISSET(i, wfds)) {
					evt_flags |= FD_WRITE;
				}
				if (FD_ISSET(i, efds)) {
					evt_flags |= FD_OOB;
				}

				evt = WSACreateEvent();
				WSAEventSelect(i, evt, evt_flags); 

				handles[fd_count + s] = evt;
				s++;
			} else {
				handles[f++] = (HANDLE)h;
			}
		}
	}

	/* calculate how long we need to wait in milliseconds */
	if (tv == NULL) {
		ms_total = INFINITE;
	} else {
		ms_total = tv->tv_sec * 1000;
		ms_total += tv->tv_usec / 1000;
	}

	waitret = MsgWaitForMultipleObjects(fd_count + sock_count, handles, FALSE, ms_total, QS_ALLEVENTS);

	if (waitret == WAIT_TIMEOUT) {
		retval = 0;
	} else if (waitret == 0xFFFFFFFF) {
		retval = -1;
	} else {

		FD_ZERO(&ard);
		FD_ZERO(&awr);
		FD_ZERO(&aex);

		f = 0;
		retval = 0;
		for (i = 0; i < max_fd; i++) {
			if (FD_ISSET(i, rfds) || FD_ISSET(i, wfds) || FD_ISSET(i, efds)) {
				if (f >= fd_count) {
					/* socket event */
					HANDLE evt = handles[f];

					if (WAIT_OBJECT_0 == WaitForSingleObject(evt, 0)) {
						/* check for various signal states */
						if (FD_ISSET(i, rfds)) {
							WSAEventSelect(i, evt, FD_READ|FD_CONNECT|FD_ACCEPT|FD_CLOSE);
							if (WAIT_OBJECT_0 == WaitForSingleObject(evt, 0)) {
								FD_SET(i, &ard);
							}
						}
						if (FD_ISSET(i, wfds)) {
							WSAEventSelect(i, evt, FD_WRITE);
							if (WAIT_OBJECT_0 == WaitForSingleObject(evt, 0)) {
								FD_SET(i, &awr);
							}
						}
						if (FD_ISSET(i, efds)) {
							WSAEventSelect(i, evt, FD_OOB);
							if (WAIT_OBJECT_0 == WaitForSingleObject(evt, 0)) {
								FD_SET(i, &aex);
							}
						}
						retval++;
					}

					WSACloseEvent(evt);

				} else {
					if (WAIT_OBJECT_0 == WaitForSingleObject(handles[f], 0)) {
						if (FD_ISSET(i, rfds)) {
							FD_SET(i, &ard);
						}
						if (FD_ISSET(i, wfds)) {
							FD_SET(i, &awr);
						}
						if (FD_ISSET(i, efds)) {
							FD_SET(i, &aex);
						}
						retval++;
					}

				}
				f++;
			}
		}

		if (rfds) {
			*rfds = ard;
		}
		if (wfds) {
			*wfds = awr;
		}
		if (efds) {
			*efds = aex;
		}
	}

	efree(handles);

	return retval;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */