ObjFW  Check-in [3b68656e8d]

Overview
Comment:OFTCPSocket: Implement async connecting.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3b68656e8daaefadae8a05a4ff428de0cb6ae4f86514989b19d6cd40b85e4d5f
User & Date: js on 2012-09-15 12:22:08
Other Links: manifest | tags
Context
2012-09-16
10:19
Add -[OFTCPSocket asyncAcceptWithBlock:]. check-in: 5d8349a8f6 user: js tags: trunk
2012-09-15
12:22
OFTCPSocket: Implement async connecting. check-in: 3b68656e8d user: js tags: trunk
12:21
OFThread: Make the block the last argument. check-in: cf8db6867d user: js tags: trunk
Changes

Modified src/OFTCPSocket.h from [df788899f8] to [3bab17e6ab].

29
30
31
32
33
34
35

36




37
38
39
40
41
42
43

#import "OFStreamSocket.h"

#ifdef _WIN32
# include <ws2tcpip.h>
#endif


@class OFString;





/**
 * \brief A class which provides functions 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.
 */







>

>
>
>
>







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

#import "OFStreamSocket.h"

#ifdef _WIN32
# include <ws2tcpip.h>
#endif

@class OFTCPSocket;
@class OFString;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_tcpsocket_async_connect_block_t)(OFTCPSocket*);
#endif

/**
 * \brief A class which provides functions 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.
 */
118
119
120
121
122
123
124













125
126
127
128
129
130
131
 * \brief Connect the OFTCPSocket to the specified destination.
 *
 * \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;














/**
 * \brief Bind the socket on the specified port and host.
 *
 * \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







>
>
>
>
>
>
>
>
>
>
>
>
>







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
 * \brief Connect the OFTCPSocket to the specified destination.
 *
 * \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_BLOCKS
/**
 * \brief Asyncronously 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 block The block to execute once the connection has been established
 */
- (void)asyncConnectToHost: (OFString*)host
		      port: (uint16_t)port
		     block: (of_tcpsocket_async_connect_block_t)block;
#endif

/**
 * \brief Bind the socket on the specified port and host.
 *
 * \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 [f2b49c30d2] to [d1ec2a4710].

31
32
33
34
35
36
37



38
39
40
41
42
43
44
# include <arpa/inet.h>
# include <netdb.h>
#endif

#import "OFTCPSocket.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFString.h"




#import "OFAcceptFailedException.h"
#import "OFAlreadyConnectedException.h"
#import "OFAddressTranslationFailedException.h"
#import "OFBindFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFInvalidArgumentException.h"







>
>
>







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# include <arpa/inet.h>
# include <netdb.h>
#endif

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

#import "OFAcceptFailedException.h"
#import "OFAlreadyConnectedException.h"
#import "OFAddressTranslationFailedException.h"
#import "OFBindFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFInvalidArgumentException.h"
281
282
283
284
285
286
287


































288
289
290
291
292
293
294
				  host: host
				  port: port];

	if (SOCKS5Host != nil)
		[self _SOCKS5ConnectToHost: destinationHost
				      port: destinationPort];
}



































- (uint16_t)bindToHost: (OFString*)host
		  port: (uint16_t)port
{
	union {
		struct sockaddr_storage storage;
		struct sockaddr_in in;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
				  host: host
				  port: port];

	if (SOCKS5Host != nil)
		[self _SOCKS5ConnectToHost: destinationHost
				      port: destinationPort];
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString*)host
		      port: (uint16_t)port
		     block: (of_tcpsocket_async_connect_block_t)block
{
	void *pool = objc_autoreleasePoolPush();
	OFRunLoop *runLoop = [OFRunLoop currentRunLoop];

	[[OFThread threadWithObject: self
			      block: ^ (id s) {
		void *pool2 = objc_autoreleasePoolPush();
		OFThread *connectThread = [OFThread currentThread];
		OFTimer *timer;

		[s connectToHost: host
			    port: port];

		timer = [OFTimer timerWithTimeInterval: 0
					       repeats: NO
						 block: ^ {
			[connectThread join];
			block(s);
		}];
		[runLoop addTimer: timer];

		objc_autoreleasePoolPop(pool2);

		return nil;
	}] start];

	objc_autoreleasePoolPop(pool);
}
#endif

- (uint16_t)bindToHost: (OFString*)host
		  port: (uint16_t)port
{
	union {
		struct sockaddr_storage storage;
		struct sockaddr_in in;