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

bool isSocketSelected (fd_set &set)
 
void addToFDSet (fd_set *set, int &maxfd)
 
virtual ~UT_NetSocket ()
 
virtual int64 getMemoryUsage (bool inclusive) const
 
virtual UT_NetSocketaccept (int 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 (char *host, int &port) const
 
virtual int setBlocking (bool blocking)
 
bool isBlocking () const
 
void setNoDelay (int usenodelay)
 
virtual int isValid ()
 
int isConnected ()
 
int getSocket () const
 
uint32 getRemoteIP4 () const
 
bool getRemoteIP4 (int ip[4])
 
uint32 getLocalIP4 () const
 
bool getLocalIP4 (int ip[4])
 

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 int getHostAddress (unsigned char address[4], const char *hostname=0)
 
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_NetSocketnewSocket (int port, bool blocking=false, bool portisonlyhint=false)
 
static UT_NetSocketnewSocketFromAddr (const char *address, int port, bool blocking=false, int localport=-1, int forcesocket=0)
 
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, bool forcesocket=false)
 
static int selectInMS (int maxfd, fd_set *r_set, fd_set *w_set, fd_set *e_set, int timeoutms=0, bool forcesocket=false)
 

Protected Member Functions

int closeInetSocket ()
 
 UT_NetSocket (UT_NetSocket *netsocket, int socket, bool blocking)
 
 UT_NetSocket (int port, bool blocking=false, bool portisonlyhint=false)
 
 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
 
uint32 myRemoteIP4
 
unsigned char myIsServer:1
 
unsigned char myConnected:1
 
unsigned char myShmFlag:1
 
unsigned char myTermOnLost:1
 
unsigned char myIsBlocking:1
 

Detailed Description

Definition at line 28 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 31 of file UT_NetSocket.h.

anonymous enum
Enumerator
UT_SHUTDOWN_RECEIVE 
UT_SHUTDOWN_SEND 
UT_SHUTDOWN_BOTH 

Definition at line 43 of file UT_NetSocket.h.

Constructor & Destructor Documentation

virtual UT_NetSocket::~UT_NetSocket ( )
virtual
UT_NetSocket::UT_NetSocket ( UT_NetSocket netsocket,
int  socket,
bool  blocking 
)
protected
UT_NetSocket::UT_NetSocket ( int  port,
bool  blocking = false,
bool  portisonlyhint = false 
)
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_NetSocket* UT_NetSocket::accept ( int  blocking,
int condition 
)
virtual
void UT_NetSocket::addToFDSet ( fd_set *  set,
int maxfd 
)
virtual int UT_NetSocket::close ( )
virtual
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 216 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 int UT_NetSocket::getHostAddress ( unsigned char  address[4],
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
uint32 UT_NetSocket::getLocalIP4 ( ) const
bool UT_NetSocket::getLocalIP4 ( int  ip[4])
virtual int64 UT_NetSocket::getMemoryUsage ( bool  inclusive) const
virtual
int UT_NetSocket::getPort ( ) const
inline

Definition at line 219 of file UT_NetSocket.h.

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

Definition at line 241 of file UT_NetSocket.h.

bool UT_NetSocket::getRemoteIP4 ( int  ip[4])
virtual int UT_NetSocket::getRemoteSocket ( char *  host,
int port 
) const
virtual
int UT_NetSocket::getSocket ( ) const
bool UT_NetSocket::isBlocking ( ) const
inline

Definition at line 228 of file UT_NetSocket.h.

int UT_NetSocket::isConnected ( )
bool UT_NetSocket::isSocketSelected ( fd_set &  set)
virtual int UT_NetSocket::isValid ( )
virtual
static int UT_NetSocket::mapToUnprivilegedPort ( int  port)
static
static 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_NetSocket* UT_NetSocket::newSocketFromAddr ( const char *  address,
int  port,
bool  blocking = false,
int  localport = -1,
int  forcesocket = 0 
)
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
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,
bool  forcesocket = false 
)
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.

WARNING: the following select will not work on UT_PipeSocket instance for write and error sets because there is no system call that probes these states. UT_PipeSocket derives from UT_NetSocket, so be careful

Use it only on sockets!

static int UT_NetSocket::selectInMS ( int  maxfd,
fd_set *  r_set,
fd_set *  w_set,
fd_set *  e_set,
int  timeoutms = 0,
bool  forcesocket = false 
)
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)
virtual int UT_NetSocket::shutdown ( int  type)
virtual
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 298 of file UT_NetSocket.h.

int UT_NetSocket::myAddressPort
protected

Definition at line 299 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myConnected
protected

Definition at line 304 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myIsBlocking
protected

Definition at line 304 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myIsServer
protected

Definition at line 304 of file UT_NetSocket.h.

uint32 UT_NetSocket::myRemoteIP4
protected

Definition at line 301 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myShmFlag
protected

Definition at line 304 of file UT_NetSocket.h.

int UT_NetSocket::mySocket
protected

Definition at line 300 of file UT_NetSocket.h.

unsigned char UT_NetSocket::myTermOnLost
protected

Definition at line 304 of file UT_NetSocket.h.


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