ObjFW  Diff

Differences From Artifact [a318aca5b3]:

To Artifact [ebf56cad93]:


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





50
51
52
53
54
55
56
 */

#include "config.h"

#include <errno.h>

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

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








static void
init(void)
{
#if defined(_WIN32)
	WSADATA wsa;

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

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





#endif

	initialized = true;
}

bool
of_socket_init()







>











>
>
>
>
>
>
>

















>
>
>
>
>







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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 */

#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 __wii__
# ifdef OF_HAVE_THREADS
static of_spinlock_t spinlock;
# endif
static uint8_t portRegistry[2][65536 / 8];
#endif

static void
init(void)
{
#if defined(_WIN32)
	WSADATA wsa;

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

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

# ifdef __wii__
	if (!of_spinlock_new(&spinlock))
		return;
# endif
#endif

	initialized = true;
}

bool
of_socket_init()
182
183
184
185
186
187
188




















































































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

	return ret;
}




















































































#endif







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

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