3.10.2013 - PIPeer release, PIConsole now can work as server and remote client. Remote console test program in directory "remote_console"
This commit is contained in:
68
piethernet.h
68
piethernet.h
@@ -46,8 +46,10 @@ public:
|
||||
void setSendAddress(const PIString & ip_port) {parseAddress(ip_port, &ip_s, &port_s);}
|
||||
void setSendIP(const PIString & ip) {ip_s = ip;}
|
||||
void setSendPort(int port) {port_s = port;}
|
||||
PIString readAddress() {return path_;}
|
||||
PIString readIP() {parseAddress(path_, &ip_, &port_); return ip_;}
|
||||
int readPort() {parseAddress(path_, &ip_, &port_); return port_;}
|
||||
PIString sendAddress() {return ip_s + ":" + PIString::fromNumber(port_s);}
|
||||
PIString sendIP() {return ip_s;}
|
||||
int sendPort() {return port_s;}
|
||||
|
||||
@@ -83,14 +85,57 @@ public:
|
||||
int read(void * read_to, int max_size);
|
||||
int write(const void * data, int max_size);
|
||||
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
|
||||
|
||||
static PIStringList interfaces();
|
||||
static PIString interfaceAddress(const PIString & interface_);
|
||||
static PIStringList allAddresses();
|
||||
|
||||
EVENT1(newConnection, PIEthernet * , client)
|
||||
EVENT0(connected)
|
||||
EVENT1(disconnected, bool, withError)
|
||||
|
||||
enum InterfaceFlag {
|
||||
ifActive = 0x1,
|
||||
ifRunning = 0x2,
|
||||
ifBroadcast = 0x4,
|
||||
ifMulticast = 0x8,
|
||||
ifLoopback = 0x10,
|
||||
ifPTP = 0x20
|
||||
};
|
||||
|
||||
typedef PIFlags<InterfaceFlag> InterfaceFlags;
|
||||
|
||||
struct Interface {
|
||||
int index;
|
||||
PIString name;
|
||||
PIString mac;
|
||||
PIString address;
|
||||
PIString netmask;
|
||||
PIString broadcast;
|
||||
PIString ptp;
|
||||
InterfaceFlags flags;
|
||||
bool isActive() const {return flags[PIEthernet::ifActive];}
|
||||
bool isRunning() const {return flags[PIEthernet::ifRunning];}
|
||||
bool isBroadcast() const {return flags[PIEthernet::ifBroadcast];}
|
||||
bool isMulticast() const {return flags[PIEthernet::ifMulticast];}
|
||||
bool isLoopback() const {return flags[PIEthernet::ifLoopback];}
|
||||
bool isPTP() const {return flags[PIEthernet::ifPTP];}
|
||||
};
|
||||
|
||||
class InterfaceList: public PIVector<PIEthernet::Interface> {
|
||||
public:
|
||||
InterfaceList(): PIVector<PIEthernet::Interface>() {}
|
||||
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 * 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 * 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 * getLoopback() const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].isLoopback()) return &((*this)[i]); return 0;}
|
||||
};
|
||||
|
||||
static InterfaceList interfaces();
|
||||
static PIString interfaceAddress(const PIString & interface_);
|
||||
static PIStringList allAddresses();
|
||||
|
||||
static void parseAddress(const PIString & ipp, PIString * ip, int * port);
|
||||
static PIString macFromBytes(const PIByteArray & mac) {PIString r; for (int i = 0; i < mac.size_s(); ++i) {r += PIString::fromNumber(mac[i], 16).expandLeftTo(2, '0'); if (i < mac.size_s() - 1) r += ":";} return r;}
|
||||
static PIByteArray macToBytes(const PIString & mac) {PIByteArray r; PIStringList sl = mac.split(":"); piForeachC (PIString & i, sl) r << uchar(i.toInt(16)); return r;}
|
||||
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));}
|
||||
|
||||
protected:
|
||||
PIEthernet(int sock, PIString ip_port);
|
||||
@@ -101,11 +146,12 @@ protected:
|
||||
bool openDevice();
|
||||
bool closeDevice();
|
||||
#ifdef WINDOWS
|
||||
void closeSocket(int & sd) {if (sd != -1) closesocket(sd); sd = -1;}
|
||||
void closeSocket(int & sd) {if (sd != -1) {shutdown(sd, SD_BOTH); closesocket(sd);} sd = -1;}
|
||||
#else
|
||||
void closeSocket(int & sd) {if (sd != -1) ::close(sd); sd = -1;}
|
||||
void closeSocket(int & sd) {if (sd != -1) {shutdown(sock, SHUT_RDWR); ::close(sd);} sd = -1;}
|
||||
static PIString getSockAddr(sockaddr * s) {return s == 0 ? PIString() : PIString(inet_ntoa(((sockaddr_in*)s)->sin_addr));}
|
||||
#endif
|
||||
void parseAddress(const PIString & ipp, PIString * ip, int * port);
|
||||
|
||||
|
||||
int sock, sock_s, port_, port_s, port_c, wrote;
|
||||
bool connected_;
|
||||
@@ -125,10 +171,10 @@ private:
|
||||
|
||||
static std::string ethErrorString() {
|
||||
#ifdef WINDOWS
|
||||
char * msg;
|
||||
int err = WSAGetLastError();
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
|
||||
return "code " + itos(err) + " - " + string(msg);
|
||||
char * msg;
|
||||
int err = WSAGetLastError();
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
|
||||
return "code " + itos(err) + " - " + string(msg);
|
||||
#else
|
||||
return errorString();
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user