1
2
3
4
5
6
7
8
9
|
1
2
3
4
5
6
7
8
9
|
-
+
|
/*
* Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
* Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
*
* 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.
*
|
| ︙ | | |
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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
|
+
+
+
+
+
+
|
#import "OFDatagramSocket.h"
#import "OFData.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFSocket.h"
#import "OFSocket+Private.h"
#import "OFAlreadyOpenException.h"
#import "OFGetOptionFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"
#if defined(OF_AMIGAOS) && !defined(UNIQUE_ID)
# define UNIQUE_ID -1
#endif
@implementation OFDatagramSocket
@synthesize delegate = _delegate;
+ (void)initialize
{
if (self != [OFDatagramSocket class])
|
| ︙ | | |
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
+
+
+
|
@try {
if (self.class == [OFDatagramSocket class]) {
[self doesNotRecognizeSelector: _cmd];
abort();
}
_socket = OFInvalidSocketHandle;
#ifdef OF_HAVE_AMIGAOS
_socketID = -1;
#endif
_canBlock = true;
} @catch (id e) {
[self release];
@throw e;
}
return self;
|
| ︙ | | |
194
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
|
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
|
-
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
+
+
|
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: length
errNo: OFSocketErrNo()];
#endif
if (sender != NULL) {
switch (((struct sockaddr *)&sender->sockaddr)->sa_family) {
case AF_INET:
sender->family = OFSocketAddressFamilyIPv4;
break;
struct sockaddr *sa = (struct sockaddr *)&sender->sockaddr;
if (sender->length >= (socklen_t)sizeof(sa->sa_family)) {
switch (sa->sa_family) {
case AF_INET:
sender->family = OFSocketAddressFamilyIPv4;
break;
#ifdef OF_HAVE_IPV6
case AF_INET6:
sender->family = OFSocketAddressFamilyIPv6;
break;
case AF_INET6:
sender->family = OFSocketAddressFamilyIPv6;
break;
#endif
#ifdef OF_HAVE_UNIX_SOCKETS
case AF_UNIX:
sender->family = OFSocketAddressFamilyUNIX;
break;
case AF_UNIX:
sender->family = OFSocketAddressFamilyUNIX;
break;
#endif
#ifdef OF_HAVE_IPX
case AF_IPX:
sender->family = OFSocketAddressFamilyIPX;
break;
case AF_IPX:
sender->family = OFSocketAddressFamilyIPX;
break;
#endif
#ifdef OF_HAVE_APPLETALK
case AF_APPLETALK:
sender->family = OFSocketAddressFamilyAppleTalk;
break;
case AF_APPLETALK:
sender->family = OFSocketAddressFamilyAppleTalk;
break;
#endif
default:
sender->family = OFSocketAddressFamilyUnknown;
break;
}
default:
sender->family = OFSocketAddressFamilyUnknown;
break;
}
} else
sender->family = OFSocketAddressFamilyUnknown;
}
return ret;
}
- (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length
{
|
| ︙ | | |
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
if (_socket > INT_MAX)
@throw [OFOutOfRangeException exception];
return (int)_socket;
#endif
}
- (void)releaseSocketFromCurrentThread
{
#ifdef OF_AMIGAOS
if (_socket == OFInvalidSocketHandle)
@throw [OFNotOpenException exceptionWithObject: self];
if ((_socketID = ReleaseSocket(_socket, UNIQUE_ID)) == -1) {
switch (Errno()) {
case ENOMEM:
@throw [OFOutOfMemoryException
exceptionWithRequestedSize: 0];
case EBADF:
@throw [OFNotOpenException exceptionWithObject: self];
default:
OFEnsure(0);
}
}
_socket = OFInvalidSocketHandle;
#endif
}
- (void)obtainSocketForCurrentThread
{
#ifdef OF_AMIGAOS
if (_socket != OFInvalidSocketHandle)
@throw [OFAlreadyOpenException exceptionWithObject: self];
if (_socketID == -1)
@throw [OFNotOpenException exceptionWithObject: self];
/*
* FIXME: We should store these, but that requires changing all
* subclasses. This only becomes a problem if IPv6 support ever
* gets added.
*/
_socket = ObtainSocket(_socketID, AF_INET, SOCK_DGRAM, 0);
if (_socket == OFInvalidSocketHandle)
@throw [OFInitializationFailedException
exceptionWithClass: self.class];
_socketID = -1;
#endif
}
- (void)close
{
if (_socket == OFInvalidSocketHandle)
@throw [OFNotOpenException exceptionWithObject: self];
closesocket(_socket);
_socket = OFInvalidSocketHandle;
}
@end
|