︙ | | |
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
-
+
|
{
@public
# ifdef OF_HAVE_BLOCKS
of_udp_socket_async_send_block_t _block;
# endif
const void *_buffer;
size_t _length;
of_udp_socket_address_t _receiver;
of_socket_address_t _receiver;
}
@end
@implementation OFRunLoop_QueueItem
- (bool)handleObject: (id)object
{
OF_UNRECOGNIZED_SELECTOR
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
# endif
@end
@implementation OFRunLoop_UDPReceiveQueueItem
- (bool)handleObject: (id)object
{
size_t length;
of_udp_socket_address_t address;
of_socket_address_t address;
id exception = nil;
@try {
length = [object receiveIntoBuffer: _buffer
length: _length
sender: &address];
} @catch (id e) {
length = 0;
exception = e;
}
# ifdef OF_HAVE_BLOCKS
if (_block != NULL)
return _block(object, _buffer, length, address, exception);
else {
# endif
bool (*func)(id, SEL, OFUDPSocket *, void *, size_t,
of_udp_socket_address_t, id, id) =
of_socket_address_t, id, id) =
(bool (*)(id, SEL, OFUDPSocket *, void *, size_t,
of_udp_socket_address_t, id, id))
of_socket_address_t, id, id))
[_target methodForSelector: _selector];
return func(_target, _selector, object, _buffer, length,
address, _context, exception);
# ifdef OF_HAVE_BLOCKS
}
# endif
|
︙ | | |
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
-
+
-
+
|
_length = _block(object, &_buffer,
(exception == nil ? _length : 0), &_receiver, exception);
return (_length > 0);
} else {
# endif
size_t (*func)(id, SEL, OFUDPSocket *, const void *, size_t,
of_udp_socket_address_t *, id, id) =
of_socket_address_t *, id, id) =
(size_t (*)(id, SEL, OFUDPSocket *, const void *, size_t,
of_udp_socket_address_t *, id, id))
of_socket_address_t *, id, id))
[_target methodForSelector: _selector];
_length = func(_target, _selector, object, &_buffer,
(exception == nil ? _length : 0), &_receiver, _context,
exception);
return (_length > 0);
|
︙ | | |
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
-
+
|
queueItem->_length = length;
})
}
+ (void)of_addAsyncSendForUDPSocket: (OFUDPSocket *)sock
buffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
target: (id)target
selector: (SEL)selector
context: (id)context
{
ADD_WRITE(OFRunLoop_UDPSendQueueItem, sock, {
queueItem->_target = [target retain];
queueItem->_selector = selector;
|
︙ | | |
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
-
+
|
queueItem->_length = length;
})
}
+ (void)of_addAsyncSendForUDPSocket: (OFUDPSocket *)sock
buffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
block: (of_udp_socket_async_send_block_t)block
{
ADD_WRITE(OFRunLoop_UDPSendQueueItem, sock, {
queueItem->_block = [block copy];
queueItem->_buffer = buffer;
queueItem->_length = length;
queueItem->_receiver = receiver;
|
︙ | | |
︙ | | |
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
70
71
72
|
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
|
-
-
-
-
-
-
-
-
-
-
-
+
-
+
|
OF_ASSUME_NONNULL_BEGIN
/*! @file */
@class OFUDPSocket;
/*!
* @struct of_udp_socket_address_t OFUDPSocket.h ObjFW/OFUDPSocket.h
*
* @brief A struct which represents a host / port pair for a UDP socket.
*/
typedef struct OF_BOXABLE {
struct sockaddr_storage address;
socklen_t length;
} of_udp_socket_address_t;
#ifdef OF_HAVE_BLOCKS
/*!
* @brief A block which is called when the host / port pair for the UDP socket
* has been resolved.
*
* @param host The host that has been resolved
* @param port The port of the host / port pair
* @param address The address of the resolved host / port pair
* @param exception An exception which occurred while resolving or `nil` on
* success
*/
typedef void (^of_udp_socket_async_resolve_block_t)(OFString *host,
uint16_t port, of_udp_socket_address_t address, id _Nullable exception);
uint16_t port, of_socket_address_t address, id _Nullable exception);
/*!
* @brief A block which is called when a packet has been received.
*
* @param socket The UDP socket which received a packet
* @param buffer The buffer the packet has been written to
* @param length The length of the packet
* @param sender The address of the sender of the packet
* @param exception An exception which occurred while receiving or `nil` on
* success
* @return A bool whether the same block should be used for the next receive
*/
typedef bool (^of_udp_socket_async_receive_block_t)(OFUDPSocket *socket,
void *buffer, size_t length, of_udp_socket_address_t sender,
void *buffer, size_t length, of_socket_address_t sender,
id _Nullable exception);
/*!
* @brief A block which is called when a packet has been sent.
*
* @param socket The UDP socket which sent a packet
* @param buffer A pointer to the buffer which was sent. This can be changed to
|
︙ | | |
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
-
+
-
+
-
+
-
-
+
|
* @return The length to repeat the send with or 0 if it should not repeat.
* The buffer and receiver may be changed, so that every time a new
* buffer, length and receiver can be specified while the callback
* stays the same.
*/
typedef size_t (^of_udp_socket_async_send_block_t)(OFUDPSocket *socket,
const void *_Nonnull *_Nonnull buffer, size_t bytesSent,
of_udp_socket_address_t *_Nonnull receiver, id exception);
of_socket_address_t *_Nonnull receiver, id exception);
#endif
/*!
* @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h
*
* @brief A class which provides methods to create and use UDP sockets.
*
* Addresses are of type @ref of_udp_socket_address_t. You can use
* Addresses are of type @ref of_socket_address_t. You can use
* @ref resolveAddressForHost:port:address: to create an address for a host /
* port pair and @ref getHost:andPort:forAddress: to get the host / port pair
* for an address. If you want to compare two addresses, you can use
* @ref of_udp_socket_address_equal and you can use
* @ref of_socket_address_equal and you can use @ref of_socket_address_hash to
* @ref of_udp_socket_address_hash to get a hash to use in e.g.
* @ref OFMapTable.
* get a hash to use in e.g. @ref OFMapTable.
*
* @warning Even though the OFCopying protocol is implemented, it does *not*
* return an independent copy of the socket, but instead retains it.
* This is so that the socket can be used as a key for a dictionary,
* so context can be associated with a socket. Using a socket in more
* than one thread at the same time is not thread-safe, even if copy
* was called to create one "instance" for every thread!
|
︙ | | |
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
-
+
-
+
|
* @param host The host to resolve
* @param port The port for the resulting address
* @param address A pointer to the address that should be filled with the
* host / port pair
*/
+ (void)resolveAddressForHost: (OFString *)host
port: (uint16_t)port
address: (of_udp_socket_address_t *)address;
address: (of_socket_address_t *)address;
#ifdef OF_HAVE_THREADS
/*!
* @brief Asynchronously resolves the specified host and creates an address for
* the host / port pair.
*
* @param host The host to resolve
* @param port The port for the resulting address
* @param target The target on which to call the selector once the host has been
* resolved
* @param selector The selector to call on the target. The signature must be
* `void (OFString *host, uint16_t port,
* of_udp_socket_address_t address, id context, id exception)`.
* of_socket_address_t address, id context, id exception)`.
* @param context A context object to pass along to the target
*/
+ (void)asyncResolveAddressForHost: (OFString *)host
port: (uint16_t)port
target: (id)target
selector: (SEL)selector
context: (nullable id)context;
|
︙ | | |
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
236
237
238
|
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
|
-
+
-
-
+
+
-
+
-
+
|
* set to the host of the host / port pair.
* @param port A pointer to an uint16_t. If it is not NULL, the port of the
* host / port pair will be written to it.
* @param address The address for which the host and port should be retrieved
*/
+ (void)getHost: (OFString *__autoreleasing _Nonnull *_Nullable)host
andPort: (nullable uint16_t *)port
forAddress: (of_udp_socket_address_t *)address;
forAddress: (of_socket_address_t *)address;
/*!
* @brief Binds the socket to the specified host and port.
*
* @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for
* IPv6 to bind to all.
* @param port The port to bind to. If the port is 0, an unused port will be
* chosen, which can be obtained using the return value.
* @return The port the socket was bound to
*/
- (uint16_t)bindToHost: (OFString *)host
port: (uint16_t)port;
/*!
* @brief Receives a datagram and stores it into the specified buffer.
*
* If the buffer is too small, the datagram is truncated.
*
* @param buffer The buffer to write the datagram to
* @param length The length of the buffer
* @param sender A pointer to an @ref of_udp_socket_address_t, which will be
* set to the address of the sender
* @param sender A pointer to an @ref of_socket_address_t, which will be set to
* the address of the sender
* @return The length of the received datagram
*/
- (size_t)receiveIntoBuffer: (void *)buffer
length: (size_t)length
sender: (of_udp_socket_address_t *)sender;
sender: (of_socket_address_t *)sender;
/*!
* @brief Asynchronously receives a datagram and stores it into the specified
* buffer.
*
* If the buffer is too small, the datagram is truncated.
*
* @param buffer The buffer to write the datagram to
* @param length The length of the buffer
* @param target The target on which the selector should be called when the
* datagram has been received. If the method returns true, it
* will be called again with the same buffer and maximum length
* when more datagrams have been received. If you want the next
* method in the queue to handle the datagram received next, you
* need to return false from the method.
* @param selector The selector to call on the target. The signature must be
* `bool (OFUDPSocket *socket, void *buffer, size_t length,
* of_udp_socket_address_t sender, id context, id exception)`.
* of_socket_address_t sender, id context, id exception)`.
* @param context A context object to pass along to the target
*/
- (void)asyncReceiveIntoBuffer: (void *)buffer
length: (size_t)length
target: (id)target
selector: (SEL)selector
context: (nullable id)context;
|
︙ | | |
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
350
351
352
353
354
355
|
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
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
|
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
#endif
/*!
* @brief Sends the specified datagram to the specified address.
*
* @param buffer The buffer to send as a datagram
* @param length The length of the buffer
* @param receiver A pointer to an @ref of_udp_socket_address_t to which the
* @param receiver A pointer to an @ref of_socket_address_t to which the
* datagram should be sent
*/
- (void)sendBuffer: (const void *)buffer
length: (size_t)length
receiver: (const of_udp_socket_address_t *)receiver;
receiver: (const of_socket_address_t *)receiver;
/*!
* @brief Asynchronously sends the specified datagram to the specified address.
*
* @param buffer The buffer to send as a datagram
* @param length The length of the buffer
* @param receiver A pointer to an @ref of_udp_socket_address_t to which the
* @param receiver A pointer to an @ref of_socket_address_t to which the
* datagram should be sent
* @param target The target on which the selector should be called when the
* packet has been sent. The method should return the length for
* the next send with the same callback or 0 if it should not
* repeat. The buffer and receiver may be changed, so that every
* time a new buffer, length and receiver can be specified while
* the callback stays the same.
* @param selector The selector to call on the target. The signature must be
* `size_t (OFUDPSocket *socket, const void **buffer,
* size_t bytesSent, of_udp_socket_address_t *receiver,
* id context, id exception)`.
* size_t bytesSent, of_socket_address_t *receiver, id context,
* id exception)`.
* @param context A context object to pass along to the target
*/
- (void)asyncSendBuffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
target: (id)target
selector: (SEL)selector
context: (nullable id)context;
#ifdef OF_HAVE_BLOCKS
/*!
* @brief Asynchronously sends the specified datagram to the specified address.
*
* @param buffer The buffer to send as a datagram
* @param length The length of the buffer
* @param receiver A pointer to an @ref of_udp_socket_address_t to which the
* @param receiver A pointer to an @ref of_socket_address_t to which the
* datagram should be sent
* @param block The block to call when the packet has been sent. It should
* return the length for the next send with the same callback or 0
* if it should not repeat. The buffer and receiver may be
* changed, so that every time a new buffer, length and receiver
* can be specified while the callback stays the same.
*/
- (void)asyncSendBuffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
block: (of_udp_socket_async_send_block_t)block;
#endif
/*!
* @brief Cancels all pending asynchronous requests on the socket.
*/
- (void)cancelAsyncRequests;
/*!
* @brief Closes the socket so that it can neither receive nor send any more
* datagrams.
*/
- (void)close;
@end
#ifdef __cplusplus
extern "C" {
#endif
/*!
* @brief Compares two of_udp_socket_address_t for equality.
*
* @param address1 The address to compare with the second address
* @param address2 The second address
* @return Whether the two addresses are equal
*/
extern bool of_udp_socket_address_equal(of_udp_socket_address_t *address1,
of_udp_socket_address_t *address2);
/*!
* @brief Returns the hash for the specified of_udp_socket_address_t.
*
* @param address The address to hash
* @return The hash for the specified of_udp_socket_address_t
*/
extern uint32_t of_udp_socket_address_hash(of_udp_socket_address_t *address);
#ifdef __cplusplus
}
#endif
OF_ASSUME_NONNULL_END
|
︙ | | |
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
-
+
|
uint16_t _port;
id _target;
SEL _selector;
id _context;
# ifdef OF_HAVE_BLOCKS
of_udp_socket_async_resolve_block_t _block;
# endif
of_udp_socket_address_t _address;
of_socket_address_t _address;
id _exception;
}
- (instancetype)initWithSourceThread: (OFThread *)sourceThread
host: (OFString *)host
port: (uint16_t)port
target: (id)target
|
︙ | | |
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
-
+
-
+
|
# ifdef OF_HAVE_BLOCKS
if (_block != NULL)
_block(_host, _port, _address, _exception);
else {
# endif
void (*func)(id, SEL, OFString *, uint16_t,
of_udp_socket_address_t, id, id) =
of_socket_address_t, id, id) =
(void (*)(id, SEL, OFString *, uint16_t,
of_udp_socket_address_t, id, id))
of_socket_address_t, id, id))
[_target methodForSelector: _selector];
func(_target, _selector, _host, _port, _address, _context,
_exception);
# ifdef OF_HAVE_BLOCKS
}
# endif
|
︙ | | |
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
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
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
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
objc_autoreleasePoolPop(pool);
return nil;
}
@end
#endif
bool
of_udp_socket_address_equal(of_udp_socket_address_t *address1,
of_udp_socket_address_t *address2)
{
struct sockaddr_in *addrIn1, *addrIn2;
#ifdef HAVE_IPV6
struct sockaddr_in6 *addrIn6_1, *addrIn6_2;
#endif
if (address1->address.ss_family != address2->address.ss_family)
return false;
switch (address1->address.ss_family) {
case AF_INET:
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
if (address1->length < (socklen_t)sizeof(struct sockaddr_in) ||
address2->length < (socklen_t)sizeof(struct sockaddr_in))
@throw [OFInvalidArgumentException exception];
#else
if (address1->length < 8 || address2->length < 8)
@throw [OFInvalidArgumentException exception];
#endif
addrIn1 = (struct sockaddr_in *)&address1->address;
addrIn2 = (struct sockaddr_in *)&address2->address;
if (addrIn1->sin_port != addrIn2->sin_port)
return false;
if (addrIn1->sin_addr.s_addr != addrIn2->sin_addr.s_addr)
return false;
break;
#ifdef HAVE_IPV6
case AF_INET6:
if (address1->length < sizeof(struct sockaddr_in6) ||
address2->length < sizeof(struct sockaddr_in6))
@throw [OFInvalidArgumentException exception];
addrIn6_1 = (struct sockaddr_in6 *)&address1->address;
addrIn6_2 = (struct sockaddr_in6 *)&address2->address;
if (addrIn6_1->sin6_port != addrIn6_2->sin6_port)
return false;
if (memcmp(addrIn6_1->sin6_addr.s6_addr,
addrIn6_2->sin6_addr.s6_addr,
sizeof(addrIn6_1->sin6_addr.s6_addr)) != 0)
return false;
break;
#endif
default:
@throw [OFInvalidArgumentException exception];
}
return true;
}
uint32_t
of_udp_socket_address_hash(of_udp_socket_address_t *address)
{
uint32_t hash = of_hash_seed;
struct sockaddr_in *addrIn;
#ifdef HAVE_IPV6
struct sockaddr_in6 *addrIn6;
uint32_t subhash;
#endif
hash += address->address.ss_family;
switch (address->address.ss_family) {
case AF_INET:
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
if (address->length < (socklen_t)sizeof(struct sockaddr_in))
@throw [OFInvalidArgumentException exception];
#else
if (address->length < 8)
@throw [OFInvalidArgumentException exception];
#endif
addrIn = (struct sockaddr_in *)&address->address;
hash += (addrIn->sin_port << 1);
hash ^= addrIn->sin_addr.s_addr;
break;
#ifdef HAVE_IPV6
case AF_INET6:
if (address->length < sizeof(struct sockaddr_in6))
@throw [OFInvalidArgumentException exception];
addrIn6 = (struct sockaddr_in6 *)&address->address;
hash += (addrIn6->sin6_port << 1);
OF_HASH_INIT(subhash);
for (size_t i = 0; i < sizeof(addrIn6->sin6_addr.s6_addr); i++)
OF_HASH_ADD(subhash, adrIn6->sin6_addr.s6_addr[i]);
OF_HASH_FINALIZE(subhash);
hash ^= subhash;
break;
#endif
default:
@throw [OFInvalidArgumentException exception];
}
return hash;
}
@implementation OFUDPSocket
+ (void)initialize
{
if (self != [OFUDPSocket class])
return;
if (!of_socket_init())
@throw [OFInitializationFailedException
exceptionWithClass: self];
}
+ (instancetype)socket
{
return [[[self alloc] init] autorelease];
}
+ (void)resolveAddressForHost: (OFString *)host
port: (uint16_t)port
address: (of_udp_socket_address_t *)address
address: (of_socket_address_t *)address
{
of_resolver_result_t **results =
of_resolve_host(host, port, SOCK_DGRAM);
assert(results[0]->addressLength <=
(socklen_t)sizeof(address->address));
|
︙ | | |
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
-
+
|
objc_autoreleasePoolPop(pool);
}
# endif
#endif
+ (void)getHost: (OFString *__autoreleasing *)host
andPort: (uint16_t *)port
forAddress: (of_udp_socket_address_t *)address
forAddress: (of_socket_address_t *)address
{
of_address_to_string_and_port(
(struct sockaddr *)&address->address, address->length, host, port);
}
- (instancetype)init
{
|
︙ | | |
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
-
+
|
port: port
socket: self
errNo: EAFNOSUPPORT];
}
- (size_t)receiveIntoBuffer: (void *)buffer
length: (size_t)length
sender: (of_udp_socket_address_t *)sender
sender: (of_socket_address_t *)sender
{
ssize_t ret;
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
sender->length = (socklen_t)sizeof(sender->address);
|
︙ | | |
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
-
+
|
length: length
block: block];
}
#endif
- (void)sendBuffer: (const void *)buffer
length: (size_t)length
receiver: (const of_udp_socket_address_t *)receiver
receiver: (const of_socket_address_t *)receiver
{
if (_socket == INVALID_SOCKET)
@throw [OFNotOpenException exceptionWithObject: self];
#ifndef OF_WINDOWS
ssize_t bytesWritten;
|
︙ | | |
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
-
+
-
+
|
requestedLength: length
bytesWritten: bytesWritten
errNo: 0];
}
- (void)asyncSendBuffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
target: (id)target
selector: (SEL)selector
context: (id)context
{
[OFRunLoop of_addAsyncSendForUDPSocket: self
buffer: buffer
length: length
receiver: receiver
target: target
selector: selector
context: context];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncSendBuffer: (const void *)buffer
length: (size_t)length
receiver: (of_udp_socket_address_t)receiver
receiver: (of_socket_address_t)receiver
block: (of_udp_socket_async_send_block_t)block
{
[OFRunLoop of_addAsyncSendForUDPSocket: self
buffer: buffer
length: length
receiver: receiver
block: block];
|
︙ | | |
︙ | | |
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
+
+
|
# ifdef __MINGW64_VERSION_MAJOR
# include <winsock2.h>
# endif
# endif
# include <windows.h>
# include <ws2tcpip.h>
#endif
/*! @file */
#ifdef OF_AMIGAOS
struct sockaddr_storage {
uint8_t ss_len;
uint8_t ss_family;
char ss_data[2 + sizeof(struct in_addr) + 8];
};
|
︙ | | |
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
#ifndef OF_WINDOWS
typedef int of_socket_t;
#else
typedef SOCKET of_socket_t;
#endif
/*!
* @struct of_socket_address_t socket.h ObjFW/socket.h
*
* @brief A struct which represents a host / port pair for a socket.
*/
typedef struct OF_BOXABLE {
struct sockaddr_storage address;
socklen_t length;
} of_socket_address_t;
#ifdef __cplusplus
extern "C" {
#endif
extern bool of_socket_init(void);
extern int of_socket_errno(void);
# ifndef OF_WII
extern int of_getsockname(of_socket_t sock, struct sockaddr *restrict addr,
socklen_t *restrict addrLen);
# endif
/*!
* @brief Compares two of_socket_address_t for equality.
*
* @param address1 The address to compare with the second address
* @param address2 The second address
* @return Whether the two addresses are equal
*/
extern bool of_socket_address_equal(of_socket_address_t *address1,
of_socket_address_t *address2);
/*!
* @brief Returns the hash for the specified of_socket_address_t.
*
* @param address The address to hash
* @return The hash for the specified of_socket_address_t
*/
extern uint32_t of_socket_address_hash(of_socket_address_t *address);
#ifdef __cplusplus
}
#endif
OF_ASSUME_NONNULL_END
|
︙ | | |
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
-
+
|
@implementation TestsAppDelegate (OFUDPSocketTests)
- (void)UDPSocketTests
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFUDPSocket *sock;
uint16_t port1, port2;
of_udp_socket_address_t addr1, addr2, addr3;
of_socket_address_t addr1, addr2, addr3;
char buf[6];
OFString *host;
TEST(@"+[socket]", (sock = [OFUDPSocket socket]))
TEST(@"-[bindToHost:port:]",
(port1 = [sock bindToHost: @"127.0.0.1"
|
︙ | | |
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
+
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
+
-
|
forAddress: &addr2]) &&
[host isEqual: @"127.0.0.1"] && port2 == port1)
[OFUDPSocket resolveAddressForHost: @"127.0.0.1"
port: port1 + 1
address: &addr3];
/*
* TODO: Move those tests elsewhere as soon as the DNS resolving part
* is no longer in OFUDPSocket.
*/
TEST(@"of_udp_socket_address_equal()",
of_udp_socket_address_equal(&addr1, &addr2) &&
!of_udp_socket_address_equal(&addr1, &addr3))
TEST(@"of_socket_address_equal()",
of_socket_address_equal(&addr1, &addr2) &&
!of_socket_address_equal(&addr1, &addr3))
TEST(@"of_udp_socket_address_hash()",
of_udp_socket_address_hash(&addr1) ==
TEST(@"of_socket_address_hash()",
of_socket_address_hash(&addr1) == of_socket_address_hash(&addr2) &&
of_udp_socket_address_hash(&addr2) &&
of_udp_socket_address_hash(&addr1) !=
of_socket_address_hash(&addr1) != of_socket_address_hash(&addr3))
of_udp_socket_address_hash(&addr3))
[pool drain];
}
@end
|