ObjFW  Check-in [6a73e7ac51]

Overview
Comment:Fallback to select() in OFSocketObserver if poll() is unavailable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6a73e7ac512d85ca3aa3846cabd75cdc52473940a951fa6a9a5d6765f74768d9
User & Date: js on 2010-06-13 17:44:26
Other Links: manifest | tags
Context
2010-06-13
21:34
Better handling of text and comments in OFXMLElements. check-in: 000706786e user: js tags: trunk
17:44
Fallback to select() in OFSocketObserver if poll() is unavailable. check-in: 6a73e7ac51 user: js tags: trunk
17:42
Fix initialization of mutations in OF{Array,Dictionary}Enumerator. check-in: e0d581d524 user: js tags: trunk
Changes

Modified configure.ac from [c9bac48eee] to [cab2f88d7a].

271
272
273
274
275
276
277





278
279
280
281
282
283
284
	AC_SUBST(ATOMIC_H, "atomic.h")
fi
AC_MSG_RESULT($atomic_ops)

AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket")
AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32")






AC_MSG_CHECKING(for getaddrinfo)
AC_TRY_COMPILE([
	#include <stddef.h>
	#ifndef _WIN32
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netdb.h>







>
>
>
>
>







271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
	AC_SUBST(ATOMIC_H, "atomic.h")
fi
AC_MSG_RESULT($atomic_ops)

AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket")
AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32")

dnl AC_CHECK_HEADER(poll.h,
dnl	[AC_DEFINE(OF_HAVE_POLL, 1, [Whether poll is supported])])
AC_CHECK_HEADERS(sys/select.h,
	[AC_DEFINE(OF_HAVE_SYS_SELECT_H, 1, [Whether we have sys/select.h])])

AC_MSG_CHECKING(for getaddrinfo)
AC_TRY_COMPILE([
	#include <stddef.h>
	#ifndef _WIN32
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netdb.h>

Modified src/OFSocketObserver.h from [3d03b149bd] to [d5659a0309].

1
2
3
4
5
6
7
8
9
10
11




12





13
14
15

16

17
18
19
20
21
22
23
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */





#import "OFObject.h"






@class OFSocket;
@class OFTCPSocket;

@class OFDataArray;

@class OFMutableDictionary;

/**
 * \brief A protocol that needs to be implemented by delegates for
 *	  OFSocketObserver.
 */
@protocol OFSocketObserverDelegate











>
>
>
>

>
>
>
>
>



>

>







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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#if !defined(OF_HAVE_POLL) && defined(OF_HAVE_SYS_SELECT_H)
# include <sys/select.h>
#endif

#import "OFObject.h"

#ifdef _WIN32
# define _WIN32_WINNT 0x0501
# include <windows.h>
#endif

@class OFSocket;
@class OFTCPSocket;
#ifdef OF_HAVE_POLL
@class OFDataArray;
#endif
@class OFMutableDictionary;

/**
 * \brief A protocol that needs to be implemented by delegates for
 *	  OFSocketObserver.
 */
@protocol OFSocketObserverDelegate
46
47
48
49
50
51
52

53





54
55
56
57
58
59
60

/**
 * \brief A class that can observe multiple sockets at once.
 */
@interface OFSocketObserver: OFObject
{
	OFObject <OFSocketObserverDelegate> *delegate;

	OFDataArray *fds;





	OFMutableDictionary *fdToSocket;
}

#ifdef OF_HAVE_PROPERTIES
@property (retain) OFObject <OFSocketObserverDelegate> *delegate;
#endif








>

>
>
>
>
>







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

/**
 * \brief A class that can observe multiple sockets at once.
 */
@interface OFSocketObserver: OFObject
{
	OFObject <OFSocketObserverDelegate> *delegate;
#ifdef OF_HAVE_POLL
	OFDataArray *fds;
#else
	fd_set readfds;
	fd_set writefds;
	int nfds;
#endif
	OFMutableDictionary *fdToSocket;
}

#ifdef OF_HAVE_PROPERTIES
@property (retain) OFObject <OFSocketObserverDelegate> *delegate;
#endif

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

131
132
133
134
135
136
137
 *
 * \param sock The socket to remove from observing for writing
 */
- (void)removeSocketToObserveForWriting: (OFSocket*)sock;

/**
 * Observes all sockets and blocks until an event happens on a socket.
 *
 * \return The number of sockets that have pending events
 */
- (int)observe;

/**
 * Observes all sockets until an event happens on a socket or the timeout is
 * reached.
 *

 * \return The number of sockets that have pending events
 */
- (int)observeWithTimeout: (int)timeout;
@end

@interface OFObject (OFSocketObserverDelegate) <OFSocketObserverDelegate>
@end







<
<

|





>
|

|




132
133
134
135
136
137
138


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 *
 * \param sock The socket to remove from observing for writing
 */
- (void)removeSocketToObserveForWriting: (OFSocket*)sock;

/**
 * Observes all sockets and blocks until an event happens on a socket.


 */
- (void)observe;

/**
 * Observes all sockets until an event happens on a socket or the timeout is
 * reached.
 *
 * \param timeout The time to wait for an event, in milliseconds
 * \return A boolean whether events occurred during the timeinterval
 */
- (BOOL)observeWithTimeout: (int)timeout;
@end

@interface OFObject (OFSocketObserverDelegate) <OFSocketObserverDelegate>
@end

Modified src/OFSocketObserver.m from [fa7926c23d] to [c502161dad].

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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"


#include <poll.h>


#import "OFSocketObserver.h"
#import "OFDataArray.h"
#import "OFDictionary.h"
#import "OFSocket.h"
#import "OFTCPSocket.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"


@implementation OFSocketObserver
+ socketObserver
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];


	fds = [[OFDataArray alloc] initWithItemSize: sizeof(struct pollfd)];




	fdToSocket = [[OFMutableDictionary alloc] init];

	return self;
}

- (void)dealloc
{
	[delegate release];

	[fds release];

	[fdToSocket release];

	[super dealloc];
}

- (OFObject <OFSocketObserverDelegate>*)delegate
{
	return [[delegate retain] autorelease];
}

- (void)setDelegate: (OFObject <OFSocketObserverDelegate>*)delegate_
{
	[delegate_ retain];
	[delegate release];
	delegate = delegate_;
}


- (void)_addSocket: (OFSocket*)sock
	withEvents: (short)events
{
	struct pollfd *fds_c = [fds cArray];
	size_t i, count = [fds count];
	BOOL found = NO;














>
|
>








>











>

>
>
>
>








>

>

















>







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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#ifdef OF_HAVE_POLL
# include <poll.h>
#endif

#import "OFSocketObserver.h"
#import "OFDataArray.h"
#import "OFDictionary.h"
#import "OFSocket.h"
#import "OFTCPSocket.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

@implementation OFSocketObserver
+ socketObserver
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

#ifdef OF_HAVE_POLL
	fds = [[OFDataArray alloc] initWithItemSize: sizeof(struct pollfd)];
#else
	FD_ZERO(&readfds);
	FD_ZERO(&writefds);
#endif
	fdToSocket = [[OFMutableDictionary alloc] init];

	return self;
}

- (void)dealloc
{
	[delegate release];
#ifdef OF_HAVE_POLL
	[fds release];
#endif
	[fdToSocket release];

	[super dealloc];
}

- (OFObject <OFSocketObserverDelegate>*)delegate
{
	return [[delegate retain] autorelease];
}

- (void)setDelegate: (OFObject <OFSocketObserverDelegate>*)delegate_
{
	[delegate_ retain];
	[delegate release];
	delegate = delegate_;
}

#ifdef OF_HAVE_POLL
- (void)_addSocket: (OFSocket*)sock
	withEvents: (short)events
{
	struct pollfd *fds_c = [fds cArray];
	size_t i, count = [fds count];
	BOOL found = NO;

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
			[fdToSocket removeObjectForKey:
			    [OFNumber numberWithInt: sock->sock]];

			[pool release];
		}
	}
}








































- (void)addSocketToObserveForIncomingConnections: (OFTCPSocket*)sock
{

	[self _addSocket: sock
	      withEvents: POLLIN];




}

- (void)addSocketToObserveForReading: (OFSocket*)sock
{

	[self _addSocket: sock
	      withEvents: POLLIN];




}

- (void)addSocketToObserveForWriting: (OFSocket*)sock
{

	[self _addSocket: sock
	      withEvents: POLLOUT];




}

- (void)removeSocketToObserveForIncomingConnections: (OFTCPSocket*)sock
{

	[self _removeSocket: sock
		 withEvents: POLLIN];




}

- (void)removeSocketToObserveForReading: (OFSocket*)sock
{

	[self _removeSocket: sock
		 withEvents: POLLIN];




}

- (void)removeSocketToObserveForWriting: (OFSocket*)sock
{

	[self _removeSocket: sock
		 withEvents: POLLOUT];




}

- (int)observe
{
	return [self observeWithTimeout: -1];
}

- (int)observeWithTimeout: (int)timeout
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	struct pollfd *fds_c = [fds cArray];
	size_t i, nfds = [fds count];
	int ret = poll(fds_c, nfds, timeout);

	if (ret <= 0)
		return ret;

	for (i = 0; i < nfds; i++) {
		OFNumber *num;
		OFSocket *sock;

		if (fds_c[i].revents & POLLIN) {
			num = [OFNumber numberWithInt: fds_c[i].fd];







>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>


>
>
>
>




>


>
>
>
>




>


>
>
>
>




>


>
>
>
>




>


>
>
>
>




>


>
>
>
>


|

|


|


>


<

|
|







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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
			[fdToSocket removeObjectForKey:
			    [OFNumber numberWithInt: sock->sock]];

			[pool release];
		}
	}
}
#else
- (void)_addSocket: (OFSocket*)sock
	 withFDSet: (fd_set*)fdset
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	if (sock->sock >= FD_SETSIZE)
		@throw [OFOutOfRangeException newWithClass: isa];

	FD_SET(sock->sock, fdset);

	if (sock->sock >= nfds)
		nfds = sock->sock + 1;

	[fdToSocket setObject: sock
		       forKey: [OFNumber numberWithInt: sock->sock]];

	[pool release];
}

- (void)_removeSocket: (OFSocket*)sock
	    withFDSet: (fd_set*)fdset
{
	if (sock->sock >= FD_SETSIZE)
		@throw [OFOutOfRangeException newWithClass: isa];

	FD_CLR(sock->sock, fdset);

	if (!FD_ISSET(sock->sock, &readfds) &&
	    !FD_ISSET(sock->sock, &writefds)) {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

		[fdToSocket removeObjectForKey:
		    [OFNumber numberWithInt: sock->sock]];

		[pool release];
	}
}
#endif

- (void)addSocketToObserveForIncomingConnections: (OFTCPSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _addSocket: sock
	      withEvents: POLLIN];
#else
	[self _addSocket: sock
	       withFDSet: &readfds];
#endif
}

- (void)addSocketToObserveForReading: (OFSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _addSocket: sock
	      withEvents: POLLIN];
#else
	[self _addSocket: sock
	       withFDSet: &readfds];
#endif
}

- (void)addSocketToObserveForWriting: (OFSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _addSocket: sock
	      withEvents: POLLOUT];
#else
	[self _addSocket: sock
	       withFDSet: &writefds];
#endif
}

- (void)removeSocketToObserveForIncomingConnections: (OFTCPSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _removeSocket: sock
		 withEvents: POLLIN];
#else
	[self _removeSocket: sock
		  withFDSet: &readfds];
#endif
}

- (void)removeSocketToObserveForReading: (OFSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _removeSocket: sock
		 withEvents: POLLIN];
#else
	[self _removeSocket: sock
		  withFDSet: &readfds];
#endif
}

- (void)removeSocketToObserveForWriting: (OFSocket*)sock
{
#ifdef OF_HAVE_POLL
	[self _removeSocket: sock
		 withEvents: POLLOUT];
#else
	[self _removeSocket: sock
		  withFDSet: &writefds];
#endif
}

- (void)observe
{
	[self observeWithTimeout: -1];
}

- (BOOL)observeWithTimeout: (int)timeout
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
#ifdef OF_HAVE_POLL
	struct pollfd *fds_c = [fds cArray];
	size_t i, nfds = [fds count];


	if (poll(fds_c, nfds, timeout) < 1)
		return NO;

	for (i = 0; i < nfds; i++) {
		OFNumber *num;
		OFSocket *sock;

		if (fds_c[i].revents & POLLIN) {
			num = [OFNumber numberWithInt: fds_c[i].fd];
178
179
180
181
182
183
184







185























186
187
188
189
190
191
192
193
194
195
			num = [OFNumber numberWithInt: fds_c[i].fd];
			sock = [fdToSocket objectForKey: num];
			[delegate socketDidBecomeReadyForReading: sock];
		}

		fds_c[i].revents = 0;
	}































	[pool release];

	return ret;
}
@end

@implementation OFObject (OFSocketObserverDelegate)
- (void)socketDidReceiveIncomingConnection: (OFTCPSocket*)sock
{
}







>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|







258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
			num = [OFNumber numberWithInt: fds_c[i].fd];
			sock = [fdToSocket objectForKey: num];
			[delegate socketDidBecomeReadyForReading: sock];
		}

		fds_c[i].revents = 0;
	}
#else
	fd_set readfds_;
	fd_set writefds_;
	fd_set exceptfds_;
	struct timeval tv;
	OFEnumerator *enumerator;
	OFSocket *sock;

	readfds_ = readfds;
	writefds_ = writefds;
	FD_ZERO(&exceptfds_);

	if (select(nfds, &readfds_, &writefds_, &exceptfds_,
	    (timeout != -1 ? &tv : NULL)) < 1)
		return NO;

	enumerator = [[[fdToSocket copy] autorelease] objectEnumerator];

	while ((sock = [enumerator nextObject]) != nil) {
		if (FD_ISSET(sock->sock, &readfds_)) {
			if (sock->listening)
				[delegate socketDidReceiveIncomingConnection:
				    (OFTCPSocket*)sock];
			else
				[delegate socketDidBecomeReadyForReading: sock];
		}

		if (FD_ISSET(sock->sock, &writefds_))
			[delegate socketDidBecomeReadyForWriting: sock];
	}
#endif
	[pool release];

	return YES;
}
@end

@implementation OFObject (OFSocketObserverDelegate)
- (void)socketDidReceiveIncomingConnection: (OFTCPSocket*)sock
{
}

Modified src/OFTCPSocket.m from [c2b198d896] to [57db58ad3d].

34
35
36
37
38
39
40




41
42
43
44
45
46
47

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
# import "OFThread.h"
# import "OFDataArray.h"

static OFMutex *mutex = nil;
#endif





@implementation OFTCPSocket
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		mutex = [[OFMutex alloc] init];







>
>
>
>







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
# import "OFThread.h"
# import "OFDataArray.h"

static OFMutex *mutex = nil;
#endif

#ifdef _WIN32
# define close(sock) closesocket(sock)
#endif

@implementation OFTCPSocket
#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		mutex = [[OFMutex alloc] init];

Modified src/objfw-defs.h.in from [5c69853c4a] to [b3381e8211].

1
2
3
4
5
6
7
8
9

10
11
12
13

14
15
16
17
#undef OF_APPLE_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_ATOMIC_OPS
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_HAVE_FAST_ENUMERATION
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H

#undef OF_HAVE_PROPERTIES
#undef OF_HAVE_PTHREADS
#undef OF_HAVE_PTHREAD_SPINLOCKS
#undef OF_HAVE_SCHED_YIELD

#undef OF_OBJFW_RUNTIME
#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX









>




>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#undef OF_APPLE_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_ATOMIC_OPS
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_HAVE_FAST_ENUMERATION
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H
#undef OF_HAVE_POLL
#undef OF_HAVE_PROPERTIES
#undef OF_HAVE_PTHREADS
#undef OF_HAVE_PTHREAD_SPINLOCKS
#undef OF_HAVE_SCHED_YIELD
#undef OF_HAVE_SYS_SELECT_H
#undef OF_OBJFW_RUNTIME
#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX