lw-support/src/liblw-support/Net/TCP/Client.h

149 lines
4.3 KiB
C++

/* lw-support/src/lib/Net/TCP/Client.h
*
* (c)2005, Laurence Withers. Released under the GNU GPL. See file
* COPYING for more information / terms of license.
*/
namespace lw {
/*! \brief TCP client socket class.
\todo docs
*/
class NetClientTCP : public NetClient {
private:
void clearAddr();
void connect(const NetAddress& addr, uint16_t port, bool block);
NetAddress* sockAddr, * peerAddr;
uint16_t sockPort, peerPort;
public:
//BEGIN // Constructors etc. ///////////////////////////////////////
/// Constructor. Does not connect client.
NetClientTCP();
/// Destructor.
virtual ~NetClientTCP();
/*! \brief Construct from an already-connected socket.
\param fd The file descriptor of the socket.
\param sockAddr Local address of the socket.
\param sockPort Local port of the socket.
\param peerAddr Address of the remote socket.
\param peerPort Remote socket port number.
This constructor is intended for use in e.g. NetServerTCP(), and is
for situations where you have an already-connected socket and want
to build a wrapper around it. It takes ownership of the \a sockAddr
and \a peerAddr objects.
*/
NetClientTCP(int fd, NetAddress* sockAddr, uint16_t sockPort,
NetAddress* peerAddr, uint16_t peerPort);
/*! \brief Construct and connect (blocking).
\param addr The address of the machine to connect to.
\param port The TCP port to connect to.
\throws SystemError if a system error occurs.
\throws NetServer::UnknownProtocol if the protocol of \a addr is
not known.
Similar to \a connect(), this function will establish a connection
to a remote host. However, this is a blocking function: it will
only return once the connection has been established.
*/
NetClientTCP(const NetAddress& addr, uint16_t port);
//BEGIN // Connection functions ////////////////////////////////////
/*! \brief Connect to a remote host (non-blocking).
\param addr The address of the machine to connect to.
\param port The TCP port to connect to.
\throws SystemError if a system error occurs.
\throws NetServer::UnknownProtocol if the protocol of \a addr is
not known.
This function begins connecting to a remote host. Note that it
doesn't actually guarantee the connection will be established,
since it is a non-blocking function. You will know the connection
is ready when you receive an IOListen event.
If the client is already connected, it will be disconnected first.
\todo figure out what happens in the case of a connection error.
*/
void connect(const NetAddress& addr, uint16_t port);
/*! \brief Connect to a remote host (blocking).
\param addr The address of the machine to connect to.
\param port The TCP port to connect to.
\throws SystemError if a system error occurs.
\throws NetServer::UnknownProtocol if the protocol of \a addr is
not known.
Similar to \a connect(), this function will establish a connection
to a remote host. However, this is a blocking function: it will
only return once the connection has been established.
\todo add some nicer exception classes for networking.
*/
void connectBlocking(const NetAddress& addr, uint16_t port);
//BEGIN // Query functions /////////////////////////////////////////
/// Query local port number; throws IOModeError if not connected.
uint16_t localPort() const;
/// Query remote port number; throws IOModeError if not connected.
uint16_t remotePort() const;
/// Query local host address; throws IOModeError if not connected.
const NetAddress& localAddr() const;
/// Query remote host address; throws IOModeError if not connected.
const NetAddress& remoteAddr() const;
//BEGIN // Misc. operations ////////////////////////////////////////
/*! \brief Set no delay option.
\param noDelay Set to \a true if you want to turn on the no delay
option; set to \a false if you want to use Nagle's algorithm.
\throws SystemError if a system error occurs.
This function allows you to turn on/off Nagle's algorithm, whereby
outgoing packets are stored until the remote system returns an ACK.
This generally results in better usage of the network but does
increase latency.
*/
void setNoDelay(bool noDelay);
};
}