HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_NetSocket Class Reference

#include <UT_NetSocket.h>

Public Types

enum  {
  UT_CONNECT_SUCCESS = 0, UT_WOULD_BLOCK = -1, UT_BAD_ADDRESS = -2, UT_CONNECT_FAILED = -3,
  UT_ERROR_OCCURED = -4, UT_WRONG_SOCKET = -5, UT_NO_CONNECTION = -6
}
 
enum  { UT_SHUTDOWN_RECEIVE = 0, UT_SHUTDOWN_SEND = 1, UT_SHUTDOWN_BOTH = 2 }
 

Public Member Functions

 UT_NetSocket (UT_NetSocket &sock)=delete
 
UT_NetSocketoperator= (UT_NetSocket &sock)=delete
 
bool isSocketSelected (fd_set &set)
 
void addToFDSet (fd_set *set, int &maxfd)
 
virtual ~UT_NetSocket ()
 
virtual int64 getMemoryUsage (bool inclusive) const
 
virtual UT_UniquePtr
< UT_NetSocket
accept (bool blocking, int &condition)
 
virtual int connect (int timeout_ms=0)
 
virtual int close ()
 
virtual int shutdown (int type)
 
virtual int write (const void *data, int len, int *numWritten=0)
 
virtual int dataAvailable (int timeout=0)
 
virtual int read (void *data, int len, int *numRead=0, int timeout_ms=-1)
 
virtual int read (UT_WorkBuffer &data, int timeout_ms=-1, bool wait_for_null_terminator=false)
 
virtual int peek (void *data, int len, int timeout_ms=-1)
 
virtual int flushRead ()
 
virtual int flushWrite ()
 
void terminateOnConnectionLost (bool term=true)
 
const char * getAddress () const
 
int getPort () const
 
virtual int getRemoteSocket (UT_IpAddressV4 &host, int &port) const
 
virtual int setBlocking (bool blocking)
 
bool isBlocking () const
 
void setNoDelay (int usenodelay)
 
void setQuickAck (int quickack)
 
virtual bool isValid () const
 
bool isServer () const
 
bool isConnected () const
 
int getSocket () const
 
const UT_IpAddressV4getRemoteIP4 () const
 
bool getRemoteIP4 (UT_IpAddressV4 &ip)
 
UT_IpAddressV4 getLocalIP4 () const
 
bool getLocalIP4 (UT_IpAddressV4 &ip)
 
SYS_NO_DISCARD_RESULT bool isLocalConnection () const
 
int sockRecv (void *data, int len, int *num_read=nullptr)
 

Static Public Member Functions

static const char * getErrorName (int code)
 
static int getPortByService (const char *service, const char *proto="tcp", int default_port=0)
 
static void getHostName (char *name, int max)
 
static bool getHostAddress (UT_IpAddressV4 &address, const char *hostname=0)
 
static bool getLocalAddresses (UT_Array< UT_IpAddress > &addresses, UT_IpAddressFamily family=UT_IpAddressFamily::IPv4)
 
static bool getLocalAddress (UT_IpAddress &address, UT_IpAddressFamily family=UT_IpAddressFamily::IPv4)
 
static bool getHostNameByAlias (UT_String &host, const char *alias=NULL)
 
static int mapToUnprivilegedPort (int port)
 
static bool sendCommandAndGetResult (int port, const char *command, UT_WorkBuffer &response, const char *host_name=0, bool remap_privileged_ports=true)
 
static bool nonBlockingSendCommandAndGetResult (int port, const char *command, UT_WorkBuffer &response, const char *host_name=0, bool remap_privileged_ports=true)
 
static UT_UniquePtr< UT_NetSocketnewSocket (int port, bool blocking=false, bool portisonlyhint=false)
 
static UT_UniquePtr< UT_NetSocketnewSocketFromAddr (const char *address, int port, bool blocking=false, int localport=-1)
 
static void fdZero (fd_set *set)
 
static void fdSet (int fd, fd_set *set)
 
static void fdClr (int fd, fd_set *set)
 
static int fdIsSet (int fd, fd_set *set)
 
static int select (int fd, fd_set *r_set, fd_set *w_set, fd_set *e_set, SYS_TimeVal *tv)
 
static int selectInMS (int maxfd, fd_set *r_set, fd_set *w_set, fd_set *e_set, int timeoutms=0)
 
static SYS_NO_DISCARD_RESULT bool isLocalConnection (int fd)
 

Protected Member Functions

int closeInetSocket ()
 
 UT_NetSocket (UT_NetSocket *netsocket, int socket, bool blocking)
 
 UT_NetSocket (const char *address, int port, bool blocking=false, int localport=-1)
 
 UT_NetSocket ()
 
int waitForDataOrTimeout (int timeout_ms)
 

Protected Attributes

char * myAddressName
 
int myAddressPort
 
int mySocket
 
UT_IpAddressV4 myRemoteIP4
 
unsigned char myIsServer:1
 
unsigned char myConnected:1
 
unsigned char myTermOnLost:1
 
unsigned char myIsBlocking:1
 

Detailed Description

Definition at line 32 of file UT_NetSocket.h.

Member Enumeration Documentation

anonymous enum
Enumerator
UT_CONNECT_SUCCESS 
UT_WOULD_BLOCK 
UT_BAD_ADDRESS 
UT_CONNECT_FAILED 
UT_ERROR_OCCURED 
UT_WRONG_SOCKET 
UT_NO_CONNECTION 

Definition at line 35 of file UT_NetSocket.h.

anonymous enum
Enumerator
UT_SHUTDOWN_RECEIVE 
UT_SHUTDOWN_SEND 
UT_SHUTDOWN_BOTH 

Definition at line 47 of file UT_NetSocket.h.

Constructor & Destructor Documentation

UT_NetSocket::UT_NetSocket ( UT_NetSocket sock)
delete
virtual UT_NetSocket::~UT_NetSocket ( )
virtual
UT_NetSocket::UT_NetSocket ( UT_NetSocket netsocket,
int  socket,
bool  blocking 
)
protected
UT_NetSocket::UT_NetSocket ( const char *  address,
int  port,
bool  blocking = false,
int  localport = -1 
)
protected
UT_NetSocket::UT_NetSocket ( )
protected

Member Function Documentation

virtual UT_UniquePtr<UT_NetSocket> UT_NetSocket::accept ( bool  blocking,
int condition 
)
virtual
void UT_NetSocket::addToFDSet ( fd_set *  set,
int maxfd 
)
virtual int UT_NetSocket::close ( )
inlinevirtual

Definition at line 176 of file UT_NetSocket.h.

int UT_NetSocket::closeInetSocket ( )
protected
virtual int UT_NetSocket::connect ( int  timeout_ms = 0)
virtual
virtual int UT_NetSocket::dataAvailable ( int  timeout = 0)
virtual
static void UT_NetSocket::fdClr ( int  fd,
fd_set *  set 
)
static
static int UT_NetSocket::fdIsSet ( int  fd,
fd_set *  set 
)
static
static void UT_NetSocket::fdSet ( int  fd,
fd_set *  set 
)
static
static void UT_NetSocket::fdZero ( fd_set *  set)
static
virtual int UT_NetSocket::flushRead ( )
virtual
virtual int UT_NetSocket::flushWrite ( )
virtual
const char* UT_NetSocket::getAddress ( ) const
inline

Definition at line 227 of file UT_NetSocket.h.

static const char* UT_NetSocket::getErrorName ( int  code)
static

Convertes the enum of UT_CONNECT_SUCCESS, etc, into an english error message roughly matching the enum's name. Be careful as this uses different errors different locations.

static bool UT_NetSocket::getHostAddress ( UT_IpAddressV4 address,
const char *  hostname = 0 
)
static
static void UT_NetSocket::getHostName ( char *  name,
int  max 
)
static
static bool UT_NetSocket::getHostNameByAlias ( UT_String host,
const char *  alias = NULL 
)
static
static bool UT_NetSocket::getLocalAddress ( UT_IpAddress address,
UT_IpAddressFamily  family = UT_IpAddressFamily::IPv4 
)
inlinestatic

Definition at line 74 of file UT_NetSocket.h.

static bool UT_NetSocket::getLocalAddresses ( UT_Array< UT_IpAddress > &  addresses,
UT_IpAddressFamily  family = UT_IpAddressFamily::IPv4 
)
static
UT_IpAddressV4 UT_NetSocket::getLocalIP4 ( ) const
bool UT_NetSocket::getLocalIP4 ( UT_IpAddressV4 ip)
virtual int64 UT_NetSocket::getMemoryUsage ( bool  inclusive) const
virtual
int UT_NetSocket::getPort ( ) const
inline

Definition at line 230 of file UT_NetSocket.h.

static int UT_NetSocket::getPortByService ( const char *  service,
const char *  proto = "tcp",
int  default_port = 0 
)
static
const UT_IpAddressV4& UT_NetSocket::getRemoteIP4 ( ) const
inline

Definition at line 257 of file UT_NetSocket.h.

bool UT_NetSocket::getRemoteIP4 ( UT_IpAddressV4 ip)
virtual int UT_NetSocket::getRemoteSocket ( UT_IpAddressV4 host,
int port 
) const
virtual
int UT_NetSocket::getSocket ( ) const
inline

Definition at line 255 of file UT_NetSocket.h.

bool UT_NetSocket::isBlocking ( ) const
inline

Definition at line 238 of file UT_NetSocket.h.

bool UT_NetSocket::isConnected ( ) const
inline

Definition at line 252 of file UT_NetSocket.h.

SYS_NO_DISCARD_RESULT bool UT_NetSocket::isLocalConnection ( ) const
static SYS_NO_DISCARD_RESULT bool UT_NetSocket::isLocalConnection ( int  fd)
static
bool UT_NetSocket::isServer ( ) const
inline

Definition at line 250 of file UT_NetSocket.h.

bool UT_NetSocket::isSocketSelected ( fd_set &  set)
virtual bool UT_NetSocket::isValid ( ) const
inlinevirtual

Definition at line 247 of file UT_NetSocket.h.

static int UT_NetSocket::mapToUnprivilegedPort ( int  port)
static
static UT_UniquePtr<UT_NetSocket> UT_NetSocket::newSocket ( int  port,
bool  blocking = false,
bool  portisonlyhint = false 
)
static

Creates a new listen socket on the specified port. A port of 0 will auto-choose a free port. Be careful overusing that, however, as Windows in particular has very few ports available by default. portisonlyhint will attempt to bind to the given port but if it fails (ie, already in use) will revert to using 0 to select a free port. The caller should thus double check the actual bound port if they care.

static UT_UniquePtr<UT_NetSocket> UT_NetSocket::newSocketFromAddr ( const char *  address,
int  port,
bool  blocking = false,
int  localport = -1 
)
static
static bool UT_NetSocket::nonBlockingSendCommandAndGetResult ( int  port,
const char *  command,
UT_WorkBuffer response,
const char *  host_name = 0,
bool  remap_privileged_ports = true 
)
static
UT_NetSocket& UT_NetSocket::operator= ( UT_NetSocket sock)
delete
virtual int UT_NetSocket::peek ( void data,
int  len,
int  timeout_ms = -1 
)
virtual
virtual int UT_NetSocket::read ( void data,
int  len,
int numRead = 0,
int  timeout_ms = -1 
)
virtual
virtual int UT_NetSocket::read ( UT_WorkBuffer data,
int  timeout_ms = -1,
bool  wait_for_null_terminator = false 
)
virtual
static int UT_NetSocket::select ( int  fd,
fd_set *  r_set,
fd_set *  w_set,
fd_set *  e_set,
SYS_TimeVal tv 
)
static

Performs a select() call on the socket.

Note, it turns out there is a discrepancy between how select() works on Linux vs. Windows.

On Windows, the select() call selects for reading the sockets even if they were closed by the remote counterparts. The rationale is that trying to read from such a socket returns zero, which is a signal that the connection was closed.

On Linux/OSX, select() call does not select such sockets for reading. The rationale is that there cannot be any more data for reading on closed sockets.

static int UT_NetSocket::selectInMS ( int  maxfd,
fd_set *  r_set,
fd_set *  w_set,
fd_set *  e_set,
int  timeoutms = 0 
)
static
static bool UT_NetSocket::sendCommandAndGetResult ( int  port,
const char *  command,
UT_WorkBuffer response,
const char *  host_name = 0,
bool  remap_privileged_ports = true 
)
static
virtual int UT_NetSocket::setBlocking ( bool  blocking)
virtual
void UT_NetSocket::setNoDelay ( int  usenodelay)
void UT_NetSocket::setQuickAck ( int  quickack)
virtual int UT_NetSocket::shutdown ( int  type)
virtual
int UT_NetSocket::sockRecv ( void data,
int  len,
int num_read = nullptr 
)
void UT_NetSocket::terminateOnConnectionLost ( bool  term = true)
int UT_NetSocket::waitForDataOrTimeout ( int  timeout_ms)
protected
virtual int UT_NetSocket::write ( const void data,
int  len,
int numWritten = 0 
)
virtual

Member Data Documentation

char* UT_NetSocket::myAddressName
protected

Definition at line 318 of file UT_NetSocket.h.

int UT_NetSocket::myAddressPort
protected

Definition at line 319 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myConnected
protected

Definition at line 324 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myIsBlocking
protected

Definition at line 324 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myIsServer
protected

Definition at line 324 of file UT_NetSocket.h.

UT_IpAddressV4 UT_NetSocket::myRemoteIP4
protected

Definition at line 321 of file UT_NetSocket.h.

int UT_NetSocket::mySocket
protected

Definition at line 320 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myTermOnLost
protected

Definition at line 324 of file UT_NetSocket.h.


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