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.
*
|
| ︙ | | |
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
|
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
|
-
+
+
+
+
+
+
|
#ifndef _XOPEN_SOURCE_EXTENDED
# define _XOPEN_SOURCE_EXTENDED
#endif
#define __NO_EXT_QNX
#define _HPUX_ALT_XOPEN_SOCKET_API
#include <assert.h>
#include <errno.h>
#include <string.h>
#import "OFStreamSocket.h"
#import "OFStreamSocket+Private.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFSocket+Private.h"
#import "OFAcceptSocketFailedException.h"
#import "OFAlreadyOpenException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenOnSocketFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"
#if defined(OF_AMIGAOS) && !defined(UNIQUE_ID)
# define UNIQUE_ID -1
#endif
@implementation OFStreamSocket
@dynamic delegate;
@synthesize listening = _listening;
+ (void)initialize
{
|
| ︙ | | |
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
+
+
+
|
@try {
if (self.class == [OFStreamSocket class]) {
[self doesNotRecognizeSelector: _cmd];
abort();
}
_socket = OFInvalidSocketHandle;
#ifdef OF_AMIGAOS
_socketID = -1;
#endif
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
| ︙ | | |
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
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
|
-
+
+
+
+
+
|
errNo: OFSocketErrNo()];
_listening = true;
}
- (instancetype)accept
{
OFStreamSocket *client = [[[[self class] alloc] init] autorelease];
OFStreamSocket *client;
#if (!defined(HAVE_PACCEPT) && !defined(HAVE_ACCEPT4)) || !defined(SOCK_CLOEXEC)
# if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
int flags;
# endif
#endif
if (_socket == OFInvalidSocketHandle)
@throw [OFNotOpenException exceptionWithObject: self];
client = [[[[self class] alloc] init] autorelease];
client->_remoteAddress.length =
(socklen_t)sizeof(client->_remoteAddress.sockaddr);
#if defined(HAVE_PACCEPT) && defined(SOCK_CLOEXEC)
if ((client->_socket = paccept(_socket,
(struct sockaddr *)&client->_remoteAddress.sockaddr,
&client->_remoteAddress.length, NULL, SOCK_CLOEXEC)) ==
|
| ︙ | | |
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
-
+
|
# if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
if ((flags = fcntl(client->_socket, F_GETFD, 0)) != -1)
fcntl(client->_socket, F_SETFD, flags | FD_CLOEXEC);
# endif
#endif
assert(client->_remoteAddress.length <=
OFAssert(client->_remoteAddress.length <=
(socklen_t)sizeof(client->_remoteAddress.sockaddr));
switch (((struct sockaddr *)&client->_remoteAddress.sockaddr)
->sa_family) {
case AF_INET:
client->_remoteAddress.family = OFSocketAddressFamilyIPv4;
break;
|
| ︙ | | |
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
@throw [OFInvalidArgumentException exception];
if (_remoteAddress.length > (socklen_t)sizeof(_remoteAddress.sockaddr))
@throw [OFOutOfRangeException exception];
return &_remoteAddress;
}
- (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_STREAM, 0);
if (_socket == OFInvalidSocketHandle)
@throw [OFInitializationFailedException
exceptionWithClass: self.class];
_socketID = -1;
#endif
}
- (void)close
{
if (_socket == OFInvalidSocketHandle)
@throw [OFNotOpenException exceptionWithObject: self];
_listening = false;
|
| ︙ | | |