ObjFW  Diff

Differences From Artifact [e955701f71]:

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