UdpSocket Class Reference

#include <Pt/Net/UdpSocket.h>

UDP server and client socket.

Inherits IODevice.

Public Member Functions

 UdpSocket ()
 Default constructor.
 
 UdpSocket (System::EventLoop &loop)
 Construct with event loop.
 
 ~UdpSocket ()
 Destructor.
 
bool beginBind (const Endpoint &ep)
 Begin bind to local endpoint. More...
 
bool beginBind (const Endpoint &ep, const UdpSocketOptions &o)
 Begin bind to local endpoint. More...
 
bool beginConnect (const Endpoint &ep)
 Begin connect to an endpoint. More...
 
bool beginConnect (const Endpoint &ep, const UdpSocketOptions &o)
 Begin connect to an endpoint. More...
 
void beginRead (char *buffer, std::size_t n)
 Begins to read data.
 
void beginWrite (const char *buffer, std::size_t n)
 Begins to write data.
 
void bind (const Endpoint &ep)
 Bind to local endpoint. More...
 
void bind (const Endpoint &ep, const UdpSocketOptions &o)
 Bind to local endpoint. More...
 
Signal< UdpSocket & > & bound ()
 Notifies that the socket was bound. More...
 
void cancel ()
 Cancels all operations.
 
void close ()
 Closes the device.
 
void connect (const Endpoint &ep)
 Connect to an endpoint. More...
 
void connect (const Endpoint &ep, const UdpSocketOptions &o)
 Connect to an endpoint. More...
 
Signal< UdpSocket & > & connected ()
 Notifies that the socket was connected. More...
 
void detach ()
 Remove from event loop and cancels outstanding operations.
 
void endBind ()
 end bind to local endpoint. More...
 
void endConnect ()
 End connect to an endpoint. More...
 
std::size_t endRead ()
 Ends reading data.
 
std::size_t endWrite ()
 Ends writing data.
 
Signal< IODevice & > & inputReady ()
 Notifies about available data. More...
 
bool isBound () const
 Returns true if bound.
 
bool isConnected () const
 Returns true if connected.
 
bool isEof () const
 Returns if the device has reached EOF.
 
bool isReading () const
 Returns true if the device is reading.
 
bool isWriting () const
 Returns true if the device is writing.
 
void joinMulticastGroup (const std::string &ipaddr)
 Joins a multicast group.
 
void localEndpoint (Endpoint &ep) const
 Gets the local endpoint.
 
EventLoop * loop () const
 Returns the used event loop.
 
Signal< IODevice & > & outputReady ()
 Notifies when data can be written. More...
 
std::size_t peek (char *buffer, std::size_t n)
 Peek data from I/O device without consuming them. More...
 
pos_type position ()
 Returns the current I/O position. More...
 
std::size_t read (char *buffer, std::size_t n)
 Read data from I/O device. More...
 
const EndpointremoteEndpoint () const
 Gets the remote endpoint.
 
bool run ()
 Run operation if it is ready.
 
pos_type seek (off_type offset, seekdir sd)
 Moves the read position to the given offset. More...
 
bool seekable () const
 Returns true if device is seekable.
 
void setActive (EventLoop &parent)
 Sets the parent loop, so that operations can be run.
 
void setTarget (const Endpoint &ep)
 Set target endpoint. More...
 
void setTarget (const Endpoint &ep, const UdpSocketOptions &o)
 Set target endpoint. More...
 
void setTimeout (std::size_t timeout)
 Sets the timeout for blocking I/O in milliseconds.
 
void sync ()
 Synchronize device. More...
 
std::size_t write (const char *buffer, std::size_t n)
 Write data to I/O device. More...
 

Protected Member Functions

virtual void onAttach (EventLoop &loop)
 Attached to loop.
 
virtual void onCancel ()
 Blocks until operation has cancelled.
 
virtual void onDetach (EventLoop &loop)
 Detached from loop.
 
virtual bool onRun ()
 Check if ready and run.
 

Member Function Documentation

void bind ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable
void bind ( const Endpoint ep,
const UdpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable
bool beginBind ( const Endpoint ep)

Begins binding to the Endpoint ep. The UdpSocket must be attached to a event loop with setActive(). Once the binding has completed, the signal bound() will be sent. In response, the method endBind() has to be called to finish the bind operation.

Exceptions
System::AccessFailedif the host is not reachable
bool beginBind ( const Endpoint ep,
const UdpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable
void endBind ( )
Exceptions
System::AccessFailedif the host is not reachable
Signal<UdpSocket&>& bound ( )

This signal is send when the UdpSocket is monitored in an EventLoop and was bound to a local endpoint.

void connect ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable
void connect ( const Endpoint ep,
const UdpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable
void setTarget ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable
void setTarget ( const Endpoint ep,
const UdpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable
bool beginConnect ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable
bool beginConnect ( const Endpoint ep,
const UdpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable
void endConnect ( )
Exceptions
System::AccessFailedif the host is not reachable
Signal<UdpSocket&>& connected ( )

This signal is send when the UdpSocket is monitored in an EventLoop and a connection was established.

std::size_t read ( char *  buffer,
std::size_t  n 
)
inherited

Reads up to n bytes and stores them in buffer. Returns the number of bytes read, which may be less than requested and even 0 if the device operates in asynchronous (non-blocking) mode. In case of EOF the IODevice is set to eof.

Parameters
bufferbuffer where to place the data to be read.
nnumber of bytes to read
Returns
number of bytes read, which may be less than requested.
Exceptions
IOError
std::size_t write ( const char *  buffer,
std::size_t  n 
)
inherited

Writes n bytes from buffer to this I/O device. Returns the number of bytes written, which may be less than requested. In case of EOF the IODevice is set to eof.

Exceptions
IOError
pos_type seek ( off_type  offset,
seekdir  sd 
)
inherited
Exceptions
IOError
std::size_t peek ( char *  buffer,
std::size_t  n 
)
inherited
void sync ( )
inherited

Commits written data to physical device.

Exceptions
IOError
pos_type position ( )
inherited

The current I/O position is returned or an IOError is thrown if the device is not seekable. Seekability can be tested with seekable().

Exceptions
IOError
Signal<IODevice&>& inputReady ( )
inherited

This signal is send when the IODevice is monitored in an EventLoop and data becomes available.

Signal<IODevice&>& outputReady ( )
inherited

This signal is send when the IODevice is monitored in an EventLoop and the device is ready to write data.