ObjFW  Check-in [6638379972]

Overview
Comment:OFTCPSocket: Async connect without threads
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6638379972b362bf6bc84526204690e6261202b6c79d2f3aad2baa5abc2b1d5c
User & Date: js on 2018-09-16 12:49:39
Other Links: manifest | tags
Context
2018-09-23
15:19
OFTCPSocket: SOCKS5 support for async connect check-in: 228ac2af9b user: js tags: trunk
2018-09-16
12:49
OFTCPSocket: Async connect without threads check-in: 6638379972 user: js tags: trunk
2018-09-15
23:35
OFDNSResolver: Send queries for unresolved CNAMEs check-in: ae92690f92 user: js tags: trunk
Changes

Modified configure.ac from [0dca5ff784] to [0289c3600c].

1395
1396
1397
1398
1399
1400
1401
1402
1403
1404

1405
1406
1407

1408
1409
1410
1411
1412
1413
1414
1395
1396
1397
1398
1399
1400
1401

1402

1403

1404

1405
1406
1407
1408
1409
1410
1411
1412







-

-
+
-

-
+







			])

			AC_MSG_RESULT($have_threadsafe_getaddrinfo)
		])
	], [
		AC_MSG_RESULT(no)
	])
])

AS_IF([test x"$enable_sockets" != x"no" -a x"$enable_threads" != x"no"], [
	AS_IF([test x"$enable_threads" != x"no"], [
	AC_SUBST(OFHTTPCLIENT_M, "OFHTTPClient.m")
	AC_SUBST(OFHTTPCLIENTTESTS_M, "OFHTTPClientTests.m")
	AC_SUBST(OFURLHANDLER_HTTP_M, "OFURLHandler_HTTP.m")
	])

	AC_SUBST(OFDNS, "ofdns")
	AS_IF([test x"$enable_files" != x"no"], [
		AC_SUBST(OFHTTP, "ofhttp")
	])
])

Modified extra.mk.in from [99dd00d3f1] to [a72b9c97f0].

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
53
54
55
56
57
58
59

60
61
62
63
64
65

66
67
68
69
70
71
72







-






-







MAP_LDFLAGS = @MAP_LDFLAGS@
OFARC = @OFARC@
OFBLOCKTESTS_M = @OFBLOCKTESTS_M@
OFDNS = @OFDNS@
OFHASH = @OFHASH@
OFHTTP = @OFHTTP@
OFHTTPCLIENTTESTS_M = @OFHTTPCLIENTTESTS_M@
OFHTTPCLIENT_M = @OFHTTPCLIENT_M@
OFKERNELEVENTOBSERVER_EPOLL_M = @OFKERNELEVENTOBSERVER_EPOLL_M@
OFKERNELEVENTOBSERVER_KQUEUE_M = @OFKERNELEVENTOBSERVER_KQUEUE_M@
OFKERNELEVENTOBSERVER_POLL_M = @OFKERNELEVENTOBSERVER_POLL_M@
OFKERNELEVENTOBSERVER_SELECT_M = @OFKERNELEVENTOBSERVER_SELECT_M@
OFPROCESS_M = @OFPROCESS_M@
OFSTDIOSTREAM_WIN32CONSOLE_M = @OFSTDIOSTREAM_WIN32CONSOLE_M@
OFURLHANDLER_HTTP_M = @OFURLHANDLER_HTTP_M@
REEXPORT_RUNTIME = @REEXPORT_RUNTIME@
REEXPORT_RUNTIME_FRAMEWORK = @REEXPORT_RUNTIME_FRAMEWORK@
RUNTIME = @RUNTIME@
RUNTIME_FRAMEWORK_LIBS = @RUNTIME_FRAMEWORK_LIBS@
RUNTIME_LIBS = @RUNTIME_LIBS@
RUN_TESTS = @RUN_TESTS@
SFDC_TARGET = @SFDC_TARGET@

Modified src/Makefile from [82b76260e6] to [a4eb0eb187].

121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
121
122
123
124
125
126
127

128
129
130
131
132
133
134
135







-
+







	     OFINICategory.m		\
	     OFINIFile.m		\
	     OFSettings.m		\
	     OFString+PathAdditions.m
SRCS_PLUGINS = OFPlugin.m
SRCS_SOCKETS = OFDNSResolver.m			\
	       OFDNSResourceRecord.m		\
	       ${OFHTTPCLIENT_M}		\
	       OFHTTPClient.m			\
	       OFHTTPCookie.m			\
	       OFHTTPCookieManager.m		\
	       OFHTTPRequest.m			\
	       OFHTTPResponse.m			\
	       OFHTTPServer.m			\
	       OFStreamSocket.m			\
	       OFTCPSocket.m			\
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
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







-

















-
+
+







	OFLHAArchive_LHStream.m		\
	OFMutableArray_adjacent.m	\
	OFMutableDictionary_hashtable.m	\
	OFMutableSet_hashtable.m	\
	OFMutableString_UTF8.m		\
	OFSet_hashtable.m		\
	OFString_UTF8.m			\
	${OFURLHANDLER_HTTP_M}		\
	OFValue_bytes.m			\
	OFValue_dimension.m		\
	OFValue_nonretainedObject.m	\
	OFValue_point.m			\
	OFValue_pointer.m		\
	OFValue_range.m			\
	OFValue_rectangle.m		\
	${AUTORELEASE_M}		\
	${FOUNDATION_COMPAT_M}		\
	${INSTANCE_M}
SRCS_FILES += OFSettings_INIFile.m	\
	      OFURLHandler_file.m
SRCS_SOCKETS += OFKernelEventObserver.m			\
		${OFKERNELEVENTOBSERVER_EPOLL_M}	\
		${OFKERNELEVENTOBSERVER_KQUEUE_M}	\
		${OFKERNELEVENTOBSERVER_POLL_M}		\
		${OFKERNELEVENTOBSERVER_SELECT_M}	\
		OFTCPSocket+SOCKS5.m
		OFTCPSocket+SOCKS5.m			\
		OFURLHandler_HTTP.m

OBJS_EXTRA = ${RUNTIME_RUNTIME_A}	\
	     ${EXCEPTIONS_EXCEPTIONS_A} \
	     ${ENCODINGS_ENCODINGS_A}	\
	     ${FORWARDING_FORWARDING_A}	\
	     ${INVOCATION_INVOCATION_A}
LIB_OBJS_EXTRA = ${RUNTIME_RUNTIME_LIB_A}	\

Modified src/OFTCPSocket.h from [0973252c95] to [0871d5445b].

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
149
150
151
152
153
154
155

156
157
158
159
160
161
162







-







 *
 * @param host The host to connect to
 * @param port The port on the host to connect to
 */
- (void)connectToHost: (OFString *)host
		 port: (uint16_t)port;

#ifdef OF_HAVE_THREADS
/*!
 * @brief Asynchronously connect the OFTCPSocket to the specified destination.
 *
 * @param host The host to connect to
 * @param port The port on the host to connect to
 * @param target The target on which to call the selector once the connection
 *		 has been established
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
178
179
180
181
182
183
184

185
186
187
188
189
190
191







-







 * @param port The port on the host to connect to
 * @param block The block to execute once the connection has been established
 */
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		     block: (of_tcp_socket_async_connect_block_t)block;
# endif
#endif

/*!
 * @brief Bind the socket to the specified host and port.
 *
 * @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for
 *	       IPv6 to bind to all.
 * @param port The port to bind to. If the port is 0, an unused port will be

Modified src/OFTCPSocket.m from [3cac08541a] to [e3252f358c].

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







+
+
+
+



-
-







+







#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

#import "OFTCPSocket.h"
#import "OFTCPSocket+Private.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"

#import "OFAcceptFailedException.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFListenFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFSetOptionFailedException.h"

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







-
-
+

-










+
+


-
+
-






-
+
-


-
+
-

+
+
+
+
+
+
+
+
+
+
+


-
-
+
+
-









-















-
+
-







-















-








+






-
-















+
-
+
+

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


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


-







}

Class of_tls_socket_class = Nil;

static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;

#ifdef OF_HAVE_THREADS
@interface OFTCPSocket_ConnectThread: OFThread
@interface OFTCPSocket_ConnectContext: OFObject
{
	OFThread *_sourceThread;
	OFTCPSocket *_socket;
	OFString *_host;
	uint16_t _port;
	id _target;
	SEL _selector;
	id _context;
# ifdef OF_HAVE_BLOCKS
	of_tcp_socket_async_connect_block_t _block;
# endif
	id _exception;
	OFData *_socketAddresses;
	size_t _socketAddressesIndex;
}

- (instancetype)initWithSourceThread: (OFThread *)sourceThread
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			      socket: (OFTCPSocket *)sock
				host: (OFString *)host
				port: (uint16_t)port
			      target: (id)target
			    selector: (SEL)selector
			     context: (id)context;
# ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSourceThread: (OFThread *)sourceThread
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			      socket: (OFTCPSocket *)sock
				host: (OFString *)host
				port: (uint16_t)port
			       block: (of_tcp_socket_async_connect_block_t)
			 block: (of_tcp_socket_async_connect_block_t)block;
					  block;
# endif
- (void)didConnect;
- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception;
- (void)tryNextAddress;
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
       socketAddresses: (OFData *)socketAddresses
	       context: (id)context
	     exception: (id)exception;
- (void)start;
@end

@implementation OFTCPSocket_ConnectThread
- (instancetype)initWithSourceThread: (OFThread *)sourceThread
@implementation OFTCPSocket_ConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			      socket: (OFTCPSocket *)sock
				host: (OFString *)host
				port: (uint16_t)port
			      target: (id)target
			    selector: (SEL)selector
			     context: (id)context
{
	self = [super init];

	@try {
		_sourceThread = [sourceThread retain];
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_target = [target retain];
		_selector = selector;
		_context = [context retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

# ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSourceThread: (OFThread *)sourceThread
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			      socket: (OFTCPSocket *)sock
				host: (OFString *)host
				port: (uint16_t)port
			       block: (of_tcp_socket_async_connect_block_t)block
{
	self = [super init];

	@try {
		_sourceThread = [sourceThread retain];
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_block = [block copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
# endif

- (void)dealloc
{
	[_sourceThread release];
	[_socket release];
	[_host release];
	[_target release];
	[_context release];
# ifdef OF_HAVE_BLOCKS
	[_block release];
# endif
	[_exception release];
	[_socketAddresses release];

	[super dealloc];
}

- (void)didConnect
{
	[self join];

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
		_block(_socket, _exception);
	else {
# endif
		void (*func)(id, SEL, OFTCPSocket *, id, id) =
		    (void (*)(id, SEL, OFTCPSocket *, id, id))
		    [_target methodForSelector: _selector];

		func(_target, _selector, _socket, _context, _exception);
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

- (void)socketDidConnect: (OFTCPSocket *)sock
- (id)main
		 context: (id)context
	       exception: (id)exception
{
	if (exception != nil) {
		if (_socketAddressesIndex >= [_socketAddresses count])
			_exception = [exception retain];
		else {
			[self tryNextAddress];
			return;
		}
	}
	void *pool = objc_autoreleasePoolPush();

	@try {
		[_socket connectToHost: _host
				  port: _port];
	} @catch (id e) {
		_exception = [e retain];

	[self didConnect];
}

- (void)tryNextAddress
{
	of_socket_address_t address = *(const of_socket_address_t *)
	    [_socketAddresses itemAtIndex: _socketAddressesIndex++];
	int errNo;

	of_socket_address_set_port(&address, _port);

	if (![_socket of_createSocketForAddress: &address
					  errNo: &errNo]) {
		if (_socketAddressesIndex >= [_socketAddresses count]) {
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port
				  socket: _socket
				   errNo: errNo];
			[self didConnect];
			return;
		}

		[self tryNextAddress];
		return;
	}

	[_socket setBlocking: false];

	if (![_socket of_connectSocketToAddress: &address
					  errNo: &errNo]) {
		if (errNo == EINPROGRESS) {
			SEL selector = @selector(socketDidConnect:context:
			    exception:);

			[OFRunLoop of_addAsyncConnectForTCPSocket: _socket
							   target: self
							 selector: selector
							  context: nil];
			return;
		} else {
			[_socket of_closeSocket];

			if (_socketAddressesIndex >= [_socketAddresses count]) {
				_exception = [[OFConnectionFailedException
				    alloc] initWithHost: _host
						   port: _port
						 socket: _socket
						  errNo: errNo];
				[self didConnect];
				return;
			}

			[self tryNextAddress];
			return;
		}
	}

	[self didConnect];
}

-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
       socketAddresses: (OFData *)socketAddresses
	       context: (id)context
	     exception: (id)exception
{
	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return;
	}

	_socketAddresses = [socketAddresses copy];
	[self tryNextAddress];
}

- (void)start
{
	@try {
		of_socket_address_t address =
		    of_socket_address_parse_ip(_host, _port);

		_socketAddresses = [[OFData alloc]
		    initWithItems: &address
			 itemSize: sizeof(address)
			    count: 1];

		[self tryNextAddress];
		return;
	} @catch (OFInvalidFormatException *e) {
	}

	[[OFThread DNSResolver]
	    asyncResolveSocketAddressesForHost: _host
					target: self
	[self performSelector: @selector(didConnect)
				      selector: @selector(resolver:
		     onThread: _sourceThread
		waitUntilDone: false];

						    didResolveDomainName:
						    socketAddresses:context:
	objc_autoreleasePoolPop(pool);

	return nil;
						    exception:)
				       context: nil];
}
@end
#endif

@implementation OFTCPSocket
@synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port;

+ (void)setSOCKS5Host: (OFString *)host
{
	id old = defaultSOCKS5Host;
382
383
384
385
386
387
388
389
390
391
392
393
394
395

396
397
398

399
400

401
402
403
404
405
406
407
408
409
410
411
412
413
414

415
416
417

418
419

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
482
483
484
485
486
487
488

489
490
491
492
493
494
495
496
497

498


499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

517


518
519
520
521
522
523
524
525

526
527
528
529
530
531
532







-






+


-
+
-
-
+














+


-
+
-
-
+







-







								errNo: errNo];

	if (_SOCKS5Host != nil)
		[self OF_SOCKS5ConnectToHost: destinationHost
					port: destinationPort];
}

#ifdef OF_HAVE_THREADS
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)context
{
	/* TODO: Support SOCKS5 */
	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectThread alloc]
	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSourceThread: [OFThread currentThread]
			  socket: self
	    initWithSocket: self
			    host: host
			    port: port
			  target: target
			selector: selector
			 context: context] autorelease] start];

	objc_autoreleasePoolPop(pool);
}

# ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		     block: (of_tcp_socket_async_connect_block_t)block
{
	/* TODO: Support SOCKS5 */
	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectThread alloc]
	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSourceThread: [OFThread currentThread]
			  socket: self
	    initWithSocket: self
			    host: host
			    port: port
			   block: block] autorelease] start];

	objc_autoreleasePoolPop(pool);
}
# endif
#endif

- (uint16_t)bindToHost: (OFString *)host
		  port: (uint16_t)port
{
	of_resolver_result_t **results;
	const int one = 1;
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)

Modified tests/Makefile from [7a90e657ea] to [aed8a5335f].

49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
49
50
51
52
53
54
55
56
57
58

59
60
61
62
63
64
65







+


-







	     OFSHA1HashTests.m		\
	     OFSHA224HashTests.m	\
	     OFSHA256HashTests.m	\
	     OFSHA384HashTests.m	\
	     OFSHA512HashTests.m
SRCS_PLUGINS = OFPluginTests.m
SRCS_SOCKETS = OFDNSResolverTests.m		\
	       ${OFHTTPCLIENTTESTS_M}		\
	       OFHTTPCookieTests.m		\
	       OFHTTPCookieManagerTests.m	\
	       ${OFHTTPCLIENTTESTS_M}		\
	       OFKernelEventObserverTests.m	\
	       OFTCPSocketTests.m		\
	       OFUDPSocketTests.m		\
	       SocketTests.m
SRCS_THREADS = OFThreadTests.m

IOS_USER ?= mobile