ObjFW  Diff

Differences From Artifact [8a2340b61c]:

To Artifact [d3b63ac877]:


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
56
57
58
59
60
61
62
63
64
65
66
67

68
69
70
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
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
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
56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
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
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







-
+





-
+










-
+










-
+












-
+







-
+














-
+











-
+













-
+







#import "OFKernelEventObserver.h"
#import "OFRunLoop.h"

#import "socket.h"

OF_ASSUME_NONNULL_BEGIN

/*! @file */
/** @file */

@class OFData;
@class OFSequencedPacketSocket;

#ifdef OF_HAVE_BLOCKS
/*!
/**
 * @brief A block which is called when a packet has been received.
 *
 * @param length The length 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_sequenced_packet_socket_async_receive_block_t)(size_t length,
    id _Nullable exception);

/*!
/**
 * @brief A block which is called when a packet has been sent.
 *
 * @param data The data which was 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 (^of_sequenced_packet_socket_async_send_data_block_t)(
    OFData *_Nonnull data, id _Nullable exception);

/*!
/**
 * @brief A block which is called when the socket accepted a connection.
 *
 * @param acceptedSocket The socket which has been accepted
 * @param exception An exception which occurred while accepting the socket or
 *		    `nil` on success
 * @return A bool whether the same block should be used for the next incoming
 *	   connection
 */
typedef bool (^of_sequenced_packet_socket_async_accept_block_t)(
    OFSequencedPacketSocket *acceptedSocket, id _Nullable exception);
#endif

/*!
/**
 * @protocol OFSequencedPacketSocketDelegate OFSequencedPacketSocket.h \
 *	     ObjFW/OFSequencedPacketSocket.h
 *
 * @brief A delegate for OFSequencedPacketSocket.
 */
@protocol OFSequencedPacketSocketDelegate <OFObject>
@optional
/*!
/**
 * @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 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: (OFSequencedPacketSocket *)socket
  didReceiveIntoBuffer: (void *)buffer
		length: (size_t)length
	     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 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: (OFSequencedPacketSocket *)socket
		didSendData: (OFData *)data
		  exception: (nullable id)exception;

/*!
/**
 * @brief A method which is called when a socket accepted a connection.
 *
 * @param socket The socket which accepted the connection
 * @param acceptedSocket The socket which has been accepted
 * @param exception An exception that occurred while accepting, or nil on
 *		    success
 * @return A bool whether to accept the next incoming connection
 */
-    (bool)socket: (OFSequencedPacketSocket *)socket
  didAcceptSocket: (OFSequencedPacketSocket *)acceptedSocket
	exception: (nullable id)exception;
@end

/*!
/**
 * @class OFSequencedPacketSocket OFSequencedPacketSocket.h \
 *	  ObjFW/OFSequencedPacketSocket.h
 *
 * @brief A base class for sequenced packet sockets.
 *
 * @warning Even though the OFCopying protocol is implemented, it does *not*
 *	    return an independent copy of the socket, but instead retains it.
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
162
163
164
165
166
167

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
228
229
230
231
232
233

234
235
236
237
238
239
240
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
162
163
164
165
166

167
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
228
229
230
231
232

233
234
235
236
237
238
239
240







-
+






-
+




-
+






-
+








-
+






-
+











-
+











-
+














-
+



















-
+







	of_socket_t _socket;
	bool _canBlock, _listening;
	of_socket_address_t _remoteAddress;
	id _Nullable _delegate;
	OF_RESERVE_IVARS(OFSequencedPacketSocket, 4)
}

/*!
/**
 * @brief Whether the socket can block.
 *
 * By default, a socket can block.
 */
@property (nonatomic) bool canBlock;

/*!
/**
 * @brief Whether the socket is a listening socket.
 */
@property (readonly, nonatomic, getter=isListening) bool listening;

/*!
/**
 * @brief The remote address.
 *
 * @note This only works for accepted sockets!
 */
@property (readonly, nonatomic) const of_socket_address_t *remoteAddress;

/*!
/**
 * @brief The delegate for asynchronous operations on the socket.
 *
 * @note The delegate is retained for as long as asynchronous operations are
 *	 still ongoing.
 */
@property OF_NULLABLE_PROPERTY (assign, nonatomic)
    id <OFSequencedPacketSocketDelegate> delegate;

/*!
/**
 * @brief Returns a new, autoreleased OFSequencedPacketSocket.
 *
 * @return A new, autoreleased OFSequencedPacketSocket
 */
+ (instancetype)socket;

/*!
/**
 * @brief Receives a packet and stores it into the specified buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 * @return The length of the received packet
 */
- (size_t)receiveIntoBuffer: (void *)buffer
		     length: (size_t)length;

/*!
/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
 */
- (void)asyncReceiveIntoBuffer: (void *)buffer
			length: (size_t)length;

/*!
/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @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 async receive
 */
- (void)asyncReceiveIntoBuffer: (void *)buffer
			length: (size_t)length
		   runLoopMode: (of_run_loop_mode_t)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/*!
/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @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)
    asyncReceiveIntoBuffer: (void *)buffer
		    length: (size_t)length
		     block: (of_sequenced_packet_socket_async_receive_block_t)
				block;

/*!
/**
 * @brief Asynchronously receives a packet and stores it into the specified
 *	  buffer.
 *
 * If the buffer is too small, the receive operation fails.
 *
 * @param buffer The buffer to write the packet to
 * @param length The length of the buffer
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
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







-
+








-
+






-
+









-
+











-
+














-
+






-
+




-
+






-
+




-
+







-
+









-
+











-
+




-
+







    asyncReceiveIntoBuffer: (void *)buffer
		    length: (size_t)length
	       runLoopMode: (of_run_loop_mode_t)runLoopMode
		     block: (of_sequenced_packet_socket_async_receive_block_t)
				block;
#endif

/*!
/**
 * @brief Sends the specified packet.
 *
 * @param buffer The buffer to send as a packet
 * @param length The length of the buffer
 */
- (void)sendBuffer: (const void *)buffer
	    length: (size_t)length;

/*!
/**
 * @brief Asynchronously sends the specified packet.
 *
 * @param data The data to send as a packet
 */
- (void)asyncSendData: (OFData *)data;

/*!
/**
 * @brief Asynchronously sends the specified packet.
 *
 * @param data The data to send as a packet
 * @param runLoopMode The run loop mode in which to perform the async send
 */
- (void)asyncSendData: (OFData *)data
	  runLoopMode: (of_run_loop_mode_t)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/*!
/**
 * @brief Asynchronously sends the specified packet.
 *
 * @param data The data to send as a 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
		block: (of_sequenced_packet_socket_async_send_data_block_t)
			   block;

/*!
/**
 * @brief Asynchronously sends the specified packet.
 *
 * @param data The data to send as a packet
 * @param runLoopMode The run loop mode in which to perform the async 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
	  runLoopMode: (of_run_loop_mode_t)runLoopMode
		block: (of_sequenced_packet_socket_async_send_data_block_t)
			   block;
#endif

/*!
/**
 * @brief Listen on the socket.
 *
 * @param backlog Maximum length for the queue of pending connections.
 */
- (void)listenWithBacklog: (int)backlog;

/*!
/**
 * @brief Listen on the socket.
 */
- (void)listen;

/*!
/**
 * @brief Accept an incoming connection.
 *
 * @return An autoreleased sequenced packet socket for the accepted connection.
 */
- (instancetype)accept;

/*!
/**
 * @brief Asynchronously accept an incoming connection.
 */
- (void)asyncAccept;

/*!
/**
 * @brief Asynchronously accept an incoming connection.
 *
 * @param runLoopMode The run loop mode in which to perform the async accept
 */
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;

#ifdef OF_HAVE_BLOCKS
/*!
/**
 * @brief Asynchronously accept an incoming connection.
 *
 * @param block The block to execute when a new connection has been accepted.
 *		Returns whether the next incoming connection should be accepted
 *		by the specified block as well.
 */
- (void)asyncAcceptWithBlock:
    (of_sequenced_packet_socket_async_accept_block_t)block;

/*!
/**
 * @brief Asynchronously accept an incoming connection.
 *
 * @param runLoopMode The run loop mode in which to perform the async accept
 * @param block The block to execute when a new connection has been accepted.
 *		Returns whether the next incoming connection should be accepted
 *		by the specified block as well.
 */
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
    block: (of_sequenced_packet_socket_async_accept_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

OF_ASSUME_NONNULL_END