code format
This commit is contained in:
@@ -5,61 +5,61 @@
|
||||
* \~russian Устройство Ethernet
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP/TCP Broadcast/Multicast
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Ethernet, UDP/TCP Broadcast/Multicast
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIETHERNET_H
|
||||
#define PIETHERNET_H
|
||||
|
||||
#include "pitimer.h"
|
||||
#include "piiodevice.h"
|
||||
#include "pitimer.h"
|
||||
|
||||
#ifdef ANDROID
|
||||
struct
|
||||
struct
|
||||
#else
|
||||
class
|
||||
class
|
||||
#endif
|
||||
sockaddr;
|
||||
|
||||
class PIP_EXPORT PIEthernet: public PIIODevice
|
||||
{
|
||||
class PIP_EXPORT PIEthernet: public PIIODevice {
|
||||
PIIODEVICE(PIEthernet, "eth");
|
||||
friend class PIPeer;
|
||||
|
||||
public:
|
||||
|
||||
//! Contructs UDP %PIEthernet with empty read address
|
||||
explicit PIEthernet();
|
||||
|
||||
//! \brief Type of %PIEthernet
|
||||
enum Type {
|
||||
UDP /** UDP - User Datagram Protocol */ ,
|
||||
TCP_Client /** TCP client - allow connection to TCP server */ ,
|
||||
UDP /** UDP - User Datagram Protocol */,
|
||||
TCP_Client /** TCP client - allow connection to TCP server */,
|
||||
TCP_Server /** TCP server - receive connections from TCP clients */
|
||||
};
|
||||
|
||||
|
||||
//! \brief Parameters of %PIEthernet
|
||||
enum Parameters {
|
||||
ReuseAddress /** Rebind address if there is already binded. Enabled by default */ = 0x1,
|
||||
Broadcast /** Broadcast send. Disabled by default */ = 0x2,
|
||||
Broadcast /** Broadcast send. Disabled by default */ = 0x2,
|
||||
SeparateSockets /** If this parameter is set, %PIEthernet will initialize two different sockets,
|
||||
for receive and send, instead of single one. Disabled by default */ = 0x4,
|
||||
MulticastLoop /** Enable receiving multicast packets from same host. Enabled by default */ = 0x8,
|
||||
KeepConnection /** Automatic reconnect TCP connection on disconnect. Enabled by default */ = 0x10,
|
||||
for receive and send, instead of single one. Disabled by default */
|
||||
= 0x4,
|
||||
MulticastLoop /** Enable receiving multicast packets from same host. Enabled by default */ = 0x8,
|
||||
KeepConnection /** Automatic reconnect TCP connection on disconnect. Enabled by default */ = 0x10,
|
||||
DisonnectOnTimeout /** Disconnect TCP connection on read timeout expired. Disabled by default */ = 0x20
|
||||
};
|
||||
|
||||
@@ -67,8 +67,8 @@ public:
|
||||
//! \brief IPv4 network address, IP and port
|
||||
class PIP_EXPORT Address {
|
||||
friend class PIEthernet;
|
||||
public:
|
||||
|
||||
public:
|
||||
//! Contructs %Address with binary representation of IP and port
|
||||
Address(uint ip = 0, ushort port = 0);
|
||||
|
||||
@@ -79,10 +79,10 @@ public:
|
||||
Address(const PIString & ip, ushort port);
|
||||
|
||||
//! Returns binary IP
|
||||
uint ip() const {return ip_;}
|
||||
uint ip() const { return ip_; }
|
||||
|
||||
//! Returns port
|
||||
ushort port() const {return port_;}
|
||||
ushort port() const { return port_; }
|
||||
|
||||
//! Returns string IP
|
||||
PIString ipString() const;
|
||||
@@ -121,6 +121,7 @@ public:
|
||||
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 {
|
||||
@@ -132,301 +133,351 @@ public:
|
||||
|
||||
|
||||
//! Contructs %PIEthernet with type "type", read address "ip_port" and parameters "params"
|
||||
explicit PIEthernet(Type type, const PIString & ip_port = PIString(), const PIFlags<Parameters> params = PIEthernet::ReuseAddress | PIEthernet::MulticastLoop | PIEthernet::KeepConnection);
|
||||
|
||||
explicit PIEthernet(Type type,
|
||||
const PIString & ip_port = PIString(),
|
||||
const PIFlags<Parameters> params = PIEthernet::ReuseAddress | PIEthernet::MulticastLoop |
|
||||
PIEthernet::KeepConnection);
|
||||
|
||||
virtual ~PIEthernet();
|
||||
|
||||
|
||||
|
||||
//! Set read address
|
||||
void setReadAddress(const PIString & ip, int port) {addr_r.set(ip, port); setPath(addr_r.toString());}
|
||||
|
||||
void setReadAddress(const PIString & ip, int port) {
|
||||
addr_r.set(ip, port);
|
||||
setPath(addr_r.toString());
|
||||
}
|
||||
|
||||
//! Set read address in format "i.i.i.i:p"
|
||||
void setReadAddress(const PIString & ip_port) {addr_r.set(ip_port); setPath(addr_r.toString());}
|
||||
void setReadAddress(const PIString & ip_port) {
|
||||
addr_r.set(ip_port);
|
||||
setPath(addr_r.toString());
|
||||
}
|
||||
|
||||
//! Set read address
|
||||
void setReadAddress(const Address & addr) {addr_r = addr; setPath(addr_r.toString());}
|
||||
|
||||
void setReadAddress(const Address & addr) {
|
||||
addr_r = addr;
|
||||
setPath(addr_r.toString());
|
||||
}
|
||||
|
||||
//! Set read IP
|
||||
void setReadIP(const PIString & ip) {addr_r.setIP(ip); setPath(addr_r.toString());}
|
||||
|
||||
void setReadIP(const PIString & ip) {
|
||||
addr_r.setIP(ip);
|
||||
setPath(addr_r.toString());
|
||||
}
|
||||
|
||||
//! Set read port
|
||||
void setReadPort(int port) {addr_r.setPort(port); setPath(addr_r.toString());}
|
||||
|
||||
|
||||
void setReadPort(int port) {
|
||||
addr_r.setPort(port);
|
||||
setPath(addr_r.toString());
|
||||
}
|
||||
|
||||
|
||||
//! Set send address
|
||||
void setSendAddress(const PIString & ip, int port) {addr_s.set(ip, port);}
|
||||
|
||||
void setSendAddress(const PIString & ip, int port) { addr_s.set(ip, port); }
|
||||
|
||||
//! Set send address in format "i.i.i.i:p"
|
||||
void setSendAddress(const PIString & ip_port) {addr_s.set(ip_port);}
|
||||
void setSendAddress(const PIString & ip_port) { addr_s.set(ip_port); }
|
||||
|
||||
//! Set send address
|
||||
void setSendAddress(const Address & addr) {addr_s = addr;}
|
||||
|
||||
//! Set send IP
|
||||
void setSendIP(const PIString & ip) {addr_s.setIP(ip);}
|
||||
|
||||
//! Set send port
|
||||
void setSendPort(int port) {addr_s.setPort(port);}
|
||||
|
||||
|
||||
//! Returns read address in format "i.i.i.i:p"
|
||||
Address readAddress() const {return addr_r;}
|
||||
|
||||
//! Returns read IP
|
||||
PIString readIP() const {return addr_r.ipString();}
|
||||
|
||||
//! Returns read port
|
||||
int readPort() const {return addr_r.port();}
|
||||
|
||||
|
||||
//! Returns send address in format "i.i.i.i:p"
|
||||
Address sendAddress() const {return addr_s;}
|
||||
|
||||
//! Returns send IP
|
||||
PIString sendIP() const {return addr_s.ipString();}
|
||||
|
||||
//! Returns send port
|
||||
int sendPort() const {return addr_s.port();}
|
||||
|
||||
|
||||
//! Returns address of last received UDP packet in format "i.i.i.i:p"
|
||||
Address lastReadAddress() const {return addr_lr;}
|
||||
|
||||
//! Returns IP of last received UDP packet
|
||||
PIString lastReadIP() const {return addr_lr.ipString();}
|
||||
|
||||
//! Returns port of last received UDP packet
|
||||
int lastReadPort() const {return addr_lr.port();}
|
||||
void setSendAddress(const Address & addr) { addr_s = addr; }
|
||||
|
||||
//! Set send IP
|
||||
void setSendIP(const PIString & ip) { addr_s.setIP(ip); }
|
||||
|
||||
//! Set send port
|
||||
void setSendPort(int port) { addr_s.setPort(port); }
|
||||
|
||||
|
||||
//! Returns read address in format "i.i.i.i:p"
|
||||
Address readAddress() const { return addr_r; }
|
||||
|
||||
//! Returns read IP
|
||||
PIString readIP() const { return addr_r.ipString(); }
|
||||
|
||||
//! Returns read port
|
||||
int readPort() const { return addr_r.port(); }
|
||||
|
||||
|
||||
//! Returns send address in format "i.i.i.i:p"
|
||||
Address sendAddress() const { return addr_s; }
|
||||
|
||||
//! Returns send IP
|
||||
PIString sendIP() const { return addr_s.ipString(); }
|
||||
|
||||
//! Returns send port
|
||||
int sendPort() const { return addr_s.port(); }
|
||||
|
||||
|
||||
//! Returns address of last received UDP packet in format "i.i.i.i:p"
|
||||
Address lastReadAddress() const { return addr_lr; }
|
||||
|
||||
//! Returns IP of last received UDP packet
|
||||
PIString lastReadIP() const { return addr_lr.ipString(); }
|
||||
|
||||
//! Returns port of last received UDP packet
|
||||
int lastReadPort() const { return addr_lr.port(); }
|
||||
|
||||
|
||||
|
||||
//! Set parameters to "parameters_". You should to reopen %PIEthernet to apply them
|
||||
void setParameters(PIFlags<PIEthernet::Parameters> parameters_) {params = parameters_;}
|
||||
|
||||
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);}
|
||||
|
||||
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];}
|
||||
|
||||
bool isParameterSet(PIEthernet::Parameters parameter) const { return params[parameter]; }
|
||||
|
||||
//! Returns parameters
|
||||
PIFlags<PIEthernet::Parameters> parameters() const {return params;}
|
||||
PIFlags<PIEthernet::Parameters> parameters() const { return params; }
|
||||
|
||||
//! Returns %PIEthernet type
|
||||
Type type() const {return eth_type;}
|
||||
|
||||
//! Returns read timeout
|
||||
double readTimeout() const {return property("readTimeout").toDouble();}
|
||||
|
||||
//! Returns write timeout
|
||||
double writeTimeout() const {return property("writeTimeout").toDouble();}
|
||||
|
||||
//! Set timeout for read
|
||||
void setReadTimeout(double ms) {setProperty("readTimeout", ms);}
|
||||
|
||||
//! Set timeout for write
|
||||
void setWriteTimeout(double ms) {setProperty("writeTimeout", ms);}
|
||||
|
||||
|
||||
//! Returns TTL (Time To Live)
|
||||
int TTL() const {return property("TTL").toInt();}
|
||||
|
||||
//! Returns multicast TTL (Time To Live)
|
||||
int multicastTTL() const {return property("MulticastTTL").toInt();}
|
||||
|
||||
//! Set TTL (Time To Live), default is 64
|
||||
void setTTL(int ttl) {setProperty("TTL", ttl);}
|
||||
|
||||
//! Set multicast TTL (Time To Live), default is 1
|
||||
void setMulticastTTL(int ttl) {setProperty("MulticastTTL", ttl);}
|
||||
Type type() const { return eth_type; }
|
||||
|
||||
//! Returns read timeout
|
||||
double readTimeout() const { return property("readTimeout").toDouble(); }
|
||||
|
||||
//! Returns write timeout
|
||||
double writeTimeout() const { return property("writeTimeout").toDouble(); }
|
||||
|
||||
//! Set timeout for read
|
||||
void setReadTimeout(double ms) { setProperty("readTimeout", ms); }
|
||||
|
||||
//! Set timeout for write
|
||||
void setWriteTimeout(double ms) { setProperty("writeTimeout", ms); }
|
||||
|
||||
|
||||
//! Returns TTL (Time To Live)
|
||||
int TTL() const { return property("TTL").toInt(); }
|
||||
|
||||
//! Returns multicast TTL (Time To Live)
|
||||
int multicastTTL() const { return property("MulticastTTL").toInt(); }
|
||||
|
||||
//! Set TTL (Time To Live), default is 64
|
||||
void setTTL(int ttl) { setProperty("TTL", ttl); }
|
||||
|
||||
//! Set multicast TTL (Time To Live), default is 1
|
||||
void setMulticastTTL(int ttl) { setProperty("MulticastTTL", ttl); }
|
||||
|
||||
|
||||
|
||||
//! 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);
|
||||
|
||||
|
||||
//! Returns joined multicast groups. Use only for UDP
|
||||
const PIStringList & multicastGroups() const {return mcast_groups;}
|
||||
|
||||
|
||||
const PIStringList & multicastGroups() const { return mcast_groups; }
|
||||
|
||||
|
||||
//! If \"threaded\" queue connect to TCP server with address \a readAddress() in
|
||||
//! any \a read() or \a write() call. Otherwise connect immediate.
|
||||
//! Use only for TCP_Client
|
||||
bool connect(bool threaded = true);
|
||||
|
||||
|
||||
//! Connect to TCP server with address "ip":"port". Use only for TCP_Client
|
||||
bool connect(const PIString & ip, int port, bool threaded = true) {setPath(ip + PIStringAscii(":") + PIString::fromNumber(port)); return connect(threaded);}
|
||||
|
||||
bool connect(const PIString & ip, int port, bool threaded = true) {
|
||||
setPath(ip + PIStringAscii(":") + PIString::fromNumber(port));
|
||||
return connect(threaded);
|
||||
}
|
||||
|
||||
//! Connect to TCP server with address "ip_port". Use only for TCP_Client
|
||||
bool connect(const PIString & ip_port, bool threaded = true) {setPath(ip_port); return connect(threaded);}
|
||||
bool connect(const PIString & ip_port, bool threaded = true) {
|
||||
setPath(ip_port);
|
||||
return connect(threaded);
|
||||
}
|
||||
|
||||
//! Connect to TCP server with address "addr". Use only for TCP_Client
|
||||
bool connect(const Address & addr, bool threaded = true) {setPath(addr.toString()); return connect(threaded);}
|
||||
|
||||
bool connect(const Address & addr, bool threaded = true) {
|
||||
setPath(addr.toString());
|
||||
return connect(threaded);
|
||||
}
|
||||
|
||||
//! Returns if %PIEthernet connected to TCP server. Use only for TCP_Client
|
||||
bool isConnected() const {return connected_;}
|
||||
|
||||
bool isConnected() const { return connected_; }
|
||||
|
||||
//! Returns if %PIEthernet is connecting to TCP server. Use only for TCP_Client
|
||||
bool isConnecting() const {return connecting_;}
|
||||
|
||||
|
||||
bool isConnecting() const { return connecting_; }
|
||||
|
||||
|
||||
//! Start listen for incoming TCP connections on address \a readAddress(). Use only for TCP_Server
|
||||
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(PIEthernet::Address(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(PIEthernet::Address(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);
|
||||
|
||||
PIEthernet * client(int index) {return clients_[index];}
|
||||
int clientsCount() const {return clients_.size_s();}
|
||||
PIVector<PIEthernet * > clients() const {return clients_;}
|
||||
PIEthernet * client(int index) { return clients_[index]; }
|
||||
int clientsCount() const { return clients_.size_s(); }
|
||||
PIVector<PIEthernet *> clients() const { return clients_; }
|
||||
|
||||
|
||||
|
||||
//! 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);
|
||||
|
||||
|
||||
//! 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);}
|
||||
|
||||
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {
|
||||
return send(PIEthernet::Address(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);}
|
||||
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {
|
||||
return send(PIEthernet::Address(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);
|
||||
|
||||
|
||||
//! 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);}
|
||||
|
||||
bool send(const PIString & ip, int port, const PIByteArray & data, bool threaded = false) {
|
||||
return send(PIEthernet::Address(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);}
|
||||
bool send(const PIString & ip_port, const PIByteArray & data, bool threaded = false) {
|
||||
return send(PIEthernet::Address(ip_port), data, threaded);
|
||||
}
|
||||
|
||||
//! Send data "data" to address "addr" for UDP
|
||||
bool send(const Address & addr, const PIByteArray & data, bool threaded = false);
|
||||
|
||||
bool canWrite() const override {return mode() & WriteOnly;}
|
||||
|
||||
bool canWrite() const override { return mode() & WriteOnly; }
|
||||
|
||||
void interrupt() override;
|
||||
|
||||
int socket() const {return sock;}
|
||||
|
||||
EVENT1(newConnection, PIEthernet * , client);
|
||||
int socket() const { return sock; }
|
||||
|
||||
EVENT1(newConnection, PIEthernet *, client);
|
||||
EVENT0(connected);
|
||||
EVENT1(disconnected, bool, withError);
|
||||
|
||||
|
||||
|
||||
|
||||
//! Flags of network interface
|
||||
enum InterfaceFlag {
|
||||
ifActive /** Is active */ = 0x1,
|
||||
ifRunning /** Is running */ = 0x2,
|
||||
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
|
||||
ifLoopback /** Is loopback */ = 0x10,
|
||||
ifPTP /** Is point-to-point */ = 0x20
|
||||
};
|
||||
|
||||
|
||||
//! %PIFlags of network interface flags
|
||||
typedef PIFlags<InterfaceFlag> InterfaceFlags;
|
||||
|
||||
|
||||
|
||||
|
||||
//! Network interface descriptor
|
||||
struct PIP_EXPORT Interface {
|
||||
|
||||
//! System index
|
||||
int index;
|
||||
|
||||
|
||||
//! MTU
|
||||
int mtu;
|
||||
|
||||
|
||||
//! 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];}
|
||||
|
||||
bool isActive() const { return flags[PIEthernet::ifActive]; }
|
||||
|
||||
//! Returns if interface is running
|
||||
bool isRunning() const {return flags[PIEthernet::ifRunning];}
|
||||
|
||||
bool isRunning() const { return flags[PIEthernet::ifRunning]; }
|
||||
|
||||
//! Returns if interface support broadcast
|
||||
bool isBroadcast() const {return flags[PIEthernet::ifBroadcast];}
|
||||
|
||||
bool isBroadcast() const { return flags[PIEthernet::ifBroadcast]; }
|
||||
|
||||
//! Returns if interface support multicast
|
||||
bool isMulticast() const {return flags[PIEthernet::ifMulticast];}
|
||||
|
||||
bool isMulticast() const { return flags[PIEthernet::ifMulticast]; }
|
||||
|
||||
//! Returns if interface is loopback
|
||||
bool isLoopback() const {return flags[PIEthernet::ifLoopback];}
|
||||
|
||||
bool isLoopback() const { return flags[PIEthernet::ifLoopback]; }
|
||||
|
||||
//! Returns if interface is point-to-point
|
||||
bool isPTP() const {return flags[PIEthernet::ifPTP];}
|
||||
bool isPTP() const { return flags[PIEthernet::ifPTP]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
//! Array of \a Interface with some features
|
||||
class PIP_EXPORT 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;}
|
||||
|
||||
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;}
|
||||
|
||||
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;}
|
||||
|
||||
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;}
|
||||
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 PIEthernet::Address interfaceAddress(const PIString & interface_);
|
||||
|
||||
|
||||
//! Returns all system network IP addresses
|
||||
static PIVector<PIEthernet::Address> 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 Address applyMask(const Address & ip, const Address & mask);
|
||||
static PIString getBroadcast(const PIString & ip, const PIString & mask);
|
||||
static Address getBroadcast(const Address & ip, const Address & mask);
|
||||
static Address getBroadcast(const Address & ip, const Address & mask);
|
||||
|
||||
//! \events
|
||||
//! \{
|
||||
//! \events
|
||||
//! \{
|
||||
|
||||
//! \fn void newConnection(PIEthernet * client)
|
||||
//! \brief Raise on new TCP connection received
|
||||
|
||||
|
||||
//! \fn void connected()
|
||||
//! \brief Raise if succesfull TCP connection
|
||||
|
||||
@@ -439,32 +490,32 @@ public:
|
||||
#ifdef DOXYGEN
|
||||
//! \brief read ip, default ""
|
||||
string ip;
|
||||
|
||||
|
||||
//! \brief read port, default 0
|
||||
int port;
|
||||
|
||||
|
||||
//! \brief ethernet parameters
|
||||
int parameters;
|
||||
|
||||
|
||||
//! \brief read timeout, default 1000 ms
|
||||
double readTimeout;
|
||||
|
||||
|
||||
//! \brief write timeout, default 1000 ms
|
||||
double writeTimeout;
|
||||
|
||||
|
||||
//! \brief time-to-live, default 64
|
||||
int TTL;
|
||||
|
||||
|
||||
//! \brief time-to-live for multicast, default 1
|
||||
int multicastTTL;
|
||||
#endif
|
||||
//! \}
|
||||
//! \}
|
||||
|
||||
protected:
|
||||
explicit PIEthernet(int sock, PIString ip_port);
|
||||
|
||||
void propertyChanged(const char * name) override;
|
||||
|
||||
|
||||
PIString constructFullPathDevice() const override;
|
||||
void configureFromFullPathDevice(const PIString & full_path) override;
|
||||
PIPropertyStorage constructVariantDevice() const override;
|
||||
@@ -475,7 +526,7 @@ protected:
|
||||
DeviceInfoFlags deviceInfoFlags() const override;
|
||||
|
||||
//! Executes when any read function was successful. Default implementation does nothing
|
||||
virtual void received(const void * data, int size) {;}
|
||||
virtual void received(const void * data, int size) { ; }
|
||||
|
||||
void construct();
|
||||
bool init();
|
||||
@@ -485,7 +536,7 @@ protected:
|
||||
void applyTimeouts();
|
||||
void applyTimeout(int fd, int opt, double ms);
|
||||
void applyOptInt(int level, int opt, int val);
|
||||
|
||||
|
||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||
int sock, sock_s;
|
||||
std::atomic_bool connected_, connecting_, listen_threaded, server_bounded;
|
||||
@@ -493,7 +544,7 @@ protected:
|
||||
Type eth_type;
|
||||
PIThread server_thread_;
|
||||
PIMutex clients_mutex;
|
||||
PIVector<PIEthernet * > clients_;
|
||||
PIVector<PIEthernet *> clients_;
|
||||
PIQueue<PIString> mcast_queue;
|
||||
PIStringList mcast_groups;
|
||||
PIFlags<PIEthernet::Parameters> params;
|
||||
@@ -518,15 +569,30 @@ private:
|
||||
static int ethSetsockoptBool(int sock, int level, int optname, bool value = true);
|
||||
static void ethNonblocking(int sock);
|
||||
static bool ethIsWriteable(int sock);
|
||||
|
||||
};
|
||||
|
||||
inline bool operator <(const PIEthernet::Interface & v0, const PIEthernet::Interface & v1) {return (v0.name < v1.name);}
|
||||
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 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::Interface & v0, const PIEthernet::Interface & v1) {
|
||||
return (v0.name < v1.name);
|
||||
}
|
||||
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 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());}
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user