TcpSocket Class Reference

#include <Pt/Net/TcpSocket.h>

TCP client socket.

Inherits IODevice.

Public Member Functions

 TcpSocket ()
 Default Constructor.
 
 TcpSocket (System::EventLoop &loop)
 Construct with event loop.
 
 TcpSocket (TcpServer &server)
 Accepts a connection.
 
 TcpSocket (const Endpoint &ep)
 Connects to a host. More...
 
 ~TcpSocket ()
 Destructor.
 
void accept (TcpServer &server)
 Accepts a connection.
 
void accept (TcpServer &server, const TcpSocketOptions &o)
 Accepts a connection.
 
void connect (const Endpoint &ep)
 Connect to a host. More...
 
void connect (const Endpoint &ep, const TcpSocketOptions &o)
 Connect to a host. More...
 
void beginConnect (const Endpoint &ep)
 Begin connecting to a host. More...
 
void beginConnect (const Endpoint &ep, const TcpSocketOptions &o)
 Begin connecting to a host. More...
 
void endConnect ()
 Ends connecting to a host. More...
 
Signal< TcpSocket & > & connected ()
 Notifies that the socket was connected. More...
 
bool isConnected () const
 Returns true if connected.
 
void localEndpoint (Endpoint &ep) const
 Gets the local endpoint.
 
void remoteEndpoint (Endpoint &ep) const
 Gets the remote endpoint.
 
void close ()
 Closes the device.
 
void setTimeout (std::size_t timeout)
 Sets the timeout for blocking I/O in milliseconds.
 
void beginRead (char *buffer, std::size_t n)
 Begins to read data.
 
std::size_t endRead ()
 Ends reading data.
 
std::size_t read (char *buffer, std::size_t n)
 Read data from I/O device. More...
 
void beginWrite (const char *buffer, std::size_t n)
 Begins to write data.
 
std::size_t endWrite ()
 Ends writing data.
 
std::size_t write (const char *buffer, std::size_t n)
 Write data to I/O device. More...
 
bool seekable () const
 Returns true if device is seekable.
 
pos_type seek (off_type offset, seekdir sd)
 Moves the read position to the given offset. More...
 
std::size_t peek (char *buffer, std::size_t n)
 Peek data from I/O device without consuming them. More...
 
void sync ()
 Synchronize device. More...
 
pos_type position ()
 Returns the current I/O position. More...
 
bool isEof () const
 Returns if the device has reached EOF.
 
Signal< IODevice & > & inputReady ()
 Notifies about available data. More...
 
Signal< IODevice & > & outputReady ()
 Notifies when data can be written. More...
 
bool isReading () const
 Returns true if the device is reading.
 
bool isWriting () const
 Returns true if the device is writing.
 
EventLoop * loop () const
 Returns the used event loop.
 
void setActive (EventLoop &parent)
 Sets the parent loop, so that operations can be run.
 
void detach ()
 Remove from event loop and cancels outstanding operations.
 
void cancel ()
 Cancels all operations.
 
bool run ()
 Run operation if it is ready.
 
EventLoop * parent () const
 Returns the used event loop.
 

Protected Member Functions

virtual bool onRun ()
 Check if ready and run.
 
virtual void onCancel ()
 Blocks until operation has cancelled.
 
virtual void onAttach (EventLoop &loop)
 Attached to loop.
 
virtual void onDetach (EventLoop &loop)
 Detached from loop.
 
void post ()
 Posts this selectable to its event loop from any thread.
 

Constructor & Destructor Documentation

◆ TcpSocket()

TcpSocket ( const Endpoint ep)
explicit
Exceptions
System::AccessFailedif the host is not reachable

Member Function Documentation

◆ connect() [1/2]

void connect ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable

◆ connect() [2/2]

void connect ( const Endpoint ep,
const TcpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable

◆ beginConnect() [1/2]

void beginConnect ( const Endpoint ep)
Exceptions
System::AccessFailedif the host is not reachable

◆ beginConnect() [2/2]

void beginConnect ( const Endpoint ep,
const TcpSocketOptions o 
)
Exceptions
System::AccessFailedif the host is not reachable

◆ endConnect()

void endConnect ( )
Exceptions
System::AccessFailedif the host is not reachable

◆ connected()

Signal<TcpSocket&>& connected ( )

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

◆ read()

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

◆ write()

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

◆ seek()

pos_type seek ( off_type  offset,
seekdir  sd 
)
inherited
Exceptions
IOError

◆ peek()

std::size_t peek ( char *  buffer,
std::size_t  n 
)
inherited
Exceptions
IOError

◆ sync()

void sync ( )
inherited

Commits written data to physical device.

Exceptions
IOError

◆ position()

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

◆ inputReady()

Signal<IODevice&>& inputReady ( )
inherited

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

◆ outputReady()

Signal<IODevice&>& outputReady ( )
inherited

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