| ︙ | | | ︙ | |
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
|
#include <errno.h>
#include <string.h>
#import "OFStreamSocket.h"
#import "OFStreamSocket+Private.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFAcceptFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"
#import "socket_helpers.h"
@implementation OFStreamSocket
@dynamic delegate;
@synthesize listening = _listening;
+ (void)initialize
{
if (self != [OFStreamSocket class])
return;
if (!of_socket_init())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
+ (instancetype)socket
{
return [[[self alloc] init] autorelease];
|
>
<
<
|
|
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
|
#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 "OFAcceptFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFSetOptionFailedException.h"
#import "OFWriteFailedException.h"
@implementation OFStreamSocket
@dynamic delegate;
@synthesize listening = _listening;
+ (void)initialize
{
if (self != [OFStreamSocket class])
return;
if (!OFSocketInit())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
+ (instancetype)socket
{
return [[[self alloc] init] autorelease];
|
| ︙ | | | ︙ | |
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
@throw [OFNotOpenException exceptionWithObject: self];
#ifndef OF_WINDOWS
if ((ret = recv(_socket, buffer, length, 0)) < 0)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: length
errNo: of_socket_errno()];
#else
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
if ((ret = recv(_socket, buffer, (int)length, 0)) < 0)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: length
errNo: of_socket_errno()];
#endif
if (ret == 0)
_atEndOfStream = true;
return ret;
}
|
|
|
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
@throw [OFNotOpenException exceptionWithObject: self];
#ifndef OF_WINDOWS
if ((ret = recv(_socket, buffer, length, 0)) < 0)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: length
errNo: OFSocketErrNo()];
#else
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
if ((ret = recv(_socket, buffer, (int)length, 0)) < 0)
@throw [OFReadFailedException
exceptionWithObject: self
requestedLength: length
errNo: OFSocketErrNo()];
#endif
if (ret == 0)
_atEndOfStream = true;
return ret;
}
|
| ︙ | | | ︙ | |
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
@throw [OFOutOfRangeException exception];
if ((bytesWritten = send(_socket, (void *)buffer, length, 0)) < 0)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: length
bytesWritten: 0
errNo: of_socket_errno()];
#else
int bytesWritten;
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
if ((bytesWritten = send(_socket, buffer, (int)length, 0)) < 0)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: length
bytesWritten: 0
errNo: of_socket_errno()];
#endif
return (size_t)bytesWritten;
}
#if defined(OF_WINDOWS) || defined(OF_AMIGAOS)
- (void)setCanBlock: (bool)canBlock
{
# ifdef OF_WINDOWS
u_long v = canBlock;
# else
char v = canBlock;
# endif
if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR)
@throw [OFSetOptionFailedException
exceptionWithObject: self
errNo: of_socket_errno()];
_canBlock = canBlock;
}
#endif
- (int)fileDescriptorForReading
{
|
|
|
|
|
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
@throw [OFOutOfRangeException exception];
if ((bytesWritten = send(_socket, (void *)buffer, length, 0)) < 0)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: length
bytesWritten: 0
errNo: OFSocketErrNo()];
#else
int bytesWritten;
if (length > INT_MAX)
@throw [OFOutOfRangeException exception];
if ((bytesWritten = send(_socket, buffer, (int)length, 0)) < 0)
@throw [OFWriteFailedException
exceptionWithObject: self
requestedLength: length
bytesWritten: 0
errNo: OFSocketErrNo()];
#endif
return (size_t)bytesWritten;
}
#if defined(OF_WINDOWS) || defined(OF_AMIGAOS)
- (void)setCanBlock: (bool)canBlock
{
# ifdef OF_WINDOWS
u_long v = canBlock;
# else
char v = canBlock;
# endif
if (ioctlsocket(_socket, FIONBIO, &v) == SOCKET_ERROR)
@throw [OFSetOptionFailedException
exceptionWithObject: self
errNo: OFSocketErrNo()];
_canBlock = canBlock;
}
#endif
- (int)fileDescriptorForReading
{
|
| ︙ | | | ︙ | |
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
|
- (int)of_socketError
{
int errNo;
socklen_t len = sizeof(errNo);
if (getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char *)&errNo,
&len) != 0)
return of_socket_errno();
return errNo;
}
#endif
- (void)listen
{
[self listenWithBacklog: SOMAXCONN];
}
- (void)listenWithBacklog: (int)backlog
{
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
if (listen(_socket, backlog) == -1)
@throw [OFListenFailedException
exceptionWithSocket: self
backlog: backlog
errNo: of_socket_errno()];
_listening = true;
}
- (instancetype)accept
{
OFStreamSocket *client = [[[[self class] alloc] init] autorelease];
|
|
|
|
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
|
- (int)of_socketError
{
int errNo;
socklen_t len = sizeof(errNo);
if (getsockopt(_socket, SOL_SOCKET, SO_ERROR, (char *)&errNo,
&len) != 0)
return OFSocketErrNo();
return errNo;
}
#endif
- (void)listen
{
[self listenWithBacklog: SOMAXCONN];
}
- (void)listenWithBacklog: (int)backlog
{
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
if (listen(_socket, backlog) == -1)
@throw [OFListenFailedException
exceptionWithSocket: self
backlog: backlog
errNo: OFSocketErrNo()];
_listening = true;
}
- (instancetype)accept
{
OFStreamSocket *client = [[[[self class] alloc] init] autorelease];
|
| ︙ | | | ︙ | |
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
#if defined(HAVE_PACCEPT) && defined(SOCK_CLOEXEC)
if ((client->_socket = paccept(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length, NULL, SOCK_CLOEXEC)) ==
INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: of_socket_errno()];
#elif defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
if ((client->_socket = accept4(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length, SOCK_CLOEXEC)) == INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: of_socket_errno()];
#else
if ((client->_socket = accept(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length)) == INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: of_socket_errno()];
# 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 <=
(socklen_t)sizeof(client->_remoteAddress.sockaddr));
switch (client->_remoteAddress.sockaddr.sockaddr.sa_family) {
case AF_INET:
client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPV4;
break;
#ifdef OF_HAVE_IPV6
case AF_INET6:
client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPV6;
break;
#endif
#ifdef OF_HAVE_IPX
case AF_IPX:
client->_remoteAddress.family = OF_SOCKET_ADDRESS_FAMILY_IPX;
break;
#endif
default:
client->_remoteAddress.family =
OF_SOCKET_ADDRESS_FAMILY_UNKNOWN;
break;
}
return client;
}
- (void)asyncAccept
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default];
}
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
{
[OFRunLoop of_addAsyncAcceptForSocket: self
mode: runLoopMode
block: NULL
delegate: _delegate];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (of_stream_socket_async_accept_block_t)block
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
block: block];
}
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
block: (of_stream_socket_async_accept_block_t)block
{
[OFRunLoop of_addAsyncAcceptForSocket: self
mode: runLoopMode
block: block
delegate: nil];
}
#endif
- (const of_socket_address_t *)remoteAddress
{
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
if (_remoteAddress.length == 0)
@throw [OFInvalidArgumentException exception];
|
|
|
|
|
|
|
|
<
|
|
|
|
<
|
|
|
|
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#if defined(HAVE_PACCEPT) && defined(SOCK_CLOEXEC)
if ((client->_socket = paccept(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length, NULL, SOCK_CLOEXEC)) ==
INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: OFSocketErrNo()];
#elif defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC)
if ((client->_socket = accept4(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length, SOCK_CLOEXEC)) == INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: OFSocketErrNo()];
#else
if ((client->_socket = accept(_socket,
&client->_remoteAddress.sockaddr.sockaddr,
&client->_remoteAddress.length)) == INVALID_SOCKET)
@throw [OFAcceptFailedException
exceptionWithSocket: self
errNo: OFSocketErrNo()];
# 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 <=
(socklen_t)sizeof(client->_remoteAddress.sockaddr));
switch (client->_remoteAddress.sockaddr.sockaddr.sa_family) {
case AF_INET:
client->_remoteAddress.family = OFSocketAddressFamilyIPv4;
break;
#ifdef OF_HAVE_IPV6
case AF_INET6:
client->_remoteAddress.family = OFSocketAddressFamilyIPv6;
break;
#endif
#ifdef OF_HAVE_IPX
case AF_IPX:
client->_remoteAddress.family = OFSocketAddressFamilyIPX;
break;
#endif
default:
client->_remoteAddress.family = OFSocketAddressFamilyUnknown;
break;
}
return client;
}
- (void)asyncAccept
{
[self asyncAcceptWithRunLoopMode: OFDefaultRunLoopMode];
}
- (void)asyncAcceptWithRunLoopMode: (OFRunLoopMode)runLoopMode
{
[OFRunLoop of_addAsyncAcceptForSocket: self
mode: runLoopMode
block: NULL
delegate: _delegate];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (OFStreamSocketAsyncAcceptBlock)block
{
[self asyncAcceptWithRunLoopMode: OFDefaultRunLoopMode block: block];
}
- (void)asyncAcceptWithRunLoopMode: (OFRunLoopMode)runLoopMode
block: (OFStreamSocketAsyncAcceptBlock)block
{
[OFRunLoop of_addAsyncAcceptForSocket: self
mode: runLoopMode
block: block
delegate: nil];
}
#endif
- (const OFSocketAddress *)remoteAddress
{
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
if (_remoteAddress.length == 0)
@throw [OFInvalidArgumentException exception];
|
| ︙ | | | ︙ | |