diff --git a/src/io/pipeer.cpp b/src/io/pipeer.cpp index 512df631..00f5ed9d 100755 --- a/src/io/pipeer.cpp +++ b/src/io/pipeer.cpp @@ -260,10 +260,31 @@ bool PIPeer::dataRead(uchar * readed, int size) { PIMutexLocker locker(eth_mutex); PIMutexLocker plocker(peers_mutex); //piCout << "[PIPeer \"" + name_ + "\"] Received packet" << type; - if (type == 5) { // ping + if (type == 5) { // ping request + PIString addr; + PISystemTime time; + ba >> to >> from >> addr >> time; + piCout << "ping request" << to << from << addr; + if (from == self_info.name) { // send ping back + const PeerInfo * pi = getPeerByName(to); + if (pi) { + if (pi->isNeighbour()) { + sba << int(6) << to << from << addr << time; + //piCout << "ping from" << from << addr << ", send back to" << pi->name; + piForeachC (PeerInfo::Address & a, pi->addresses) { + if (eth_send.send(a.address, sba)) + diag_s.received(sba.size_s()); + } + } + } + } + return true; + } + if (type == 6) { // ping request PIString addr; PISystemTime time, ptime, ctime = PISystemTime::current(true); ba >> to >> from >> addr >> time; + piCout << "ping reply" << to << from << addr; if (to == self_info.name) { // ping echo piForeach (PeerInfo & p, peers) { if (!p.isNeighbour()) continue; @@ -282,22 +303,6 @@ bool PIPeer::dataRead(uchar * readed, int size) { } return true; } - if (from == self_info.name) { // send ping back - const PeerInfo * pi = getPeerByName(to); - if (pi) { - if (pi->isNeighbour()) { - sba = PIByteArray(readed, size); - piCout << "ping from" << to << addr << ", send back to" << pi->name; - piForeachC (PeerInfo::Address & a, pi->addresses) { - if (eth_send.send(a.address, sba)) - diag_s.received(sba.size_s()); - } - } - return true; - } - } - //PIEthernet * eth = (PIEthernet*)emitter(); - //()->send(); return true; } if (type != 4) return true; @@ -538,7 +543,7 @@ void PIPeer::pingNeighbours() { a.wait_ping = true; sba = ba; sba << p.name << a.address << PISystemTime::current(true); - //piCout << "ping" << p.name << a.address << a.last_ping; + piCout << "ping" << p.name << a.address << a.last_ping; if (eth_send.send(a.address, sba)) diag_s.sended(sba.size_s()); } diff --git a/src/io/pipeer.h b/src/io/pipeer.h index c12fbf43..d016bb04 100755 --- a/src/io/pipeer.h +++ b/src/io/pipeer.h @@ -158,7 +158,7 @@ private: bool sendToNeighbour(PeerInfo * peer, const PIByteArray & ba); inline static bool isPeerRecent(const PeerInfo & my, const PeerInfo & income) {return (my.cnt < income.cnt) || (my.time < income.time);} - // 1 - new peer, 2 - remove peer, 3 - sync peers, 4 - data, 5 - ping + // 1 - new peer, 2 - remove peer, 3 - sync peers, 4 - data, 5 - ping request, 6 - ping reply // Data packet: 4, from, to, ticks, data_size, data typedef PIPair > napair;