InetTCPMaster Class Reference

handle tcp connections. More...

#include <InetTCPMaster.h>

Inheritance diagram for InetTCPMaster:

[legend]
Collaboration diagram for InetTCPMaster:
[legend]
List of all members.

Public Types

enum  InetOSCAddrType { OSC_IN_IPv4, OSC_IN_IPv6 }
 address family type More...
enum  InetOSCProto { OSC_IN_UDP, OSC_IN_TCP }
 protocol type More...
enum  InetOSCError {
  OSC_IN_SUCCESS = 0x0000, OSC_IN_UNKNOWN = 0x0001, OSC_IN_NOT_IDLE = 0x0002, OSC_IN_WOULDBLOCK = 0x0003,
  OSC_IN_MAXCYCLES = 0x0004, OSC_IN_CLOSED_CONN = 0x0005, OSC_IN_PROTO_UNKNOWN = 0x0100, OSC_IN_ADDR_UNKNOWN = 0x0101,
  OSC_IN_NO_PROCESSOR = 0x0200, OSC_IN_NO_TRANSMGR = 0x0201, OSC_IN_NO_TCPMASTER = 0x0202, OSC_IN_ALREADY_SLAVE = 0x0203,
  OSC_IN_NOT_SLAVE = 0x0204, OSC_IN_GETHOST_ERRNO = 0xC000, OSC_IN_GETPROTO_ERRNO = 0xC001, OSC_IN_SOCKET_ERRNO = 0xC002,
  OSC_IN_SETSOCKOPT_ERRNO = 0xC003, OSC_IN_FCNTL_ERRNO = 0xC004, OSC_IN_BIND_ERRNO = 0xC005, OSC_IN_LISTEN_ERRNO = 0xC006,
  OSC_IN_CONNECT_ERRNO = 0xC007, OSC_IN_SHUTDOWN_ERRNO = 0xC008, OSC_IN_CLOSE_ERRNO = 0xC009, OSC_IN_RECV_ERRNO = 0xC00A,
  OSC_IN_SEND_ERRNO = 0xC00B, OSC_IN_POLL_ERRNO = 0xC00C, OSC_IN_ACCEPT_ERRNO = 0xC00D, OSC_IN_MALLOC_ERRNO = 0xC00E
}
 OSC Inet errors. More...

Public Member Functions

 InetTCPMaster (InetOSCAddrType addrType=OSC_IN_IPv4)
 constructor
virtual ~InetTCPMaster ()
 destructor
bool handle ()
 read incoming data from socket
bool transmit (const std::string &data)
 transmit outgoing data
const int getHandle () const
 return socket descriptor
bool process (const std::string &data, Transmit *reply)
 process an osc message by forwarding it to our processor
InetTCPSlave *const newconnection (const std::string &host, const short port, const InetOSCAddrType addrType=OSC_IN_IPv4)
 connect to host
bool disconnectAll ()
 close connections of all associated InetTCPSlave objects
bool startlisten (const short port)
 start listening on port
bool stoplisten ()
 stop listening
bool registerSlave (InetTCPSlave *slave)
 associate a slave with this master
bool deregisterSlave (InetTCPSlave *slave)
 deregister a slave from this master
bool setTransportManager (InetTransportManager *const transMan)
 set the transport manager for this master
void setProcessor (Processor *processor)
 set the processor for this object
bool isListening ()
 return listening status of this object
const int getConnectedSlavesNum (void) const
 return number of connected slaves
void setSlavesMaxHandleCycles (const int cycles=-1)
 setMaxHandleCycles() to slaves default value
const int getSlavesMaxHandleCycles (void) const
 return setting for slaves' default _maxHandleCycle
const std::string getStatusString (void)
 get human readable string describing the status of this object
virtual std::string getName ()
 get name (specializes getName in Transmit)
void setReuseSockAddr (bool reuseAddr)
 set SO_REUSEADDR after socket creation
virtual Transmitclone ()
 Copies self.
virtual bool setReturnPort (const short)
 Set the return port. Defined only for UDP. A hack to get Max to work with wireTAP.
const InetOSCError getError () const
 get last error
const int getLastErrno () const
 get last errno code
const InetOSCAddrType getAddrType (void) const
 get address type of this object
void setMaxHandleCycles (const int cycles=-1)
 set maximum number of cycles in handle()
const int getMaxHandleCycles (void) const
 get maximum number of cycles in handle() setting

Static Public Member Functions

static const bool getErrorString (const InetOSCError err, std::string &errString)
 get a string description of an InetOSCError

Protected Member Functions

bool setSocketTcpNodelay (int *const sd, bool nodelay=true)
 set TCP_NODELAY option of a tcp socket
bool createSocket (int *const sd, const InetOSCProto proto, const InetOSCAddrType addrType)
 create socket
bool bindSocketToAny (int *const sd, const short port, const InetOSCAddrType addrType)
 bind socket to any local address
bool closeSocket (int *const sd, const bool setError=true)
 close socket
bool allocSockAddr (const InetOSCAddrType addrType, struct sockaddr **const addr, socklen_t *const addrlen)
 allocate memory for socket address structs
bool setRemoteAddr (const std::string &host, const short port, const InetOSCAddrType addrType, struct sockaddr *const addr)
 resolve hostname and set remote address struct
bool setRemotePort (const short port, const InetOSCAddrType addrType, struct sockaddr *const addr)
 This is here to support a hack which supports the Max wireTAP stack.
std::string getHostAddress (const InetOSCAddrType addrType, const struct sockaddr *const addr)
 get character string notation out of a network address structure
std::string getHostName (const InetOSCAddrType addrType, const struct sockaddr *const addr)
 reverse lookup hostname out of a network address structure
short getPortNumber (const InetOSCAddrType addrType, const struct sockaddr *const addr)
 get port number out of a network address structure
bool setSocketNonBlock (int *const sd)
 set a socket descriptor non blocking
bool setSocketReuseAddr (int *const sd, bool reuse=true)
 set SO_REUSEADDR option of a socket descriptor

Protected Attributes

bool _reuseAddr
 true: set SO_REUSEADDR on socket
InetOSCAddrType _addrType
 address type of this object
InetOSCError _error
 error code of this object
int _lasterrno
 last errno returned by system call
int _socket
 socket descriptor
int _maxHandleCycles
 maximum handle() cycles

Detailed Description

handle tcp connections.

this class listens for inbound tcp objects, connects to other hosts via tcp, creates InetTCPSlave objects...

See also:
InetTCPSlave
Author:
Martin Rumori


Member Enumeration Documentation

enum InetOSC::InetOSCAddrType [inherited]
 

address family type

Enumerator:
OSC_IN_IPv4  internet protocol version 4
OSC_IN_IPv6  internet protocol version 6

enum InetOSC::InetOSCError [inherited]
 

OSC Inet errors.

This enum is used to sign an error occurred in a OSCInet object

An error usually is signed by a 'false' return value of methods in the OSCInet objects. Use getError() in these objects to get the error code.

Note:
Error codes ending on _ERRNO (and >= 0xC000) sign that an error occured during a system call and that the errno (or h_errno in case of OSC_IN_GETHOST_ERRNO) was saved for further processing. You have to use getLastErrno() to get these saved value and process it by your own, if you need detailed error codes.
See also:
InetTCPMaster, InetTCPSlave, InetUDPMaster
Enumerator:
OSC_IN_SUCCESS  no error, succeeded
OSC_IN_UNKNOWN  unspecified error
OSC_IN_NOT_IDLE  object is not idle
OSC_IN_WOULDBLOCK  operation would block
OSC_IN_MAXCYCLES  _maxHandleCycles reached before EAGAIN (no error)
OSC_IN_CLOSED_CONN  object is not longer connected
OSC_IN_PROTO_UNKNOWN  given protocol unknown
OSC_IN_ADDR_UNKNOWN  given address type unknown
OSC_IN_NO_PROCESSOR  no processor set (e. g. in InetTCPMaster)
OSC_IN_NO_TRANSMGR  no transport manager set (e.g. in InetTCPMaster)
OSC_IN_NO_TCPMASTER  no tcp master set (e.g. in InetTCPSlave)
OSC_IN_ALREADY_SLAVE  slave is already inserted (InetTCPMaster)
OSC_IN_NOT_SLAVE  slave object not found (InetTCPMaster)
OSC_IN_GETHOST_ERRNO  error while gethostbyname()
OSC_IN_GETPROTO_ERRNO  error while getprotobyname()
OSC_IN_SOCKET_ERRNO  error while socket()
OSC_IN_SETSOCKOPT_ERRNO  error while setsockopt()
OSC_IN_FCNTL_ERRNO  error while fcntl()
OSC_IN_BIND_ERRNO  error while bind()
OSC_IN_LISTEN_ERRNO  error while listen()
OSC_IN_CONNECT_ERRNO  error while connect()
OSC_IN_SHUTDOWN_ERRNO  error while shutdown()
OSC_IN_CLOSE_ERRNO  error while close()
OSC_IN_RECV_ERRNO  error while recv() or recvfrom()
OSC_IN_SEND_ERRNO  error while send() or sendto()
OSC_IN_POLL_ERRNO  error while poll()
OSC_IN_ACCEPT_ERRNO  error while accept()
OSC_IN_MALLOC_ERRNO  error while malloc()

enum InetOSC::InetOSCProto [inherited]
 

protocol type

Enumerator:
OSC_IN_UDP  user datagram protocol
OSC_IN_TCP  transmission control protocol


Constructor & Destructor Documentation

InetTCPMaster::InetTCPMaster InetOSCAddrType  addrType = OSC_IN_IPv4  ) 
 

constructor

Parameters:
addrType the address type to use for this object
currently only IPv4 is tested


Member Function Documentation

bool InetOSC::allocSockAddr const InetOSCAddrType  addrType,
struct sockaddr **const   addr,
socklen_t *const   addrlen
[protected, inherited]
 

allocate memory for socket address structs

allocates a struct sockaddr_inX according to the given address type

Parameters:
addrType the address type to use
addr pointer to to allocate
addrlen pointer where the address length gets filled in
Returns:
true on success, false on failure

bool InetOSC::bindSocketToAny int *const   sd,
const short  port,
const InetOSCAddrType  addrType
[protected, inherited]
 

bind socket to any local address

Parameters:
sd a pointer to the socket descriptor
port the port in network byteorder to bind the socket to
addrType the address type of the socket
Returns:
true on success, false on failure

bool InetOSC::closeSocket int *const   sd,
const bool  setError = true
[protected, inherited]
 

close socket

Parameters:
sd a pointer to the socket descriptor
setError false: do not set _error variable this might be useful in the case where you close a socket as reaction on a previous error and you don't want to overwrite the "real" error
Returns:
true on success, false on failure

bool InetOSC::createSocket int *const   sd,
const InetOSCProto  proto,
const InetOSCAddrType  addrType
[protected, inherited]
 

create socket

getprotobyname, socket ...

Parameters:
sd a pointer to the socket descriptor
proto the protocol to use for the socket
addrType the address type to use for the socket
Returns:
true on success, false on failure

bool InetTCPMaster::deregisterSlave InetTCPSlave slave  ) 
 

deregister a slave from this master

Parameters:
slave a pointer to the slave to deregister
Returns:
true on success, false on failure

bool InetTCPMaster::disconnectAll  ) 
 

close connections of all associated InetTCPSlave objects

this method will force all associated InetTCPSlave objects to shutdown their connections. they will be deleted after the connections have been shut down

Note:
if at least one slave object returns an error from its transmit() method, this method will return false with an OSC_IN_UNKNOWN error.
Returns:
true on success, false on failure

const InetOSC::InetOSCAddrType InetOSC::getAddrType void   )  const [inherited]
 

get address type of this object

Returns:
the address type of this object

const int InetTCPMaster::getConnectedSlavesNum void   )  const
 

return number of connected slaves

Returns:
true: object is connected, false: object is disconnected

const InetOSC::InetOSCError InetOSC::getError  )  const [inherited]
 

get last error

Returns:
the code of the last error occured

const bool InetOSC::getErrorString const InetOSCError  err,
std::string &  errString
[static, inherited]
 

get a string description of an InetOSCError

Parameters:
err the error to get the string for
errString a reference to a string to fill with the description
Returns:
true on success, false on unknown error code given

const int InetTCPMaster::getHandle  )  const [virtual]
 

return socket descriptor

Returns:
the socket descriptor of this object

Implements InetTransport.

std::string InetOSC::getHostAddress const InetOSCAddrType  addrType,
const struct sockaddr *const   addr
[protected, inherited]
 

get character string notation out of a network address structure

Parameters:
addrType the address type to interpret
addr pointer to the sockaddr struct to extract the address from
Returns:
a string containing the character string notation of the address or empty string ("") on error

std::string InetOSC::getHostName const InetOSCAddrType  addrType,
const struct sockaddr *const   addr
[protected, inherited]
 

reverse lookup hostname out of a network address structure

Parameters:
addrType the adress type to interpret
addr pointer to the sockadddr struct to extract the address from
Returns:
the hostname looked up or "" if not found
Note:
this method currently only works with IPv4

const int InetOSC::getLastErrno  )  const [inherited]
 

get last errno code

Returns:
the last errno returned by a system call

const int InetOSC::getMaxHandleCycles void   )  const [inherited]
 

get maximum number of cycles in handle() setting

See also:
setMaxHandleCycles()
Returns:
the maximum number of handle() cycles set to this object

std::string InetTCPMaster::getName  )  [virtual]
 

get name (specializes getName in Transmit)

Returns:
a string which identifies this object (incl. type).

Implements InetTransport.

short InetOSC::getPortNumber const InetOSCAddrType  addrType,
const struct sockaddr *const   addr
[protected, inherited]
 

get port number out of a network address structure

Parameters:
addrType the adress type to interpret
addr pointer to the sockadddr struct to extract the port from
Returns:
the port number entry (network byteorder) from the address structure or 0 on error

const int InetTCPMaster::getSlavesMaxHandleCycles void   )  const
 

return setting for slaves' default _maxHandleCycle

Returns:
the slaves' default value for _maxHandleCycle

const std::string InetTCPMaster::getStatusString void   )  [virtual]
 

get human readable string describing the status of this object

Returns:
status string of this object

Implements InetTransport.

bool InetTCPMaster::handle  )  [virtual]
 

read incoming data from socket

Returns:
true on success, false on error

Implements InetTransport.

bool InetTCPMaster::isListening  )  [virtual]
 

return listening status of this object

Returns:
true if listening, false if not listening

Implements InetMaster.

InetTCPSlave *const InetTCPMaster::newconnection const std::string &  host,
const short  port,
const InetOSCAddrType  addrType = OSC_IN_IPv4
 

connect to host

this method creates a new socket, tries to connect to the given host, after the connection has been established, it creates a new InetTCPSlave object, associates it with this master and returns a pointer to it

Parameters:
host the hostname or address to connect to
port the port number to connect to
addrType the address type to use for the connection
Returns:
a pointer the connected object on success, NULL on failure
currently only IPv4 is tested

bool InetTCPMaster::process const std::string &  data,
Transmit reply
 

process an osc message by forwarding it to our processor

Parameters:
data the osc message buffer
reply the Transmit object to reply to
Returns:
true on success, false on failure

bool InetTCPMaster::registerSlave InetTCPSlave slave  ) 
 

associate a slave with this master

Parameters:
slave a pointer to the slave object to register
Returns:
true on success, false on failure

void InetOSC::setMaxHandleCycles const int  cycles = -1  )  [inherited]
 

set maximum number of cycles in handle()

this method determines the maximum number of cycles to read within a handle() call. this is to avoid a "block" of the system if a handle() call never comes back on never-ending receiving of messages. handle() returns in any case if recv got EAGAIN.

for InetUDPMaster this settings correlates directly to the maximum number of received OSC packets within one one handle() call, for InetTCPSlave this number means the the number of receive buffers filled within one handle() call, which may contain more or less OSC packets. for InetTCPMaster, this parameter means the maximum number of pending connections getting accepted within one handle() call.

Parameters:
cycles number of cycles. a negative value means unlimited (read always until EAGAIN), a value of zero means no handle() cycles (receiving disabled)

void InetTCPMaster::setProcessor Processor processor  )  [virtual]
 

set the processor for this object

Parameters:
processor a pointer to the OSCProcessor

Implements InetMaster.

bool InetOSC::setRemoteAddr const std::string &  host,
const short  port,
const InetOSCAddrType  addrType,
struct sockaddr *const   addr
[protected, inherited]
 

resolve hostname and set remote address struct

Parameters:
host the hostname or address to set
port the port number in network byteorder to set
addrType the address type to use
addr pointer to the (allocated) address structure to fill
Returns:
true on success, false on failure

bool InetOSC::setRemotePort const short  port,
const InetOSCAddrType  addrType,
struct sockaddr *const   addr
[protected, inherited]
 

This is here to support a hack which supports the Max wireTAP stack.

The Max OSC objects won't let you send and receive UDP from the same socke t * (thus port): so you must receive OSC on a different port than you send on. * But the default behavior when someone sends a subscribe message is to * subscribe the host and port on which the request was sent (so the reply wi ll * go back to the same place.) The hack lets Max people send an integer value * in the subscribe message, which will be used as the port to send future * traffic back to, on the same address. This requires the ability to reset t he * port on a Transmit. But only really a UDPTransmit - except those don't exi st * anymore, they're InetUDPMasters now.

Parameters:
port the port number in network byteorder to set
addrType the address type to use
addr pointer to the (allocated) address structure to change
Returns:
true if ok, false if error

void InetMaster::setReuseSockAddr bool  reuseAddr  )  [inline, inherited]
 

set SO_REUSEADDR after socket creation

Parameters:
reuseAddr true: reuse socket address, false: do not reuse socket address

void InetTCPMaster::setSlavesMaxHandleCycles const int  cycles = -1  ) 
 

setMaxHandleCycles() to slaves default value

this method sets a new value of _maxHandleCycles to all InetTCPSlaves associated with this InetTCPMaster and sets the default value which is "inherited" by all new spawned slaves.

See also:
setMaxHandleCycles()
Parameters:
cycles value for slaves' _maxHandleCycles

bool InetOSC::setSocketNonBlock int *const   sd  )  [protected, inherited]
 

set a socket descriptor non blocking

Parameters:
sd pointer to the socket descriptor
Returns:
true on sucess, false on failure

bool InetOSC::setSocketReuseAddr int *const   sd,
bool  reuse = true
[protected, inherited]
 

set SO_REUSEADDR option of a socket descriptor

Parameters:
sd pointer to the socket descriptor
reuse true: set SO_REUSEADDR, false: unset SO_REUSEADDR
Returns:
true on success, false on failure

bool InetTCPMaster::setSocketTcpNodelay int *const   sd,
bool  nodelay = true
[protected]
 

set TCP_NODELAY option of a tcp socket

Parameters:
sd pointer to the socket descriptor
nodelay true: set TCP_NODELAY, false: unset TCP_NODELAY
Returns:
true on success, false on failure

bool InetTCPMaster::setTransportManager InetTransportManager *const   transMan  )  [virtual]
 

set the transport manager for this master

Note:
you may not set nor change the transport manager if there are still slaves being served or this object is listening for inbound connections
Parameters:
transMan a pointer to the transport manager
Returns:
true on success, false on failure

Implements InetMaster.

bool InetTCPMaster::startlisten const short  port  )  [virtual]
 

start listening on port

this method creates a socket for listening. inbound connections are processed by creating a new InetTCPSlave object, which will get served by the servicemanager of this object

Parameters:
port the port to listen to
Returns:
true on success, false on failure

Implements InetMaster.

bool InetTCPMaster::stoplisten  )  [virtual]
 

stop listening

this method stops listening. further inbound connections are not possible. established connections are not touched by this method

Returns:
true on success, false on failure

Implements InetMaster.

bool InetTCPMaster::transmit const std::string &  data  )  [virtual]
 

transmit outgoing data

this method transmits the given data over all associated InetTCPSlave objects

Note:
if at least one slave object returns an error from its transmit() method, this method will return false with an OSC_IN_UNKNOWN error. take care especially if you set one or more of the InetTCPSlave objects non blocking on write. you should use the slave's transmit() method directly to be sure to catch the OSC_IN_WOULDBLOCK error set by the slave instead of blocking
Parameters:
data the data buffer to be transmit
Returns:
true on success, false on error

Implements InetTransport.


The documentation for this class was generated from the following files:
Generated on Thu Mar 2 23:51:35 2006 for libOSC++ by  doxygen 1.4.6