ObjFW
OFUDPSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFObject.h"
18 #import "OFKernelEventObserver.h"
19 
20 #import "socket.h"
21 
22 OF_ASSUME_NONNULL_BEGIN
23 
26 @class OFUDPSocket;
27 @class OFException;
28 
34 typedef struct {
35  struct sockaddr_storage address;
36  socklen_t length;
38 
39 #ifdef OF_HAVE_BLOCKS
40 
51  uint16_t port, of_udp_socket_address_t address,
52  OFException *_Nullable exception);
53 
66  void *buffer, size_t length, of_udp_socket_address_t sender,
67  OFException *_Nullable exception);
68 #endif
69 
92 {
93  of_socket_t _socket;
94 #ifdef OF_WII
95  uint16_t _port;
96 #endif
97 }
98 
104 + (instancetype)socket;
105 
115 + (void)resolveAddressForHost: (OFString *)host
116  port: (uint16_t)port
117  address: (of_udp_socket_address_t *)address;
118 
119 #ifdef OF_HAVE_THREADS
120 
132 + (void)asyncResolveAddressForHost: (OFString *)host
133  port: (uint16_t)port
134  target: (id)target
135  selector: (SEL)selector;
136 
137 # ifdef OF_HAVE_BLOCKS
138 
146 + (void)asyncResolveAddressForHost: (OFString *)host
147  port: (uint16_t)port
149 # endif
150 #endif
151 
161 + (void)getHost: (OFString *__autoreleasing _Nonnull *_Nullable)host
162  andPort: (nullable uint16_t *)port
163  forAddress: (of_udp_socket_address_t *)address;
164 
174 - (uint16_t)bindToHost: (OFString *)host
175  port: (uint16_t)port;
176 
188 - (size_t)receiveIntoBuffer: (void *)buffer
189  length: (size_t)length
190  sender: (of_udp_socket_address_t *)sender;
191 
210 - (void)asyncReceiveIntoBuffer: (void *)buffer
211  length: (size_t)length
212  target: (id)target
213  selector: (SEL)selector;
214 
215 #ifdef OF_HAVE_BLOCKS
216 
231 - (void)asyncReceiveIntoBuffer: (void *)buffer
232  length: (size_t)length
234 #endif
235 
244 - (void)sendBuffer: (const void *)buffer
245  length: (size_t)length
246  receiver: (const of_udp_socket_address_t *)receiver;
247 
251 - (void)cancelAsyncRequests;
252 
257 - (void)close;
258 @end
259 
260 #ifdef __cplusplus
261 extern "C" {
262 #endif
263 
271  of_udp_socket_address_t *address2);
272 
279 extern uint32_t of_udp_socket_address_hash(of_udp_socket_address_t *address);
280 #ifdef __cplusplus
281 }
282 #endif
283 
284 OF_ASSUME_NONNULL_END
bool of_udp_socket_address_equal(of_udp_socket_address_t *address1, of_udp_socket_address_t *address2)
Compares two of_udp_socket_address_t for equality.
Definition: OFUDPSocket.m:177
uint32_t of_udp_socket_address_hash(of_udp_socket_address_t *address)
Returns the hash for the specified of_udp_socket_address_t.
Definition: OFUDPSocket.m:234
A class for handling strings.
Definition: OFString.h:114
A class which provides methods to create and use UDP sockets.
Definition: OFUDPSocket.h:90
void(^ of_udp_socket_async_resolve_block_t)(OFString *host, uint16_t port, of_udp_socket_address_t address, OFException *_Nullable exception)
A block which is called when the host / port pair for the UDP socket has been resolved.
Definition: OFUDPSocket.h:50
The base class for all exceptions in ObjFW.
Definition: OFException.h:143
A protocol for the creation of copies.
Definition: OFObject.h:914
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
bool(^ of_udp_socket_async_receive_block_t)(OFUDPSocket *socket, void *buffer, size_t length, of_udp_socket_address_t sender, OFException *_Nullable exception)
A block which is called when a packet has been received.
Definition: OFUDPSocket.h:65
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
A struct which represents a host / port pair for a UDP socket.
Definition: OFUDPSocket.h:34
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...