NetSocket Class Reference
[Networking]

The basic network socket class. More...

#include <netsocket.h>

Inheritance diagram for NetSocket:

Inheritance graph
[legend]
Collaboration diagram for NetSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

DECLSPEC NetSocket (const char *name, NetBasicManager *p, TCPsocket connection=0)
DECLSPEC NetSocket (const char *name, NetBasicManager *p, IPaddress ip)
DECLSPEC NetSocket (TiXmlElement *el, NetBasicManager *p)
virtual bool isNetSocket () const
 
Returns:
whether this is derived from NetSocket.

bool isValid () const
 
Returns:
true, if this socket is in the manager's socket list, false if not

DECLSPEC bool connect (const char *host, int port)
DECLSPEC bool connect (IPaddress &ip)
DECLSPEC void disconnect ()
virtual bool connected () const
 
Returns:
true, if the socket is connected, otherwise false

virtual bool deleteOnDisconnected () const
virtual DECLSPEC bool wait4data (Uint32 time)
virtual bool sendData (char *data, unsigned int size)
virtual bool sendUDP (char *data, unsigned int size)
virtual int readData (char *buffer, int maxsize)
virtual char * readData (int *size)=0
virtual bool isActive () const
TCPsocket getSocket ()
 
Returns:
the SDL TCP-socket that is used for communication

const IPaddress * getAddress () const
const char * getHostName () const

Protected Member Functions

virtual DECLSPEC void onInit ()
virtual DECLSPEC bool onReceive ()
virtual DECLSPEC void onReceiveUDP (UDPpacket &udp)
virtual void onConnect ()
virtual void onDisconnect ()

Protected Attributes

TCPsocket sock
 the SDL TCP socket that is used for communication
bool bValid
 see isValid() for description
bool bActive
 see isActive() for description
bool bDelOnDisconn
 see deleteOnDisconnected() for description

Friends

class NetBasicManager

Detailed Description

The basic network socket class.

This class implements basic socket functions network communication. It works closely together with NetBasicManager.

Used error codes: E_NET_NONE, E_NET_NO_HOST, E_NET_DISCONNECTED, E_NET_RAW_DATA

See also:
NetBasicManager


Constructor & Destructor Documentation

NetSocket::NetSocket ( const char *  name,
NetBasicManager p,
TCPsocket  connection = 0 
)

Constructor.

Parameters:
connection the SDL TCPsocket to use, or NULL, if no connection is used.

NetSocket::NetSocket ( const char *  name,
NetBasicManager p,
IPaddress  ip 
)

Constructor. Connects to a host.

Parameters:
ip the host's IP address, that should be connected.
Warning:
this construction can take some time, if the host is not reachable immediatelly.

NetSocket::NetSocket ( TiXmlElement el,
NetBasicManager p 
)

Constructor. Uses XML-data.

Parameters:
el the XML-data, without attributes (except the name) it behaves like NetSocket(const char*, NetBasicManager*). If the attribute "IP" is specified it behaves similar to NetSocket(const char*, NetBasicManager*, IPaddress), the form of the address must be like "192.168.0.1:1234".
Test:
this function


Member Function Documentation

bool NetSocket::connect ( const char *  host,
int  port 
)

Connects to a host. This may take some time, especially if the host is unreachable.

Parameters:
host the hostname or IP address to connect to (without port)
port the port to connect on (in OSBO)
Returns:
false, if no connection could be established or an error occured, true if all went perfect

bool NetSocket::connect ( IPaddress &  ip  ) 

Connects to a host. This may take some time, especially if the host is unreachable.

Parameters:
ip the IP address to connect to
Returns:
false, if no connection could be established or an error occured, true if all went perfect

void NetSocket::disconnect (  ) 

Disconnects an existing connection.

virtual bool NetSocket::deleteOnDisconnected (  )  const [inline, virtual]

Returns:
true, if it will be deleted automatically, when disconnected; false, if it can be reused and has finally to be deleted by the application.

bool NetSocket::wait4data ( Uint32  time  )  [virtual]

Parameters:
time the time in msecs, how long to wait on data, if no data can be read
Returns:
true, if data can be read, otherwise false

bool NetSocket::sendData ( char *  data,
unsigned int  size 
) [virtual]

Sends raw data via TCP to the host. This method should be overridden in derived classes that implement a special structure for transmitted data.

Parameters:
data the data to send
size the amount of bytes to send
Returns:
true on success, false on error

Reimplemented in NetStandardSocket.

bool NetSocket::sendUDP ( char *  data,
unsigned int  size 
) [virtual]

Sends raw data via UDP to the host.

Note:
Using this makes only sense, if all clients, communicating via UDP, have their UDP port number one above their TCP port number.
Parameters:
data the data to send
size the amount of bytes to send
Returns:
true on success, false on error

int NetSocket::readData ( char *  buffer,
int  maxsize 
) [virtual]

Receives raw data from the host. This method does not block, but is probably not very efficient, especially when reading large packets.

Parameters:
buffer the buffer where received data is stored
maxsize the size of the buffer, that can be filled
Returns:
the size of the received data; 0 if there is no data to read; a negative value (an error-code) on error

Reimplemented in NetStandardSocket.

virtual char* NetSocket::readData ( int *  size  )  [pure virtual]

Parameters:
size pointer to an integer, where either the size (in bytes) of the returned data will be stored or the error code, if something failed.
Returns:
a pointer to space with the recieved data or NULL, if an error occured.
Warning:
the result has to be deleted after usage!

Implemented in NetStandardSocket.

virtual bool NetSocket::isActive (  )  const [inline, virtual]

While the socket is active, the netmanager ignores the incomming-message-status

Returns:
true, if data is currently received or however the socket is active, otherwise false.

const IPaddress* NetSocket::getAddress (  )  const [inline]

Returns:
a pointer to the IP address of the connected host or NULL if not connected

const char* NetSocket::getHostName (  )  const [inline]

This method may block for a short time, due to reverse DNS lookup.

Returns:
the hostname of the connected host; NULL, if not connected
Note:
this is often simply the IP address of the host

void NetSocket::onInit (  )  [protected, virtual]

Initializes the object's variables by reading data from the XML element. If there were no XML data used for creation, nothing is done. This function calls onInit() of its children recursively (after it has inited itself). It is highly recommended to call BasicObject::onInit(), usually at the end of the onInit() method, when overwriting it in a derived class.

Reimplemented from BasicObject.

Reimplemented in NetStandardSocket.

bool NetSocket::onReceive (  )  [protected, virtual]

Called from the manager, when data arrived for this socket. In this base class, it calls the default callback function NetBasicManager::cbPeer.

Returns:
false, if an error occured and the socket has to be closed, otherwise true

Reimplemented in NetPeerSocket, and NetStandardSocket.

void NetSocket::onReceiveUDP ( UDPpacket &  udp  )  [protected, virtual]

Called from the manager, when UDP data arrived for this socket. In this base class, it calls the default callback function NetBasicManager::cbUDP.

Parameters:
udp the recieved packet. Its content is only valid until the next packet is received!

virtual void NetSocket::onConnect (  )  [inline, protected, virtual]

Called, after a connection has been established successfully. If you want to overwrite this method, do not forget to call the parent class's onConnect();

Reimplemented in NetPeerSocket, and NetStandardSocket.

virtual void NetSocket::onDisconnect (  )  [inline, protected, virtual]

Called, before a connection is terminated. If you want to overwrite this method, do not forget to call the parent class's onConnect();


The documentation for this class was generated from the following files:
Generated on Wed May 9 17:36:31 2007 for netrinjo by  doxygen 1.5.1