ObjFW  Check-in [816c13da9b]

Overview
Comment:OFSCTPSocket: Rename packet to message

Let's use the proper terminology for the protocol.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sctp
Files: files | file ages | folders
SHA3-256: 816c13da9ba3f865f3558c70ca99469c8caae920e589252e37294f2d63e1b14e
User & Date: js on 2024-05-01 23:23:46
Other Links: branch diff | manifest | tags
Context
2024-05-04
20:05
OFSCTPSocket: Use a dictionary for message info Closed-Leaf check-in: 0fd2b1b4c8 user: js tags: sctp
2024-05-01
23:23
OFSCTPSocket: Rename packet to message check-in: 816c13da9b user: js tags: sctp
23:14
OFSCTPSocket: Add support for packet flags check-in: c4c40e01a8 user: js tags: sctp
Changes

Modified src/OFRunLoop+Private.h from [7785e31575] to [a9ea36d376].

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
     block: (nullable OFSCTPSocketAsyncReceiveBlock)block
#  endif
  delegate: (nullable id <OFSCTPSocketDelegate>)delegate;
+ (void)of_addAsyncSendForSCTPSocket: (OFSCTPSocket *)socket
      data: (OFData *)data
  streamID: (uint16_t)streamID
      PPID: (uint32_t)PPID
     flags: (OFSCTPPacketFlags)flags
      mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
     block: (nullable OFSCTPSocketAsyncSendDataBlock)block
# endif
  delegate: (nullable id <OFSCTPSocketDelegate>)delegate;
# endif
+ (void)of_cancelAsyncRequestsForObject: (id)object mode: (OFRunLoopMode)mode;







|







136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
     block: (nullable OFSCTPSocketAsyncReceiveBlock)block
#  endif
  delegate: (nullable id <OFSCTPSocketDelegate>)delegate;
+ (void)of_addAsyncSendForSCTPSocket: (OFSCTPSocket *)socket
      data: (OFData *)data
  streamID: (uint16_t)streamID
      PPID: (uint32_t)PPID
     flags: (OFSCTPMessageFlags)flags
      mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
     block: (nullable OFSCTPSocketAsyncSendDataBlock)block
# endif
  delegate: (nullable id <OFSCTPSocketDelegate>)delegate;
# endif
+ (void)of_cancelAsyncRequestsForObject: (id)object mode: (OFRunLoopMode)mode;

Modified src/OFRunLoop.m from [99717b7858] to [0a75a2f08b].

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
@public
#  ifdef OF_HAVE_BLOCKS
	OFSCTPSocketAsyncSendDataBlock _block;
#  endif
	OFData *_data;
	uint16_t _streamID;
	uint32_t _PPID;
	OFSCTPPacketFlags _flags;
}
@end
# endif
#endif

@implementation OFRunLoopState
- (instancetype)init







|







220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
@public
#  ifdef OF_HAVE_BLOCKS
	OFSCTPSocketAsyncSendDataBlock _block;
#  endif
	OFData *_data;
	uint16_t _streamID;
	uint32_t _PPID;
	OFSCTPMessageFlags _flags;
}
@end
# endif
#endif

@implementation OFRunLoopState
- (instancetype)init
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# ifdef OF_HAVE_SCTP
@implementation OFRunLoopSCTPReceiveQueueItem
- (bool)handleObject: (id)object
{
	size_t length;
	uint16_t streamID;
	uint32_t PPID;
	OFSCTPPacketFlags flags;
	id exception = nil;

	@try {
		length = [object receiveIntoBuffer: _buffer
					    length: _length
					  streamID: &streamID
					      PPID: &PPID







|







1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# ifdef OF_HAVE_SCTP
@implementation OFRunLoopSCTPReceiveQueueItem
- (bool)handleObject: (id)object
{
	size_t length;
	uint16_t streamID;
	uint32_t PPID;
	OFSCTPMessageFlags flags;
	id exception = nil;

	@try {
		length = [object receiveIntoBuffer: _buffer
					    length: _length
					  streamID: &streamID
					      PPID: &PPID
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
	QUEUE_ITEM
}

+ (void)of_addAsyncSendForSCTPSocket: (OFSCTPSocket *)sock
				data: (OFData *)data
			    streamID: (uint16_t)streamID
				PPID: (uint32_t)PPID
			       flags: (OFSCTPPacketFlags)flags
				mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			       block: (OFSCTPSocketAsyncSendDataBlock)block
# endif
			    delegate: (id <OFSCTPSocketDelegate>)delegate
{
	NEW_WRITE(OFRunLoopSCTPSendQueueItem, sock, mode)







|







1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
	QUEUE_ITEM
}

+ (void)of_addAsyncSendForSCTPSocket: (OFSCTPSocket *)sock
				data: (OFData *)data
			    streamID: (uint16_t)streamID
				PPID: (uint32_t)PPID
			       flags: (OFSCTPMessageFlags)flags
				mode: (OFRunLoopMode)mode
# ifdef OF_HAVE_BLOCKS
			       block: (OFSCTPSocketAsyncSendDataBlock)block
# endif
			    delegate: (id <OFSCTPSocketDelegate>)delegate
{
	NEW_WRITE(OFRunLoopSCTPSendQueueItem, sock, mode)

Modified src/OFSCTPSocket.h from [ec4a2962e7] to [2e3d49a6fe].

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

/** @file */

@class OFSCTPSocket;
@class OFString;

/**
 * @brief Flags for an SCTP packet.
 */
typedef enum {
	/** The packet is sent / received out of order. */
	OFSCTPPacketUnordered = 1
} OFSCTPPacketFlags;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief A block which is called when the socket connected.
 *
 * @param exception An exception which occurred while connecting the socket or
 *		    `nil` on success
 */
typedef void (^OFSCTPSocketAsyncConnectBlock)(id _Nullable exception);

/**
 * @brief A block which is called when a packet has been received.
 *
 * @param length The length of the packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for 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 (^OFSCTPSocketAsyncReceiveBlock)(size_t length, uint16_t streamID,
    uint32_t PPID, OFSCTPPacketFlags flags, id _Nullable exception);

/**
 * @brief A block which is called when a packet has been sent.
 *
 * @param exception An exception which occurred while reading or `nil` on
 *		    success
 * @return The data to repeat the send with or nil if it should not repeat
 */
typedef OFData *_Nullable (^OFSCTPSocketAsyncSendDataBlock)(
    id _Nullable exception);







|


|
|
|











|

|


|





|


|







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

/** @file */

@class OFSCTPSocket;
@class OFString;

/**
 * @brief Flags for an SCTP message.
 */
typedef enum {
	/** The message is sent / received out of order. */
	OFSCTPMessageUnordered = 1
} OFSCTPMessageFlags;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief A block which is called when the socket connected.
 *
 * @param exception An exception which occurred while connecting the socket or
 *		    `nil` on success
 */
typedef void (^OFSCTPSocketAsyncConnectBlock)(id _Nullable exception);

/**
 * @brief A block which is called when a message has been received.
 *
 * @param length The length of the message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @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 (^OFSCTPSocketAsyncReceiveBlock)(size_t length, uint16_t streamID,
    uint32_t PPID, OFSCTPMessageFlags flags, id _Nullable exception);

/**
 * @brief A block which is called when a message has been sent.
 *
 * @param exception An exception which occurred while reading or `nil` on
 *		    success
 * @return The data to repeat the send with or nil if it should not repeat
 */
typedef OFData *_Nullable (^OFSCTPSocketAsyncSendDataBlock)(
    id _Nullable exception);
87
88
89
90
91
92
93
94
95
96
97
98
99
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
149
150
151
152
153
154
155
156
157
158
159
160
161
 */
-     (void)socket: (OFSCTPSocket *)socket
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
	 exception: (nullable id)exception;

/**
 * @brief This method is called when a packet has been received.
 *
 * @param socket The sequenced packet socket which received a packet
 * @param buffer The buffer the packet has been written to
 * @param length The length of the packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @param exception An exception that occurred while receiving, or nil on
 *		    success
 * @return A bool whether the same block should be used for the next receive
 */
-	  (bool)socket: (OFSCTPSocket *)socket
  didReceiveIntoBuffer: (void *)buffer
		length: (size_t)length
	      streamID: (uint16_t)streamID
		  PPID: (uint32_t)PPID
		 flags: (OFSCTPPacketFlags)flags
	     exception: (nullable id)exception;

/**
 * @brief This method is called when a packet has been sent.
 *
 * @param socket The sequenced packet socket which sent a packet
 * @param data The data which was sent
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @param exception An exception that occurred while sending, or nil on success
 * @return The data to repeat the send with or nil if it should not repeat
 */
- (nullable OFData *)socket: (OFSCTPSocket *)socket
		didSendData: (OFData *)data
		   streamID: (uint16_t)streamID
		       PPID: (uint32_t)PPID
		      flags: (OFSCTPPacketFlags)flags
		  exception: (nullable id)exception;
@end

/**
 * @class OFSCTPSocket OFSCTPSocket.h ObjFW/OFSCTPSocket.h
 *
 * @brief A class which provides methods to create and use SCTP sockets in
 *	  one-to-one mode.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
 */
@interface OFSCTPSocket: OFSequencedPacketSocket
{
	OF_RESERVE_IVARS(OFSCTPSocket, 4)
}

/**
 * @brief Whether sending packets can be delayed. Setting this to NO sets
 *        SCTP_NODELAY on the socket.
 *
 * @throw OFGetOptionFailedException The option could not be retrieved
 * @throw OFSetOptionFailedException The option could not be set
 */
@property (nonatomic) bool canDelaySendingPackets;

/**
 * @brief The delegate for asynchronous operations on the socket.
 *
 * @note The delegate is retained for as long as asynchronous operations are
 *	 still ongoing.
 */







|

|
|
|


|









|



|

|



|







|


















|





|







87
88
89
90
91
92
93
94
95
96
97
98
99
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
149
150
151
152
153
154
155
156
157
158
159
160
161
 */
-     (void)socket: (OFSCTPSocket *)socket
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
	 exception: (nullable id)exception;

/**
 * @brief This method is called when a message has been received.
 *
 * @param socket The SCTP socket which received a message
 * @param buffer The buffer the message has been written to
 * @param length The length of the message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @param exception An exception that occurred while receiving, or nil on
 *		    success
 * @return A bool whether the same block should be used for the next receive
 */
-	  (bool)socket: (OFSCTPSocket *)socket
  didReceiveIntoBuffer: (void *)buffer
		length: (size_t)length
	      streamID: (uint16_t)streamID
		  PPID: (uint32_t)PPID
		 flags: (OFSCTPMessageFlags)flags
	     exception: (nullable id)exception;

/**
 * @brief This method is called when a message has been sent.
 *
 * @param socket The SCTP socket which sent a message
 * @param data The data which was sent
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @param exception An exception that occurred while sending, or nil on success
 * @return The data to repeat the send with or nil if it should not repeat
 */
- (nullable OFData *)socket: (OFSCTPSocket *)socket
		didSendData: (OFData *)data
		   streamID: (uint16_t)streamID
		       PPID: (uint32_t)PPID
		      flags: (OFSCTPMessageFlags)flags
		  exception: (nullable id)exception;
@end

/**
 * @class OFSCTPSocket OFSCTPSocket.h ObjFW/OFSCTPSocket.h
 *
 * @brief A class which provides methods to create and use SCTP sockets in
 *	  one-to-one mode.
 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
 */
@interface OFSCTPSocket: OFSequencedPacketSocket
{
	OF_RESERVE_IVARS(OFSCTPSocket, 4)
}

/**
 * @brief Whether sending messages can be delayed. Setting this to NO sets
 *        SCTP_NODELAY on the socket.
 *
 * @throw OFGetOptionFailedException The option could not be retrieved
 * @throw OFSetOptionFailedException The option could not be set
 */
@property (nonatomic) bool canDelaySendingMessages;

/**
 * @brief The delegate for asynchronous operations on the socket.
 *
 * @note The delegate is retained for as long as asynchronous operations are
 *	 still ongoing.
 */
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
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
356
357
358
359
360
361
362
363
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
 * @return The address the socket was bound to
 * @throw OFBindIPSocketFailedException Binding failed
 * @throw OFAlreadyOpenException The socket is already connected or bound
 */
- (OFSocketAddress)bindToHost: (OFString *)host port: (uint16_t)port;

/**
 * @brief Receives a packet for the specified stream and stores it into the
 *	  specified buffer.
 *
 * If the buffer is too small, the packet is truncated.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @return The length of the received packet
 * @throw OFReadFailedException Receiving failed
 * @throw OFNotOpenException The socket is not open
 */
- (size_t)receiveIntoBuffer: (void *)buffer
		     length: (size_t)length
		   streamID: (nullable uint16_t *)streamID
		       PPID: (nullable uint32_t *)PPID
		      flags: (nullable OFSCTPPacketFlags *)flags;

/**
 * @brief Asynchronously receives a packet with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the packet is truncated.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length;

/**
 * @brief Asynchronously receives a packet with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the packet is truncated.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      receive
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
			   runLoopMode: (OFRunLoopMode)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously receives a packet with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the packet is truncated.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @param block The block to call when the packet has been received. If the
 *		block returns true, it will be called again with the same
 *		buffer and maximum length when more packets have been received.
 *		If you want the next method in the queue to handle the packet
 *		received next, you need to return false from the method.
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
				 block: (OFSCTPSocketAsyncReceiveBlock)block;

/**
 * @brief Asynchronously receives a packet with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the packet is truncated.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      receive
 * @param block The block to call when the packet has been received. If the
 *		block returns true, it will be called again with the same
 *		buffer and maximum length when more packets have been received.
 *		If you want the next method in the queue to handle the packet
 *		received next, you need to return false from the method.
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
			   runLoopMode: (OFRunLoopMode)runLoopMode
				 block: (OFSCTPSocketAsyncReceiveBlock)block;
#endif

/**
 * @brief Sends the specified packet on the specified stream.
 *
 * @param buffer The buffer to send as a packet
 * @param length The length of the buffer
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @throw OFWriteFailedException Sending failed
 * @throw OFNotOpenException The socket is not open
 */
- (void)sendBuffer: (const void *)buffer
	    length: (size_t)length
	  streamID: (uint16_t)streamID
	      PPID: (uint32_t)PPID
	     flags: (OFSCTPPacketFlags)flags;

/**
 * @brief Asynchronously sends the specified packet on the specified stream.
 *
 * @param data The data to send as a packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags;

/**
 * @brief Asynchronously sends the specified packet on the specified stream.
 *
 * @param data The data to send as a packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      send
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously sends the specified packet on the specified stream.
 *
 * @param data The data to send as a packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @param block The block to call when the packet has been sent. It should



















 *		return the data for the next send with the same callback or nil
 *		if it should not repeat.
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
		block: (OFSCTPSocketAsyncSendDataBlock)block;

/**
 * @brief Asynchronously sends the specified packet on the specified stream.
 *
 * @param data The data to send as a packet
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the packet
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      send
 * @param block The block to call when the packet has been sent. It should
 *		return the data for the next send with the same callback or nil
 *		if it should not repeat.
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode
		block: (OFSCTPSocketAsyncSendDataBlock)block;
#endif
@end

OF_ASSUME_NONNULL_END







|


|

|



|
|







|


|


|

|






|


|

|










|


|

|

|

|
|







|


|

|



|

|
|









|

|



|







|


|

|


|




|


|

|


|






|




|

|


|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






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
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
356
357
358
359
360
361
362
363
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
 * @return The address the socket was bound to
 * @throw OFBindIPSocketFailedException Binding failed
 * @throw OFAlreadyOpenException The socket is already connected or bound
 */
- (OFSocketAddress)bindToHost: (OFString *)host port: (uint16_t)port;

/**
 * @brief Receives a message for the specified stream ID and stores it into the
 *	  specified buffer.
 *
 * If the buffer is too small, the message is truncated.
 *
 * @param buffer The buffer to write the message to
 * @param length The length of the buffer
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @return The length of the received message
 * @throw OFReadFailedException Receiving failed
 * @throw OFNotOpenException The socket is not open
 */
- (size_t)receiveIntoBuffer: (void *)buffer
		     length: (size_t)length
		   streamID: (nullable uint16_t *)streamID
		       PPID: (nullable uint32_t *)PPID
		      flags: (nullable OFSCTPMessageFlags *)flags;

/**
 * @brief Asynchronously receives a message with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the message is truncated.
 *
 * @param buffer The buffer to write the message to
 * @param length The length of the buffer
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length;

/**
 * @brief Asynchronously receives a message with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the message is truncated.
 *
 * @param buffer The buffer to write the message to
 * @param length The length of the buffer
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      receive
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
			   runLoopMode: (OFRunLoopMode)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously receives a message with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the message is truncated.
 *
 * @param buffer The buffer to write the message to
 * @param length The length of the buffer
 * @param block The block to call when the message has been received. If the
 *		block returns true, it will be called again with the same
 *		buffer and maximum length when more messages have been received.
 *		If you want the next method in the queue to handle the message
 *		received next, you need to return false from the method.
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
				 block: (OFSCTPSocketAsyncReceiveBlock)block;

/**
 * @brief Asynchronously receives a message with stream ID and PPID and stores
 *	  it into the specified buffer.
 *
 * If the buffer is too small, the message is truncated.
 *
 * @param buffer The buffer to write the message to
 * @param length The length of the buffer
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      receive
 * @param block The block to call when the message has been received. If the
 *		block returns true, it will be called again with the same
 *		buffer and maximum length when more messages have been received.
 *		If you want the next method in the queue to handle the message
 *		received next, you need to return false from the method.
 */
- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
			   runLoopMode: (OFRunLoopMode)runLoopMode
				 block: (OFSCTPSocketAsyncReceiveBlock)block;
#endif

/**
 * @brief Sends the specified message on the specified stream.
 *
 * @param buffer The buffer to send as a message
 * @param length The length of the buffer
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @throw OFWriteFailedException Sending failed
 * @throw OFNotOpenException The socket is not open
 */
- (void)sendBuffer: (const void *)buffer
	    length: (size_t)length
	  streamID: (uint16_t)streamID
	      PPID: (uint32_t)PPID
	     flags: (OFSCTPMessageFlags)flags;

/**
 * @brief Asynchronously sends the specified message on the specified stream.
 *
 * @param data The data to send as a message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags;

/**
 * @brief Asynchronously sends the specified message on the specified stream.
 *
 * @param data The data to send as a message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      send
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/**
 * @brief Asynchronously sends the specified message on the specified stream.
 *
 * @param data The data to send as a message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @param block The block to call when the message has been sent. It should
 *		return the data for the next send with the same callback or nil
 *		if it should not repeat.
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
		block: (OFSCTPSocketAsyncSendDataBlock)block;

/**
 * @brief Asynchronously sends the specified message on the specified stream.
 *
 * @param data The data to send as a message
 * @param streamID The stream ID for the message
 * @param PPID The Payload Protocol Identifier for the message
 * @param flags Flags for the message
 * @param runLoopMode The run loop mode in which to perform the asynchronous
 *		      send
 * @param block The block to call when the message has been sent. It should
 *		return the data for the next send with the same callback or nil
 *		if it should not repeat.
 */
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags



















	  runLoopMode: (OFRunLoopMode)runLoopMode
		block: (OFSCTPSocketAsyncSendDataBlock)block;
#endif
@end

OF_ASSUME_NONNULL_END

Modified src/OFSCTPSocket.m from [4b227c3bdd] to [908828903c].

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
				 flags: NULL];
}

- (size_t)receiveIntoBuffer: (void *)buffer
		     length: (size_t)length
		   streamID: (uint16_t *)streamID
		       PPID: (uint32_t *)PPID
		      flags: (OFSCTPPacketFlags *)flags
{
	ssize_t ret;
	struct iovec iov = {
		.iov_base = buffer,
		.iov_len = length
	};
	struct sctp_rcvinfo rcvinfo;







|







352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
				 flags: NULL];
}

- (size_t)receiveIntoBuffer: (void *)buffer
		     length: (size_t)length
		   streamID: (uint16_t *)streamID
		       PPID: (uint32_t *)PPID
		      flags: (OFSCTPMessageFlags *)flags
{
	ssize_t ret;
	struct iovec iov = {
		.iov_base = buffer,
		.iov_len = length
	};
	struct sctp_rcvinfo rcvinfo;
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

	if (flags != NULL) {
		*flags = 0;

		if (infotype == SCTP_RECVV_RCVINFO &&
		    rcvinfoSize >= (socklen_t)sizeof(rcvinfo) &&
		    rcvinfo.rcv_flags & SCTP_UNORDERED)
			*flags |= OFSCTPPacketUnordered;
	}

	return ret;
}

- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length







|







395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

	if (flags != NULL) {
		*flags = 0;

		if (infotype == SCTP_RECVV_RCVINFO &&
		    rcvinfoSize >= (socklen_t)sizeof(rcvinfo) &&
		    rcvinfo.rcv_flags & SCTP_UNORDERED)
			*flags |= OFSCTPMessageUnordered;
	}

	return ret;
}

- (void)asyncReceiveWithInfoIntoBuffer: (void *)buffer
				length: (size_t)length
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	[self sendBuffer: buffer length: length streamID: 0 PPID: 0 flags: 0];
}

- (void)sendBuffer: (const void *)buffer
	    length: (size_t)length
	  streamID: (uint16_t)streamID
	      PPID: (uint32_t)PPID
	     flags: (OFSCTPPacketFlags)flags
{
	ssize_t bytesWritten;
	struct iovec iov = {
		.iov_base = (void *)buffer,
		.iov_len = length
	};
	struct sctp_sndinfo sndinfo = {
		.snd_sid = streamID,
		.snd_ppid = PPID,
		.snd_flags =
		    ((flags & OFSCTPPacketUnordered) ? SCTP_UNORDERED : 0),
	};

	if (_socket == OFInvalidSocketHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];







|










|







458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	[self sendBuffer: buffer length: length streamID: 0 PPID: 0 flags: 0];
}

- (void)sendBuffer: (const void *)buffer
	    length: (size_t)length
	  streamID: (uint16_t)streamID
	      PPID: (uint32_t)PPID
	     flags: (OFSCTPMessageFlags)flags
{
	ssize_t bytesWritten;
	struct iovec iov = {
		.iov_base = (void *)buffer,
		.iov_len = length
	};
	struct sctp_sndinfo sndinfo = {
		.snd_sid = streamID,
		.snd_ppid = PPID,
		.snd_flags =
		    ((flags & OFSCTPMessageUnordered) ? SCTP_UNORDERED : 0),
	};

	if (_socket == OFInvalidSocketHandle)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (length > SSIZE_MAX)
		@throw [OFOutOfRangeException exception];
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
						      bytesWritten: bytesWritten
							     errNo: 0];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
{
	[self asyncSendData: data
		   streamID: streamID
		       PPID: PPID
		      flags: flags
		runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode
{
	[OFRunLoop of_addAsyncSendForSCTPSocket: self
					   data: data
				       streamID: streamID
					   PPID: PPID
					  flags: flags
					   mode: runLoopMode
# ifdef OF_HAVE_BLOCKS
					  block: NULL
# endif
				       delegate: _delegate];
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
		block: (OFSequencedPacketSocketAsyncSendDataBlock)block
{
	[self asyncSendData: data
		   streamID: streamID
		       PPID: PPID
		      flags: flags
		runLoopMode: OFDefaultRunLoopMode
		      block: block];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPPacketFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode
		block: (OFSequencedPacketSocketAsyncSendDataBlock)block
{
	[OFRunLoop of_addAsyncSendForSCTPSocket: self
					   data: data
				       streamID: streamID
					   PPID: PPID
					  flags: flags
					   mode: runLoopMode
					  block: block
				       delegate: nil];
}
#endif

- (void)setCanDelaySendingPackets: (bool)canDelaySendingPackets
{
	int v = !canDelaySendingPackets;

	if (setsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,
	    (char *)&v, (socklen_t)sizeof(v)) != 0)
		@throw [OFSetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];
}

- (bool)canDelaySendingPackets
{
	int v;
	socklen_t len = sizeof(v);

	if (getsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,
	    (char *)&v, &len) != 0 || len != sizeof(v))
		@throw [OFGetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];

	return !v;
}
@end







|











|


















|
|












|

|












|

|








|













496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
						      bytesWritten: bytesWritten
							     errNo: 0];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
{
	[self asyncSendData: data
		   streamID: streamID
		       PPID: PPID
		      flags: flags
		runLoopMode: OFDefaultRunLoopMode];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode
{
	[OFRunLoop of_addAsyncSendForSCTPSocket: self
					   data: data
				       streamID: streamID
					   PPID: PPID
					  flags: flags
					   mode: runLoopMode
# ifdef OF_HAVE_BLOCKS
					  block: NULL
# endif
				       delegate: _delegate];
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
		block: (OFSCTPSocketAsyncSendDataBlock)block
{
	[self asyncSendData: data
		   streamID: streamID
		       PPID: PPID
		      flags: flags
		runLoopMode: OFDefaultRunLoopMode
		      block: block];
}

- (void)asyncSendData: (OFData *)data
	     streamID: (uint16_t)streamID
		 PPID: (uint32_t)PPID
		flags: (OFSCTPMessageFlags)flags
	  runLoopMode: (OFRunLoopMode)runLoopMode
		block: (OFSCTPSocketAsyncSendDataBlock)block
{
	[OFRunLoop of_addAsyncSendForSCTPSocket: self
					   data: data
				       streamID: streamID
					   PPID: PPID
					  flags: flags
					   mode: runLoopMode
					  block: block
				       delegate: nil];
}
#endif

- (void)setCanDelaySendingMessages: (bool)canDelaySendingMessages
{
	int v = !canDelaySendingMessages;

	if (setsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,
	    (char *)&v, (socklen_t)sizeof(v)) != 0)
		@throw [OFSetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];
}

- (bool)canDelaySendingMessages
{
	int v;
	socklen_t len = sizeof(v);

	if (getsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,
	    (char *)&v, &len) != 0 || len != sizeof(v))
		@throw [OFGetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];

	return !v;
}
@end

Modified tests/OFSCTPSocketTests.m from [edc2aaa24d] to [4d2bc6e16e].

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
- (void)testSCTPSocket
{
	OFSCTPSocket *server, *client, *accepted;
	OFSocketAddress address;
	char buffer[6];
	uint16_t streamID;
	uint32_t PPID;
	OFSCTPPacketFlags flags;

	server = [OFSCTPSocket socket];
	client = [OFSCTPSocket socket];

	@try {
		address = [server bindToHost: @"127.0.0.1" port: 0];
	} @catch (OFBindSocketFailedException *e) {







|







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
- (void)testSCTPSocket
{
	OFSCTPSocket *server, *client, *accepted;
	OFSocketAddress address;
	char buffer[6];
	uint16_t streamID;
	uint32_t PPID;
	OFSCTPMessageFlags flags;

	server = [OFSCTPSocket socket];
	client = [OFSCTPSocket socket];

	@try {
		address = [server bindToHost: @"127.0.0.1" port: 0];
	} @catch (OFBindSocketFailedException *e) {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	OTAssertEqualObjects(OFSocketAddressString(accepted.remoteAddress),
	    @"127.0.0.1");

	[client sendBuffer: "Hello!"
		    length: 6
		  streamID: 1
		      PPID: 1234
		     flags: OFSCTPPacketUnordered];

	[accepted receiveIntoBuffer: buffer
			     length: 6
			   streamID: &streamID
			       PPID: &PPID
			      flags: &flags];
	OTAssertEqual(memcmp(buffer, "Hello!", 6), 0);
	OTAssertEqual(streamID, 1);
	OTAssertEqual(PPID, 1234);
	OTAssertTrue(flags & OFSCTPPacketUnordered);
}
@end







|









|


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	OTAssertEqualObjects(OFSocketAddressString(accepted.remoteAddress),
	    @"127.0.0.1");

	[client sendBuffer: "Hello!"
		    length: 6
		  streamID: 1
		      PPID: 1234
		     flags: OFSCTPMessageUnordered];

	[accepted receiveIntoBuffer: buffer
			     length: 6
			   streamID: &streamID
			       PPID: &PPID
			      flags: &flags];
	OTAssertEqual(memcmp(buffer, "Hello!", 6), 0);
	OTAssertEqual(streamID, 1);
	OTAssertEqual(PPID, 1234);
	OTAssertTrue(flags & OFSCTPMessageUnordered);
}
@end