Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -235,10 +235,11 @@ 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 */; }; @@ -610,10 +611,11 @@ 4B3D236D1337FB5800DD29B8 /* base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = base64.h; path = src/base64.h; sourceTree = ""; }; 4B3D236E1337FB5800DD29B8 /* base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = base64.m; path = src/base64.m; sourceTree = ""; }; 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 = ""; }; + 4B40EC1A189FE2650031E19E /* socket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = socket.m; path = src/socket.m; sourceTree = ""; }; 4B45355113DCFE1E0037AB4D /* OFCountedSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFCountedSet.h; path = src/OFCountedSet.h; sourceTree = ""; }; 4B45355213DCFE1E0037AB4D /* OFCountedSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFCountedSet.m; path = src/OFCountedSet.m; sourceTree = ""; }; 4B48B95214DC23B100546D39 /* OFXMLProcessingInstructions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFXMLProcessingInstructions.h; path = src/OFXMLProcessingInstructions.h; sourceTree = ""; }; 4B48B95314DC23B100546D39 /* OFXMLProcessingInstructions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLProcessingInstructions.m; path = src/OFXMLProcessingInstructions.m; sourceTree = ""; }; 4B49EA65143B39CE0005BBC6 /* OFXMLNodeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLNodeTests.m; path = tests/OFXMLNodeTests.m; sourceTree = ""; }; @@ -1365,10 +1367,11 @@ 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 */, @@ -1946,10 +1949,11 @@ 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 */, Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -618,11 +618,10 @@ 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") Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -45,11 +45,10 @@ 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@ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -85,17 +85,17 @@ 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 \ @@ -109,11 +109,10 @@ ${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 \ Index: src/OFKernelEventObserver.m ================================================================== --- src/OFKernelEventObserver.m +++ src/OFKernelEventObserver.m @@ -60,10 +60,20 @@ 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]; } @@ -115,13 +125,10 @@ #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) Index: src/OFStreamSocket.m ================================================================== --- src/OFStreamSocket.m +++ src/OFStreamSocket.m @@ -33,26 +33,16 @@ #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)) + if (!of_init_sockets()) @throw [OFInitializationFailedException exceptionWithClass: self]; -#elif defined(__wii__) - if (net_init() < 0) - @throw [OFInitializationFailedException - exceptionWithClass: self]; -#endif } + (instancetype)socket { return [[[self alloc] init] autorelease]; Index: src/OFUDPSocket.m ================================================================== --- src/OFUDPSocket.m +++ src/OFUDPSocket.m @@ -26,10 +26,11 @@ #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" @@ -274,10 +275,20 @@ 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]; } Index: src/socket.h ================================================================== --- src/socket.h +++ src/socket.h @@ -17,10 +17,12 @@ #import "objfw-defs.h" #ifndef OF_HAVE_SOCKETS # error No sockets available! #endif + +#include #ifdef OF_HAVE_SYS_SOCKET_H # include #endif @@ -49,5 +51,7 @@ 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 Index: src/socket.m ================================================================== --- src/socket.m +++ src/socket.m @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + * Jonathan Schleifer + * + * 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; +}