separate PIEthernet::Address to PINetworkAddress, typedef PIEthernet::Address to PINetworkAddress and mark as deprecated

PIValueTree new attributes for File and Dir
This commit is contained in:
2022-12-19 14:29:18 +03:00
parent 6c3f305562
commit 2ac215c19e
18 changed files with 505 additions and 328 deletions

View File

@@ -27,7 +27,7 @@
#define PIETHERNET_H
#include "piiodevice.h"
#include "pitimer.h"
#include "pinetworkaddress.h"
#ifdef ANDROID
struct
@@ -63,74 +63,7 @@ public:
DisonnectOnTimeout /** Disconnect TCP connection on read timeout expired. Disabled by default */ = 0x20
};
//! \brief IPv4 network address, IP and port
class PIP_EXPORT Address {
friend class PIEthernet;
public:
//! Contructs %Address with binary representation of IP and port
Address(uint ip = 0, ushort port = 0);
//! Contructs %Address with string representation "i.i.i.i:p"
Address(const PIString & ip_port);
//! Contructs %Address with IP string representation "i.i.i.i" and port
Address(const PIString & ip, ushort port);
//! Returns binary IP
uint ip() const { return ip_; }
//! Returns port
ushort port() const { return port_; }
//! Returns string IP
PIString ipString() const;
//! Returns string representation of IP and port "i.i.i.i:p"
PIString toString() const;
//! Set address IP
void setIP(uint ip);
//! Set address IP
void setIP(const PIString & ip);
//! Set address port
void setPort(ushort port);
//! Set address IP and port, "i.i.i.i:p"
void set(const PIString & ip_port);
//! Set address IP and port, "i.i.i.i"
void set(const PIString & ip, ushort port);
//! Set address binary IP and port
void set(uint ip, ushort port);
//! Set IP and port to 0
void clear();
//! Returns if IP and port is 0
bool isNull() const;
//! Resolve hostname "host:port" and return it address or null address on error
static Address resolve(const PIString & host_port);
//! Resolve hostname "host" with port "port" and return it address or null address on error
static Address resolve(const PIString & host, ushort port);
static void splitIPPort(const PIString & ipp, PIString * ip, int * port);
private:
void initIP(const PIString & _ip);
union {
uint ip_;
uchar ip_b[4];
};
ushort port_;
};
typedef ::PINetworkAddress Address DEPRECATEDM("use PINetworkAddress instead");
//! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params"
explicit PIEthernet(Type type,
@@ -154,7 +87,7 @@ public:
}
//! Set read address
void setReadAddress(const Address & addr) {
void setReadAddress(const PINetworkAddress & addr) {
addr_r = addr;
setPath(addr_r.toString());
}
@@ -179,7 +112,7 @@ public:
void setSendAddress(const PIString & ip_port) { addr_s.set(ip_port); }
//! Set send address
void setSendAddress(const Address & addr) { addr_s = addr; }
void setSendAddress(const PINetworkAddress & addr) { addr_s = addr; }
//! Set send IP
void setSendIP(const PIString & ip) { addr_s.setIP(ip); }
@@ -189,7 +122,7 @@ public:
//! Returns read address in format "i.i.i.i:p"
Address readAddress() const { return addr_r; }
PINetworkAddress readAddress() const { return addr_r; }
//! Returns read IP
PIString readIP() const { return addr_r.ipString(); }
@@ -199,7 +132,7 @@ public:
//! Returns send address in format "i.i.i.i:p"
Address sendAddress() const { return addr_s; }
PINetworkAddress sendAddress() const { return addr_s; }
//! Returns send IP
PIString sendIP() const { return addr_s.ipString(); }
@@ -209,7 +142,7 @@ public:
//! Returns address of last received UDP packet in format "i.i.i.i:p"
Address lastReadAddress() const { return addr_lr; }
PINetworkAddress lastReadAddress() const { return addr_lr; }
//! Returns IP of last received UDP packet
PIString lastReadIP() const { return addr_lr.ipString(); }
@@ -287,7 +220,7 @@ public:
}
//! Connect to TCP server with address "addr". Use only for TCP_Client
bool connect(const Address & addr, bool threaded = true) {
bool connect(const PINetworkAddress & addr, bool threaded = true) {
setPath(addr.toString());
return connect(threaded);
}
@@ -303,13 +236,13 @@ public:
bool listen(bool threaded = false);
//! Start listen for incoming TCP connections on address "ip":"port". Use only for TCP_Server
bool listen(const PIString & ip, int port, bool threaded = false) { return listen(PIEthernet::Address(ip, port), threaded); }
bool listen(const PIString & ip, int port, bool threaded = false) { return listen(PINetworkAddress(ip, port), threaded); }
//! Start listen for incoming TCP connections on address "ip_port". Use only for TCP_Server
bool listen(const PIString & ip_port, bool threaded = false) { return listen(PIEthernet::Address(ip_port), threaded); }
bool listen(const PIString & ip_port, bool threaded = false) { return listen(PINetworkAddress(ip_port), threaded); }
//! Start listen for incoming TCP connections on address "addr". Use only for TCP_Server
bool listen(const Address & addr, bool threaded = false);
bool listen(const PINetworkAddress & addr, bool threaded = false);
PIEthernet * client(int index) { return clients_[index]; }
int clientsCount() const { return clients_.size_s(); }
@@ -321,32 +254,32 @@ public:
//! 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) {
return send(PIEthernet::Address(ip, port), data, size, threaded);
return send(PINetworkAddress(ip, port), data, size, threaded);
}
//! Send data "data" with size "size" to address "ip_port"
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {
return send(PIEthernet::Address(ip_port), data, size, threaded);
return send(PINetworkAddress(ip_port), data, size, threaded);
}
//! Send data "data" with size "size" to address "addr"
bool send(const Address & addr, const void * data, int size, bool threaded = false);
bool send(const PINetworkAddress & addr, const void * data, int size, bool threaded = false);
//! Send data "data" to address \a sendAddress() for UDP or \a readAddress() for TCP_Client
bool send(const PIByteArray & data, bool threaded = false);
//! Send data "data" to address "ip":"port" for UDP
bool send(const PIString & ip, int port, const PIByteArray & data, bool threaded = false) {
return send(PIEthernet::Address(ip, port), data, threaded);
return send(PINetworkAddress(ip, port), data, threaded);
}
//! Send data "data" to address "ip_port" for UDP
bool send(const PIString & ip_port, const PIByteArray & data, bool threaded = false) {
return send(PIEthernet::Address(ip_port), data, threaded);
return send(PINetworkAddress(ip_port), data, threaded);
}
//! Send data "data" to address "addr" for UDP
bool send(const Address & addr, const PIByteArray & data, bool threaded = false);
bool send(const PINetworkAddress & addr, const PIByteArray & data, bool threaded = false);
bool canWrite() const override { return mode() & WriteOnly; }
@@ -460,17 +393,17 @@ public:
//! Returns all system network interfaces
static InterfaceList interfaces();
static PIEthernet::Address interfaceAddress(const PIString & interface_);
static PINetworkAddress interfaceAddress(const PIString & interface_);
//! Returns all system network IP addresses
static PIVector<PIEthernet::Address> allAddresses();
static PIVector<PINetworkAddress> allAddresses();
static PIString macFromBytes(const PIByteArray & mac);
static PIByteArray macToBytes(const PIString & mac);
static PIString applyMask(const PIString & ip, const PIString & mask);
static Address applyMask(const Address & ip, const Address & mask);
static PINetworkAddress applyMask(const PINetworkAddress & ip, const PINetworkAddress & mask);
static PIString getBroadcast(const PIString & ip, const PIString & mask);
static Address getBroadcast(const Address & ip, const Address & mask);
static PINetworkAddress getBroadcast(const PINetworkAddress & ip, const PINetworkAddress & mask);
//! \events
//! \{
@@ -540,7 +473,7 @@ protected:
PRIVATE_DECLARATION(PIP_EXPORT)
int sock, sock_s;
std::atomic_bool connected_, connecting_, listen_threaded, server_bounded;
mutable Address addr_r, addr_s, addr_lr;
mutable PINetworkAddress addr_r, addr_s, addr_lr;
Type eth_type;
PIThread server_thread_;
PIMutex clients_mutex;
@@ -580,19 +513,5 @@ inline bool operator==(const PIEthernet::Interface & v0, const PIEthernet::Inter
inline bool operator!=(const PIEthernet::Interface & v0, const PIEthernet::Interface & v1) {
return (v0.name != v1.name || v0.address != v1.address || v0.netmask != v1.netmask);
}
inline PICout operator<<(PICout s, const PIEthernet::Address & v) {
s.space();
s.saveAndSetControls(0);
s << "Address(" << v.toString() << ")";
s.restoreControls();
return s;
}
inline bool operator==(const PIEthernet::Address & v0, const PIEthernet::Address & v1) {
return (v0.ip() == v1.ip() && v0.port() == v1.port());
}
inline bool operator!=(const PIEthernet::Address & v0, const PIEthernet::Address & v1) {
return (v0.ip() != v1.ip() || v0.port() != v1.port());
}
#endif // PIETHERNET_H