API
class netclient : public netbase {
netclient();
int doConnect( //Connect to address:port
const std::string& address,
int remotePort,
int localPort = 0);
int doDisconnect(); //Drop connection
int run(); //Run callbacks if needed
bool setConnTimeout( //Set connect timeout
int seconds=3,
int microsec=0);
};
netbase.h
class netbase {
//Function pointer types
typedef size_t (*connectionFP)( int ID, void *cb_data);
netbase( //Max number of connections
unsigned int);
int sendPacket( //Send packet via connection
int connection, netpacket &pkt);
void setPktCB( //Set the incoming pkt callback
netpacket::netPktCB cbFunc,
void *cbData);
bool setConPktCB( //Add connection specific CB
int connection,
netpacket::netPktCB cbFunc,
void *cbData );
bool unsetConPktCB( //Remove callback on connection
int connection);
void unsetAllPktCB(); //Remove all connection callbacks
void setConnectCB( //Set "new client" callback
connectionFP cbFunc,
void *cbData);
void setDisconnectCB( //Set disconnection callback
connectionFP cbFunc,
void *cbData);
void removeDisconnectCB(); //Remove disconnection callback
bool isConnected() const; //Am I listening?
bool isClosed( //Is connection closed?
int connection) const;
mutable std::ofstream debugLog; //Debug output stream
mutable std::string lastError; //Last known error msg
};
netpacket.h
class netpacket {
typedef size_t (*netPktCB)( //Callback function type
netpacket* pkt,
void *cb_data);
netpacket( ); //Allocate 1024 bytes, position = 0
netpacket( //Allocate size bytes, position = 0
size_t size);
netpacket( //Use buffer, position = length
size_t length,
unsigned char* buffer);
netpacket( //Use buffer, position < length
size_t size,
unsigned char* buffer,
size_t position);
size_t ID; //Connection ID
size_t get_length(); //Get position
size_t get_maxsize(); //Get length
const unsigned char* get_ptr(); //Get &buffer
void reset() //position=0
//Copy from packet into val, increment position
template <class T> size_t read(T&);
template <class T> size_t read (
const T *val_array,
size_t size);
size_t read (bool& val);
size_t read (unsigned char& val);
size_t read (char& val);
size_t read (unsigned short& val);
size_t read (short& val);
size_t read (unsigned long& val);
size_t read (long& val);
size_t read (long long& val);
size_t read (float& val);
size_t read (double& val);
size_t read (char *val, size_t size);
size_t read (unsigned char *val, size_t size);
//Copy val to packet, increment position
template <class T> size_t append(T val);
template <class T> size_t append(
const T *val_array,
size_t size);
size_t append (bool val); //1 byte bool
size_t append (unsigned char val);
size_t append (char val);
size_t append (unsigned short val);
size_t append (short val);
size_t append (unsigned long val);
size_t append (long val);
size_t append (long long val);
size_t append (float val);
size_t append (double val);
size_t append (const char *val, size_t size);
size_t append (const unsigned char *val, size_t size);
};
These are the public interfaces of the library header files.
Version: October 24, 2010
netserver.h
class netserver : public netbase {
netserver( //Max number of clients
unsigned int max);
int openPort( //Listen on port
short port);
void closePort(); //Close port
int run(); //Run callbacks if needed
};
netclient.h