ObjFW
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 
25 OF_ASSUME_NONNULL_BEGIN
26 
29 @class OFData;
30 @class OFDatagramSocket;
31 
32 #ifdef OF_HAVE_BLOCKS
42 typedef bool (^OFDatagramSocketAsyncReceiveBlock)(size_t length,
43  const OFSocketAddress *_Nonnull sender, id _Nullable exception);
44 
53  id _Nullable exception);
54 #endif
55 
63 @optional
75 - (bool)socket: (OFDatagramSocket *)socket
76  didReceiveIntoBuffer: (void *)buffer
77  length: (size_t)length
78  sender: (const OFSocketAddress *_Nonnull)sender
79  exception: (nullable id)exception;
80 
90 - (nullable OFData *)socket: (OFDatagramSocket *)socket
91  didSendData: (OFData *)data
92  receiver: (const OFSocketAddress *_Nonnull)receiver
93  exception: (nullable id)exception;
94 @end
95 
110 {
111  OFSocketHandle _socket;
112 #ifdef OF_AMIGAOS
113  LONG _socketID;
114  int _family; /* unused, reserved for ABI stability */
115 #endif
116  bool _canBlock;
117 #ifdef OF_WII
118  bool _canSendToBroadcastAddresses;
119 #endif
120  id <OFDatagramSocketDelegate> _Nullable _delegate;
121  OF_RESERVE_IVARS(OFDatagramSocket, 4)
122 }
123 
132 @property (nonatomic) bool canBlock;
133 
140 @property (nonatomic) bool canSendToBroadcastAddresses;
141 
148 @property OF_NULLABLE_PROPERTY (assign, nonatomic)
149  id <OFDatagramSocketDelegate> delegate;
150 
156 + (instancetype)socket;
157 
171 - (size_t)receiveIntoBuffer: (void *)buffer
172  length: (size_t)length
173  sender: (nullable OFSocketAddress *)sender;
174 
184 - (void)asyncReceiveIntoBuffer: (void *)buffer length: (size_t)length;
185 
197 - (void)asyncReceiveIntoBuffer: (void *)buffer
198  length: (size_t)length
199  runLoopMode: (OFRunLoopMode)runLoopMode;
200 
201 #ifdef OF_HAVE_BLOCKS
217 - (void)asyncReceiveIntoBuffer: (void *)buffer
218  length: (size_t)length
219  block: (OFDatagramSocketAsyncReceiveBlock)block;
220 
238 - (void)asyncReceiveIntoBuffer: (void *)buffer
239  length: (size_t)length
240  runLoopMode: (OFRunLoopMode)runLoopMode
241  block: (OFDatagramSocketAsyncReceiveBlock)block;
242 #endif
243 
254 - (void)sendBuffer: (const void *)buffer
255  length: (size_t)length
256  receiver: (const OFSocketAddress *)receiver;
257 
265 - (void)asyncSendData: (OFData *)data
266  receiver: (const OFSocketAddress *)receiver;
267 
277 - (void)asyncSendData: (OFData *)data
278  receiver: (const OFSocketAddress *)receiver
279  runLoopMode: (OFRunLoopMode)runLoopMode;
280 
281 #ifdef OF_HAVE_BLOCKS
292 - (void)asyncSendData: (OFData *)data
293  receiver: (const OFSocketAddress *)receiver
295 
308 - (void)asyncSendData: (OFData *)data
309  receiver: (const OFSocketAddress *)receiver
310  runLoopMode: (OFRunLoopMode)runLoopMode
312 #endif
313 
324 - (void)releaseSocketFromCurrentThread;
325 
336 - (void)obtainSocketForCurrentThread;
337 
341 - (void)cancelAsyncRequests;
342 
349 - (void)close;
350 @end
351 
352 OF_ASSUME_NONNULL_END
bool(^ OFDatagramSocketAsyncReceiveBlock)(size_t length, const OFSocketAddress *sender, id exception)
A block which is called when a packet has been received.
Definition: OFDatagramSocket.h:42
OFData *(^ OFDatagramSocketAsyncSendDataBlock)(id exception)
A block which is called when a packet has been sent.
Definition: OFDatagramSocket.h:52
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:111
The root class for all other classes inside ObjFW.
Definition: OFObject.h:692
A protocol for the creation of copies.
Definition: OFObject.h:1350
A delegate for OFDatagramSocket.
Definition: OFDatagramSocket.h:62
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...
Definition: OFKernelEventObserver.h:88
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
Definition: OFKernelEventObserver.h:103
A struct which represents a host / port pair for a socket.
Definition: OFSocket.h:186