ObjFW  Check-in [52dcb22b8c]

Overview
Comment:Rename OFSocketObserver to OFStreamObserver and make it more general.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 52dcb22b8c2fbdc5e409ac070be1881cdb663f90c1388c2d27ef7e5a510757d5
User & Date: js on 2010-07-07 20:48:07
Other Links: manifest | tags
Context
2010-07-07
20:57
Rename OFSocket to OFStreamSocket. check-in: 28ea4a8a67 user: js tags: trunk
20:48
Rename OFSocketObserver to OFStreamObserver and make it more general. check-in: 52dcb22b8c user: js tags: trunk
20:46
Add -[fileDescriptor] to OFStream. check-in: 9d064462a3 user: js tags: trunk
Changes

Deleted src/OFSocketObserver.h version [d5659a0309].

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
/*
 * 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
/*
 * This callback is called when a listening socket got a new incoming
 * connection.
 *
 * \param sock The socket which did receive an incoming connection
 */
- (void)socketDidReceiveIncomingConnection: (OFTCPSocket*)sock;

/**
 * This callback is called when a socket did get ready for reading.
 *
 * \param sock The socket which did become ready for reading
 */
- (void)socketDidBecomeReadyForReading: (OFSocket*)sock;

/**
 * This callback is called when a socket did get ready for writing.
 *
 * \param sock The socket which did become ready for writing
 */
- (void)socketDidBecomeReadyForWriting: (OFSocket*)sock;
@end

/**
 * \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

/**
 * \return A new, autoreleased OFSocketObserver
 */
+ socketObserver;

/**
 * \return The delegate for the OFSocketObserver
 */
- (OFObject <OFSocketObserverDelegate>*)delegate;

/**
 * Sets the delegate for the OFSocketObserver.
 *
 * \param delegate The delegate for the OFSocketObserver
 */
- (void)setDelegate: (OFObject <OFSocketObserverDelegate>*)delegate;

/**
 * Adds a socket to observe for incoming connections.
 *
 * \param sock The socket to observe for incoming connections
 */
- (void)addSocketToObserveForIncomingConnections: (OFTCPSocket*)sock;

/**
 * Adds a socket to observe for reading.
 *
 * \param sock The socket to observe for reading
 */
- (void)addSocketToObserveForReading: (OFSocket*)sock;

/**
 * Adds a socket to observe for writing.
 *
 * \param sock The socket to observe for writing
 */
- (void)addSocketToObserveForWriting: (OFSocket*)sock;

/**
 * Removes a socket to observe for incoming connections.
 *
 * \param sock The socket to remove from observing for incoming connections
 */
- (void)removeSocketToObserveForIncomingConnections: (OFTCPSocket*)sock;

/**
 * Removes a socket to observe for reading.
 *
 * \param sock The socket to remove from observing for reading
 */
- (void)removeSocketToObserveForReading: (OFSocket*)sock;

/**
 * Removes a socket to observe for writing.
 *
 * \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
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


















































































































































































































































































































Deleted src/OFSocketObserver.m version [7ff6b2b470].

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
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
248
249
250
251
252
253
254
255
256
257
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
306
307
308
309
310
311
312
313
314
315
316
/*
 * 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 <string.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;

	for (i = 0; i < count; i++) {
		if (fds_c[i].fd == sock->sock) {
			fds_c[i].events |= events;
			found = YES;
		}
	}

	if (!found) {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		struct pollfd p = { sock->sock, events, 0 };
		[fds addItem: &p];
		[fdToSocket setObject: sock
			       forKey: [OFNumber numberWithInt: sock->sock]];
		[pool release];
	}
}

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

	for (i = 0; i < nfds; i++) {
		if (fds_c[i].fd == sock->sock) {
			OFAutoreleasePool *pool;

			fds_c[i].events &= ~events;

			if (fds_c[i].events != 0)
				return;

			pool = [[OFAutoreleasePool alloc] init];

			[fds removeItemAtIndex: i];
			[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];
			sock = [fdToSocket objectForKey: num];

			if (sock->listening)
				[delegate socketDidReceiveIncomingConnection:
				    (OFTCPSocket*)sock];
			else
				[delegate socketDidBecomeReadyForReading: sock];
		}

		if (fds_c[i].revents & POLLOUT) {
			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
{
}

- (void)socketDidBecomeReadyForReading: (OFSocket*)sock
{
}

- (void)socketDidBecomeReadyForWriting: (OFSocket*)sock
{
}
@end
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
























































































































































































































































































































































































































































































































































































































































Added src/OFStreamObserver.h version [c015b1c190].







































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 OFStream;
#ifdef OF_HAVE_POLL
@class OFDataArray;
#endif
@class OFMutableDictionary;

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

/**
 * This callback is called when a stream did get ready for reading.
 *
 * \param stream The stream which did become ready for reading
 */
- (void)streamDidBecomeReadyForReading: (OFStream*)stream;

/**
 * This callback is called when a stream did get ready for writing.
 *
 * \param stream The stream which did become ready for writing
 */
- (void)streamDidBecomeReadyForWriting: (OFStream*)stream;
@end

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

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

/**
 * \return A new, autoreleased OFStreamObserver
 */
+ streamObserver;

/**
 * \return The delegate for the OFStreamObserver
 */
- (OFObject <OFStreamObserverDelegate>*)delegate;

/**
 * Sets the delegate for the OFStreamObserver.
 *
 * \param delegate The delegate for the OFStreamObserver
 */
- (void)setDelegate: (OFObject <OFStreamObserverDelegate>*)delegate;

/**
 * Adds a stream to observe for reading.
 *
 * \param stream The stream to observe for reading
 */
- (void)addStreamToObserveForReading: (OFStream*)stream;

/**
 * Adds a stream to observe for writing.
 *
 * \param stream The stream to observe for writing
 */
- (void)addStreamToObserveForWriting: (OFStream*)stream;

/**
 * Removes a stream to observe for reading.
 *
 * \param stream The stream to remove from observing for reading
 */
- (void)removeStreamToObserveForReading: (OFStream*)stream;

/**
 * Removes a stream to observe for writing.
 *
 * \param stream The stream to remove from observing for writing
 */
- (void)removeStreamToObserveForWriting: (OFStream*)stream;

/**
 * Observes all streams and blocks until an event happens on a stream.
 */
- (void)observe;

/**
 * Observes all streams until an event happens on a stream 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 (OFStreamObserverDelegate) <OFStreamObserverDelegate>
@end

Added src/OFStreamObserver.m version [282f7af8d3].

























































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
248
249
250
251
252
253
254
255
256
257
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
/*
 * 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 <string.h>

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

#import "OFStreamObserver.h"
#import "OFDataArray.h"
#import "OFDictionary.h"
#import "OFStream.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"

@implementation OFStreamObserver
+ streamObserver
{
	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
	fdToStream = [[OFMutableDictionary alloc] init];

	return self;
}

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

	[super dealloc];
}

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

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

#ifdef OF_HAVE_POLL
- (void)_addStream: (OFStream*)stream
	withEvents: (short)events
{
	struct pollfd *fds_c = [fds cArray];
	size_t i, count = [fds count];
	int fd = [stream fileDescriptor];
	BOOL found = NO;

	for (i = 0; i < count; i++) {
		if (fds_c[i].fd == fd) {
			fds_c[i].events |= events;
			found = YES;
		}
	}

	if (!found) {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		struct pollfd p = { fd, events, 0 };
		[fds addItem: &p];
		[fdToStream setObject: stream
			       forKey: [OFNumber numberWithInt: fd]];
		[pool release];
	}
}

- (void)_removeStream: (OFStream*)stream
	   withEvents: (short)events
{
	struct pollfd *fds_c = [fds cArray];
	size_t i, nfds = [fds count];
	int fd = [stream fileDescriptor];

	for (i = 0; i < nfds; i++) {
		if (fds_c[i].fd == fd) {
			OFAutoreleasePool *pool;

			fds_c[i].events &= ~events;

			if (fds_c[i].events != 0)
				return;

			pool = [[OFAutoreleasePool alloc] init];

			[fds removeItemAtIndex: i];
			[fdToStream removeObjectForKey:
			    [OFNumber numberWithInt: fd]];

			[pool release];
		}
	}
}
#else
- (void)_addStream: (OFStream*)stream
	 withFDSet: (fd_set*)fdset
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	int fd = [stream fileDescriptor];

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

	FD_SET(fd, fdset);

	if (fd >= nfds)
		nfds = fd + 1;

	[fdToStream setObject: stream
		       forKey: [OFNumber numberWithInt: fd]];

	[pool release];
}

- (void)_removeStream: (OFStream*)stream
	    withFDSet: (fd_set*)fdset
{
	int fd = [stream fileDescriptor];

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

	FD_CLR(fd, fdset);

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

		[fdToStream removeObjectForKey: [OFNumber numberWithInt: fd]];

		[pool release];
	}
}
#endif

- (void)addStreamToObserveForReading: (OFStream*)stream
{
#ifdef OF_HAVE_POLL
	[self _addStream: stream
	      withEvents: POLLIN];
#else
	[self _addStream: stream
	       withFDSet: &readfds];
#endif
}

- (void)addStreamToObserveForWriting: (OFStream*)stream
{
#ifdef OF_HAVE_POLL
	[self _addStream: stream
	      withEvents: POLLOUT];
#else
	[self _addStream: stream
	       withFDSet: &writefds];
#endif
}

- (void)removeStreamToObserveForReading: (OFStream*)stream
{
#ifdef OF_HAVE_POLL
	[self _removeStream: stream
		 withEvents: POLLIN];
#else
	[self _removeStream: stream
		  withFDSet: &readfds];
#endif
}

- (void)removeStreamToObserveForWriting: (OFStream*)stream
{
#ifdef OF_HAVE_POLL
	[self _removeStream: stream
		 withEvents: POLLOUT];
#else
	[self _removeStream: stream
		  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;
		OFStream *stream;

		if (fds_c[i].revents & POLLIN) {
			num = [OFNumber numberWithInt: fds_c[i].fd];
			stream = [fdToStream objectForKey: num];
			[delegate streamDidBecomeReadyForReading: stream];
		}

		if (fds_c[i].revents & POLLOUT) {
			num = [OFNumber numberWithInt: fds_c[i].fd];
			stream = [fdToStream objectForKey: num];
			[delegate streamDidBecomeReadyForReading: stream];
		}

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

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

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

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

	while ((stream = [enumerator nextObject]) != nil) {
		int fd = [stream fileDescriptor];

		if (FD_ISSET(fd, &readfds_))
			[delegate streamDidBecomeReadyForReading: stream];

		if (FD_ISSET(fd, &writefds_))
			[delegate streamDidBecomeReadyForWriting: stream];
	}
#endif
	[pool release];

	return YES;
}
@end

@implementation OFObject (OFStreamObserverDelegate)
- (void)streamDidBecomeReadyForReading: (OFStream*)stream
{
}

- (void)streamDidBecomeReadyForWriting: (OFStream*)stream
{
}
@end