ObjFW
Loading...
Searching...
No Matches
OFDatagramSocket.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#import "OFObject.h"
21#import "OFKernelEventObserver.h"
22#import "OFRunLoop.h"
23#import "OFSocket.h"
24
25OF_ASSUME_NONNULL_BEGIN
26
29@class OFData;
30@class OFDatagramSocket;
31
32#ifdef OF_HAVE_BLOCKS
44typedef bool (^OFDatagramSocketAsyncReceiveBlock)(size_t length,
45 const OFSocketAddress *_Nonnull sender, id _Nullable exception)
46 OF_DEPRECATED(ObjFW, 1, 2,
47 "Use OFDatagramSocketPacketReceivedHandler instead");
48
61 void *buffer, size_t length, const OFSocketAddress *_Nonnull sender,
62 id _Nullable exception);
63
74 id _Nullable exception)
75 OF_DEPRECATED(ObjFW, 1, 2,
76 "Use OFDatagramSocketDataSentHandler instead");
77
89 OFDatagramSocket *socket, OFData *data,
90 const OFSocketAddress *_Nonnull receiver, id _Nullable exception);
91#endif
92
98@protocol OFDatagramSocketDelegate <OFObject>
99@optional
111- (bool)socket: (OFDatagramSocket *)socket
112 didReceiveIntoBuffer: (void *)buffer
113 length: (size_t)length
114 sender: (const OFSocketAddress *_Nonnull)sender
115 exception: (nullable id)exception;
116
126- (nullable OFData *)socket: (OFDatagramSocket *)socket
127 didSendData: (OFData *)data
128 receiver: (const OFSocketAddress *_Nonnull)receiver
129 exception: (nullable id)exception;
130@end
131
146{
147 OFSocketHandle _socket;
148#ifdef OF_AMIGAOS
149 LONG _socketID;
150 int _family; /* unused, reserved for ABI stability */
151#endif
152 bool _canBlock;
153#ifdef OF_WII
154 bool _canSendToBroadcastAddresses;
155#endif
156 id <OFDatagramSocketDelegate> _Nullable _delegate;
157 OF_RESERVE_IVARS(OFDatagramSocket, 4)
158}
159
168@property (nonatomic) bool canBlock;
176@property (nonatomic) bool canSendToBroadcastAddresses;
184@property OF_NULLABLE_PROPERTY (assign, nonatomic)
185 id <OFDatagramSocketDelegate> delegate;
192+ (instancetype)socket;
193
207- (size_t)receiveIntoBuffer: (void *)buffer
208 length: (size_t)length
209 sender: (nullable OFSocketAddress *)sender;
210
220- (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length;
221
233- (void)asyncReceiveIntoBuffer: (void *)buffer
234 length: (size_t)length
235 runLoopMode: (OFRunLoopMode)runLoopMode;
236
237#ifdef OF_HAVE_BLOCKS
255- (void)asyncReceiveIntoBuffer: (void *)buffer
256 length: (size_t)length
258 OF_DEPRECATED(ObjFW, 1, 2,
259 "Use -[asyncReceiveIntoBuffer:length:handler:] instead");
260
276- (void)asyncReceiveIntoBuffer: (void *)buffer
277 length: (size_t)length
279
300- (void)asyncReceiveIntoBuffer: (void *)buffer
301 length: (size_t)length
302 runLoopMode: (OFRunLoopMode)runLoopMode
304 OF_DEPRECATED(ObjFW, 1, 2,
305 "Use -[asyncReceiveIntoBuffer:length:runLoopMode:handler:] instead");
306
324- (void)asyncReceiveIntoBuffer: (void *)buffer
325 length: (size_t)length
326 runLoopMode: (OFRunLoopMode)runLoopMode
328#endif
329
340- (void)sendBuffer: (const void *)buffer
341 length: (size_t)length
342 receiver: (const OFSocketAddress *)receiver;
343
351- (void)asyncSendData: (OFData *)data
352 receiver: (const OFSocketAddress *)receiver;
353
363- (void)asyncSendData: (OFData *)data
364 receiver: (const OFSocketAddress *)receiver
365 runLoopMode: (OFRunLoopMode)runLoopMode;
366
367#ifdef OF_HAVE_BLOCKS
380- (void)asyncSendData: (OFData *)data
381 receiver: (const OFSocketAddress *)receiver
383 OF_DEPRECATED(ObjFW, 1, 2,
384 "Use -[asyncSendData:receiver:handler:] instead");
385
396- (void)asyncSendData: (OFData *)data
397 receiver: (const OFSocketAddress *)receiver
398 handler: (OFDatagramSocketDataSentHandler)handler;
399
414- (void)asyncSendData: (OFData *)data
415 receiver: (const OFSocketAddress *)receiver
416 runLoopMode: (OFRunLoopMode)runLoopMode
418 OF_DEPRECATED(ObjFW, 1, 2,
419 "Use -[asyncSendData:receiver:runLoopMode:handler:] instead");
420
433- (void)asyncSendData: (OFData *)data
434 receiver: (const OFSocketAddress *)receiver
435 runLoopMode: (OFRunLoopMode)runLoopMode
436 handler: (OFDatagramSocketDataSentHandler)handler;
437#endif
438
449- (void)releaseSocketFromCurrentThread;
450
461- (void)obtainSocketForCurrentThread;
462
466- (void)cancelAsyncRequests;
467
474- (void)close;
475@end
476
477OF_ASSUME_NONNULL_END
bool(^ OFDatagramSocketPacketReceivedHandler)(OFDatagramSocket *socket, void *buffer, size_t length, const OFSocketAddress *sender, id exception)
A handler which is called when a packet has been received.
Definition OFDatagramSocket.h:60
OFData *(^ OFDatagramSocketAsyncSendDataBlock)(id exception)
A block which is called when a packet has been sent.
Definition OFDatagramSocket.h:73
bool(^ OFDatagramSocketAsyncReceiveBlock)(size_t length, const OFSocketAddress *sender, id exception)
A block which is called when a packet has been received.
Definition OFDatagramSocket.h:44
OFData *(^ OFDatagramSocketDataSentHandler)(OFDatagramSocket *socket, OFData *data, const OFSocketAddress *receiver, id exception)
A handler which is called when a packet has been sent.
Definition OFDatagramSocket.h:88
A class for storing constant strings using the @"" literal.
Definition OFConstantString.h:42
A class for storing arbitrary data in an array.
Definition OFData.h:46
A base class for datagram sockets.
Definition OFDatagramSocket.h:147
The root class for all other classes inside ObjFW.
Definition OFObject.h:692
A protocol for the creation of copies.
Definition OFObject.h:1350
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...
Definition OFKernelEventObserver.h:83
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
Definition OFKernelEventObserver.h:97
A struct which represents a host / port pair for a socket.
Definition OFSocket.h:189