ObjFW  Check-in [7390eb7270]

Overview
Comment:Add OFSocketObserver.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7390eb727040eb21b7caeb358fe4b48050919b413de310ae33a9d3b6a25d6d41
User & Date: js on 2010-06-13 03:23:30
Other Links: manifest | tags
Context
2010-06-13
12:15
Improve OFSocketObserver API. check-in: 4ca583737c user: js tags: trunk
03:23
Add OFSocketObserver. check-in: 7390eb7270 user: js tags: trunk
00:16
Add protocol to type of delegate of OFApplication. check-in: 1009e97dcd user: js tags: trunk
Changes

Modified src/Makefile from [6ef5c721ca] to [861c5b4d8f].

21
22
23
24
25
26
27

28
29
30
31
32
33
34
       OFMutableString.m	\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSeekableStream.m	\
       OFSHA1Hash.m		\
       OFSocket.m		\

       OFStream.m		\
       OFString.m		\
       OFString+Hashing.m	\
       OFString+URLEncoding.m	\
       OFString+XMLEscaping.m	\
       OFString+XMLUnescaping.m	\
       OFTCPSocket.m		\







>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
       OFMutableString.m	\
       OFNumber.m		\
       OFObject.m		\
       ${OFPLUGIN_M}		\
       OFSeekableStream.m	\
       OFSHA1Hash.m		\
       OFSocket.m		\
       OFSocketObserver.m	\
       OFStream.m		\
       OFString.m		\
       OFString+Hashing.m	\
       OFString+URLEncoding.m	\
       OFString+XMLEscaping.m	\
       OFString+XMLUnescaping.m	\
       OFTCPSocket.m		\

Modified src/OFApplication.m from [728d40e47c] to [792653c017].

171
172
173
174
175
176
177
178
179
180

181
182
183
184
185
186
187
- (OFObject <OFApplicationDelegate>*)delegate
{
	return [[delegate retain] autorelease];
}

- (void)setDelegate: (OFObject <OFApplicationDelegate>*)delegate_
{
	id old = delegate;
	delegate = [delegate_ retain];
	[old release];

}

- (void)run
{
	[delegate applicationDidFinishLaunching];
}








<
|
|
>







171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
- (OFObject <OFApplicationDelegate>*)delegate
{
	return [[delegate retain] autorelease];
}

- (void)setDelegate: (OFObject <OFApplicationDelegate>*)delegate_
{

	[delegate_ retain];
	[delegate release];
	delegate = delegate_;
}

- (void)run
{
	[delegate applicationDidFinishLaunching];
}

Modified src/OFNumber.h from [6d290d5b08] to [60fe00fa34].

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	OF_NUMBER_FLOAT,
	OF_NUMBER_DOUBLE,
};

/**
 * \brief Provides a way to store a number in an object.
 */
@interface OFNumber: OFObject
{
	union {
		char	       char_;
		short	       short_;
		int	       int_;
		long	       long_;
		unsigned char  uchar;







|







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	OF_NUMBER_FLOAT,
	OF_NUMBER_DOUBLE,
};

/**
 * \brief Provides a way to store a number in an object.
 */
@interface OFNumber: OFObject <OFCopying>
{
	union {
		char	       char_;
		short	       short_;
		int	       int_;
		long	       long_;
		unsigned char  uchar;

Modified src/OFNumber.m from [8047d2e395] to [892bfa3920].

1000
1001
1002
1003
1004
1005
1006





1007
	case OF_NUMBER_DOUBLE:
		return [OFNumber
		    numberWithDouble: fmod(value.double_, [num doubleValue])];
	default:
		@throw [OFInvalidFormatException newWithClass: isa];
	}
}





@end







>
>
>
>
>

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
	case OF_NUMBER_DOUBLE:
		return [OFNumber
		    numberWithDouble: fmod(value.double_, [num doubleValue])];
	default:
		@throw [OFInvalidFormatException newWithClass: isa];
	}
}

- copy
{
	return [self retain];
}
@end

Modified src/OFSocket.h from [cc8482609a] to [c13d8752b7].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
#endif

/**
 * \brief A class which provides functions to create and use sockets.
 */
@interface OFSocket: OFStream
{

#ifndef _WIN32
	int		sock;
#else
	SOCKET		sock;
#endif
	BOOL		eos;
}







>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#endif

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

Added src/OFSocketObserver.h version [f4eafbf792].











































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 OFDataArray;
@class OFMutableDictionary;

/**
 * \brief A protocol that needs to be implemented by delegates for
 *	  OFSocketObserver.
 */
@protocol OFSocketObserverDelegate
/**
 * 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
 */
- (void)socketDidGetReadyForReading: (OFSocket*)sock;

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

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

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

Added src/OFSocketObserver.m version [e955701f71].

























































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * 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 "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)addSocketToObserveForReading: (OFSocket*)sock
{
	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;
			found = YES;
		}
	}

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

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

	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;

			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
{
	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 &= ~POLLOUT;

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

			pool = [[OFAutoreleasePool alloc] init];

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

			[pool release];
		}
	}
}

- (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];
			sock = [fdToSocket objectForKey: num];
			[delegate socketDidGetReadyForReading: sock];
		}

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

		fds_c[i].revents = 0;
	}

	[pool release];

	return ret;
}
@end

@implementation OFObject (OFSocketObserverDelegate)
- (void)socketDidGetReadyForReading: (OFSocket*)sock
{
}

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

Modified src/ObjFW.h from [491ce3d35b] to [b5f7e1402e].

29
30
31
32
33
34
35

36
37




38

39
40
41
42
43
44
45

#import "OFStream.h"

#import "OFFile.h"

#import "OFSocket.h"
#import "OFTCPSocket.h"


#import "OFHash.h"




#import "OFXMLElement.h"


#import "OFApplication.h"

#import "macros.h"

#ifdef OF_PLUGINS
# import "OFPlugin.h"







>


>
>
>
>

>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#import "OFStream.h"

#import "OFFile.h"

#import "OFSocket.h"
#import "OFTCPSocket.h"
#import "OFSocketObserver.h"

#import "OFHash.h"
#import "OFMD5Hash.h"
#import "OFSHA1Hash.h"

#import "OFXMLAttribute.h"
#import "OFXMLElement.h"
#import "OFXMLParser.h"

#import "OFApplication.h"

#import "macros.h"

#ifdef OF_PLUGINS
# import "OFPlugin.h"