ObjFW  Check-in [27153bf8cf]

Overview
Comment:OFTCPSocket: Use a delegate for async operations
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 27153bf8cfe2b7bbd6fcb8520f50a9c7b5899aeeccc59ec4e29a28ee3fe9f38f
User & Date: js on 2018-12-08 16:05:51
Other Links: manifest | tags
Context
2018-12-08
16:53
Separate error methods for async method delegates check-in: 2b6a12065e user: js tags: trunk
16:05
OFTCPSocket: Use a delegate for async operations check-in: 27153bf8cf user: js tags: trunk
2018-12-07
01:33
OFStream: Use a delegate for async operations check-in: d16ad96cbd user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [376bac39d6] to [66db22090b].

47
48
49
50
51
52
53
54

55
56
57
58
59
60
61
47
48
49
50
51
52
53

54
55
56
57
58
59
60
61







-
+







#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFUnsupportedVersionException.h"
#import "OFWriteFailedException.h"

#define REDIRECTS_DEFAULT 10

@interface OFHTTPClientRequestHandler: OFObject <OFStreamDelegate>
@interface OFHTTPClientRequestHandler: OFObject <OFTCPSocketDelegate>
{
@public
	OFHTTPClient *_client;
	OFHTTPRequest *_request;
	OFString *_requestString;
	unsigned int _redirects;
	id _context;
611
612
613
614
615
616
617
618
619
620



621
622
623
624
625
626
627

628
629
630
631
632
633
634
611
612
613
614
615
616
617



618
619
620
621






622
623
624
625
626
627
628
629







-
-
-
+
+
+

-
-
-
-
-
-
+







				length: [_requestString UTF8StringLength]];
	} @catch (id e) {
		[self raiseException: e];
		return;
	}
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{
	if (exception != nil) {
		[self raiseException: exception];
		return;
	}

	[sock setDelegate: self];
	[(OFTCPSocket *)sock setDelegate: self];

	if ([_client->_delegate respondsToSelector:
	    @selector(client:didCreateSocket:request:context:)])
		[_client->_delegate client: _client
			   didCreateSocket: sock
				   request: _request
				   context: _context];
693
694
695
696
697
698
699

700
701

702
703
704
705
706
707
708
709
710
711
712
688
689
690
691
692
693
694
695
696

697




698
699
700
701
702
703
704







+

-
+
-
-
-
-







			port = 80;
		}

		URLPort = [URL port];
		if (URLPort != nil)
			port = [URLPort uInt16Value];

		[sock setDelegate: self];
		[sock asyncConnectToHost: [URL host]
				    port: port
				    port: port];
				  target: self
				selector: @selector(socketDidConnect:context:
					      exception:)
				 context: nil];
	} @catch (id e) {
		[self raiseException: e];
	}
}
@end

@implementation OFHTTPClientRequestBodyStream

Modified src/OFHTTPServer.m from [fe26303808] to [abf11296e3].

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







-
+
-
-
-
-















-
+







#define BUFFER_SIZE 1024

/*
 * FIXME: Key normalization replaces headers like "DNT" with "Dnt".
 * FIXME: Errors are not reported to the user.
 */

@interface OFHTTPServer ()
@interface OFHTTPServer () <OFTCPSocketDelegate>
- (bool)of_socket: (OFTCPSocket *)sock
  didAcceptSocket: (OFTCPSocket *)clientSocket
	  context: (id)context
	exception: (id)exception;
@end

@interface OFHTTPServerResponse: OFHTTPResponse <OFReadyForWritingObserving>
{
	OF_KINDOF(OFTCPSocket *) _socket;
	OFHTTPServer *_server;
	OFHTTPRequest *_request;
	bool _chunked, _headersSent;
}

- (instancetype)initWithSocket: (OF_KINDOF(OFTCPSocket *))sock
			server: (OFHTTPServer *)server
		       request: (OFHTTPRequest *)request;
@end

@interface OFHTTPServer_Connection: OFObject <OFStreamDelegate>
@interface OFHTTPServer_Connection: OFObject <OFTCPSocketDelegate>
{
@public
	OF_KINDOF(OFTCPSocket *) _socket;
	OFHTTPServer *_server;
	OFTimer *_timer;
	enum {
		AWAITING_PROLOG,
776
777
778
779
780
781
782

783

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798


799
800
801
802



803
804
805

806
807
808

809
810

811
812
813
814


815
816
817
818
819
820






821
822
772
773
774
775
776
777
778
779

780




781
782
783
784
785
786
787
788
789


790
791


792

793
794
795
796


797



798
799

800
801
802


803
804






805
806
807
808
809
810
811
812







+
-
+
-
-
-
-









-
-
+
+
-
-

-
+
+
+

-
-
+
-
-
-
+

-
+


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


	} else
		_listeningSocket = [[OFTCPSocket alloc] init];

	_port = [_listeningSocket bindToHost: _host
					port: _port];
	[_listeningSocket listen];

	[(OFTCPSocket *)_listeningSocket setDelegate: self];
	[_listeningSocket asyncAcceptWithTarget: self
	[_listeningSocket asyncAccept];
				       selector: @selector(of_socket:
						     didAcceptSocket:context:
						     exception:)
					context: nil];
}

- (void)stop
{
	[_listeningSocket cancelAsyncRequests];
	[_listeningSocket release];
	_listeningSocket = nil;
}

- (bool)of_socket: (OFTCPSocket *)sock
  didAcceptSocket: (OFTCPSocket *)clientSocket
-    (bool)socket: (OF_KINDOF(OFTCPSocket *))sock
  didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket
	  context: (id)context
	exception: (id)exception
{
	OFHTTPServer_Connection *connection;
	OFHTTPServer_Connection *connection = [[[OFHTTPServer_Connection alloc]
	    initWithSocket: acceptedSocket
		    server: self] autorelease];

	if (exception != nil) {
		if ([_delegate respondsToSelector:
	[(OFTCPSocket *)acceptedSocket setDelegate: connection];
		    @selector(server:didReceiveExceptionOnListeningSocket:)])
			return [_delegate		  server: self
			    didReceiveExceptionOnListeningSocket: exception];
	[acceptedSocket asyncReadLine];

		return false;
	return true;
	}

	connection = [[[OFHTTPServer_Connection alloc]
	    initWithSocket: clientSocket
-	  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailWithException: (id)exception
		    server: self] autorelease];

	[clientSocket setDelegate: connection];
	[clientSocket asyncReadLine];

	return true;
{
	if ([_delegate respondsToSelector:
	    @selector(server:didReceiveExceptionOnListeningSocket:)])
		if ([_delegate			  server: self
		    didReceiveExceptionOnListeningSocket: exception])
			[stream asyncAccept];
}
@end

Modified src/OFRunLoop+Private.h from [02b97312cb] to [de6ad95d5c].

51
52
53
54
55
56
57
58

59
60
61
62

63
64
65
66
67
68
69
70
71
51
52
53
54
55
56
57

58

59
60

61


62
63
64
65
66
67
68







-
+
-


-
+
-
-







			   buffer: (const void *)buffer
			   length: (size_t)length
			     mode: (of_run_loop_mode_t)mode
			 delegate: (id <OFStreamDelegate>)delegate;
+ (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)socket
				  mode: (of_run_loop_mode_t)mode
				target: (id)target
			      selector: (SEL)selector
			      selector: (SEL)selector;
			       context: (nullable id)context;
+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)socket
				 mode: (of_run_loop_mode_t)mode
			       target: (id)target
			     delegate: (id <OFTCPSocketDelegate>)delegate;
			     selector: (SEL)selector
			      context: (nullable id)context;
+ (void)of_addAsyncReceiveForUDPSocket: (OFUDPSocket *)socket
				buffer: (void *)buffer
				length: (size_t)length
				  mode: (of_run_loop_mode_t)mode
				target: (id)target
			      selector: (SEL)selector
			       context: (nullable id)context;

Modified src/OFRunLoop.m from [692cbbd5b1] to [ab8e75f5ed].

563
564
565
566
567
568
569
570

571
572
573
574

575
576

577
578
579
580
581
582

583
584




585
586
587








588
589

590

591
592
593
594
595
596
597
563
564
565
566
567
568
569

570
571
572
573

574
575

576
577
578
579
580
581

582
583
584
585
586
587
588



589
590
591
592
593
594
595
596
597

598

599
600
601
602
603
604
605
606







-
+



-
+

-
+





-
+


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

-
+
-
+







	return false;
}
@end

@implementation OFRunLoop_AcceptQueueItem
- (bool)handleObject: (id)object
{
	OFTCPSocket *newSocket;
	OFTCPSocket *acceptedSocket;
	id exception = nil;

	@try {
		newSocket = [object accept];
		acceptedSocket = [object accept];
	} @catch (id e) {
		newSocket = nil;
		acceptedSocket = nil;
		exception = e;
	}

# ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
		return _block(object, newSocket, exception);
		return _block(object, acceptedSocket, exception);
	else {
# endif
		if (exception == nil) {
			if (![_delegate respondsToSelector:
			    @selector(socket:didAcceptSocket:)])
				return false;
		bool (*func)(id, SEL, OFTCPSocket *, OFTCPSocket *, id, id) =
		    (bool (*)(id, SEL, OFTCPSocket *, OFTCPSocket *, id, id))
		    [_target methodForSelector: _selector];

			return [_delegate socket: object
				 didAcceptSocket: acceptedSocket];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

		return func(_target, _selector, object, newSocket, _context,
			return false;
		    exception);
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

# ifdef OF_HAVE_BLOCKS
- (void)dealloc
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837

838
839
840
841
842
843

844
845
846
847
848
849
850
851
828
829
830
831
832
833
834

835
836
837
838

839
840
841
842
843

844


845
846


847

848
849
850
851
852
853
854







-




-





-
+
-
-


-
-
+
-







	})
}

+ (void)of_addAsyncConnectForTCPSocket: (OFTCPSocket *)stream
				  mode: (of_run_loop_mode_t)mode
				target: (id)target
			      selector: (SEL)selector
			       context: (id)context
{
	ADD_WRITE(OFRunLoop_ConnectQueueItem, stream, mode, {
		queueItem->_target = [target retain];
		queueItem->_selector = selector;
		queueItem->_context = [context retain];
	})
}

+ (void)of_addAsyncAcceptForTCPSocket: (OFTCPSocket *)stream
				 mode: (of_run_loop_mode_t)mode
			       target: (id)target
			     delegate: (id <OFTCPSocketDelegate>)delegate
			     selector: (SEL)selector
			      context: (id)context
{
	ADD_READ(OFRunLoop_AcceptQueueItem, stream, mode, {
		queueItem->_target = [target retain];
		queueItem->_selector = selector;
		queueItem->_delegate = [delegate retain];
		queueItem->_context = [context retain];
	})
}

+ (void)of_addAsyncReceiveForUDPSocket: (OFUDPSocket *)sock
				buffer: (void *)buffer
				length: (size_t)length
				  mode: (of_run_loop_mode_t)mode

Modified src/OFStream.h from [aea2292c00] to [c566a77887].

176
177
178
179
180
181
182
183

184
185
186
187
188
189
190
176
177
178
179
180
181
182

183
184
185
186
187
188
189
190







-
+







#endif
	char *_Nullable _readBuffer, *_Nullable _readBufferMemory;
	char *_Nullable _writeBuffer;
	size_t _readBufferLength, _writeBufferLength;
	bool _writeBuffered, _waitingForDelimiter;
@protected
	bool _blocking;
	id <OFStreamDelegate> _Nullable _delegate;
	id _Nullable _delegate;
}

/*!
 * @brief Whether the end of the stream has been reached.
 */
@property (readonly, nonatomic, getter=isAtEndOfStream) bool atEndOfStream;

Modified src/OFTCPSocket.h from [65cbd0fc74] to [4f0f6c4d0d].

49
50
51
52
53
54
55



























56
57
58
59
60
61
62
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 *	   connection
 */
typedef bool (^of_tcp_socket_async_accept_block_t)(
    OF_KINDOF(OFTCPSocket *) socket, OF_KINDOF(OFTCPSocket *) acceptedSocket,
    id _Nullable exception);
#endif

/*!
 * @protocol OFTCPSocketDelegate OFTCPSocket.h ObjFW/OFTCPSocket.h
 *
 * A delegate for OFTCPSocket.
 */
@protocol OFTCPSocketDelegate <OFStreamDelegate>
@optional
/*!
 * @brief A method which is called when a socket connected.
 *
 * @param socket The socket which connected
 */
-     (void)socket: (OF_KINDOF(OFTCPSocket *))socket
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port;

/*!
 * @brief A method which is called when a socket accepted a connection.
 *
 * @param socket The socket which accepted the connection
 * @param acceptedSocket The socket which has been accepted
 * @return A bool whether to accept the next incoming connection
 */
-    (bool)socket: (OF_KINDOF(OFTCPSocket *))socket
  didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket;
@end

/*!
 * @class OFTCPSocket OFTCPSocket.h ObjFW/OFTCPSocket.h
 *
 * @brief A class which provides methods to create and use TCP sockets.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
113
114
115
116
117
118
119









120
121
122
123
124
125
126
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162







+
+
+
+
+
+
+
+
+







@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *SOCKS5Host;

/*!
 * @brief The port to use on the SOCKS5 proxy.
 */
@property (nonatomic) uint16_t SOCKS5Port;

/*!
 * @brief The delegate for asynchronous operations on the socket.
 *
 * @note The delegate is retained for as long as asynchronous operations are
 *	 still outstanding.
 */
@property OF_NULLABLE_PROPERTY (assign, nonatomic)
    id <OFTCPSocketDelegate> delegate;

/*!
 * @brief Sets the global SOCKS5 proxy host to use when creating a new socket
 *
 * @param SOCKS5Host The host to use as a SOCKS5 proxy when creating a new
 *		     socket
 */
+ (void)setSOCKS5Host: (nullable OFString *)SOCKS5Host;
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
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







-
-
-
-
-


-
+
-
-
-







-
-
-
-
-



-
+
-
-
-







		 port: (uint16_t)port;

/*!
 * @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
 * @param selector The selector to call on the target. The signature must be
 *		   `void (OFTCPSocket *socket, id context, id exception)`.
 * @param context A context object to pass along to the target
 */
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		      port: (uint16_t)port;
		    target: (id)target
		  selector: (SEL)selector
		   context: (nullable id)context;

/*!
 * @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 runLoopMode The run loop mode in which to perform the async connect
 * @param target The target on which to call the selector once the connection
 *		 has been established
 * @param selector The selector to call on the target. The signature must be
 *		   `void (OFTCPSocket *socket, id context, id exception)`.
 * @param context A context object to pass along to the target
 */
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
	       runLoopMode: (of_run_loop_mode_t)runLoopMode
	       runLoopMode: (of_run_loop_mode_t)runLoopMode;
		    target: (id)target
		  selector: (SEL)selector
		   context: (nullable id)context;

#ifdef OF_HAVE_BLOCKS
/*!
 * @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
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
266
267
268
269
270
271
272









273

274


275
276
277
278
279








280

281



282
283
284
285
286
287
288







-
-
-
-
-
-
-
-
-

-
+
-
-





-
-
-
-
-
-
-
-

-
+
-
-
-







 *
 * @return An autoreleased OFTCPSocket for the accepted connection.
 */
- (instancetype)accept;

/*!
 * @brief Asynchronously accept an incoming connection.
 *
 * @param target The target on which to execute the selector when a new
 *		 connection has been accepted. The method returns whether the
 *		 next incoming connection should be accepted by the specified
 *		 block as well.
 * @param selector The selector to call on the target. The signature must be
 *		   `bool (OFTCPSocket *socket, OFTCPSocket *acceptedSocket,
 *		   id context, id exception)`.
 * @param context A context object to pass along to the target
 */
- (void)asyncAcceptWithTarget: (id)target
- (void)asyncAccept;
		     selector: (SEL)selector
		      context: (nullable id)context;

/*!
 * @brief Asynchronously accept an incoming connection.
 *
 * @param runLoopMode The run loop mode in which to perform the async accept
 * @param target The target on which to execute the selector when a new
 *		 connection has been accepted. The method returns whether the
 *		 next incoming connection should be accepted by the specified
 *		 block as well.
 * @param selector The selector to call on the target. The signature must be
 *		   `bool (OFTCPSocket *socket, OFTCPSocket *acceptedSocket,
 *		   id context, id exception)`.
 * @param context A context object to pass along to the target
 */
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
			    target: (id)target
			  selector: (SEL)selector
			   context: (nullable id)context;

#ifdef OF_HAVE_BLOCKS
/*!
 * @brief Asynchronously accept an incoming connection.
 *
 * @param block The block to execute when a new connection has been accepted.
 *		Returns whether the next incoming connection should be accepted

Modified src/OFTCPSocket.m from [df6639e5af] to [8cf85678b9].

60
61
62
63
64
65
66
67

68
69
70
71
72
73
74

75
76
77
78
79
80
81
82
83
60
61
62
63
64
65
66

67
68
69
70
71
72
73

74


75
76
77
78
79
80
81







-
+






-
+
-
-








Class of_tls_socket_class = Nil;

static of_run_loop_mode_t connectRunLoopMode = @"of_tcp_socket_connect_mode";
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;

@interface OFTCPSocket_AsyncConnectContext: OFObject <OFStreamDelegate>
@interface OFTCPSocket_AsyncConnectContext: OFObject <OFTCPSocketDelegate>
{
	OFTCPSocket *_socket;
	OFString *_host;
	uint16_t _port;
	OFString *_SOCKS5Host;
	uint16_t _SOCKS5Port;
	id _target;
	id <OFTCPSocketDelegate> _delegate;
	SEL _selector;
	id _context;
#ifdef OF_HAVE_BLOCKS
	of_tcp_socket_async_connect_block_t _block;
#endif
	id _exception;
	OFData *_socketAddresses;
	size_t _socketAddressesIndex;
	enum {
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
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







-
+
-
-










-











-
+





-
-
-
-








-
+
-
-









-
+
-
-







}

- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			target: (id)target
		      delegate: (id <OFTCPSocketDelegate>)delegate;
		      selector: (SEL)selector
		       context: (id)context;
#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			 block: (of_tcp_socket_async_connect_block_t)block;
#endif
- (void)didConnect;
- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception;
- (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
       socketAddresses: (OFData *)socketAddresses
	       context: (id)context
	     exception: (id)exception;
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)sendSOCKS5Request;
@end

@interface OFTCPSocket_ConnectContext: OFObject
@interface OFTCPSocket_ConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
	bool _done;
	id _exception;
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception;
@end

@implementation OFTCPSocket_AsyncConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			target: (id)target
		      delegate: (id <OFTCPSocketDelegate>)delegate
		      selector: (SEL)selector
		       context: (id)context
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_SOCKS5Host = [SOCKS5Host copy];
		_SOCKS5Port = SOCKS5Port;
		_target = [target retain];
		_delegate = [delegate retain];
		_selector = selector;
		_context = [context retain];

		[_socket setDelegate: self];
	} @catch (id e) {
		[self release];
		@throw e;
	}

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







+
+
+

-
+




-
+
-












-
-








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






-








	return self;
}
#endif

- (void)dealloc
{
#ifdef OF_HAVE_BLOCKS
	if (_block != NULL)
#endif
	if ([_socket delegate] == self)
		[_socket setDelegate: nil];
			[_socket setDelegate: _delegate];

	[_socket release];
	[_host release];
	[_SOCKS5Host release];
	[_target release];
	[_delegate release];
	[_context release];
#ifdef OF_HAVE_BLOCKS
	[_block release];
#endif
	[_exception release];
	[_socketAddresses release];
	[_request release];

	[super dealloc];
}

- (void)didConnect
{
	[_socket setDelegate: nil];

	if (_exception == nil)
		[_socket setBlocking: true];

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

		[_socket setDelegate: _delegate];

		if (_exception == nil) {
			if ([_delegate respondsToSelector:
			    @selector(socket:didConnectToHost:port:)])
				[_delegate    socket: _socket
				    didConnectToHost: _host
						port: _port];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: _socket
				    didFailWithException: _exception];
		}
		func(_target, _selector, _socket, _context, _exception);
#ifdef OF_HAVE_BLOCKS
	}
#endif
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
{
	if (exception != nil) {
		if (_socketAddressesIndex >= [_socketAddresses count]) {
			_exception = [exception retain];
			[self didConnect];
		} else {
284
285
286
287
288
289
290
291

292
293
294
295
296
297

298
299
300
301
302
303
304
305
279
280
281
282
283
284
285

286

287
288
289
290

291

292
293
294
295
296
297
298







-
+
-




-
+
-







	}

	[_socket setBlocking: false];

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

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

			if (_socketAddressesIndex >= [_socketAddresses count]) {
				_exception = [[OFConnectionFailedException
				    alloc] initWithHost: _host
565
566
567
568
569
570
571
572

573
574
575
576
577
578
579
580
581
582









583
584
585

586
587
588
589
590

591
592
593
594
595
596
597
558
559
560
561
562
563
564

565
566
567
568
569
570
571
572



573
574
575
576
577
578
579
580
581
582

583
584
585
586
587
588
589
590
591
592
593
594
595
596
597







-
+







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

-

+





+







  didFailWithException: (id)exception
{
	_exception = [exception retain];
	[self didConnect];
}
@end

@implementation OFTCPSocket_ConnectContext
@implementation OFTCPSocket_ConnectDelegate
- (void)dealloc
{
	[_exception release];

	[super dealloc];
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{
	_done = true;
}

-	  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailWithException: (id)exception
{
	_exception = [exception retain];
	_done = true;
	_exception = [exception retain];
}
@end

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

+ (void)setSOCKS5Host: (OFString *)host
{
	id old = defaultSOCKS5Host;
	defaultSOCKS5Host = [host copy];
	[old release];
}
691
692
693
694
695
696
697

698
699


700
701

702
703
704

705
706
707
708
709

710
711
712
713
714
715
716
717
718




719
720
721
722
723
724
725
726
727
728
729
730
731

732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754

755
756
757
758
759
760
761
691
692
693
694
695
696
697
698


699
700
701
702
703
704
705

706



707

708
709
710
711
712
713
714
715


716
717
718
719
720
721
722
723
724
725



726
727
728

729



730
731
732
733
734



735
736
737
738
739
740
741
742
743



744
745
746
747
748
749
750
751







+
-
-
+
+


+


-
+
-
-
-

-
+







-
-
+
+
+
+






-
-
-



-
+
-
-
-





-
-
-









-
-
-
+







	return errNo;
}

- (void)connectToHost: (OFString *)host
		 port: (uint16_t)port
{
	void *pool = objc_autoreleasePoolPush();
	id <OFTCPSocketDelegate> delegate = [_delegate retain];
	OFTCPSocket_ConnectContext *context =
	    [[[OFTCPSocket_ConnectContext alloc] init] autorelease];
	OFTCPSocket_ConnectDelegate *connectDelegate =
	    [[[OFTCPSocket_ConnectDelegate alloc] init] autorelease];
	OFRunLoop *runLoop = [OFRunLoop currentRunLoop];

	[self setDelegate: connectDelegate];
	[self asyncConnectToHost: host
			    port: port
		     runLoopMode: connectRunLoopMode
		     runLoopMode: connectRunLoopMode];
			  target: context
			selector: @selector(socketDidConnect:context:exception:)
			 context: nil];

	while (!context->_done)
	while (!connectDelegate->_done)
		[runLoop runMode: connectRunLoopMode
		      beforeDate: nil];

	/* Cleanup */
	[runLoop runMode: connectRunLoopMode
	      beforeDate: [OFDate date]];

	if (context->_exception != nil)
		@throw context->_exception;
	if (connectDelegate->_exception != nil)
		@throw connectDelegate->_exception;

	[self setDelegate: delegate];

	objc_autoreleasePoolPop(pool);
}

- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)context
{
	[self asyncConnectToHost: host
			    port: port
		     runLoopMode: of_run_loop_mode_default
		     runLoopMode: of_run_loop_mode_default];
			  target: target
			selector: selector
			 context: context];
}

- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
	       runLoopMode: (of_run_loop_mode_t)runLoopMode
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)context
{
	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_AsyncConnectContext alloc]
	    initWithSocket: self
		      host: host
		      port: port
		SOCKS5Host: _SOCKS5Host
		SOCKS5Port: _SOCKS5Port
		    target: target
		  selector: selector
		   context: context] autorelease]
			delegate: _delegate] autorelease]
	    startWithRunLoopMode: runLoopMode];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
1003
1004
1005
1006
1007
1008
1009
1010

1011
1012
1013
1014

1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028

1029
1030
1031
1032
1033
1034
1035
1036
993
994
995
996
997
998
999

1000


1001

1002



1003
1004
1005



1006
1007
1008


1009

1010
1011
1012
1013
1014
1015
1016







-
+
-
-

-
+
-
-
-



-
-
-



-
-
+
-







		    OF_SOCKET_ADDRESS_FAMILY_UNKNOWN;
		break;
	}

	return client;
}

- (void)asyncAcceptWithTarget: (id)target
- (void)asyncAccept
		     selector: (SEL)selector
		      context: (id)context
{
	[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
	[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default];
				  target: target
				selector: selector
				 context: context];
}

- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
			    target: (id)target
			  selector: (SEL)selector
			   context: (id)context
{
	[OFRunLoop of_addAsyncAcceptForTCPSocket: self
					    mode: runLoopMode
					  target: target
					selector: selector
					delegate: _delegate];
					 context: context];
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block
{
	[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
				   block: block];