libcommonc++
0.7
|
A reliable, connection-oriented stream (TCP) socket. More...
#include <StreamSocket.h++>
Public Member Functions | |
StreamSocket (uint16_t port=0) | |
Construct a new StreamSocket. More... | |
~StreamSocket () | |
Destructor. More... | |
void | connect (const String &addr, uint16_t port) |
Connect the socket to a remote endpoint. More... | |
void | connect (const SocketAddress &addr) |
Connect the socket to a remote endpoint. More... | |
size_t | read (byte_t *buffer, size_t buflen) |
Read data from the stream into a raw buffer. More... | |
size_t | write (const byte_t *buffer, size_t buflen) |
Write data to the stream from a raw buffer, until either the entire buffer is written or an error or timeout occurs. More... | |
size_t | read (ByteBuffer &buffer) |
Read data from the stream into a ByteBuffer. More... | |
size_t | write (ByteBuffer &buffer) |
Write data to the stream from a ByteBuffer. More... | |
size_t | read (CharBuffer &buffer) |
Read data from the stream into a CharBuffer. More... | |
size_t | write (CharBuffer &buffer) |
Write data to the stream from a CharBuffer. More... | |
size_t | read (byte_t *buffer, size_t buflen, int64_t offset, AsyncIOTask &task) |
size_t | write (const byte_t *buffer, size_t buflen, int64_t offset, AsyncIOTask &task) |
size_t | read (ByteBuffer &buffer, int64_t offset, AsyncIOTask &task) |
size_t | write (ByteBuffer &buffer, int64_t offset, AsyncIOTask &task) |
size_t | read (MemoryBlock *vec, uint_t count) |
Read data from the stream into a series of I/O buffers. More... | |
size_t | write (const MemoryBlock *vec, uint_t count) |
Write data to the stream from a series of I/O buffers. More... | |
byte_t | readOOB () |
Read a byte of out-of-band data from the socket. More... | |
void | writeOOB (byte_t data) |
Write a byte of out-of-band data to the socket. More... | |
void | close (IOMode mode=IOReadWrite) |
Close the stream for reading, writing, or both. More... | |
virtual void | shutdown () |
Shut down the socket. More... | |
void | setTimeout (timespan_ms_t timeout) |
Set the stream I/O timeout, in milliseconds. More... | |
template<typename T > | |
size_t | read (Buffer< T > &buffer, size_t &partial) |
Read data from the stream into a Buffer of arbitrary type. More... | |
template<typename T > | |
size_t | write (Buffer< T > &buffer, size_t &partial) |
Write data to the stream from a Buffer of arbitrary type. More... | |
virtual size_t | readFully (ByteBuffer &buffer, size_t count=0) |
Read data from the stream into a ByteBuffer, until either the buffer is full or an error or timeout occurs. More... | |
virtual size_t | readFully (CharBuffer &buffer, size_t count=0) |
Read data from the stream into a CharBuffer, until either the buffer is full or an error or timeout occurs. More... | |
template<typename T > | |
size_t | readFully (Buffer< T > &buffer, size_t &partial) |
Read data from the stream into a Buffer of arbitrary type, until either the buffer is full or an error or timeout occurs. More... | |
virtual size_t | readFully (byte_t *buffer, size_t buflen) |
Read data from the stream into a raw buffer, until either the buffer is full or an error or timeout occurs. More... | |
virtual size_t | writeFully (ByteBuffer &buffer) |
Write data to the stream from a ByteBuffer, until either the entire buffer is written or an error or timeout occurs. More... | |
virtual size_t | writeFully (CharBuffer &buffer) |
Write data to the stream from a CharBuffer, until either the entire buffer is written or an error or timeout occurs. More... | |
template<typename T > | |
size_t | writeFully (Buffer< T > &buffer, size_t &partial) |
Write data to the stream from a Buffer of arbitrary type, until either the entire buffer is written or an error or timeout occurs. More... | |
virtual size_t | writeFully (const byte_t *buffer, size_t buflen) |
Write data to the stream from a raw buffer. More... | |
virtual int64_t | seek (int64_t offset, SeekMode mode=SeekAbsolute) |
Reposition the seek pointer in the stream. More... | |
virtual int64_t | tell () |
Get the current (absolute) offset of the seek pointer. More... | |
bool | isOpen () const |
Test if the stream is open. More... | |
bool | isSeekable () const |
Test if the stream supports seeking. More... | |
bool | isReadable () const |
Test if the stream can be read from. More... | |
bool | isWritable () const |
Test if the stream can be written to. More... | |
bool | isFullDuplex () const |
Test if the stream is full-duplex (i.e., supports both reading and writing). More... | |
bool | isHalfDuplex () const |
Test if the stream is half-duplex (i.e., supports either reading or writing, but not both). More... | |
timespan_ms_t | getTimeout () const |
Get the stream I/O timeout, in milliseconds. More... | |
virtual void | init () |
Initialize the socket. More... | |
void | setReceiveBufSize (size_t size) |
Set the size of the socket's internal receive buffer. More... | |
size_t | getReceiveBufSize () const |
Get the size of the socket's internal receive buffer. More... | |
void | setSendBufSize (size_t size) |
Set the size of the socket's internal send buffer. More... | |
size_t | getSendBufSize () const |
Get the size of the socket's internal send buffer. More... | |
void | setLingerTime (timespan_s_t timeout) |
Set the linger time for the socket. More... | |
timespan_s_t | getLingerTime () const |
Get the linger time for the socket. More... | |
void | setReuseAddress (bool enable) |
Enable or disable the SO_REUSEADDR option on the socket. More... | |
bool | getReuseAddress () const |
Determine if the SO_REUSEADDR option is enabled or disabled. More... | |
void | setKeepAlive (bool enable) |
Enable or disable the SO_KEEPALIVE option on the socket. More... | |
bool | getKeepAlive () |
Determine if the SO_KEEPALIVE option is enabled or disabled. More... | |
void | setTCPDelay (bool enable) |
Enable or disable the Nagle algorithm for send coalescing. More... | |
bool | getTCPDelay () |
Determine if the Nagle algorithm is enabled or disabled. More... | |
bool | isInitialized () const |
Determine if the socket has been initialized. More... | |
bool | isConnected () const |
Determine if the socket is connected. More... | |
NetProtocol | getType () const |
Get the socket type. More... | |
const SocketAddress & | getLocalAddress () const |
Get the address of the local end of the socket. More... | |
const SocketAddress & | getRemoteAddress () const |
Get the address of the remote (peer) end of the socket. More... | |
Static Public Attributes | |
static const uint_t | MAX_IOBLOCK_COUNT = 16 |
The maximum number of I/O buffers that can be passed to the vector I/O methods. More... | |
Protected Types | |
enum | IOWaitMode { WaitWrite, WaitRead } |
Protected Member Functions | |
void | _init (FileHandle handle, bool seekable, bool readable, bool writable) |
Initialize the stream for use with an open file. More... | |
SocketHandle | getSocketHandle () const |
Get the underlying socket handle for this socket. More... | |
void | waitForIO (IOWaitMode mode) |
Wait for the socket to become ready for reading or writing. More... | |
Protected Attributes | |
NetProtocol | _type |
The socket type. More... | |
SocketHandle | _socket |
A handle to the socket itself. More... | |
SocketAddress | _raddr |
The remote address. More... | |
SocketAddress | _laddr |
The local address. More... | |
int | _sotimeout |
The timeout value. More... | |
Friends | |
class | ServerSocket |
A reliable, connection-oriented stream (TCP) socket.
|
protectedinherited |
StreamSocket | ( | uint16_t | port = 0 | ) |
Construct a new StreamSocket.
port | The local port to bind to, or 0 to bind to an arbitrary port. |
~StreamSocket | ( | ) |
Destructor.
|
protectedinherited |
Initialize the stream for use with an open file.
handle | The handle to an open file. |
seekable | A flag indicating whether the stream is seekable. |
readable | A flag indicating whether the stream is readable. |
writable | A flag indicating whether the stream is writable. |
|
virtual |
Close the stream for reading, writing, or both.
mode | The close mode. |
Reimplemented from Stream.
|
virtual |
Connect the socket to a remote endpoint.
Note that if this call fails, the socket will revert to an uninitialized state; call init() again to reuse the socket.
addr | The address, which may either be a dot-separated IP address or a DNS name. |
port | The port number. |
IOException | If an error occurs. |
Reimplemented from Socket.
|
virtual |
Connect the socket to a remote endpoint.
Note that if this call fails, the socket will revert to an uninitialized state; call init() again to reuse the socket.
addr | The address. |
IOException | If an error occurs. |
Reimplemented from Socket.
|
inherited |
Determine if the SO_KEEPALIVE option is enabled or disabled.
SocketException | If a socket error occurs. |
|
inherited |
Get the linger time for the socket.
SocketException | If the linger time could not be retrieved. |
|
inlineinherited |
Get the address of the local end of the socket.
|
inherited |
Get the size of the socket's internal receive buffer.
SocketException | If the size could not be changed. |
|
inlineinherited |
Get the address of the remote (peer) end of the socket.
|
inherited |
Determine if the SO_REUSEADDR option is enabled or disabled.
SocketException | If a socket error occurs. |
|
inherited |
Get the size of the socket's internal send buffer.
SocketException | If the size could not be changed. |
|
inlineprotectedinherited |
Get the underlying socket handle for this socket.
|
inherited |
Determine if the Nagle algorithm is enabled or disabled.
SocketException | If a socket error occurs. |
|
inlineinherited |
Get the stream I/O timeout, in milliseconds.
|
inlineinherited |
Get the socket type.
|
virtualinherited |
Initialize the socket.
This method creates the underlying socket object.
SocketException | If the socket could not be created. |
Reimplemented in ServerSocket.
|
inlineinherited |
Determine if the socket is connected.
|
inlineinherited |
Test if the stream is full-duplex (i.e., supports both reading and writing).
|
inlineinherited |
Test if the stream is half-duplex (i.e., supports either reading or writing, but not both).
|
inlineinherited |
Determine if the socket has been initialized.
|
inlineinherited |
Test if the stream is open.
|
inlineinherited |
Test if the stream can be read from.
|
inlineinherited |
Test if the stream supports seeking.
|
inlineinherited |
Test if the stream can be written to.
|
virtual |
Read data from the stream into a raw buffer.
buffer | The buffer to read into. |
buflen | The number of bytes to read. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
Reimplemented from Stream.
|
inlinevirtual |
Read data from the stream into a ByteBuffer.
buffer | The buffer to read into. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
Reimplemented from Stream.
|
inlinevirtual |
Read data from the stream into a CharBuffer.
buffer | The buffer to read into. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
Reimplemented from Stream.
|
inline |
|
inline |
|
virtual |
Read data from the stream into a series of I/O buffers.
This operation is known as "vector read" or "scatter read." The buffers are read into in order. On systems that support it, the operation is performed with a single system call.
vec | An array of buffers to read into. |
count | The number of buffers in the array. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
Reimplemented from Stream.
|
inlineinherited |
Read data from the stream into a Buffer of arbitrary type.
If sizeof(T) is greater than 1 byte, then there can be no guarantee that the number of bytes read before a timeout or error occurs will be evenly divisible by sizeof(T). If the final element was partially read, the number of bytes of the element that were read will be stored in partial; this value should be passed unmodified to the next invocation of this method to continue reading from the appropriate offset within the partially-read element.
buffer | The buffer to read into. |
partial | The number of bytes remaining to read for a partially-read element; on return, the number of bytes read of a partially-read element, or 0 if the last element was read completely. Should be set to 0 prior to the first call to this method for a fresh buffer. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
|
virtualinherited |
Read data from the stream into a ByteBuffer, until either the buffer is full or an error or timeout occurs.
buffer | The buffer to read into. |
count | The number of bytes to read. If 0 or greater than the number of bytes between the position and the limit, reads up to the limit. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
|
virtualinherited |
Read data from the stream into a CharBuffer, until either the buffer is full or an error or timeout occurs.
buffer | The buffer to read into. |
count | The number of characters to read. If 0 or greater than the number of bytes between the position and the limit, reads up to the limit. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
|
inlineinherited |
Read data from the stream into a Buffer of arbitrary type, until either the buffer is full or an error or timeout occurs.
If sizeof(T) is greater than 1 byte, then there can be no guarantee that the number of bytes read before a timeout or error occurs will be evenly divisible by sizeof(T). If the final element was partially read, the number of bytes of the element that were read will be stored in partial; this value should be passed unmodified to the next invocation of this method to continue reading from the appropriate offset within the partially-read element.
buffer | The buffer to read into. |
partial | The number of bytes remaining to read for a partially-read element; on return, the number of bytes read of a partially-read element, or 0 if the last element was read completely. Should be set to 0 prior to the first call to this method for a fresh buffer. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
|
virtualinherited |
Read data from the stream into a raw buffer, until either the buffer is full or an error or timeout occurs.
buffer | The buffer to read into. |
buflen | The number of bytes to read. |
IOException | If the end-of-file was reached or some other I/O error occurred. |
byte_t readOOB | ( | ) |
Read a byte of out-of-band data from the socket.
IOException | If an I/O error occurs. |
|
virtualinherited |
Reposition the seek pointer in the stream.
offset | The new offset. |
mode | The seek mode. |
IOException | If the stream does not support seeking, or if some other I/O error occurred. |
|
inherited |
Enable or disable the SO_KEEPALIVE option on the socket.
enable | true to enable the option false to disable it. |
SocketException | If a socket error occurs. |
|
inherited |
Set the linger time for the socket.
timeout | The new linger time, in seconds; a negative value disables the linger feature. |
SocketException | If the linger time could not be changed. |
|
inherited |
Set the size of the socket's internal receive buffer.
size | The new size, in bytes. |
SocketException | If the size could not be changed. |
|
inherited |
Enable or disable the SO_REUSEADDR option on the socket.
This method may only be called before the socket has been initialized. This option is off by default in newly created sockets.
enable | true to enable the option false to disable it. |
SocketException | If a socket error occurs, or if the call was made after the socket was initialized. |
|
inherited |
Set the size of the socket's internal send buffer.
size | The new size, in bytes. |
SocketException | If the size could not be changed. |
|
inherited |
Enable or disable the Nagle algorithm for send coalescing.
enable | true to enable the Nagle algorithm (TCP_NODELAY off), false to disable it (TCP_NODELAY on). |
SocketException | If a socket error occurs. |
|
virtual |
Set the stream I/O timeout, in milliseconds.
A timeout of 0 indicates no timeout, and a timeout of -1 indicates infinite timeout (blocking I/O).
Reimplemented from Stream.
|
virtual |
Shut down the socket.
Closes the connection and destroys the underlying socket object.
Reimplemented from Socket.
|
virtualinherited |
Get the current (absolute) offset of the seek pointer.
IOException | If the stream does not support seeking, or if some other I/O error occurred. |
|
protectedinherited |
Wait for the socket to become ready for reading or writing.
mode | The wait mode; one of WaitWrite or WaitRead. |
IOException | On timeout or other error. |
|
virtual |
Write data to the stream from a raw buffer, until either the entire buffer is written or an error or timeout occurs.
buffer | A pointer to the buffer. |
buflen | The number of bytes to write. |
IOException | If an I/O error occurred. |
Reimplemented from Stream.
|
inlinevirtual |
Write data to the stream from a ByteBuffer.
buffer | The buffer to write. |
IOException | If an I/O error occurred. |
Reimplemented from Stream.
|
inlinevirtual |
Write data to the stream from a CharBuffer.
buffer | The buffer to write. |
IOException | If an I/O error occurred. |
Reimplemented from Stream.
|
inline |
|
inline |
|
virtual |
Write data to the stream from a series of I/O buffers.
This operation is known as "vector write" or "gather write." The buffers are written in order. On systems that support it, the operation is performed with a single system call.
vec | An array of buffers to write. |
count | The number of buffers in the array. |
IOException | If an I/O error occurred. |
Reimplemented from Stream.
|
inlineinherited |
Write data to the stream from a Buffer of arbitrary type.
If sizeof(T) is greater than 1 byte, then there can be no guarantee that the number of bytes written before a timeout or error occurs will be evenly divisible by sizeof(T). If the final element was partially written, the number of bytes of the element that were written will be stored in partial; this value should be passed unmodified to the next invocation of this method to continue writing from the appropriate offset within the partially-written element.
buffer | The buffer to write. |
partial | The number of bytes remaining to write for a partially-written element; on return, the number of bytes written of a partially-written element, or 0 if the last element was written completely. Should be set to 0 prior to the first call to this method for a fresh buffer. |
IOException | If an I/O error occurred. |
|
virtualinherited |
Write data to the stream from a ByteBuffer, until either the entire buffer is written or an error or timeout occurs.
buffer | The buffer to write. |
IOException | If and I/O error occurred. |
|
virtualinherited |
Write data to the stream from a CharBuffer, until either the entire buffer is written or an error or timeout occurs.
buffer | The buffer to write. |
IOException | If an I/O error occurred. |
|
inlineinherited |
Write data to the stream from a Buffer of arbitrary type, until either the entire buffer is written or an error or timeout occurs.
If sizeof(T) is greater than 1 byte, then there can be no guarantee that the number of bytes written before a timeout or error occurs will be evenly divisible by sizeof(T). If the final element was partially written, the number of bytes of the element that were written will be stored in partial; this value should be passed unmodified to the next invocation of this method to continue writing from the appropriate offset within the partially-written element.
buffer | The buffer to write. |
partial | The number of bytes remaining to write for a partially-written element; on return, the number of bytes written of a partially-written element, or 0 if the last element was written completely. Should be set to 0 prior to the first call to this method for a fresh buffer. |
IOException | If an I/O error occurred. |
|
virtualinherited |
Write data to the stream from a raw buffer.
buffer | A pointer to the buffer. |
buflen | The number of bytes to write. |
IOException | If an I/O error occurred. |
void writeOOB | ( | byte_t | data | ) |
Write a byte of out-of-band data to the socket.
data | The byte of OOB data to write. |
IOException | If an I/O error occurs. |
|
friend |
|
protectedinherited |
The local address.
|
protectedinherited |
The remote address.
|
protectedinherited |
A handle to the socket itself.
|
protectedinherited |
The timeout value.
|
protectedinherited |
The socket type.
|
staticinherited |
The maximum number of I/O buffers that can be passed to the vector I/O methods.