TcpSocket Class Reference

#include <Pt/Net/TcpSocket.h>

Connected TCP byte stream. More...

Inherits IODevice.

Public Member Functions

 TcpSocket ()
 Creates a closed socket.
 TcpSocket (System::EventLoop &loop)
 Creates a socket and attaches it to loop.
 TcpSocket (TcpServer &server)
 Creates a socket and accepts a connection from server.
 TcpSocket (const Endpoint &ep)
 Creates a socket and connects to ep.
 ~TcpSocket ()
 Destroys the socket.
void accept (TcpServer &server)
 Accepts a connection from server.
void accept (TcpServer &server, const TcpSocketOptions &o)
 Accepts a connection from server with o.
void connect (const Endpoint &ep)
 Connects to ep.
void connect (const Endpoint &ep, const TcpSocketOptions &o)
 Connects to ep with o.
void beginConnect (const Endpoint &ep)
 Begins connecting to ep.
void beginConnect (const Endpoint &ep, const TcpSocketOptions &o)
 Begins connecting to ep with o.
void endConnect ()
 Ends connecting to a host.
Signal< TcpSocket & > & connected ()
 Returns the signal that the socket was connected.
bool isConnected () const
 Returns true if the socket is connected.
void localEndpoint (Endpoint &ep) const
 Writes the local endpoint into ep.
void remoteEndpoint (Endpoint &ep) const
 Writes the remote endpoint into ep.
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.
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.
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.
std::size_t peek (char *buffer, std::size_t n)
 Peek data from I/O device without consuming them.
void sync ()
 Synchronize device.
pos_type position ()
 Returns the current I/O position.
bool isEof () const
 Returns if the device has reached EOF.
Signal< IODevice & > & inputReady ()
 Notifies about available data.
Signal< IODevice & > & outputReady ()
 Notifies when data can be written.
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.
void post ()
 Posts this selectable to its event loop from any thread.
virtual void onAttach (EventLoop &)
 Attached to loop.
virtual void onDetach (EventLoop &)
 Detached from loop.

Detailed Description

TcpSocket is the connected TCP IODevice, and the same type is the client side of a connect() and the accepted side of a pending TcpServer connection. After the stream is up, read() and write() are the inherited device operations, blocking or asynchronous.

connect() reaches a remote endpoint and makes this socket the client stream, throwing AccessFailed if the host is not reachable, while accept() takes a pending connection from a listening server and makes this socket the accepted stream. A socket is one side or the other, not both at once, because connect() and accept() close any previous connection first.

Asynchronous connect

beginConnect() starts an asynchronous connect, which requires the socket to be attached to an event loop. connected() is emitted when the attempt finishes, and endConnect() completes it, throwing AccessFailed if the host is not reachable. isConnected() reports whether the stream is up, and localEndpoint() and remoteEndpoint() write the two sides of the connection.

TcpSocketOptions configure the connection but do not open a socket. Keep-alive, when set to a non-negative interval in seconds, enables periodic probes on the stream, and a negative value leaves keep-alive unset.

The example is an asynchronous client connect, in which the slot calls endConnect() and then writes through the inherited device operation. The accept path is the TcpServer pending-connection slot.

void onConnected(Pt::Net::TcpSocket& socket)
{
socket.endConnect();
socket.beginWrite("Hello", 5);
}
socket.setActive(loop);
socket.connected() += Pt::slot(onConnected);
socket.beginConnect(Pt::Net::Endpoint("127.0.0.1", 9000));
Host and service address.
Definition Endpoint.h:77
Connected TCP byte stream.
Definition TcpSocket.h:141
void endConnect()
Ends connecting to a host.
Signal< TcpSocket & > & connected()
Returns the signal that the socket was connected.
Definition TcpSocket.h:209
void beginConnect(const Endpoint &ep)
Begins connecting to ep.
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
void run()
Starts the loop.
void beginWrite(const char *buffer, std::size_t n)
Begins to write data.
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
Platform event loop.
Definition MainLoop.h:51
void setActive(EventLoop &parent)
Sets the parent loop, so that operations can be run.

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)

The socket must be attached to an event loop.

Exceptions
%System::AccessFailedif the host is not reachable.

◆ beginConnect() [2/2]

void beginConnect ( const Endpoint & ep,
const TcpSocketOptions & o )

The socket must be attached to an event loop.

Exceptions
%System::AccessFailedif the host is not reachable.

◆ endConnect()

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

◆ 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.