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

#include <evo/iosock.h>

Inheritance diagram for SocketCast:
Inheritance graph
[legend]

Detailed Description

Socket for I/O casting (datagram/UDP).

Methods

Public Member Functions

 SocketCast (bool exceptions=1)
 Constructor. More...
 
 SocketCast (SocketAddressBase *address, bool exceptions=1)
 Constructor. More...
 
 ~SocketCast ()
 Destructor. More...
 
bool bind (const SocketAddressBase &address, int socktype=SOCK_DGRAM, int protocol=0)
 Create and bind datagram socket to address (read/write). More...
 
bool cast (const SocketAddressBase *address=NULL, int family=AF_UNSPEC, int socktype=SOCK_DGRAM, int protocol=0)
 Create unbound datagram socket for casting (write only). More...
 
void close ()
 Close 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...
 
virtual 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...
 
ulong get_timeout () const
 Get timeout for socket operations. More...
 
virtual bool isopen () const
 Get whether stream is open. 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...
 
ulong read (void *buf, ulong size, int flags=0, SocketAddressBase *address=NULL)
 Read message from socket. More...
 
ulong readbin (void *buf, ulong size)
 Read binary 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...
 
SocketCastset_timeout (ulong timeout_ms)
 Set timeout for socket operations. More...
 
ulong write (const void *buf, ulong size, int flags=0, const SocketAddressBase *address=NULL)
 Write message to socket. More...
 
ulong writebin (const void *buf, ulong size)
 Write binary output to stream. More...
 

Protected Attributes

Error error_
 Last error code. More...
 
bool excep_
 Whether to throw exceptions. More...
 

Constructor & Destructor Documentation

◆ SocketCast() [1/2]

SocketCast ( bool  exceptions = 1)
inline

Constructor.

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

◆ SocketCast() [2/2]

SocketCast ( SocketAddressBase address,
bool  exceptions = 1 
)
inline

Constructor.

Parameters
addressDefault address to cast (write) to, NULL for none – may be overridden by write()
exceptionsWhether to enable exceptions on error, default set by Evo config: EVO_EXCEPTIONS

◆ ~SocketCast()

~SocketCast ( )
inline

Destructor.

Member Function Documentation

◆ bind()

bool bind ( const SocketAddressBase address,
int  socktype = SOCK_DGRAM,
int  protocol = 0 
)
inline

Create and bind datagram socket to address (read/write).

Parameters
addressAddress to bind to – must be correct type for socket
socktypeSocket type value, defaults to standard UDP
protocolSocket protocol, 0 for default
Returns
Whether successful, false on error

◆ cast()

bool cast ( const SocketAddressBase address = NULL,
int  family = AF_UNSPEC,
int  socktype = SOCK_DGRAM,
int  protocol = 0 
)
inline

Create unbound datagram socket for casting (write only).

  • If address is specified, this will reference that pointer so it must remain valid
  • Unbound sockets can only cast (write only), use bind() for read/write
Parameters
addressDefault address to cast (write) to, NULL for none – may be overridden by write()
familySocket family type (AF_INET, AF_INET6), AF_UNSPEC to use family from address (if not NULL), or default to AF_INET (IPv4)
socktypeSocket type value, defaults to standard UDP
protocolSocket protocol, 0 for default
Returns
Whether successful, false on error

◆ close()

void close ( )
inline

Close socket.

◆ 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

◆ flush()

virtual 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 in Stream< T >, Stream< IoFile >, Stream< IoSocket >, StreamOut< T >, and StreamOut< IoFile >.

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

◆ isopen()

virtual bool isopen ( ) const
inlinevirtualinherited

Get whether stream is open.

Returns
Whether open

Reimplemented in Stream< T >, Stream< IoFile >, Stream< IoSocket >, StreamOut< T >, StreamOut< IoFile >, StreamIn< T >, and StreamIn< IoFile >.

◆ 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

◆ read()

ulong read ( void *  buf,
ulong  size,
int  flags = 0,
SocketAddressBase address = NULL 
)
inline

Read message from socket.

Parameters
bufBuffer to store message
sizeBuffer size in bytes
flagsFlags passed to recvfrom(), 0 for none
addressPointer to store source address (must be correct type for socket), NULL to skip
Returns
Message size read, 0 on error

◆ readbin()

ulong readbin ( void *  buf,
ulong  size 
)
inlinevirtual

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.

◆ 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_timeout()

SocketCast& 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

◆ write()

ulong write ( const void *  buf,
ulong  size,
int  flags = 0,
const SocketAddressBase address = NULL 
)
inline

Write message to socket.

Parameters
bufBuffer with message
sizeMessage size in bytes
flagsFlags passed to sendto(), 0 for none
addressTarget address to write to, NULL for default set by set_target()
Returns
Size sent (should match size), 0 on error

◆ writebin()

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

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.

Member Data Documentation

◆ error_

Error error_
protectedinherited

Last error code.

◆ excep_

bool excep_
protectedinherited

Whether to throw exceptions.


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