ObjFW  Check-in [4ca583737c]

Overview
Comment:Improve OFSocketObserver API.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4ca583737c9f0a0ae36095a02926f4c18f0bdf115919c3adc4591215f9e4609c
User & Date: js on 2010-06-13 12:15:29
Other Links: manifest | tags
Context
2010-06-13
15:40
Add +[elementWithText:] and +[elementWithComment:] to OFXMLElement. check-in: 29384131f7 user: js tags: trunk
12:15
Improve OFSocketObserver API. check-in: 4ca583737c user: js tags: trunk
03:23
Add OFSocketObserver. check-in: 7390eb7270 user: js tags: trunk
Changes

Modified src/OFSocket.h from [c13d8752b7] to [1c92be9a99].

19
20
21
22
23
24
25
26

27
28

29


30

31
32
33
34
35
36
37
38
39
40
41
42
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







-
+

-
+

+
+
-
+












/**
 * \brief A class which provides functions to create and use sockets.
 */
@interface OFSocket: OFStream
{
@public
#ifndef _WIN32
	int		sock;
	int    sock;
#else
	SOCKET		sock;
	SOCKET sock;
#endif
	BOOL   listening;
@protected
	BOOL		eos;
	BOOL   eos;
}

/**
 * \return A new autoreleased OFTCPSocket
 */
+ socket;

/**
 * Enables/disables non-blocking I/O.
 */
- (void)setBlocking: (BOOL)enable;
@end

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

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
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







+








+
+
+
+
+
+
+
+



-
-
-
-
+

-
+




-
+

-
+







 * 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
/*
 * 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.
 *
 * This callback is also called when a listening socket got a new incoming
 * connection.
 *
 * \param sock The socket which did get ready for reading
 * \param sock The socket which did become ready for reading
 */
- (void)socketDidGetReadyForReading: (OFSocket*)sock;
- (void)socketDidBecomeReadyForReading: (OFSocket*)sock;

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

/**
 * \brief A class that can observe multiple sockets at once.
 */
@interface OFSocketObserver: OFObject
{
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
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







+
+
+
+
+
+
+














+
+
+
+
+
+
+







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

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

13
14
15
16
17
18
19

20
21
22
23
24
25
26
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27







+








#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];
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
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







-
+
+







-
+






-
+







-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








-
+















-
+

-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







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

- (void)addSocketToObserveForReading: (OFSocket*)sock
- (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 |= POLLIN;
			fds_c[i].events |= events;
			found = YES;
		}
	}

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

- (void)addSocketToObserveForWriting: (OFSocket*)sock
- (void)_removeSocket: (OFSocket*)sock
{
	struct pollfd *fds_c = [fds cArray];
	size_t i, nfds = [fds count];
	BOOL found = NO;

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

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

- (void)removeSocketToObserveForReading: (OFSocket*)sock
{
	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 &= ~POLLIN;
			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];
		}
	}
}

- (void)removeSocketToObserveForWriting: (OFSocket*)sock
- (void)addSocketToObserveForIncomingConnections: (OFTCPSocket*)sock
{
	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) {
	[self _addSocket: sock
	      withEvents: POLLIN];
}

- (void)addSocketToObserveForReading: (OFSocket*)sock
			OFAutoreleasePool *pool;

			fds_c[i].events &= ~POLLOUT;

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

			pool = [[OFAutoreleasePool alloc] init];

			[fds removeItemAtIndex: i];
			[fdToSocket removeObjectForKey:
			    [OFNumber numberWithInt: sock->sock]];

			[pool release];
		}
	}
{
	[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];
}

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
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







+
+
+
+
+
-
+





-
+












+
+
+
+
-
+



-
+



	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 socketDidGetReadyForReading: sock];
				[delegate socketDidBecomeReadyForReading: sock];
		}

		if (fds_c[i].revents & POLLOUT) {
			num = [OFNumber numberWithInt: fds_c[i].fd];
			sock = [fdToSocket objectForKey: num];
			[delegate socketDidGetReadyForReading: sock];
			[delegate socketDidBecomeReadyForReading: sock];
		}

		fds_c[i].revents = 0;
	}

	[pool release];

	return ret;
}
@end

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

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

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

Modified src/OFTCPSocket.m from [dd20905993] to [c2b198d896].

310
311
312
313
314
315
316


317
318
319
320
321
322
323
324
325
326


327
328
329
330
331
332
333
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337







+
+










+
+







{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: isa
						     backLog: backlog];

	listening = YES;
}

- (void)listen
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: isa
						     backLog: 5];

	listening = YES;
}

- (OFTCPSocket*)accept
{
	OFTCPSocket *newsock;
	struct sockaddr *addr;
	socklen_t addrlen;
411
412
413
414
415
416
417


418
419
420
421
422
423
424
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430







+
+







- (void)close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	close(sock);
	sock = INVALID_SOCKET;
	listening = NO;
	eos = NO;

	[self freeMemory: sockAddr];
	sockAddr = NULL;
	sockAddrLen = 0;
}

- (void)dealloc