#include <netsocket.h>
Inheritance diagram for NetSocket:
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 |
| |
bool | isValid () const |
| |
DECLSPEC bool | connect (const char *host, int port) |
DECLSPEC bool | connect (IPaddress &ip) |
DECLSPEC void | disconnect () |
virtual bool | connected () const |
| |
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 () |
| |
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 |
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
NetSocket::NetSocket | ( | const char * | name, | |
NetBasicManager * | p, | |||
TCPsocket | connection = 0 | |||
) |
Constructor.
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.
ip | the host's IP address, that should be connected. |
NetSocket::NetSocket | ( | TiXmlElement * | el, | |
NetBasicManager * | p | |||
) |
Constructor. Uses XML-data.
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". |
bool NetSocket::connect | ( | const char * | host, | |
int | port | |||
) |
Connects to a host. This may take some time, especially if the host is unreachable.
host | the hostname or IP address to connect to (without port) | |
port | the port to connect on (in OSBO) |
bool NetSocket::connect | ( | IPaddress & | ip | ) |
Connects to a host. This may take some time, especially if the host is unreachable.
ip | the IP address to connect to |
void NetSocket::disconnect | ( | ) |
Disconnects an existing connection.
virtual bool NetSocket::deleteOnDisconnected | ( | ) | const [inline, virtual] |
bool NetSocket::wait4data | ( | Uint32 | time | ) | [virtual] |
time | the time in msecs, how long to wait on data, if no data can be read |
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.
data | the data to send | |
size | the amount of bytes to send |
Reimplemented in NetStandardSocket.
bool NetSocket::sendUDP | ( | char * | data, | |
unsigned int | size | |||
) | [virtual] |
Sends raw data via UDP to the host.
data | the data to send | |
size | the amount of bytes to send |
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.
buffer | the buffer where received data is stored | |
maxsize | the size of the buffer, that can be filled |
Reimplemented in NetStandardSocket.
virtual char* NetSocket::readData | ( | int * | size | ) | [pure virtual] |
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. |
Implemented in NetStandardSocket.
virtual bool NetSocket::isActive | ( | ) | const [inline, virtual] |
While the socket is active, the netmanager ignores the incomming-message-status
const IPaddress* NetSocket::getAddress | ( | ) | const [inline] |
const char* NetSocket::getHostName | ( | ) | const [inline] |
This method may block for a short time, due to reverse DNS lookup.
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.
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.
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();