ObjFW  Diff

Differences From Artifact [fa7926c23d]:

To Artifact [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
{
}