30.11.2013 - New PICollection namespace, Android support, my own PIVector implementation

This commit is contained in:
peri4
2013-11-30 19:34:53 +04:00
parent ec5530053a
commit f50891b376
64 changed files with 5466 additions and 3392 deletions

View File

@@ -32,106 +32,241 @@ class PIP_EXPORT PIEthernet: public PIIODevice
PIOBJECT(PIEthernet)
friend class PIPeer;
public:
// slot is any function format "bool <func>(void*, uchar*, int)"
PIEthernet(void * data, ReadRetFunc slot);
//! Contructs UDP %PIEthernet with empty read address
PIEthernet();
enum Type {UDP, TCP_Client, TCP_Server, TCP_SingleTCP};
enum Parameters {ReuseAddress = 0x1, Broadcast = 0x2};
//! \brief Type of %PIEthernet
enum Type {
UDP /** UDP - User Datagram Protocol */ ,
TCP_Client /** TCP client - allow connection to TCP server */ ,
TCP_Server /** TCP server - receive connections from TCP clients */ ,
TCP_SingleTCP
};
//! \brief Parameters of %PIEthernet
enum Parameters {
ReuseAddress /** Rebind address if there is already binded */ = 0x1,
Broadcast /** Broadcast send */ = 0x2
};
PIEthernet(Type type = UDP, void * data = 0, ReadRetFunc slot = 0);
//! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params"
PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags<Parameters> params = 0);
virtual ~PIEthernet();
//! Set read address
void setReadAddress(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port);}
//! Set read address in format "i.i.i.i:p"
void setReadAddress(const PIString & ip_port) {path_ = ip_port;}
//! Set read IP
void setReadIP(const PIString & ip) {parseAddress(path_, &ip_, &port_); path_ = ip + ":" + PIString::fromNumber(port_);}
//! Set read port
void setReadPort(int port) {parseAddress(path_, &ip_, &port_); path_ = ip_ + ":" + PIString::fromNumber(port);}
//! Set send address
void setSendAddress(const PIString & ip, int port) {ip_s = ip; port_s = port;}
//! Set send address in format "i.i.i.i:p"
void setSendAddress(const PIString & ip_port) {parseAddress(ip_port, &ip_s, &port_s);}
//! Set send IP
void setSendIP(const PIString & ip) {ip_s = ip;}
//! Set send port
void setSendPort(int port) {port_s = port;}
//! Returns read address in format "i.i.i.i:p"
PIString readAddress() {return path_;}
//! Returns read IP
PIString readIP() {parseAddress(path_, &ip_, &port_); return ip_;}
//! Returns read port
int readPort() {parseAddress(path_, &ip_, &port_); return port_;}
//! Returns send address in format "i.i.i.i:p"
PIString sendAddress() {return ip_s + ":" + PIString::fromNumber(port_s);}
//! Returns send IP
PIString sendIP() {return ip_s;}
//! Returns send port
int sendPort() {return port_s;}
//! Set parameters to "parameters_". You should to reopen %PIEthernet to apply them
void setParameters(PIFlags<PIEthernet::Parameters> parameters_) {params = parameters_;}
//! Set parameter "parameter" to state "on". You should to reopen %PIEthernet to apply this
void setParameter(PIEthernet::Parameters parameter, bool on = true) {params.setFlag(parameter, on);}
//! Returns if parameter "parameter" is set
bool isParameterSet(PIEthernet::Parameters parameter) const {return params[parameter];}
//! Returns parameters
PIFlags<PIEthernet::Parameters> parameters() const {return params;}
//PIByteArray macAddress() {if (!init_) init(); struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); memcpy(ifr.ifr_name, "eth0", 5); ioctl(sock, SIOCSIFHWADDR, &ifr); return PIByteArray(&ifr.ifr_hwaddr.sa_data, 6);}
//! Returns %PIEthernet type
Type type() const {return type_;}
//! Join to multicast group with address "group". Use only for UDP
bool joinMulticastGroup(const PIString & group);
//! Leave multicast group with address "group". Use only for UDP
bool leaveMulticastGroup(const PIString & group);
//! Connect to TCP server with address \a readAddress(). Use only for TCP_Client
bool connect();
//! Connect to TCP server with address "ip":"port". Use only for TCP_Client
bool connect(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port); return connect();}
//! Connect to TCP server with address "ip_port". Use only for TCP_Client
bool connect(const PIString & ip_port) {path_ = ip_port; return connect();}
//! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client
bool isConnected() const {return connected_;}
//! Start listen for incoming TCP connections on address \a readAddress(). Use only for TCP_Server
bool listen();
//! Start listen for incoming TCP connections on address "ip":"port". Use only for TCP_Server
bool listen(const PIString & ip, int port) {setReadAddress(ip, port); return listen();}
//! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server
bool listen(const PIString & ip_port) {setReadAddress(ip_port); return listen();}
PIEthernet * client(int index) {return clients_[index];}
int clientsCount() const {return clients_.size_s();}
PIVector<PIEthernet * > clients() {return clients_;}
//! Send data "data" with size "size" to address "ip":"port"
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {ip_s = ip; port_s = port; if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
//! Send data "data" with size "size" to address "ip_port"
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {parseAddress(ip_port, &ip_s, &port_s); if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
//! Send data "data" with size "size" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
bool send(const void * data, int size, bool threaded = false) {if (threaded) {writeThreaded(data, size); return true;} return (write(data, size) == size);}
bool send(const PIByteArray & ba, bool threaded = false) {if (threaded) {writeThreaded(ba); return true;} return (write(ba) == ba.size_s());}
//! Send data "data" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
bool send(const PIByteArray & data, bool threaded = false) {if (threaded) {writeThreaded(data); return true;} return (write(data) == data.size_s());}
//! Wait for some data and read it to "read_to"
int read(void * read_to, int max_size);
//! Send data "read_to" with size "max_size" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
int write(const void * data, int max_size);
//! Send data "data" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
EVENT1(newConnection, PIEthernet * , client)
EVENT0(connected)
EVENT1(disconnected, bool, withError)
//! Flags of network interface
enum InterfaceFlag {
ifActive = 0x1,
ifRunning = 0x2,
ifBroadcast = 0x4,
ifMulticast = 0x8,
ifLoopback = 0x10,
ifPTP = 0x20
ifActive /** Is active */ = 0x1,
ifRunning /** Is running */ = 0x2,
ifBroadcast /** Support broadcast */ = 0x4,
ifMulticast /** Support multicast */ = 0x8,
ifLoopback /** Is loopback */ = 0x10,
ifPTP /** Is point-to-point */ = 0x20
};
//! %PIFlags of network interface flags
typedef PIFlags<InterfaceFlag> InterfaceFlags;
//! Network interface descriptor
struct Interface {
//! System index
int index;
//! System name
PIString name;
//! MAC address in format "hh:hh:hh:hh:hh:hh" or empty if there is no MAC address
PIString mac;
//! IP address in format "i.i.i.i" or empty if there is no IP address
PIString address;
//! Netmask of IP address in format "i.i.i.i" or empty if there is no netmask
PIString netmask;
//! Broadcast address in format "i.i.i.i" or empty if there is no broadcast address
PIString broadcast;
//! Point-to-point address or empty if there is no point-to-point address
PIString ptp;
//! Flags of interface
InterfaceFlags flags;
//! Returns if interface is active
bool isActive() const {return flags[PIEthernet::ifActive];}
//! Returns if interface is running
bool isRunning() const {return flags[PIEthernet::ifRunning];}
//! Returns if interface support broadcast
bool isBroadcast() const {return flags[PIEthernet::ifBroadcast];}
//! Returns if interface support multicast
bool isMulticast() const {return flags[PIEthernet::ifMulticast];}
//! Returns if interface is loopback
bool isLoopback() const {return flags[PIEthernet::ifLoopback];}
//! Returns if interface is point-to-point
bool isPTP() const {return flags[PIEthernet::ifPTP];}
};
//! Array of \a Interface with some features
class InterfaceList: public PIVector<PIEthernet::Interface> {
public:
InterfaceList(): PIVector<PIEthernet::Interface>() {}
//! Get interface with system index "index" or 0 if there is no one
const Interface * getByIndex(int index) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].index == index) return &((*this)[i]); return 0;}
//! Get interface with system name "name" or 0 if there is no one
const Interface * getByName(const PIString & name) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].name == name) return &((*this)[i]); return 0;}
//! Get interface with IP address "address" or 0 if there is no one
const Interface * getByAddress(const PIString & address) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].address == address) return &((*this)[i]); return 0;}
//! Get loopback interface or 0 if there is no one
const Interface * getLoopback() const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].isLoopback()) return &((*this)[i]); return 0;}
};
//! Returns all system network interfaces
static InterfaceList interfaces();
static PIString interfaceAddress(const PIString & interface_);
//! Returns all system network IP addresses
static PIStringList allAddresses();
static void parseAddress(const PIString & ipp, PIString * ip, int * port);
@@ -140,11 +275,43 @@ public:
static PIString applyMask(const PIString & ip, const PIString & mask) {struct in_addr ia; ia.s_addr = inet_addr(ip.data()) & inet_addr(mask.data()); return PIString(inet_ntoa(ia));}
static PIString getBroadcast(const PIString & ip, const PIString & mask) {struct in_addr ia; ia.s_addr = inet_addr(ip.data()) | ~inet_addr(mask.data()); return PIString(inet_ntoa(ia));}
//! \events
//! \{
//! \fn void newConnection(PIEthernet * client)
//! \brief Raise on new TCP connection received
//! \fn void connected()
//! \brief Raise if succesfull TCP connection
//! \fn void disconnected(bool withError)
//! \brief Raise if TCP connection was closed
//! \}
//! \ioparams
//! \{
#ifdef DOXYGEN
//! \brief read ip, default ""
string ip;
//! \brief read port, default 0
int port;
//! \brief Broadcast parameter, default false
bool broadcast;
//! \brief ReuseAddress parameter, default false
bool reuseAddress;
#endif
//! \}
protected:
PIEthernet(int sock, PIString ip_port);
bool configureDevice(const void * e_main, const void * e_parent = 0);
virtual void received(void * data, int size) {;}
//! Executes when any read function was successful. Default implementation does nothing
virtual void received(const void * data, int size) {;}
bool init();
bool openDevice();