ObjFW
OFUDPSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
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 *OF_NULLABLE exception);
53 
66  void *buffer, size_t length, of_udp_socket_address_t sender,
67  OFException *OF_NULLABLE exception);
68 #endif
69 
91 {
92  of_socket_t _socket;
93 }
94 
100 + (instancetype)socket;
101 
111 + (void)resolveAddressForHost: (OFString*)host
112  port: (uint16_t)port
113  address: (of_udp_socket_address_t*)address;
114 
115 #ifdef OF_HAVE_THREADS
116 
128 + (void)asyncResolveAddressForHost: (OFString*)host
129  port: (uint16_t)port
130  target: (id)target
131  selector: (SEL)selector;
132 
133 # ifdef OF_HAVE_BLOCKS
134 
142 + (void)asyncResolveAddressForHost: (OFString*)host
143  port: (uint16_t)port
145 # endif
146 #endif
147 
157 + (void)getHost: (OFString *__autoreleasing OF_NONNULL *OF_NULLABLE)host
158  andPort: (uint16_t *OF_NULLABLE)port
159  forAddress: (of_udp_socket_address_t*)address;
160 
170 - (uint16_t)bindToHost: (OFString*)host
171  port: (uint16_t)port;
172 
184 - (size_t)receiveIntoBuffer: (void*)buffer
185  length: (size_t)length
186  sender: (of_udp_socket_address_t*)sender;
187 
206 - (void)asyncReceiveIntoBuffer: (void*)buffer
207  length: (size_t)length
208  target: (id)target
209  selector: (SEL)selector;
210 
211 #ifdef OF_HAVE_BLOCKS
212 
227 - (void)asyncReceiveIntoBuffer: (void*)buffer
228  length: (size_t)length
230 #endif
231 
240 - (void)sendBuffer: (const void*)buffer
241  length: (size_t)length
242  receiver: (of_udp_socket_address_t*)receiver;
243 
253 - (void)cancelAsyncRequests;
254 
259 - (void)close;
260 @end
261 
262 #ifdef __cplusplus
263 extern "C" {
264 #endif
265 
273  of_udp_socket_address_t *address2);
274 
281 extern uint32_t of_udp_socket_address_hash(of_udp_socket_address_t *address);
282 #ifdef __cplusplus
283 }
284 #endif
285 
286 OF_ASSUME_NONNULL_END
A class which provides functions to create and use UDP sockets.
Definition: OFUDPSocket.h:89
This protocol is implemented by classes which can be observed for readiness for reading by OFKernelEv...
Definition: OFKernelEventObserver.h:73
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
A class for handling strings.
Definition: OFString.h:91
The base class for all exceptions in ObjFW.
Definition: OFException.h:144
This protocol is implemented by classes which can be observed for readiness for writing by OFKernelEv...
Definition: OFKernelEventObserver.h:91
A protocol for the creation of copies.
Definition: OFObject.h:896
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:231
bool(^ of_udp_socket_async_receive_block_t)(OFUDPSocket *socket, void *buffer, size_t length, of_udp_socket_address_t sender, OFException *OF_NULLABLE exception)
A block which is called when a packet has been received.
Definition: OFUDPSocket.h:65
void(^ of_udp_socket_async_resolve_block_t)(OFString *host, uint16_t port, of_udp_socket_address_t address, OFException *OF_NULLABLE exception)
A block which is called when the host / port pair for the UDP socket has been resolved.
Definition: OFUDPSocket.h:50
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:179
A struct which represents a host / port pair for a UDP socket.
Definition: OFUDPSocket.h:34