ObjFW  Check-in [414c87ec13]

Overview
Comment:Move socket initialization to a single place
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 414c87ec13b6e278bf0b1e8b40a0ae82b496cf43aac480bb8eb0e9e70e682efd
User & Date: js on 2014-02-03 14:43:51
Other Links: manifest | tags
Context
2014-02-03
18:29
configure: --disable-tls -> --disable-compiler-tls check-in: 8ca9721ffe user: js tags: trunk
14:43
Move socket initialization to a single place check-in: 414c87ec13 user: js tags: trunk
2014-01-31
13:33
Make sure there's always an #else in abstractions check-in: 0a8dca300b user: js tags: trunk
Changes

Modified ObjFW.xcodeproj/project.pbxproj from [e875b7096f] to [2ab14c9d0d].

233
234
235
236
237
238
239

240
241
242
243
244
245
246
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247







+







		4B3D23E71337FCB000DD29B8 /* base64.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B3D236D1337FB5800DD29B8 /* base64.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B3D23E81337FCB000DD29B8 /* macros.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BBA36C511406AB700CBA3AC /* macros.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B3D23E91337FCB000DD29B8 /* of_asprintf.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BB50DCF12F863C700C9393F /* of_asprintf.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B3D23EA1337FCB000DD29B8 /* threading.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B67998B1099E7C50041064A /* threading.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B3D23EB1337FCB000DD29B8 /* unicode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B67998C1099E7C50041064A /* unicode.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B3D23EE1337FFD000DD29B8 /* of_asprintf.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BB50DD012F863C700C9393F /* of_asprintf.m */; };
		4B3D5694139A617D0010A78F /* OFSerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B3D5693139A617D0010A78F /* OFSerializationTests.m */; };
		4B40EC1B189FE2650031E19E /* socket.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B40EC1A189FE2650031E19E /* socket.m */; };
		4B45355313DCFE1E0037AB4D /* OFCountedSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B45355113DCFE1E0037AB4D /* OFCountedSet.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B45355413DCFE1E0037AB4D /* OFCountedSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B45355213DCFE1E0037AB4D /* OFCountedSet.m */; };
		4B48B95414DC23B100546D39 /* OFXMLProcessingInstructions.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B48B95214DC23B100546D39 /* OFXMLProcessingInstructions.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B48B95514DC23B100546D39 /* OFXMLProcessingInstructions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B48B95314DC23B100546D39 /* OFXMLProcessingInstructions.m */; };
		4B49EA66143B39CE0005BBC6 /* OFXMLNodeTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B49EA65143B39CE0005BBC6 /* OFXMLNodeTests.m */; };
		4B49EA6D143B3A090005BBC6 /* OFXMLCDATA.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B49EA67143B3A090005BBC6 /* OFXMLCDATA.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B49EA6E143B3A090005BBC6 /* OFXMLCDATA.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B49EA68143B3A090005BBC6 /* OFXMLCDATA.m */; };
608
609
610
611
612
613
614

615
616
617
618
619
620
621
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623







+







		4B3B0796166978780044E634 /* OFMapTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMapTable.h; path = src/OFMapTable.h; sourceTree = "<group>"; };
		4B3B0797166978780044E634 /* OFMapTable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFMapTable.m; path = src/OFMapTable.m; sourceTree = "<group>"; };
		4B3D236D1337FB5800DD29B8 /* base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = base64.h; path = src/base64.h; sourceTree = "<group>"; };
		4B3D236E1337FB5800DD29B8 /* base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = base64.m; path = src/base64.m; sourceTree = "<group>"; };
		4B3D23761337FBC800DD29B8 /* ObjFW.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ObjFW.framework; sourceTree = BUILT_PRODUCTS_DIR; };
		4B3D23BB1337FC5800DD29B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; };
		4B3D5693139A617D0010A78F /* OFSerializationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSerializationTests.m; path = tests/OFSerializationTests.m; sourceTree = "<group>"; };
		4B40EC1A189FE2650031E19E /* socket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = socket.m; path = src/socket.m; sourceTree = "<group>"; };
		4B45355113DCFE1E0037AB4D /* OFCountedSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFCountedSet.h; path = src/OFCountedSet.h; sourceTree = "<group>"; };
		4B45355213DCFE1E0037AB4D /* OFCountedSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFCountedSet.m; path = src/OFCountedSet.m; sourceTree = "<group>"; };
		4B48B95214DC23B100546D39 /* OFXMLProcessingInstructions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFXMLProcessingInstructions.h; path = src/OFXMLProcessingInstructions.h; sourceTree = "<group>"; };
		4B48B95314DC23B100546D39 /* OFXMLProcessingInstructions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLProcessingInstructions.m; path = src/OFXMLProcessingInstructions.m; sourceTree = "<group>"; };
		4B49EA65143B39CE0005BBC6 /* OFXMLNodeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLNodeTests.m; path = tests/OFXMLNodeTests.m; sourceTree = "<group>"; };
		4B49EA67143B3A090005BBC6 /* OFXMLCDATA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFXMLCDATA.h; path = src/OFXMLCDATA.h; sourceTree = "<group>"; };
		4B49EA68143B3A090005BBC6 /* OFXMLCDATA.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLCDATA.m; path = src/OFXMLCDATA.m; sourceTree = "<group>"; };
1363
1364
1365
1366
1367
1368
1369

1370
1371
1372
1373
1374
1375
1376
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379







+







				4BB50DCF12F863C700C9393F /* of_asprintf.h */,
				4BB50DD012F863C700C9393F /* of_asprintf.m */,
				4BA355BC14879BF700442EF4 /* of_strptime.h */,
				4BA355B914879BDD00442EF4 /* of_strptime.m */,
				4B7769EB1895C07D00D12284 /* resolver.h */,
				4B7769EC1895C07D00D12284 /* resolver.m */,
				4B7DD58718943D4A00990FD6 /* socket.h */,
				4B40EC1A189FE2650031E19E /* socket.m */,
				4B7DD58118942FE200990FD6 /* socket_helpers.h */,
				4B67998B1099E7C50041064A /* threading.h */,
				4B67998C1099E7C50041064A /* unicode.h */,
				4BFBDD1610A0724800051AFB /* unicode.m */,
				4B6AF97210A8D42E0003FB0A /* windows_1252.m */,
			);
			name = ObjFW;
1944
1945
1946
1947
1948
1949
1950

1951
1952
1953
1954
1955
1956
1957
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961







+







				4B3D23B31337FC0D00DD29B8 /* base64.m in Sources */,
				4BB52CC717B8EA7F00B7EBF5 /* codepage_437.m in Sources */,
				4B3D23B41337FC0D00DD29B8 /* iso_8859_15.m in Sources */,
				4B3D23B51337FC0D00DD29B8 /* foundation-compat.m in Sources */,
				4B3D23EE1337FFD000DD29B8 /* of_asprintf.m in Sources */,
				4BA355BA14879BDD00442EF4 /* of_strptime.m in Sources */,
				4B7769EE1895C07D00D12284 /* resolver.m in Sources */,
				4B40EC1B189FE2650031E19E /* socket.m in Sources */,
				4B3D23B91337FC0D00DD29B8 /* unicode.m in Sources */,
				4B3D23BA1337FC0D00DD29B8 /* windows_1252.m in Sources */,
				4B90B79F133AD87D00BD33CB /* OFAcceptFailedException.m in Sources */,
				4B90B7A1133AD87D00BD33CB /* OFAddressTranslationFailedException.m in Sources */,
				4B17FF80133A2D17003E6DCD /* OFAllocFailedException.m in Sources */,
				4B90B78E133AD46700BD33CB /* OFAlreadyConnectedException.m in Sources */,
				4B90B7A3133AD87D00BD33CB /* OFBindFailedException.m in Sources */,

Modified configure.ac from [6ffb277586] to [8730f14296].

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
616
617
618
619
620
621
622

623
624
625
626
627
628
629







-







])

AC_ARG_ENABLE(sockets,
	AS_HELP_STRING([--disable-sockets], [disable socket support]))
AS_IF([test x"$enable_sockets" != x"no"], [
	AC_DEFINE(OF_HAVE_SOCKETS, 1, [Whether we have sockets])
	AC_SUBST(USE_SRCS_SOCKETS, '${SRCS_SOCKETS}')
	AC_SUBST(USE_INCLUDES_SOCKETS, '${INCLUDES_SOCKETS}')

	AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket")
	AC_CHECK_LIB(network, socket, LIBS="$LIBS -lnetwork")
	AC_CHECK_LIB(ws2_32, main, LIBS="$LIBS -lws2_32")

	AC_CHECK_HEADER(sys/socket.h, [
		AC_DEFINE(OF_HAVE_SYS_SOCKET_H, 1,

Modified extra.mk.in from [fb07d84b4d] to [b747bd17c6].

43
44
45
46
47
48
49
50
51
52
53
54
55
43
44
45
46
47
48
49

50
51
52
53
54







-





RUNTIME_RUNTIME_A = @RUNTIME_RUNTIME_A@
RUNTIME_RUNTIME_LIB_A = @RUNTIME_RUNTIME_LIB_A@
RUNTIME_LIB_A = @RUNTIME_LIB_A@
TESTPLUGIN = @TESTPLUGIN@
TESTS = @TESTS@
TESTS_LIBS = @TESTS_LIBS@
TEST_LAUNCHER = @TEST_LAUNCHER@
USE_INCLUDES_SOCKETS = @USE_INCLUDES_SOCKETS@
USE_INCLUDES_THREADS = @USE_INCLUDES_THREADS@
USE_SRCS_FILES = @USE_SRCS_FILES@
USE_SRCS_PLUGINS = @USE_SRCS_PLUGINS@
USE_SRCS_SOCKETS = @USE_SRCS_SOCKETS@
USE_SRCS_THREADS = @USE_SRCS_THREADS@

Modified src/Makefile from [df94192d41] to [9bf87537a2].

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







-
+
+





-

















-







	       OFHTTPRequest.m		\
	       OFHTTPResponse.m		\
	       OFHTTPServer.m		\
	       OFKernelEventObserver.m	\
	       OFStreamSocket.m		\
	       OFTCPSocket.m		\
	       OFUDPSocket.m		\
	       resolver.m
	       resolver.m		\
	       socket.m
SRCS_THREADS = OFCondition.m		\
	       OFMutex.m		\
	       OFRecursiveMutex.m	\
	       OFThreadPool.m

INCLUDES_SOCKETS = socket.h
INCLUDES_THREADS = threading.h
INCLUDES := ${SRCS:.m=.h}			\
	    OFCollection.h			\
	    OFHash.h				\
	    OFJSONRepresentation.h		\
	    OFLocking.h				\
	    OFMessagePackRepresentation.h	\
	    OFSerialization.h			\
	    OFTLSSocket.h			\
	    ObjFW.h				\
	    asprintf.h				\
	    autorelease.h			\
	    ${ATOMIC_H}				\
	    block.h				\
	    instance.h				\
	    macros.h				\
	    objfw-defs.h			\
	    ${USE_INCLUDES_SOCKETS}		\
	    ${USE_INCLUDES_THREADS}

SRCS += OFArray_adjacent.m		\
	OFArray_adjacentSubarray.m	\
	OFCountedSet_hashtable.m	\
	OFDictionary_hashtable.m	\
	OFMutableArray_adjacent.m	\

Modified src/OFKernelEventObserver.m from [71bb648f05] to [e92efb6b43].

58
59
60
61
62
63
64










65
66
67
68
69
70
71
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81







+
+
+
+
+
+
+
+
+
+







	QUEUE_REMOVE = 1,
	QUEUE_READ = 0,
	QUEUE_WRITE = 2
};
#define QUEUE_ACTION (QUEUE_ADD | QUEUE_REMOVE)

@implementation OFKernelEventObserver
+ (void)initialize
{
	if (self != [OFKernelEventObserver class])
		return;

	if (!of_init_sockets())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

+ (instancetype)observer
{
	return [[[self alloc] init] autorelease];
}

#if defined(HAVE_KQUEUE)
+ alloc
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
123
124
125
126
127
128
129



130
131
132
133
134
135
136







-
-
-







		_queueFDs = [[OFDataArray alloc] initWithItemSize: sizeof(int)];

#ifdef OF_HAVE_PIPE
		if (pipe(_cancelFD))
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];
#else
		/* Make sure network has been initialized */
		[OFStreamSocket class];

		_cancelFD[0] = socket(AF_INET, SOCK_DGRAM, 0);
		_cancelFD[1] = socket(AF_INET, SOCK_DGRAM, 0);

		if (_cancelFD[0] == INVALID_SOCKET ||
		    _cancelFD[1] == INVALID_SOCKET)
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

Modified src/OFStreamSocket.m from [0ca4a3134a] to [917f870ee4].

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
31
32
33
34
35
36
37




38
39
40






41
42
43

44
45
46
47
48
49
50







-
-
-
-



-
-
-
-
-
-
+


-







#import "OFWriteFailedException.h"

#import "socket_helpers.h"

@implementation OFStreamSocket
+ (void)initialize
{
#ifdef _WIN32
	WSADATA wsa;
#endif

	if (self != [OFStreamSocket class])
		return;

#if defined(_WIN32)
	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#elif defined(__wii__)
	if (net_init() < 0)
	if (!of_init_sockets())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
#endif
}

+ (instancetype)socket
{
	return [[[self alloc] init] autorelease];
}

Modified src/OFUDPSocket.m from [851d26fee8] to [e488747830].

24
25
26
27
28
29
30

31
32
33
34
35
36
37
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38







+







#ifdef OF_HAVE_THREADS
# import "OFThread.h"
#endif
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"

#import "OFBindFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotConnectedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"

#import "autorelease.h"
#import "macros.h"
272
273
274
275
276
277
278










279
280
281
282
283
284
285
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296







+
+
+
+
+
+
+
+
+
+







		@throw [OFInvalidArgumentException exception];
	}

	return hash;
}

@implementation OFUDPSocket
+ (void)initialize
{
	if (self != [OFUDPSocket class])
		return;

	if (!of_init_sockets())
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

+ (instancetype)socket
{
	return [[[self alloc] init] autorelease];
}

+ (void)resolveAddressForHost: (OFString*)host
			 port: (uint16_t)port

Modified src/socket.h from [2c4b5dfd5d] to [49c77e6e7b].

15
16
17
18
19
20
21


22
23
24
25
26
27
28
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+
+







 */

#import "objfw-defs.h"

#ifndef OF_HAVE_SOCKETS
# error No sockets available!
#endif

#include <stdbool.h>

#ifdef OF_HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#ifdef _WIN32
# include <ws2tcpip.h>
47
48
49
50
51
52
53


49
50
51
52
53
54
55
56
57







+
+
	uint8_t	       ss_len;
	sa_family_t    ss_family;
	in_port_t      ss_data1;
	struct in_addr ss_data2;
	int8_t	       ss_data3[8];
};
#endif

extern bool of_init_sockets(void);

Added src/socket.m version [75d8ae44e0].










































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014
 *   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.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "socket.h"

static bool initialized = false;

bool
of_init_sockets()
{
	if (initialized)
		return true;

#ifdef _WIN32
	WSADATA wsa;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		return false;
#elif defined(__wii__)
	if (net_init() < 0)
		return false;
#endif

	initialized = true;
	return true;
}