ObjFW
Instance Methods | Class Methods | List of all members
OFUDPSocket Class Reference

A class which provides functions to create and use UDP sockets. More...

#import <ObjFW/OFUDPSocket.h>

Inheritance diagram for OFUDPSocket:
OFObject <OFCopying> <OFReadyForReadingObserving> <OFReadyForWritingObserving> <OFObject> <OFObject> <OFObject>

Instance Methods

(uint16_t) - bindToHost:port:
 Binds the socket to the specified host and port. More...
 
(size_t) - receiveIntoBuffer:length:sender:
 Receives a datagram and stores it into the specified buffer. More...
 
(void) - asyncReceiveIntoBuffer:length:target:selector:
 Asynchronously receives a datagram and stores it into the specified buffer. More...
 
(void) - asyncReceiveIntoBuffer:length:block:
 Asynchronously receives a datagram and stores it into the specified buffer. More...
 
(void) - sendBuffer:length:receiver:
 Sends the specified datagram to the specified address. More...
 
(void) - cancelAsyncRequests
 Cancels all pending asyncronous requests on the socket. More...
 
(void) - close
 Closes the socket so that it can neither receive nor send any more datagrams.
 
- Instance Methods inherited from OFObject
(id) - init
 Initializes an already allocated object. More...
 
(OFString *) - className
 Returns the name of the object's class. More...
 
(OFString *) - description
 Returns a description for the object. More...
 
(void *) - allocMemoryWithSize:
 Allocates memory and stores it in the object's memory pool. More...
 
(void *) - allocMemoryWithSize:count:
 Allocates memory for the specified number of items and stores it in the object's memory pool. More...
 
(nullable void *) - resizeMemory:size:
 Resizes memory in the object's memory pool to the specified size. More...
 
(nullable void *) - resizeMemory:size:count:
 Resizes memory in the object's memory pool to the specific number of items of the specified size. More...
 
(void) - freeMemory:
 Frees allocated memory and removes it from the object's memory pool. More...
 
(void) - dealloc
 Deallocates the object. More...
 
(void) - performSelector:afterDelay:
 Performs the specified selector after the specified delay. More...
 
(void) - performSelector:withObject:afterDelay:
 Performs the specified selector with the specified object after the specified delay. More...
 
(void) - performSelector:withObject:withObject:afterDelay:
 Performs the specified selector with the specified objects after the specified delay. More...
 
(void) - performSelector:onThread:waitUntilDone:
 Performs the specified selector on the specified thread. More...
 
(void) - performSelector:onThread:withObject:waitUntilDone:
 Performs the specified selector on the specified thread with the specified object. More...
 
(void) - performSelector:onThread:withObject:withObject:waitUntilDone:
 Performs the specified selector on the specified thread with the specified objects. More...
 
(void) - performSelectorOnMainThread:waitUntilDone:
 Performs the specified selector on the main thread. More...
 
(void) - performSelectorOnMainThread:withObject:waitUntilDone:
 Performs the specified selector on the main thread with the specified object. More...
 
(void) - performSelectorOnMainThread:withObject:withObject:waitUntilDone:
 Performs the specified selector on the main thread with the specified objects. More...
 
(void) - performSelector:onThread:afterDelay:
 Performs the specified selector on the specified thread after the specified delay. More...
 
(void) - performSelector:onThread:withObject:afterDelay:
 Performs the specified selector on the specified thread with the specified object after the specified delay. More...
 
(void) - performSelector:onThread:withObject:withObject:afterDelay:
 Performs the specified selector on the specified thread with the specified objects after the specified delay. More...
 
(nullable id) - forwardingTargetForSelector:
 This method is called when resolveClassMethod: or resolveInstanceMethod: returned false. It should return a target to which the message should be forwarded. More...
 
(void) - doesNotRecognizeSelector:
 Handles messages which are not understood by the receiver. More...
 
(OFString *) - stringBySerializing
 Creates a string by serializing the receiver. More...
 
- Instance Methods inherited from <OFObject>
(bool) - isKindOfClass:
 Returns a boolean whether the object of the specified kind. More...
 
(bool) - isMemberOfClass:
 Returns a boolean whether the object is a member of the specified class. More...
 
(bool) - respondsToSelector:
 Returns a boolean whether the object responds to the specified selector. More...
 
(nullable IMP) - methodForSelector:
 Returns the implementation for the specified selector. More...
 
(nullable const char *) - typeEncodingForSelector:
 Returns the type encoding for the specified selector. More...
 
(nullable id) - performSelector:
 Performs the specified selector. More...
 
(nullable id) - performSelector:withObject:
 Performs the specified selector with the specified object. More...
 
(nullable id) - performSelector:withObject:withObject:
 Performs the specified selector with the specified objects. More...
 
(bool) - isEqual:
 Checks two objects for equality. More...
 
(uint32_t) - hash
 Calculates a hash for the object. More...
 
(id) - retain
 Increases the retain count. More...
 
(unsigned int) - retainCount
 Returns the retain count. More...
 
(void) - release
 Decreases the retain count. More...
 
(id) - autorelease
 Adds the object to the topmost OFAutoreleasePool of the thread's autorelease pool stack. More...
 
(id) - self
 Returns the receiver. More...
 
(bool) - isProxy
 Returns whether the object is a proxy object. More...
 
- Instance Methods inherited from <OFCopying>
(id) - copy
 Copies the object. More...
 
- Instance Methods inherited from <OFReadyForReadingObserving>
(int) - fileDescriptorForReading
 Returns the file descriptor for reading that should be checked by the OFKernelEventObserver. More...
 
- Instance Methods inherited from <OFReadyForWritingObserving>
(int) - fileDescriptorForWriting
 Returns the file descriptor for writing that should be checked by the OFKernelEventObserver. More...
 

Class Methods

(instancetype) + socket
 Returns a new, autoreleased OFUDPSocket. More...
 
(void) + resolveAddressForHost:port:address:
 Resolves the specified host and creates a an address for the host / port pair. More...
 
(void) + asyncResolveAddressForHost:port:target:selector:
 Asynchronously resolves the specified host and creates an address for the host / port pair. More...
 
(void) + asyncResolveAddressForHost:port:block:
 Asynchronously resolves the specified host and creates an address for the host / port pair. More...
 
(void) + getHost:andPort:forAddress:
 Gets the host and port for the specified address. More...
 
- Class Methods inherited from OFObject
(void) + load
 A method which is called once when the class is loaded into the runtime. More...
 
(void) + unload
 A method which is called when the class is unloaded from the runtime. More...
 
(void) + initialize
 A method which is called the moment before the first call to the class is being made. More...
 
(id) + alloc
 Allocates memory for an instance of the class and sets up the memory pool for the object. More...
 
(id) + new
 Allocates memory for a new instance and calls init on it. More...
 
(Class) + class
 Returns the class. More...
 
(OFString *) + className
 Returns the name of the class as a string. More...
 
(bool) + isSubclassOfClass:
 Returns a boolean whether the class is a subclass of the specified class. More...
 
(Class) + superclass
 Returns the superclass of the class. More...
 
(bool) + instancesRespondToSelector:
 Checks whether instances of the class respond to a given selector. More...
 
(bool) + conformsToProtocol:
 Checks whether the class conforms to a given protocol. More...
 
(nullable IMP) + instanceMethodForSelector:
 Returns the implementation of the instance method for the specified selector. More...
 
(nullable const char *) + typeEncodingForInstanceSelector:
 Returns the type encoding of the instance method for the specified selector. More...
 
(OFString *) + description
 Returns a description for the class, which is usually the class name. More...
 
(nullable IMP) + replaceClassMethod:withMethodFromClass:
 Replaces a class method with a class method from another class. More...
 
(nullable IMP) + replaceInstanceMethod:withMethodFromClass:
 Replaces an instance method with an instance method from another class. More...
 
(nullable IMP) + replaceClassMethod:withImplementation:typeEncoding:
 Replaces or adds a class method. More...
 
(nullable IMP) + replaceInstanceMethod:withImplementation:typeEncoding:
 Replaces or adds an instance method. More...
 
(void) + inheritMethodsFromClass:
 Adds all methods from the specified class to the class that is the receiver. More...
 
(bool) + resolveClassMethod:
 Try to resolve the specified class method. More...
 
(bool) + resolveInstanceMethod:
 Try to resolve the specified instance method. More...
 
(id) + copy
 Returns the class. More...
 

Detailed Description

A class which provides functions to create and use UDP sockets.

Addresses are of type of_udp_socket_address_t. You can use getHost:andPort:forAddress: (OFUDPSocket) to create an address for a host / port pair and getHost:andPort:forAddress: (OFUDPSocket) to get the host / port pair for an address. If you want to compare two addresses, you can use of_udp_socket_address_equal and you can use of_udp_socket_address_hash to get a hash to use in e.g. OFMapTable.

Warning
Even though the OFCopying protocol is implemented, it does not return an independent copy of the socket, but instead retains it. This is so that the socket can be used as a key for a dictionary, so context can be associated with a socket. Using a socket in more than one thread at the same time is not thread-safe, even if copy was called to create one "instance" for every thread!

Method Documentation

- (void) asyncReceiveIntoBuffer: (void*)  buffer
length: (size_t)  length
block: (of_udp_socket_async_receive_block_t block 

Asynchronously receives a datagram and stores it into the specified buffer.

If the buffer is too small, the datagram is truncated.

Parameters
bufferThe buffer to write the datagram to
lengthThe length of the buffer
blockThe block to call when the datagram has been received. If the block returns true, it will be called again with the same buffer and maximum length when more datagrams have been received. If you want the next method in the queue to handle the datagram received next, you need to return false from the method.
- (void) asyncReceiveIntoBuffer: (void*)  buffer
length: (size_t)  length
target: (id)  target
selector: (SEL)  selector 

Asynchronously receives a datagram and stores it into the specified buffer.

If the buffer is too small, the datagram is truncated.

Parameters
bufferThe buffer to write the datagram to
lengthThe length of the buffer
targetThe target on which the selector should be called when the datagram has been received. If the method returns true, it will be called again with the same buffer and maximum length when more datagrams have been received. If you want the next method in the queue to handle the datagram received next, you need to return false from the method.
selectorThe selector to call on the target. The signature must be bool (OFUDPSocket *socket, void *buffer, size_t length, of_udp_socket_address_t, OFException *exception).
+ (void) asyncResolveAddressForHost: (OFString*)  host
port: (uint16_t)  port
block: (of_udp_socket_async_resolve_block_t block 

Asynchronously resolves the specified host and creates an address for the host / port pair.

Parameters
hostThe host to resolve
portThe port for the resulting address
blockTHe block to execute once the host has been resolved
+ (void) asyncResolveAddressForHost: (OFString*)  host
port: (uint16_t)  port
target: (id)  target
selector: (SEL)  selector 

Asynchronously resolves the specified host and creates an address for the host / port pair.

Parameters
hostThe host to resolve
portThe port for the resulting address
targetThe target on which to call the selector once the host has been resolved
selectorThe selector to call on the target. The signature must be void (OFString *host, uint16_t port, of_udp_socket_address_t address, OFException *exception).
- (uint16_t) bindToHost: (OFString*)  host
port: (uint16_t)  port 

Binds the socket to the specified host and port.

Parameters
hostThe host to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6 to bind to all.
portThe port to bind to. If the port is 0, an unused port will be chosen, which can be obtained using the return value.
Returns
The port the socket was bound to
- (void) cancelAsyncRequests

Cancels all pending asyncronous requests on the socket.

Warning
You are not allowed to call this inside the handler of an asyncronous request, as this would cancel the asyncronous request that is currently being handled! To cancel all pending asyncronous requests after the handler has finished executing, you may schedule a timer for this method with a timeout of 0 from inside the handler.
+ (void) getHost: (OFString *__autoreleasing OF_NONNULL *OF_NULLABLE)  host
andPort: (uint16_t *OF_NULLABLE)  port
forAddress: (of_udp_socket_address_t *)  address 

Gets the host and port for the specified address.

Parameters
hostA pointer to an OFString*. If it is not NULL, it will be set to the host of the host / port pair.
portA pointer to an uint16_t. If it is not NULL, the port of the host / port pair will be written to it.
addressThe address for which the host and port should be retrieved
- (size_t) receiveIntoBuffer: (void*)  buffer
length: (size_t)  length
sender: (of_udp_socket_address_t*)  sender 

Receives a datagram and stores it into the specified buffer.

If the buffer is too small, the datagram is truncated.

Parameters
bufferThe buffer to write the datagram to
lengthThe length of the buffer
senderA pointer to an of_udp_socket_address_t, which will be set to the address of the sender
Returns
The length of the received datagram
+ (void) resolveAddressForHost: (OFString*)  host
port: (uint16_t)  port
address: (of_udp_socket_address_t*)  address 

Resolves the specified host and creates a an address for the host / port pair.

Parameters
hostThe host to resolve
portThe port for the resulting address
addressA pointer to the address that should be filled with the host / port pair
- (void) sendBuffer: (const void*)  buffer
length: (size_t)  length
receiver: (of_udp_socket_address_t*)  receiver 

Sends the specified datagram to the specified address.

Parameters
bufferThe buffer to send as a datagram
lengthThe length of the buffer
receiverA pointer to an of_udp_socket_address_t to which the datagram should be sent
+ (instancetype) socket

Returns a new, autoreleased OFUDPSocket.

Returns
A new, autoreleased OFUDPSocket

The documentation for this class was generated from the following files: