All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::TCPSocket Class Reference

A client TCP socket for connecting to remote hosts. More...

#include <Radiant/TCPSocket.hpp>

Inheritance diagram for Radiant::TCPSocket:
Radiant::BinaryStream Patterns::NotCopyable

Public Types

enum  Flags { NONBLOCK, WAIT_SOME, WAIT_ALL }
 Flags used with read() -function. More...
 

Public Member Functions

virtual bool close ()
 Closes the socket. More...
 
const QString & host () const
 Returns the hostname. More...
 
virtual bool isHungUp () const
 Returns true if the socket has been closed. More...
 
virtual bool isOpen () const
 Returns true of the socket is open. More...
 
virtual bool isPendingInput (unsigned int waitMicroSeconds=0)
 Return 'true' if readable data is pending. More...
 
int open (const char *host, int port)
 Opens a TCP socket to desired host:port. More...
 
TCPSocketoperator= (TCPSocket &&socket)
 Move the given socket. More...
 
int port () const
 Returns the port number. More...
 
int read (void *buffer, int bytes, Flags flags)
 Read bytes from the socket. More...
 
virtual int read (void *buffer, int bytes, bool waitfordata=true)
 Read bytes from the socket. More...
 
unsigned long rxBytes () const
 Number of bytes received through the socket. More...
 
bool setNoDelay (bool noDelay)
 Turn the Nagle algorithm on or off. More...
 
 TCPSocket ()
 Constructor.
 
 TCPSocket (int fd)
 Construct a socket based on a file descriptor This method is potentially non-portable as not all platforms use file descriptors to handle sockets. More...
 
 TCPSocket (TCPSocket &&socket)
 Move the given socket. More...
 
unsigned long txBytes () const
 Number of bytes sent through the socket. More...
 
virtual int write (const void *buffer, int bytes)
 Write bytes to the socket. More...
 
 ~TCPSocket ()
 Destructor. Closes the socket.
 
- Public Member Functions inherited from Radiant::BinaryStream
 BinaryStream ()
 Constructor.
 
virtual ~BinaryStream ()
 Destructor.
 

Friends

class TCPServerSocket
 

Detailed Description

A client TCP socket for connecting to remote hosts.

Author
Tommi Ilmonen

Member Enumeration Documentation

Flags used with read() -function.

Enumerator
NONBLOCK 

Never block.

WAIT_SOME 

Block until some data is read or socket has a read error.

WAIT_ALL 

Blocks until all requested data is read or socket has a read error.

Constructor & Destructor Documentation

Radiant::TCPSocket::TCPSocket ( int  fd)

Construct a socket based on a file descriptor This method is potentially non-portable as not all platforms use file descriptors to handle sockets.

Parameters
fdsocket file descriptor
Radiant::TCPSocket::TCPSocket ( TCPSocket &&  socket)

Move the given socket.

Parameters
socketsocket to move

Member Function Documentation

virtual bool Radiant::TCPSocket::close ( )
virtual

Closes the socket.

Returns
True if there was socket to close, false otherwise

Reimplemented from Radiant::BinaryStream.

const QString& Radiant::TCPSocket::host ( ) const

Returns the hostname.

Returns
Hostname of the socket
virtual bool Radiant::TCPSocket::isHungUp ( ) const
virtual

Returns true if the socket has been closed.

Returns
True if socket has been closed

Reimplemented from Radiant::BinaryStream.

virtual bool Radiant::TCPSocket::isOpen ( ) const
virtual

Returns true of the socket is open.

Returns
True if there is an open socket

Reimplemented from Radiant::BinaryStream.

virtual bool Radiant::TCPSocket::isPendingInput ( unsigned int  waitMicroSeconds = 0)
virtual

Return 'true' if readable data is pending.

Parameters
waitMicroSecondsHow long this call will block at most
Returns
True If the socket is pending input

Reimplemented from Radiant::BinaryStream.

int Radiant::TCPSocket::open ( const char *  host,
int  port 
)

Opens a TCP socket to desired host:port.

Parameters
hosthostname
portport
Returns
On successful execution, returns zero, otherwise an error code (as in errno.h).
TCPSocket& Radiant::TCPSocket::operator= ( TCPSocket &&  socket)

Move the given socket.

Parameters
socketsocket to move
Returns
reference to this
int Radiant::TCPSocket::port ( ) const

Returns the port number.

Returns
Port number of the socket
int Radiant::TCPSocket::read ( void *  buffer,
int  bytes,
Flags  flags 
)

Read bytes from the socket.

Parameters
[out]bufferpointer to a buffer to store the read data to
byteshow many bytes the buffer has room for
flagsBlocking behaviour
Returns
the number of bytes actually read
virtual int Radiant::TCPSocket::read ( void *  buffer,
int  bytes,
bool  waitfordata = true 
)
inlinevirtual

Read bytes from the socket.

Parameters
[out]bufferpointer to a buffer to store the read data to
byteshow many bytes the buffer has room for
waitfordatawait for all data or do not block at all
See Also
read(void*,int,Flags)
Returns
the number of bytes actually read

Implements Radiant::BinaryStream.

unsigned long Radiant::TCPSocket::rxBytes ( ) const

Number of bytes received through the socket.

This count gets cleared when a socket is opened.

Returns
number of bytes received through the socket
bool Radiant::TCPSocket::setNoDelay ( bool  noDelay)

Turn the Nagle algorithm on or off.

This controls the queuing of messages to fill packets.

Parameters
noDelayif true, the queing is not used and packets are sent immediately
Returns
true on success
unsigned long Radiant::TCPSocket::txBytes ( ) const

Number of bytes sent through the socket.

This count gets cleared when a socket is opened.

Returns
number of bytes sent through the socket
virtual int Radiant::TCPSocket::write ( const void *  buffer,
int  bytes 
)
virtual

Write bytes to the socket.

Parameters
bufferData to write
bytesHow many bytes is requested to be written
Returns
How many bytes were actually written

Implements Radiant::BinaryStream.