@@ -13,10 +13,14 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" + +#ifdef OF_NINTENDO_3DS +# include /* For memalign() */ +#endif #include #import "OFException.h" /* For some E* -> WSAE* defines */ #import "OFInvalidArgumentException.h" @@ -24,11 +28,18 @@ #import "OFUnlockFailedException.h" #import "socket.h" #ifdef OF_HAVE_THREADS # include "threading.h" +#endif + +#ifdef OF_NINTENDO_3DS +# include <3ds/types.h> +# include <3ds/services/soc.h> +#endif +#ifdef OF_HAVE_THREADS static of_once_t onceControl = OF_ONCE_INIT; static of_mutex_t mutex; #endif static bool initialized = false; @@ -48,10 +59,20 @@ if (WSAStartup(MAKEWORD(2, 0), &wsa)) return; #elif defined(OF_WII) if (net_init() < 0) return; +#elif defined(OF_NINTENDO_3DS) + void *ctx; + + if ((ctx = memalign(0x1000, 0x100000)) == NULL) + return; + + if (socInit(ctx, 0x100000) != 0) + return; + + atexit((void(*))socExit); #endif #ifdef OF_HAVE_THREADS if (!of_mutex_new(&mutex)) return; @@ -197,90 +218,6 @@ @throw [OFUnlockFailedException exception]; # endif return ret; } -#else -static size_t -type_to_index(int type) -{ - switch (type) { - case SOCK_STREAM: - return 0; - case SOCK_DGRAM: - return 1; - default: - @throw [OFInvalidArgumentException exception]; - } -} - -bool -of_socket_port_register(uint16_t port, int type) -{ - size_t index; - bool wasSet; - - if (port == 0) - @throw [OFInvalidArgumentException exception]; - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_lock(&spinlock)) - @throw [OFLockFailedException exception]; -# endif - - index = type_to_index(type); - wasSet = of_bitset_isset(portRegistry[index], port); - - of_bitset_set(portRegistry[index], port); - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_unlock(&spinlock)) - @throw [OFUnlockFailedException exception]; -# endif - - return !wasSet; -} - -void -of_socket_port_free(uint16_t port, int type) -{ - if (port == 0) - @throw [OFInvalidArgumentException exception]; - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_lock(&spinlock)) - @throw [OFLockFailedException exception]; -# endif - - of_bitset_clear(portRegistry[type_to_index(type)], port); - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_unlock(&spinlock)) - @throw [OFUnlockFailedException exception]; -# endif -} - -uint16_t -of_socket_port_find(int type) -{ - uint16_t port; - size_t index; - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_lock(&spinlock)) - @throw [OFLockFailedException exception]; -# endif - - index = type_to_index(type); - - do { - port = rand(); - } while (port == 0 || of_bitset_isset(portRegistry[index], port)); - -# ifdef OF_HAVE_THREADS - if (!of_spinlock_unlock(&spinlock)) - @throw [OFUnlockFailedException exception]; -# endif - - return port; -} #endif