UDPSocket Class Reference

#include <sockets.h>

Inheritance diagram for UDPSocket:

Inheritance graph
[legend]
Collaboration diagram for UDPSocket:

Collaboration graph
[legend]
List of all members.

Detailed Description

UDP socket is connection-less socket, using the UDP protocol.

Usage: Construct UDPSocket object on heap and call listen() member function to start up the socket. After that, when incoming data is detected, events are emitted, and data may be received using recv() member function. Where the data originated will be written into the passed IPV4Address parameter. To send data to a specific location, send() method may be used, where the last parameter indicates where to send the data.

Note:
UDP sockets are not reliable. There is no way of knowing whether the data sent out actually reached the recipient.

Definition at line 397 of file sockets.h.


Public Types

typedef UDPSEventHandler HandlerType
 Type of event handler used by this object.

Public Member Functions

 DECLARE_EVENT_TABLE (UDPSocket *, SocketEvent)
 UDPSocket (UDPSEventHandler handler=0)
 Construct new UDP socket, optionally setting handler.
void destroy ()
 Destroy this socket.
void listen (IPV4Address addr)
 Starting listening on a local address.
void listen (uint32_t addr, uint16_t port)
 Provided for convenience.
void listen (const std::string &addr, uint16_t port)
 Provided for convenience.
uint32_t recv (char *buf, uint32_t len, IPV4Address *from)
 Receive data from a location.
uint32_t send (const std::string &data, IPV4Address to)
 Send data to a specific location.
bool isListening () const
 If the socket is in listening state.
bool hasData () const
 If the socket has pending incoming data.
bool isConnected () const
 Helper for Scheduler API; UDPSockets are never in "connected" state, e.g.
IPV4Address getAddr () const

Private Member Functions

virtual ~UDPSocket ()
 Destructor.

Private Attributes

bool m_listening
 Indicates whether we are listening.
bool m_hasData
 Indicates the presence of data.
bool m_erronous
 Indicates erronous status.
IPV4Address m_addr
 Local address.

Friends

class SocketWatcher

Member Typedef Documentation

typedef UDPSEventHandler UDPSocket::HandlerType
 

Type of event handler used by this object.

Definition at line 402 of file sockets.h.


Constructor & Destructor Documentation

UDPSocket::UDPSocket UDPSEventHandler  handler = 0  ) 
 

Construct new UDP socket, optionally setting handler.

Definition at line 523 of file sockets.cpp.

References initSockets(), INVALID_SOCKET, logTrace(), setNonBlocking(), setReUsable(), and TRACE_SOCKET.

Here is the call graph for this function:

UDPSocket::~UDPSocket  )  [private, virtual]
 

Destructor.

Definition at line 542 of file sockets.cpp.


Member Function Documentation

UDPSocket::DECLARE_EVENT_TABLE UDPSocket ,
SocketEvent 
 

void UDPSocket::destroy  )  [virtual]
 

Destroy this socket.

It is safe to call this method multiple times.

Implements SocketBase.

Definition at line 546 of file sockets.cpp.

References closesocket, logDebug(), m_listening, SocketWatcher::removeSocket(), and socketError().

Here is the call graph for this function:

IPV4Address UDPSocket::getAddr  )  const [inline]
 

Returns:
The local address of this socket

Definition at line 462 of file sockets.h.

Referenced by send().

bool UDPSocket::hasData  )  const [inline]
 

If the socket has pending incoming data.

Definition at line 451 of file sockets.h.

bool UDPSocket::isConnected  )  const [inline]
 

Helper for Scheduler API; UDPSockets are never in "connected" state, e.g.

they are not counted towards connection-counts.

Definition at line 457 of file sockets.h.

bool UDPSocket::isListening  )  const [inline]
 

If the socket is in listening state.

Definition at line 449 of file sockets.h.

void UDPSocket::listen const std::string &  addr,
uint16_t  port
[inline]
 

Provided for convenience.

Definition at line 425 of file sockets.h.

void UDPSocket::listen uint32_t  addr,
uint16_t  port
[inline]
 

Provided for convenience.

Definition at line 421 of file sockets.h.

void UDPSocket::listen IPV4Address  addr  ) 
 

Starting listening on a local address.

If addr.ip is 0, we will listen on all available addresses. Addr.port may not be 0.

Parameters:
addr Address to listen on

Definition at line 557 of file sockets.cpp.

References SocketWatcher::addSocket(), CHECK_THROW_MSG, IPV4Address::getAddr(), IPV4Address::getPort(), logDebug(), logTrace(), m_addr, m_erronous, m_listening, IPV4Address::setAddr(), SOCKET_ERROR, socketError(), and TRACE_SOCKET.

Here is the call graph for this function:

uint32_t UDPSocket::recv char *  buf,
uint32_t  len,
IPV4Address from
 

Receive data from a location.

Parameters:
buf Buffer to write the received data
len Length of the buffer
from Receives the data source address
Returns:
The amount of data actually received

Definition at line 597 of file sockets.cpp.

References CHECK, CHECK_THROW_MSG, getLastError(), m_hasData, m_listening, MSG_NOSIGNAL, IPV4Address::setAddr(), IPV4Address::setPort(), SOCK_EAGAIN, SOCKET_ERROR, and socketError().

Here is the call graph for this function:

uint32_t UDPSocket::send const std::string &  data,
IPV4Address  to
 

Send data to a specific location.

Parameters:
data Data to be sent out
to Address to send the data to
Returns:
Amount of data actually written to socket

Definition at line 632 of file sockets.cpp.

References CHECK_THROW_MSG, getAddr(), IPV4Address::getAddr(), getLastError(), IPV4Address::getPort(), logDebug(), logTrace(), MSG_NOSIGNAL, SOCK_EACCESS, SOCK_EINVALIDPARAM, SOCK_EPERM, SOCKET_ERROR, socketError(), and TRACE_SOCKET.

Here is the call graph for this function:


Friends And Related Function Documentation

friend class SocketWatcher [friend]
 

Definition at line 467 of file sockets.h.


Member Data Documentation

IPV4Address UDPSocket::m_addr [private]
 

Local address.

Definition at line 471 of file sockets.h.

Referenced by listen().

bool UDPSocket::m_erronous [private]
 

Indicates erronous status.

Definition at line 470 of file sockets.h.

Referenced by listen().

bool UDPSocket::m_hasData [private]
 

Indicates the presence of data.

Definition at line 469 of file sockets.h.

Referenced by SocketWatcher::handleReadableSocket(), and recv().

bool UDPSocket::m_listening [private]
 

Indicates whether we are listening.

Definition at line 468 of file sockets.h.

Referenced by destroy(), listen(), and recv().


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