ObjFW  Diff

Differences From Artifact [deff42ec22]:

To Artifact [1832892042]:

  • File src/socket.m — part of check-in [12b8a7552e] at 2016-03-27 23:10:53 on branch trunk — Add socket support for Nintendo 3DS

    This removes the port registry that existed for the Wii and instead
    tries binding to random ports in a loop until it succeeds or errno is
    not EADDRINUSE. This is done for the Wii and Nintendo 3DS now.

    This also includes several other socket-related cleanups. (user: js, size: 4219) [annotate] [blame] [check-ins using]


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





#include <errno.h>

#import "OFException.h"  /* For some E* -> WSAE* defines */
#import "OFInvalidArgumentException.h"
#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"

#import "socket.h"
#ifdef OF_HAVE_THREADS
# include "threading.h"








static of_once_t onceControl = OF_ONCE_INIT;
static of_mutex_t mutex;
#endif
static bool initialized = false;

#ifdef OF_WII
# ifdef OF_HAVE_THREADS







>
>
>
>











>

>
>
>
>
>
>







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
42
43
44
45
46
47
 * 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"

#ifdef OF_NINTENDO_3DS
# include <malloc.h>  /* For memalign() */
#endif

#include <errno.h>

#import "OFException.h"  /* For some E* -> WSAE* defines */
#import "OFInvalidArgumentException.h"
#import "OFLockFailedException.h"
#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;

#ifdef OF_WII
# ifdef OF_HAVE_THREADS
46
47
48
49
50
51
52










53
54
55
56
57
58
59
	WSADATA wsa;

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










#endif

#ifdef OF_HAVE_THREADS
	if (!of_mutex_new(&mutex))
		return;

# ifdef OF_WII







>
>
>
>
>
>
>
>
>
>







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

	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;

# ifdef OF_WII
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
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
# ifdef OF_HAVE_THREADS
	if (!of_mutex_unlock(&mutex))
		@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







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

216
217
218
219
220
221
222




















































































223
# ifdef OF_HAVE_THREADS
	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
}




















































































#endif