Evo C++ Library v0.5.1
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | List of all members
Socket Class Reference

#include <evo/iosock.h>

Inheritance diagram for Socket:
Inheritance graph
[legend]

Detailed Description

Socket I/O stream.

Methods
Example for client
#include <evo/iosock.h>
using namespace evo;
int main() {
const ushort PORT = 12345;
Socket client;
client.connect_ip("localhost", PORT);
// ...
return 0;
}
Example for server
#include <evo/iosock.h>
using namespace evo;
int main() {
const ushort PORT = 12345;
Socket server, client;
server.listen_ip(PORT);
while (server.accept(client)) {
// ...
client.close();
}
return 0;
}

Public Types

typedef Stream< IoSocketBase
 Base class alias (used internally) More...
 
typedef IoSocket ::ExceptionInT ExceptionInT
 Stream input exception type More...
 
typedef IoSocket ::ExceptionOutT ExceptionOutT
 Stream output exception type More...
 
typedef StreamFormatter< ThisFormat
 Stream output formatter with state. More...
 
typedef IoSocket ::Handle Handle
 Low-level handle type (OS dependent) More...
 
typedef This Out
 Type returned by write_out() More...
 
typedef ulong Size
 Data size type (ulong) More...
 
typedef Stream< IoSocketThis
 This stream type. More...
 

Public Member Functions

 Socket (Newline nl=NL_SYS, bool exceptions=1)
 Constructor. More...
 
 Socket (bool exceptions)
 Constructor. More...
 
bool accept (Socket &client_socket, SocketAddressBase *client_address=NULL)
 Accept connection from listening socket. More...
 
void attach (Open mode, Handle handle, bool owned=true, bool flushlines=false)
 Attach existing stream. More...
 
RawBufferbufread ()
 Advanced: Access primary read buffer. More...
 
IoWriterbufwrite ()
 Advanced: Access primary write buffer. More...
 
bool close ()
 Close stream. More...
 
bool connect_ip (const char *host, ushort port, int family=AF_INET)
 Connect TCP socket to host address. More...
 
bool connect_ip (const SubString &host, ushort port, int family=AF_INET)
 Connect TCP socket to host address. More...
 
bool connect_ud (const SubString &path)
 Connect to Unix Domain socket at file path (linux/unix). More...
 
Handle detach ()
 Detach current stream. More...
 
IoSocketdevice ()
 Access low-level I/O device for socket. More...
 
Error error () const
 Get error code from last operation. More...
 
template<class TOut >
TOut & errormsg_out (TOut &out)
 Write detailed error message with errno to output stream/string. More...
 
bool excep () const
 Get whether exceptions are enabled. More...
 
void excep (bool val)
 Set whether exceptions are enabled. More...
 
bool finish ()
 Finish (shutdown) socket input and output (reads and writes). More...
 
bool finish_in ()
 Finish (shutdown) socket input (reads). More...
 
bool finish_out ()
 Finish (shutdown) socket output (writes). More...
 
bool flush ()
 Flush any pending output in stream write buffer, if buffered. More...
 
template<class T >
T * get_opt (int level, int optname, T *buf)
 Get socket option value. More...
 
Long get_opt_num (int level, int optname)
 Get socket option flag or numeric value. More...
 
bool get_resolve () const
 Get whether full resolver is used when resolving addresses. More...
 
ulong get_timeout () const
 Get timeout for socket operations. More...
 
Handle handle () const
 Get stream handle for low-level calls. More...
 
bool isopen () const
 Get whether stream is open. More...
 
bool listen_ip (const SubString &host, ushort port, int family=AF_INET, int backlog=BACKLOG_DEFAULT)
 Create and bind TCP socket on host interface and listen for connections. More...
 
bool listen_ip (ushort port, int family=AF_INET, int backlog=BACKLOG_DEFAULT)
 Create and bind TCP socket on all interfaces and listen for connections. More...
 
bool listen_ud (const SubString &path, int backlog=BACKLOG_DEFAULT)
 Create and bind Unix Domain socket to file path and listen for connections (linux/unix). More...
 
 operator SafeBoolType () const
 Safe (explicit) evaluation as bool type. More...
 
bool operator! () const
 Negation operator checks whether an error was set by a previous operation. More...
 
Thisoperator<< (This &)
 No-op used by formatting helpers. More...
 
Thisoperator<< (Newline nl)
 Write an explicit newline and flush stream. More...
 
Thisoperator<< (NewlineDefault nl)
 Write default newline and flush stream. More...
 
Thisoperator<< (const NewlineValue &nl)
 Write newline value and flush stream. More...
 
Thisoperator<< (Flush)
 Flush buffer by writing to stream. More...
 
Thisoperator<< (bool val)
 Append a bool value to stream. More...
 
Thisoperator<< (char ch)
 Write character to stream. More...
 
Thisoperator<< (const char *str)
 Write terminated string to stream. More...
 
Thisoperator<< (const SubString &str)
 Write substring to stream. More...
 
Thisoperator<< (const ListBase< char, TSize > &str)
 Write substring to stream. More...
 
Thisoperator<< (int num)
 Write formatted number to stream. More...
 
Thisoperator<< (long num)
 Write formatted number to stream. More...
 
Thisoperator<< (longl num)
 Write formatted number to stream. More...
 
Thisoperator<< (uint num)
 Write formatted number to stream. More...
 
Thisoperator<< (ulong num)
 Write formatted number to stream. More...
 
Thisoperator<< (ulongl num)
 Write formatted number to stream. More...
 
Thisoperator<< (const IntegerT< U > &num)
 Write formatted class number to stream. More...
 
Thisoperator<< (float num)
 Write formatted floating-point number to stream. More...
 
Thisoperator<< (double num)
 Write formatted floating-point number to stream. More...
 
Thisoperator<< (ldouble num)
 Write formatted floating-point number to stream. More...
 
Thisoperator<< (const FloatT< U > &num)
 Write formatted class floating-point number to stream. More...
 
Thisoperator<< (const FmtChar &fmt)
 Write repeated character to stream. More...
 
Thisoperator<< (const FmtString &fmt)
 Write formatted string field to stream. More...
 
Thisoperator<< (const FmtShort &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtInt &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtLong &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtLongL &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtUShort &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtUInt &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtULong &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtULongL &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtFloat &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtFloatD &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtFloatL &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtFieldNum< U > &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtFieldFloat< U > &fmt)
 Write formatted number field to stream. More...
 
Thisoperator<< (const FmtPtr &fmtptr)
 Writer formatted pointer field to stream. More...
 
Thisoperator<< (const FmtDump &fmt)
 Write formatted data dump to stream. More...
 
ulong readbin (void *buf, ulong size)
 Read binary input from stream. More...
 
bool readline (String &str, ulong maxlen=0)
 Read text line input from stream. More...
 
ulong readtext (char *buf, ulong size)
 Read text input from stream. More...
 
template<class T >
bool set_opt (int level, int optname, const T &val)
 Set socket option value. More...
 
bool set_opt_num (int level, int optname, long val)
 Set socket option flag or numeric value. More...
 
Socketset_resolve (bool enable)
 Set whether full resolver is used when resolving addresses. More...
 
Socketset_timeout (ulong timeout_ms)
 Set timeout for socket operations. More...
 
char * write_direct (Size size)
 Get pointer for writing directly to buffer to append data. More...
 
bool write_direct_finish (Size size)
 Finish writing directly to buffer. More...
 
char * write_direct_flush (Size &available, Size written_size, Size reserve_size)
 
char * write_direct_multi (Size &available, Size reserve_size)
 
Outwrite_out ()
 
ulong writebin (const void *buf, ulong size)
 Write binary output to stream. More...
 
ulong writechar (char ch, ulong count=1)
 Write repeat character as text output to stream. More...
 
bool writefmtchar (char ch, ulong count, const FmtSetField &field)
 Write formatted and/or repeated character. More...
 
bool writefmtdump (const FmtDump &fmt)
 Write formatted data dump. More...
 
bool writefmtdump (const FmtDump &fmt, Newline nl)
 Write formatted data dump with explicit newline type. More...
 
bool writefmtnum (TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL)
 Write formatted signed number with field alignment. More...
 
bool writefmtnumf (TNum num, const FmtSetFloat &fmt, const FmtSetField *field=NULL)
 Write formatted floating point number with field alignment. More...
 
bool writefmtnumu (TNum num, const FmtSetInt &fmt, const FmtSetField *field=NULL)
 Write formatted unsigned number with field alignment. More...
 
bool writefmtstr (const char *buf, ulong size, const FmtSetField &field)
 Write text with field alignment. More...
 
ulong writeline (const char *buf, ulong size)
 Write text line output to stream. More...
 
bool writenum (TNum num, int base=fDEC)
 Write formatted signed number. More...
 
bool writenumf (TNum num, int precision=fPREC_AUTO)
 Write formatted floating-point number. More...
 
bool writenumu (TNum num, int base=fDEC)
 Write formatted unsigned number. More...
 
Size writequoted (const char *buf, Size size, char delim, bool optional=false)
 Write quoted text output to string. More...
 
ulong writetext (const char *buf, ulong size)
 Write text output to stream. More...
 

Static Public Member Functions

static void sysinit ()
 Initialize socket library. More...
 

Static Public Attributes

static const int BACKLOG_DEFAULT = 5
 Default backlog queue size. More...
 

Protected Types

enum  RwLast
 Current read/write mode. More...
 

Protected Member Functions

void init (Open mode, bool flushlines=false)
 Initialize and reset buffers for a new stream. More...
 

Protected Attributes

IoReader bufrd_
 Buffered reader. More...
 
IoWriter bufwr_
 Buffered writer. More...
 
IoSocket device_
 I/O device. More...
 
Error error_
 Last error code. More...
 
bool excep_
 Whether to throw exceptions. More...
 
bool owned_
 Whether handle is owned (to be closed here) More...
 
RwLast rwlast_
 Read/Write: Used to track last operation when switching between read/write. More...
 
ulongl savepos_
 Read/Write: Used to save buffered read position when switching between read/write. More...
 

Member Typedef Documentation

◆ Base

typedef Stream<IoSocket> Base

Base class alias (used internally)

◆ ExceptionInT

typedef IoSocket ::ExceptionInT ExceptionInT
inherited

Stream input exception type

◆ ExceptionOutT

typedef IoSocket ::ExceptionOutT ExceptionOutT
inherited

Stream output exception type

◆ Format

typedef StreamFormatter<This> Format
inherited

Stream output formatter with state.

  • This is associated with a Stream object and supports stateful (i.e. sticky) output formatting with "<<" operator on this object
  • Formatting attributes include:
    • Aligned fields for formatting text "columns"
    • Integer base, prefix, and padding (in addition to field padding)
    • Floating point precision and padding (in additon to field padding)
    • String to use for "null" values (null char*, String, Int, etc)
  • Note: Single character (char) and Newline (NL) values are not formatted as fields (i.e. not padded) since they're usually delimiters
  • See: Stream Formatting
Examples

Example using File:

#include <evo/io.h>
using namespace evo;
int main() {
File file("file.txt", oWRITE_NEW);
// Use a temporary formatter to write line: 7B,1C8
File::Format(file) << fHEX << 123 << ',' << 456 << NL;
// Create a formatter and use to write line: 001,002
File::Format out(file);
out << FmtSetInt(fDEC, 3) << 1 << ',' << 2 << NL;
return 0;
}

Example using Console (stdout):

#include <evo/io.h>
using namespace evo;
int main() {
Console& c = con();
// Use a temporary formatter to write line: 7B,1C8
Console::Format(c.out) << fHEX << 123 << ',' << 456 << NL;
// Create a formatter and use to write line: 001,002
out << FmtSetInt(fDEC, 3) << 1 << ',' << 2 << NL;
return 0;
}

For more examples see: Stream Formatting

◆ Handle

typedef IoSocket ::Handle Handle
inherited

Low-level handle type (OS dependent)

◆ Out

typedef This Out
inherited

Type returned by write_out()

◆ Size

typedef ulong Size
inherited

Data size type (ulong)

◆ This

typedef Stream<IoSocket > This
inherited

This stream type.

Member Enumeration Documentation

◆ RwLast

enum RwLast
protectedinherited

Current read/write mode.

Constructor & Destructor Documentation

◆ Socket() [1/2]

Socket ( Newline  nl = NL_SYS,
bool  exceptions = 1 
)
inline

Constructor.

Parameters
nlDefault newline value to use for text reads/writes
exceptionsWhether to enable exceptions on error, default set by Evo config: EVO_EXCEPTIONS

◆ Socket() [2/2]

Socket ( bool  exceptions)
inline

Constructor.

Parameters
exceptionsWhether to enable exceptions on error, default set by Evo config: EVO_EXCEPTIONS

Member Function Documentation

◆ accept()

bool accept ( Socket client_socket,
SocketAddressBase client_address = NULL 
)
inline

Accept connection from listening socket.

  • Socket must be in listen mode, see: listen_ip(), listen_ud()
  • If client_address is specified, it must be the correct type for the socket, otherwise results are implementation dependent (may truncated the address or return an error)
  • See the listen method used for the expected socket address type
Parameters
client_socketAttached to connected client socket on success (unchanged on error)
client_addressPointer used to set connected client address on success (invalidated on error), NULL to skip
Returns
Whether connection successfully accepted, false on error

◆ attach()

void attach ( Open  mode,
Handle  handle,
bool  owned = true,
bool  flushlines = false 
)
inlineinherited

Attach existing stream.

Parameters
modeAccess mode to use (oREAD, oREAD_WRITE, or oWRITE), must be correct for handle
handleHandle to attach, must be valid
ownedWhether to take ownership and close handle, false detaches on close()
flushlinesWhether to flush text output on newlines (line buffering)

◆ bufread()

RawBuffer& bufread ( )
inlineinherited

Advanced: Access primary read buffer.

  • Caution: This returns a low-level interface that must be used properly
  • Note: Other buffers may be involved if filters are used
  • This never throws any exception
Returns
Read buffer

◆ bufwrite()

IoWriter& bufwrite ( )
inlineinherited

Advanced: Access primary write buffer.

  • Caution: This returns a low-level interface that must be used properly
  • Note: Other buffers may be involved if filters are used
  • This never throws any exception
Returns
Write buffer

◆ close()

bool close ( )
inlineinherited

Close stream.

  • This will flush output before closing
  • Never throws an exception, even if flush or close fails
Returns
Whether successful, false on flush error (stream will still close)

◆ connect_ip() [1/2]

bool connect_ip ( const char *  host,
ushort  port,
int  family = AF_INET 
)
inline

Connect TCP socket to host address.

  • This blocks while connecting, and could take a while for the implementation to timeout
  • This resolves the host address (without lookup) and port and connects a socket to it
Parameters
hostHost address to resolve (terminated string)
portPort number to use
familyProtocol family to use: AF_INET for IPv4, AF_INET6 for IPv6
Returns
Whether successful, false on error

◆ connect_ip() [2/2]

bool connect_ip ( const SubString host,
ushort  port,
int  family = AF_INET 
)
inline

Connect TCP socket to host address.

  • This blocks while connecting, and could take a while for the implementation to timeout
  • This resolves the host address (without lookup) and port and connects a socket to it
Parameters
hostHost address to resolve
portPort number to use
familyProtocol family to use: AF_INET for IPv4, AF_INET6 for IPv6
Returns
Whether successful, false on error

◆ connect_ud()

bool connect_ud ( const SubString path)
inline

Connect to Unix Domain socket at file path (linux/unix).

  • Not supported in Windows – fails with ENotImpl
  • Note that the path length limit is lower than normal file paths
    • Limit is usually between 92 - 108 (exclusive), depending on the system
    • This returns error ESize if path is too long
Parameters
pathFile path for Unix Domain socket
Returns
Whether successful, false on error

◆ detach()

Handle detach ( )
inlineinherited

Detach current stream.

  • This will flush output before detaching
  • Never throws an exception, even if flush fails
Returns
Detached handle

◆ device()

IoSocket& device ( )
inline

Access low-level I/O device for socket.

Returns
Reference to underlying IoSocket

◆ error()

Error error ( ) const
inlineinherited

Get error code from last operation.

Returns
Error code, ENone for success (no error)

◆ errormsg_out()

TOut& errormsg_out ( TOut &  out)
inline

Write detailed error message with errno to output stream/string.

  • Must call right after the error, otherwise errno may be out of date
  • This includes the system formatted message for errno, if applicable
Parameters
outStream or String to write output to
Returns
out

◆ excep() [1/2]

bool excep ( ) const
inlineinherited

Get whether exceptions are enabled.

Returns
Whether exceptions enabled

◆ excep() [2/2]

void excep ( bool  val)
inlineinherited

Set whether exceptions are enabled.

Parameters
valWhether to enable exceptions

◆ finish()

bool finish ( )
inline

Finish (shutdown) socket input and output (reads and writes).

  • This is a gracefull way to stop input and output at this point
    • Input already received is still readable
  • Only use under normal conditions (not on communication error)
  • This doesn't throw any exception (if enabled) on error since an error closing communication doesn't block progress
Returns
Whether successful, false on error (not connected, invalid, etc)

◆ finish_in()

bool finish_in ( )
inline

Finish (shutdown) socket input (reads).

  • This is a gracefull way to stop input at this point
    • Input already received is still readable
  • Only use under normal conditions (not on communication error)
  • This doesn't throw any exception (if enabled) on error since an error closing communication doesn't block progress
Returns
Whether successful, false on error (not connected, invalid, etc)

◆ finish_out()

bool finish_out ( )
inline

Finish (shutdown) socket output (writes).

  • This is a gracefull way to stop output at this point
  • Only use under normal conditions (not on communication error)
  • This doesn't throw any exception (if enabled) on error since an error closing communication doesn't block progress
Returns
Whether successful, false on error (not connected, invalid, etc)

◆ flush()

bool flush ( )
inlinevirtualinherited

Flush any pending output in stream write buffer, if buffered.

  • This is a no-op if writes aren't buffered
  • Call error() to check error code
Returns
Whether successful, false on error

Reimplemented from IoBase.

◆ get_opt()

T* get_opt ( int  level,
int  optname,
T *  buf 
)
inline

Get socket option value.

Parameters
levelProtocol level ID (SOL_SOCKET, IPPROTO_IP, etc)
optnameOption name ID
bufBuffer to store option value
Returns
Value pointer, NULL on error – must be correct type for level and optname
Template Parameters
TOption value type – results are system dependent on incorrect type for option, may return an error or invalid value

◆ get_opt_num()

Long get_opt_num ( int  level,
int  optname 
)
inline

Get socket option flag or numeric value.

Parameters
levelProtocol level ID (SOL_SOCKET, IPPROTO_IP, etc)
optnameOption name ID
Returns
Value, null on error

◆ get_resolve()

bool get_resolve ( ) const
inline

Get whether full resolver is used when resolving addresses.

  • This sets how listen and connect methods resolve addresses:
    • Enabled means listen and connect methods may call external services (like DNS) to resolve an address, which may block
    • Otherwise addresses passed to listen and connect methods are assumed to be numeric, so simply need conversion to resolve
  • Full resolving is enabled by default
Returns
Whether full resolving enabled

◆ get_timeout()

ulong get_timeout ( ) const
inline

Get timeout for socket operations.

  • This applies to read, write, and accept calls, but doesn't apply to connect calls
Returns
Timeout in milliseconds, 0 for indefinite

◆ handle()

Handle handle ( ) const
inlineinherited

Get stream handle for low-level calls.

Returns
Stream handle

◆ init()

void init ( Open  mode,
bool  flushlines = false 
)
inlineprotectedinherited

Initialize and reset buffers for a new stream.

◆ isopen()

bool isopen ( ) const
inlinevirtualinherited

Get whether stream is open.

Returns
Whether open

Reimplemented from IoBase.

◆ listen_ip() [1/2]

bool listen_ip ( const SubString host,
ushort  port,
int  family = AF_INET,
int  backlog = BACKLOG_DEFAULT 
)
inline

Create and bind TCP socket on host interface and listen for connections.

  • Use SocketAddressIp* for client_address with accept()
Parameters
hostHost address string to bind to (terminated string)
portPort number to bind to and listen on
familyProtocol family to use: AF_INET for IPv4, AF_INET6 for IPv6
backlogListener queue backlog size
Returns
Whether successful, false on error

◆ listen_ip() [2/2]

bool listen_ip ( ushort  port,
int  family = AF_INET,
int  backlog = BACKLOG_DEFAULT 
)
inline

Create and bind TCP socket on all interfaces and listen for connections.

Parameters
portPort number to bind to and listen on
familyProtocol family to use: AF_INET for IPv4, AF_INET6 for IPv6
backlogListener queue backlog size
Returns
Whether successful, false on error

◆ listen_ud()

bool listen_ud ( const SubString path,
int  backlog = BACKLOG_DEFAULT 
)
inline

Create and bind Unix Domain socket to file path and listen for connections (linux/unix).

  • Use SocketAddressUnix* for client_address with accept()
  • Not supported in Windows – fails with ENotImpl
  • Note that the path length limit is lower than normal file paths
    • Limit is usually between 92 - 108 (inclusive), depending on the system
    • This returns error ESize if path is too long
Parameters
pathFile path for Unix Domain socket
backlogListener queue backlog size
Returns
Whether successful, false on error

◆ operator SafeBoolType()

operator SafeBoolType ( ) const
inlineinherited

Safe (explicit) evaluation as bool type.

  • This is called when object is directly evaluated as a bool, and is equivalent to: !operator!()
  • See SafeBool

◆ operator!()

bool operator! ( ) const
inlineinherited

Negation operator checks whether an error was set by a previous operation.

  • Call error() to get last error code
  • Alternatively, use SafeBool evaluation to check whether previous operations were successful
Returns
Whether last operation set an error

◆ operator<<() [1/38]

This& operator<< ( This )
inlineinherited

No-op used by formatting helpers.

Returns
This

◆ operator<<() [2/38]

This& operator<< ( Newline  nl)
inlineinherited

Write an explicit newline and flush stream.

  • Use NL for default newline value, where default is set by the stream
Parameters
nlNewline type to write, NL for current default or NL_SYS for system default
Returns
This

◆ operator<<() [3/38]

This& operator<< ( NewlineDefault  nl)
inlineinherited

Write default newline and flush stream.

Parameters
nlDefault newline value (NL), where default is set by the stream
Returns
This

◆ operator<<() [4/38]

This& operator<< ( const NewlineValue nl)
inlineinherited

Write newline value and flush stream.

Parameters
nlNewline value to use, use current stream default if null
Returns
This

◆ operator<<() [5/38]

This& operator<< ( Flush  )
inlineinherited

Flush buffer by writing to stream.

  • This calls flush()
  • This is useful when writing a console prompt, flush so the prompt is immediately visible
Returns
This

◆ operator<<() [6/38]

This& operator<< ( bool  val)
inlineinherited

Append a bool value to stream.

  • Bool value is formatted as either "true" or "false" (without quotes)
  • This calls writetext()
Parameters
valBool value to append
Returns
This

◆ operator<<() [7/38]

This& operator<< ( char  ch)
inlineinherited

Write character to stream.

Parameters
chCharacter to write
Returns
This

◆ operator<<() [8/38]

This& operator<< ( const char *  str)
inlineinherited

Write terminated string to stream.

Parameters
strString to write, must be terminated
Returns
This

◆ operator<<() [9/38]

This& operator<< ( const SubString str)
inlineinherited

Write substring to stream.

Parameters
strSubstring to write
Returns
This

◆ operator<<() [10/38]

This& operator<< ( const ListBase< char, TSize > &  str)
inlineinherited

Write substring to stream.

  • This effectively calls writetext()
  • This handles all Evo character list or string types
Template Parameters
TSizeList size type (inferred from parameter)
Parameters
strSubstring to write
Returns
This

◆ operator<<() [11/38]

This& operator<< ( int  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [12/38]

This& operator<< ( long  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [13/38]

This& operator<< ( longl  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [14/38]

This& operator<< ( uint  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [15/38]

This& operator<< ( ulong  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [16/38]

This& operator<< ( ulongl  num)
inlineinherited

Write formatted number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [17/38]

This& operator<< ( const IntegerT< U > &  num)
inlineinherited

Write formatted class number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Template Parameters
UNumber class type (Int, Uint, etc)
Parameters
numNumber to format and write, no-op if null
Returns
This

◆ operator<<() [18/38]

This& operator<< ( float  num)
inlineinherited

Write formatted floating-point number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [19/38]

This& operator<< ( double  num)
inlineinherited

Write formatted floating-point number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [20/38]

This& operator<< ( ldouble  num)
inlineinherited

Write formatted floating-point number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
numNumber to format and write
Returns
This

◆ operator<<() [21/38]

This& operator<< ( const FloatT< U > &  num)
inlineinherited

Write formatted class floating-point number to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Template Parameters
UNumber class type (Float, FloatD, etc)
Parameters
numNumber to format and write, no-op if null
Returns
This

◆ operator<<() [22/38]

This& operator<< ( const FmtChar fmt)
inlineinherited

Write repeated character to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtCharacter info
Returns
This

◆ operator<<() [23/38]

This& operator<< ( const FmtString fmt)
inlineinherited

Write formatted string field to stream.

Parameters
fmtString info
Returns
This

◆ operator<<() [24/38]

This& operator<< ( const FmtShort fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [25/38]

This& operator<< ( const FmtInt fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [26/38]

This& operator<< ( const FmtLong fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [27/38]

This& operator<< ( const FmtLongL fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [28/38]

This& operator<< ( const FmtUShort fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [29/38]

This& operator<< ( const FmtUInt fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [30/38]

This& operator<< ( const FmtULong fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [31/38]

This& operator<< ( const FmtULongL fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [32/38]

This& operator<< ( const FmtFloat fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [33/38]

This& operator<< ( const FmtFloatD fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [34/38]

This& operator<< ( const FmtFloatL fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [35/38]

This& operator<< ( const FmtFieldNum< U > &  fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [36/38]

This& operator<< ( const FmtFieldFloat< U > &  fmt)
inlineinherited

Write formatted number field to stream.

  • This will flush current buffer if it's too full
  • This formats directly to write buffer so it must be large enough
Parameters
fmtNumber info
Returns
This

◆ operator<<() [37/38]

This& operator<< ( const FmtPtr fmtptr)
inlineinherited

Writer formatted pointer field to stream.

Parameters
fmtptrPointer info
Returns
This

◆ operator<<() [38/38]

This& operator<< ( const FmtDump fmt)
inlineinherited

Write formatted data dump to stream.

Parameters
fmtDump info
Returns
This

◆ readbin()

ulong readbin ( void *  buf,
ulong  size 
)
inlinevirtualinherited

Read binary input from stream.

  • This does a binary read – no conversion on newlines
  • Depending on the stream type, this may:
    • be a blocking call
    • read at least 1 byte, but less than requested
    • return a read error, some stream types may timeout
  • Call error() to check error code
  • This never throws any exception
Parameters
bufBuffer to store data read
sizeSize to read
Returns
Bytes read and stored in buf, 0 if end-of-stream or error

Reimplemented from IoBase.

◆ readline()

bool readline ( String str,
ulong  maxlen = 0 
)
inlinevirtualinherited

Read text line input from stream.

  • This will read and return the next line as a string, not including the newline
  • Depending on the stream type, this may:
    • be a blocking call
    • read at least 1 byte, but less than requested
    • return a read error
  • Call error() to check error code
  • Error codes:
    • EEnd if no more lines (end-of-file)
    • EOutOfBounds if line exceeds maxlen
    • ELoss if previous readtext() call left a pending partial newline pair that must be read first – this prevents data loss on the newline pair
      • Only occurs when readtext() was used with size=1 while reading a newline pair (and newline conversion to 2-byte newline pairs is used), and next readline() is called in the middle of the newline pair, triggering this special case
      • Resolve by calling readtext() again with size=1 to read the pending newline char, then resume reading as desired
      • Calling readtext() with size=1 followed by readline() on same stream is best avoided so this special case never happens
    • Other codes for read errors
  • This never throws any exception
Parameters
strString to store line (cleared first) [out]
maxlenMaximum line length, 0 for no limit
Returns
Whether successful, false if no more lines (end-of-file) or error

Reimplemented from StreamBase.

◆ readtext()

ulong readtext ( char *  buf,
ulong  size 
)
inlinevirtualinherited

Read text input from stream.

  • This does a text read, converting newlines as needed
  • Depending on the stream type, this may:
    • be a blocking call
    • read at least 1 byte, but less than requested
    • return a read error
  • Call error() to check error code
  • After calling this with size=1 (not recommended), calling readline() next may trigger a special case error in certain conditions – see readline() error code ELoss
Parameters
bufBuffer to store data read
sizeSize in bytes to read from file (must be positive)
Returns
Bytes read, 0 if end-of-file or error

Reimplemented from StreamBase.

◆ set_opt()

bool set_opt ( int  level,
int  optname,
const T &  val 
)
inline

Set socket option value.

Parameters
levelProtocol level ID (SOL_SOCKET, IPPROTO_IP, etc)
optnameOption name ID
valValue to set – must be correct type for level and optname
Returns
Whether successful, false on error
Template Parameters
TOption value type

◆ set_opt_num()

bool set_opt_num ( int  level,
int  optname,
long  val 
)
inline

Set socket option flag or numeric value.

Parameters
levelProtocol level ID (SOL_SOCKET, IPPROTO_IP, etc)
optnameOption name ID
valValue to set
Returns
Whether successful, false on error

◆ set_resolve()

Socket& set_resolve ( bool  enable)
inline

Set whether full resolver is used when resolving addresses.

  • This sets how listen and connect methods resolve addresses:
    • Enabled means listen and connect methods may call external services (like DNS) to resolve an address, which may block
    • Otherwise addresses passed to listen and connect methods are assumed to be numeric, so simply need conversion to resolve
  • Full resolving is enabled by default
Parameters
enableTrue to enable full resolving, false to disable
Returns
This

◆ set_timeout()

Socket& set_timeout ( ulong  timeout_ms)
inline

Set timeout for socket operations.

  • This applies to read, write, and accept calls, but doesn't apply to connect calls
Parameters
timeout_msTimeout in milliseconds, 0 for indefinite
Returns
This

◆ sysinit()

static void sysinit ( )
inlinestatic

Initialize socket library.

  • This should be called near the beginning of the process to load the socket library
  • Implementation is OS specific, used in Windows to load WinSock DLL

◆ write_direct()

char* write_direct ( Size  size)
inlinevirtualinherited

Get pointer for writing directly to buffer to append data.

  • Call write_direct_finish() to commit written data, or don't to cancel
  • This will flush buffer to make room, if needed
  • Newlines in data written aren't converted
Parameters
sizeRequred size in bytes to reserve
Returns
Buffer to write to (at append position), NULL on error or if buffer not large enough or if not supported

Reimplemented from StreamBase.

◆ write_direct_finish()

bool write_direct_finish ( Size  size)
inlinevirtualinherited

Finish writing directly to buffer.

Parameters
sizeSize written in bytes, must not be greater than size passed to write_direct()
Returns
Whether successful, false if not supported

Reimplemented from StreamBase.

◆ write_direct_flush()

char* write_direct_flush ( Size available,
Size  written_size,
Size  reserve_size 
)
inlineinherited

◆ write_direct_multi()

char* write_direct_multi ( Size available,
Size  reserve_size 
)
inlineinherited

◆ write_out()

Out& write_out ( )
inlineinherited

◆ writebin()

ulong writebin ( const void *  buf,
ulong  size 
)
inlinevirtualinherited

Write binary output to stream.

  • This does a binary write – no conversion on newlines
  • Depending on the stream type, this may:
    • be a blocking call
    • write at least 1 byte, but less than requested
    • return a write error, some stream types may timeout
  • Call error() to check error code
Parameters
bufData to write
sizeSize to write
Returns
Bytes written, 0 on error

Reimplemented from IoBase.

◆ writechar()

ulong writechar ( char  ch,
ulong  count = 1 
)
inlinevirtualinherited

Write repeat character as text output to stream.

  • This does a text write, converting newlines as needed
    • Note that writing newline text characters 1 char at a time or by string will give the same end result either way – the edge cases are covered
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
chCharacter to write
countCharacter count to write, must be positive
Returns
Size actually written (should be the same as count), 0 on error

Reimplemented from StreamBase.

◆ writefmtchar()

bool writefmtchar ( char  ch,
ulong  count,
const FmtSetField field 
)
inlineinherited

Write formatted and/or repeated character.

Parameters
chCharacter to write
countCharacter repeat count to use
fieldField attributes to use
Returns
Whether successful, false on error

◆ writefmtdump() [1/2]

bool writefmtdump ( const FmtDump fmt)
inlineinherited

Write formatted data dump.

  • Output may span multiple lines, and always ends with a newline (unless dump data is empty)
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
fmtFormat data, including buffer to dump
Returns
Whether successful, false on error

◆ writefmtdump() [2/2]

bool writefmtdump ( const FmtDump fmt,
Newline  nl 
)
inlineinherited

Write formatted data dump with explicit newline type.

  • Output may span multiple lines, and always ends with a newline (unless dump data is empty)
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
fmtFormat data, including buffer to dump
nlNewline type to use, NL for current default or NL_SYS for system default
Returns
Whether successful, false on error

◆ writefmtnum()

bool writefmtnum ( TNum  num,
const FmtSetInt fmt,
const FmtSetField field = NULL 
)
inlineinherited

Write formatted signed number with field alignment.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
fmtInteger formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
Whether successful, false on error

◆ writefmtnumf()

bool writefmtnumf ( TNum  num,
const FmtSetFloat fmt,
const FmtSetField field = NULL 
)
inlineinherited

Write formatted floating point number with field alignment.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
fmtFloating point formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
Whether successful, false on error

◆ writefmtnumu()

bool writefmtnumu ( TNum  num,
const FmtSetInt fmt,
const FmtSetField field = NULL 
)
inlineinherited

Write formatted unsigned number with field alignment.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
fmtInteger formatting attributes to use
fieldField formatting attributes to use, NULL for none
Returns
Whether successful, false on error

◆ writefmtstr()

bool writefmtstr ( const char *  buf,
ulong  size,
const FmtSetField field 
)
inlineinherited

Write text with field alignment.

  • This does a text write, converting newlines as needed
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
bufBuffer to write from
sizeSize to write in bytes
fieldField attributes to use
Returns
Whether successful, false on error

◆ writeline()

ulong writeline ( const char *  buf,
ulong  size 
)
inlinevirtualinherited

Write text line output to stream.

  • This always writes the whole line followed by a newline on success
  • This does a text write, converting newlines as needed, which could turn into multiple lines
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
bufData buffer to write from
sizeData size to write in bytes, must be positive
Returns
Size actually written (including newline), 0 on error

Reimplemented from StreamBase.

◆ writenum()

bool writenum ( TNum  num,
int  base = fDEC 
)
inlineinherited

Write formatted signed number.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
baseBase to use for formatting
Returns
Whether successful, false on error

◆ writenumf()

bool writenumf ( TNum  num,
int  precision = fPREC_AUTO 
)
inlineinherited

Write formatted floating-point number.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
precisionFormatting precision (number of fractional digits), 0 for none, fPREC_AUTO for automatic
Returns
Whether successful, false on error

◆ writenumu()

bool writenumu ( TNum  num,
int  base = fDEC 
)
inlineinherited

Write formatted unsigned number.

  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Template Parameters
TNumNumber type, inferred by param
Parameters
numNumber to write
baseBase to use for formatting
Returns
Whether successful, false on error

◆ writequoted()

Size writequoted ( const char *  buf,
Size  size,
char  delim,
bool  optional = false 
)
inlinevirtualinherited

Write quoted text output to string.

  • Newlines and unprintable characters are written as-is
  • This uses Smart Quoting – see Smart Quoting
Parameters
bufData to quote and write
sizeData size to write
delimDelimiter for next field to escape via quoting
optionalWhether quoting is optional, true to avoid quoting if possible
Returns
Size actually written, 0 on error (write error or unquotable text)

Reimplemented from StreamBase.

◆ writetext()

ulong writetext ( const char *  buf,
ulong  size 
)
inlinevirtualinherited

Write text output to stream.

  • This does a text write, converting newlines as needed
  • Depending on the stream type, this may be a blocking call
  • If exceptions are disabled, call error() to check error code
Parameters
bufData to write
sizeSize to write
Returns
Size actually written, 0 on error

Reimplemented from StreamBase.

Member Data Documentation

◆ BACKLOG_DEFAULT

const int BACKLOG_DEFAULT = 5
static

Default backlog queue size.

◆ bufrd_

IoReader bufrd_
protectedinherited

Buffered reader.

◆ bufwr_

IoWriter bufwr_
protectedinherited

Buffered writer.

◆ device_

IoSocket device_
protectedinherited

I/O device.

◆ error_

Error error_
protectedinherited

Last error code.

◆ excep_

bool excep_
protectedinherited

Whether to throw exceptions.

◆ owned_

bool owned_
protectedinherited

Whether handle is owned (to be closed here)

◆ rwlast_

RwLast rwlast_
protectedinherited

Read/Write: Used to track last operation when switching between read/write.

◆ savepos_

ulongl savepos_
protectedinherited

Read/Write: Used to save buffered read position when switching between read/write.


The documentation for this class was generated from the following file: