diff --git a/CMakeLists.txt b/CMakeLists.txt index d42367b0..cb4d942d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ project(pip) cmake_minimum_required(VERSION 2.6) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -pg") include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) include(CheckFunctionExists) diff --git a/src/containers/pideque.h b/src/containers/pideque.h index 26291534..6ec1b102 100755 --- a/src/containers/pideque.h +++ b/src/containers/pideque.h @@ -81,7 +81,7 @@ public: PIDeque * parent; size_t pos; public: - iterator(): parent(0) {} + iterator(): parent(0), pos(0) {} T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {++pos;} @@ -99,7 +99,7 @@ public: const PIDeque * parent; size_t pos; public: - const_iterator(): parent(0) {} + const_iterator(): parent(0), pos(0) {} //T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {++pos;} @@ -117,7 +117,7 @@ public: PIDeque * parent; size_t pos; public: - reverse_iterator(): parent(0) {} + reverse_iterator(): parent(0), pos(0) {} T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {--pos;} @@ -135,7 +135,7 @@ public: const PIDeque * parent; size_t pos; public: - const_reverse_iterator(): parent(0) {} + const_reverse_iterator(): parent(0), pos(0) {} //T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {--pos;} @@ -333,7 +333,7 @@ private: for (size_t i = 0; i < s; ++i) elementNew(dst + i, src[i]); } - inline T * newRaw(size_t s) { + static T * newRaw(size_t s) { //std::cout << std::dec << " ![("< ok]~" << endl; } - inline void deleteRaw(T *& d) { + static void deleteRaw(T *& d) { //cout << " ~[("< ok]~" << endl; } - void zeroRaw(T * d, size_t s) { + static void zeroRaw(T * d, size_t s) { //cout << " ~[("< ok]~" << endl; diff --git a/src/containers/pimap.h b/src/containers/pimap.h index f910dad1..f4f1640f 100644 --- a/src/containers/pimap.h +++ b/src/containers/pimap.h @@ -77,7 +77,7 @@ public: const PIMap * parent; ssize_t pos; public: - iterator(): parent(0) {} + iterator(): parent(0), pos(0) {} const Key & key() const {return const_cast * >(parent)->_key(pos);} T & value() const {return const_cast * >(parent)->_value(pos);} void operator ++() {++pos;} @@ -95,7 +95,7 @@ public: const PIMap * parent; ssize_t pos; public: - reverse_iterator(): parent(0) {} + reverse_iterator(): parent(0), pos(0) {} const Key & key() const {return const_cast * >(parent)->_key(pos);} T & value() const {return const_cast * >(parent)->_value(pos);} void operator ++() {--pos;} @@ -113,7 +113,7 @@ public: const PIMap * parent; ssize_t pos; public: - const_iterator(): parent(0) {} + const_iterator(): parent(0), pos(0) {} const PIMap::value_type operator *() const {return parent->_pair(pos);} const PIMap::value_type* operator ->() const {cval = parent->_pair(pos); return &cval;} void operator ++() {++pos;} @@ -132,7 +132,7 @@ public: const PIMap * parent; ssize_t pos; public: - const_reverse_iterator(): parent(0) {} + const_reverse_iterator(): parent(0), pos(0) {} const PIMap::value_type operator *() const {return parent->_pair(pos);} const PIMap::value_type* operator ->() const {cval = parent->_pair(pos); return &cval;} void operator ++() {--pos;} @@ -422,7 +422,7 @@ template inline std::ostream & operator <<(std::ostream & s, const PIMap & v) { s << "{"; bool first = true; - for (typename PIMap::const_iterator i = v.begin(); i != v.end(); i++) { + for (typename PIMap::const_iterator i = v.begin(); i != v.end(); ++i) { if (!first) s << ", "; first = false; @@ -438,7 +438,7 @@ inline PICout operator <<(PICout s, const PIMap & v) { s.setControl(0, true); s << "{"; bool first = true; - for (typename PIMap::const_iterator i = v.begin(); i != v.end(); i++) { + for (typename PIMap::const_iterator i = v.begin(); i != v.end(); ++i) { if (!first) s << ", "; first = false; diff --git a/src/containers/pivector.h b/src/containers/pivector.h index 04db1bd1..d0cdf7e0 100755 --- a/src/containers/pivector.h +++ b/src/containers/pivector.h @@ -103,7 +103,7 @@ public: PIVector * parent; size_t pos; public: - iterator(): parent(0) {} + iterator(): parent(0), pos(0) {} T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {++pos;} @@ -121,7 +121,7 @@ public: const PIVector * parent; size_t pos; public: - const_iterator(): parent(0) {} + const_iterator(): parent(0), pos(0) {} //T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {++pos;} @@ -139,7 +139,7 @@ public: PIVector * parent; size_t pos; public: - reverse_iterator(): parent(0) {} + reverse_iterator(): parent(0), pos(0) {} T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {--pos;} @@ -157,7 +157,7 @@ public: const PIVector * parent; size_t pos; public: - const_reverse_iterator(): parent(0) {} + const_reverse_iterator(): parent(0), pos(0) {} //T & operator *() {return (*parent)[pos];} const T & operator *() const {return (*parent)[pos];} void operator ++() {--pos;} @@ -331,13 +331,13 @@ private: } //cout << " > ok]~" << endl; } - void deleteRaw(T *& d) { + inline void deleteRaw(T *& d) { //cout << " ~[("< ok]~" << endl; } - void zeroRaw(T * d, size_t s) { + inline void zeroRaw(T * d, size_t s) { //cout << " ~[("< ok]~" << endl; diff --git a/src/core/pibitarray.h b/src/core/pibitarray.h index 7b63a04c..6e44222b 100755 --- a/src/core/pibitarray.h +++ b/src/core/pibitarray.h @@ -91,7 +91,7 @@ public: void operator =(const ullong & val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));} private: - uint bytesInBits(const uint & bits) const {return (bits + 7) / 8;} + static uint bytesInBits(const uint & bits) {return (bits + 7) / 8;} PIVector data_; uint size_; diff --git a/src/core/picout.cpp b/src/core/picout.cpp index b04404be..4e36c94e 100644 --- a/src/core/picout.cpp +++ b/src/core/picout.cpp @@ -154,7 +154,7 @@ PICout PICout::operator <<(const PICoutAction v) { memset(line, ' ', dx); line[dx] = 0; printf("%s", line); - delete line; + delete[] line; } SetConsoleCursorPosition(__Private__::hOut, coord); #else diff --git a/src/core/picout.h b/src/core/picout.h index d4e7bb0c..b2ec2e97 100644 --- a/src/core/picout.h +++ b/src/core/picout.h @@ -175,7 +175,7 @@ public: PICout operator <<(const PICoutManipulators::PICoutSpecialChar v); //! Output operator for \a PIFlags values - PICout operator <<(const PIFlags v) { + PICout operator <<(const PIFlags & v) { if (v[PICoutManipulators::Bin]) cnb_ = 2; if (v[PICoutManipulators::Oct]) cnb_ = 8; if (v[PICoutManipulators::Dec]) cnb_ = 10; diff --git a/src/core/pistring.cpp b/src/core/pistring.cpp index b04e2965..cab4de47 100755 --- a/src/core/pistring.cpp +++ b/src/core/pistring.cpp @@ -110,7 +110,7 @@ void PIString::appendFromChars(const char * c, int s, const char * cp) { //printf("appendFromChars %d -> %d\n", s, rs); for (int i = 0; i < rs; ++i) push_back(PIChar(ucs[i])); - delete ucs; + delete[] ucs; ucnv_close(cc); return; } @@ -510,7 +510,7 @@ int PIString::find(const char str, const int start) const { } -int PIString::find(const PIString str, const int start) const { +int PIString::find(const PIString & str, const int start) const { int l = str.length(); for (int i = start; i < length() - l + 1; ++i) if (mid(i, l) == str) @@ -527,7 +527,7 @@ int PIString::findLast(const char str, const int start) const { } -int PIString::findLast(const PIString str, const int start) const { +int PIString::findLast(const PIString & str, const int start) const { int l = str.length(); for (int i = length() - l; i >= start; --i) if (mid(i, l) == str) diff --git a/src/core/pistring.h b/src/core/pistring.h index 09b29798..2497f482 100755 --- a/src/core/pistring.h +++ b/src/core/pistring.h @@ -529,7 +529,7 @@ public: //! \brief Search substring "str" from symbol at index "start" and return first occur position //! \details Example: \snippet pistring.cpp PIString::find - int find(const PIString str, const int start = 0) const; + int find(const PIString & str, const int start = 0) const; //! \brief Search substring "str" from symbol at index "start" and return first occur position //! \details Example: \snippet pistring.cpp PIString::find @@ -541,7 +541,7 @@ public: //! \brief Search substring "str" from symbol at index "start" and return last occur position //! \details Example: \snippet pistring.cpp PIString::findLast - int findLast(const PIString str, const int start = 0) const; + int findLast(const PIString & str, const int start = 0) const; //! \brief Search substring "str" from symbol at index "start" and return last occur position //! \details Example: \snippet pistring.cpp PIString::findLast diff --git a/src/core/pitime.cpp b/src/core/pitime.cpp index 42b50db4..a0254797 100755 --- a/src/core/pitime.cpp +++ b/src/core/pitime.cpp @@ -208,7 +208,7 @@ PISystemTime::PISystemTime(const FILETIME & t) { PISystemTime PISystemTime::abs() const { if (seconds < 0) - return PISystemTime(piAbsl(seconds) - 1, 1e+9 - piAbsl(nanoseconds)); + return PISystemTime(piAbsl(seconds) - 1, 1000000000l - piAbsl(nanoseconds)); else return PISystemTime(piAbsl(seconds), piAbsl(nanoseconds)); } diff --git a/src/core/pitime.h b/src/core/pitime.h index cea67523..0484003d 100755 --- a/src/core/pitime.h +++ b/src/core/pitime.h @@ -298,10 +298,10 @@ public: //! \brief Returns PISystemTime elapsed from last \a reset() execution or from timer measurer creation. PISystemTime elapsed(); - double reset_time_n() {return t_st.toNanoseconds();} - double reset_time_u() {return t_st.toMicroseconds();} - double reset_time_m() {return t_st.toMilliseconds();} - double reset_time_s() {return t_st.toSeconds();} + double reset_time_n() const {return t_st.toNanoseconds();} + double reset_time_u() const {return t_st.toMicroseconds();} + double reset_time_m() const {return t_st.toMilliseconds();} + double reset_time_s() const {return t_st.toSeconds();} //! \brief Returns time mark of last \a reset() execution or timer measurer creation. PISystemTime reset_time() {return t_st;} diff --git a/src/io/pibasetransfer.cpp b/src/io/pibasetransfer.cpp index 202df623..713c828c 100644 --- a/src/io/pibasetransfer.cpp +++ b/src/io/pibasetransfer.cpp @@ -35,7 +35,7 @@ void PIBaseTransfer::stopSend() { void PIBaseTransfer::stopReceive() { if (!is_receiving) return; break_ = true; - piCoutObj << "stopReceive()"; + //piCoutObj << "stopReceive()"; finish_receive(false); } @@ -62,7 +62,7 @@ void PIBaseTransfer::received(PIByteArray data) { diag.received(data.size(), false); return; } else diag.received(data.size(), true); - piCoutObj << "receive" << h.session_id << h.type << h.id; + //piCoutObj << "receive" << h.session_id << h.type << h.id; switch (pt) { case pt_Unknown: break; case pt_Data: @@ -75,7 +75,7 @@ void PIBaseTransfer::received(PIByteArray data) { if (rcrc != ccrc) { header.id = h.id; sendReply(pt_ReplyInvalid); - piCoutObj << "invalid CRC"; + //piCoutObj << "invalid CRC"; } else { processData(h.id, data); } @@ -103,7 +103,7 @@ void PIBaseTransfer::received(PIByteArray data) { break; case pt_Break: break_ = true; - piCoutObj << "BREAK"; + //piCoutObj << "BREAK"; if (is_receiving) { stopReceive(); return; @@ -129,7 +129,7 @@ void PIBaseTransfer::received(PIByteArray data) { if (header.session_id != h.session_id) { //sendBreak(h.session_id); //return; - piCoutObj << "restart receive"; + //piCoutObj << "restart receive"; finish_receive(false, true); } else return; } @@ -148,7 +148,7 @@ void PIBaseTransfer::received(PIByteArray data) { replies.fill(pt_Unknown); diag.reset(); diag.start(100); - piCoutObj << "receiveStarted()"; + //piCoutObj << "receiveStarted()"; is_receiving = true; break_ = false; receiveStarted(); @@ -160,16 +160,16 @@ void PIBaseTransfer::received(PIByteArray data) { break; case pt_Pause: if (header.session_id == h.session_id) { - piCout << "receive pause"; + //piCout << "receive pause"; if (!is_pause && pause_tm.elapsed_s() < timeout_/10) { - piCout << "resume"; + //piCout << "resume"; sendReply(pt_Start); return; } if (!is_pause) paused(); is_pause = true; if (is_receiving && pause_tm.elapsed_m() > 40) { - piCout << "send pause"; + //piCout << "send pause"; sendReply(pt_Pause); } if (is_sending) send_tm.reset(); @@ -203,7 +203,7 @@ bool PIBaseTransfer::send_process() { piMSleep(1); if (is_pause) { piMSleep(40); - piCout << "send pause"; + //piCout << "send pause"; sendReply(pt_Pause); if (pause_tm.elapsed_s() > timeout())return finish_send(false); } @@ -232,7 +232,7 @@ bool PIBaseTransfer::send_process() { } if (is_pause) { piMSleep(40); - piCout << "send pause"; + //piCout << "send pause"; sendReply(pt_Pause); if (pause_tm.elapsed_s() > timeout())return finish_send(false); else continue; @@ -240,7 +240,7 @@ bool PIBaseTransfer::send_process() { prev_chk = chk; if (chk > 0) { if (tm2.elapsed_s() > timeout_ / 10.) { - piCoutObj << "recovery packet" << chk; + //piCoutObj << "recovery packet" << chk; if (send_queue >= packets_count) { piMSleep(10); continue; @@ -270,7 +270,7 @@ int PIBaseTransfer::checkSession() { if (replies[i] != pt_ReplySuccess) return i; } if (miss > 0) { - piCoutObj << "missing" << miss << "packets"; + //piCoutObj << "missing" << miss << "packets"; return -miss; } else return 0; } @@ -289,12 +289,12 @@ void PIBaseTransfer::buildSession(PIVector parts) { for (int i = 0; i < parts.size_s(); i++) { state_string = "calculating parts ... " + PIString::fromNumber(i) + " of " + PIString::fromNumber(parts.size()); fi.id = parts[i].id; - piCout << fi.id << state_string; + //piCout << fi.id << state_string; bytes_all += parts[i].size; //fi.size = fi.entry.size; fi.start = 0; llong rest = parts[i].size - (packet_size - cur_size); - piCout << i << fi.size << rest << bytes_all; + //piCout << i << fi.size << rest << bytes_all; if (rest <= 0) { fi.size = parts[i].size; lfi << fi; @@ -334,7 +334,7 @@ void PIBaseTransfer::buildSession(PIVector parts) { void PIBaseTransfer::sendBreak(int session_id) { - piCoutObj << "sendBreak"; + //piCoutObj << "sendBreak"; uint psid = header.session_id; header.session_id = session_id; sendReply(pt_Break); @@ -343,7 +343,7 @@ void PIBaseTransfer::sendBreak(int session_id) { void PIBaseTransfer::sendReply(PacketType reply) { - piCoutObj << "sendReply" << reply; + //piCoutObj << "sendReply" << reply; header.type = reply; PIByteArray ba; ba << header; @@ -379,7 +379,7 @@ bool PIBaseTransfer::getStartRequest() { void PIBaseTransfer::processData(int id, PIByteArray & data) { - piCoutObj << "received packet" << id << ", size" << data.size(); + //piCoutObj << "received packet" << id << ", size" << data.size(); if (id < 1 || id > replies.size_s()) return; if (!session[id - 1].isEmpty()) { header.id = id; @@ -402,7 +402,7 @@ void PIBaseTransfer::processData(int id, PIByteArray & data) { data >> ba; //fi.fsize = ba.size(); bytes_cur += fi.size; - piCoutObj << "recv" << fi; + //piCoutObj << "recv" << fi; session[id - 1] << fi; state_string = "receiving..."; receivePart(fi, ba, pheader); @@ -419,13 +419,13 @@ PIByteArray PIBaseTransfer::build_packet(int id) { PIByteArray ba; header.id = id + 1; header.type = pt_Data; - piCoutObj << "session id" << header.session_id; + //piCoutObj << "session id" << header.session_id; //ret << header; ret.append(customHeader()); for (int i = 0; i < session[id].size_s(); i++) { Part fi = session[id][i]; ret << fi; - piCout << "biuld" << fi; + //piCout << "biuld" << fi; ba = buildPacket(fi); bytes_cur += ba.size(); if (ba.size() != fi.size) piCoutObj << "***error while build packet, wrong part size"; @@ -434,7 +434,7 @@ PIByteArray PIBaseTransfer::build_packet(int id) { header.crc = crc.calculate(ret); PIByteArray hdr; hdr << header; ret.insert(0, hdr); - piCoutObj << "Packet" << header.id << ret.size(); + //piCoutObj << "Packet" << header.id << ret.size(); return ret; } @@ -443,7 +443,7 @@ bool PIBaseTransfer::finish_send(bool ok) { is_sending = false; if (ok) state_string = "send done"; else state_string = "send failed"; - piCoutObj << state_string << PIString::readableSize(bytes_all); + //piCoutObj << state_string << PIString::readableSize(bytes_all); header.id = 0; if (!ok) sendBreak(header.session_id); else sendReply(pt_ReplySuccess); @@ -458,7 +458,7 @@ void PIBaseTransfer::finish_receive(bool ok, bool quet) { is_receiving = false; if (ok) state_string = "receive done"; else state_string = "receive failed"; - piCoutObj << state_string << PIString::readableSize(bytes_all); + //piCoutObj << state_string << PIString::readableSize(bytes_all); if (!ok && !quet) sendBreak(header.session_id); receiveFinished(ok); diag.stop(); @@ -469,7 +469,7 @@ void PIBaseTransfer::finish_receive(bool ok, bool quet) { void PIBaseTransfer::diagChanged(PIDiagnostics::Quality new_quality, PIDiagnostics::Quality old_quality) { if (is_receiving) { if (new_quality == PIDiagnostics::Failure) { - piCout << "disconnected!"; + //piCout << "disconnected!"; break_ = true; is_receiving = false; state_string = "receive failed"; diff --git a/src/io/pidir.cpp b/src/io/pidir.cpp index 73c72940..3184bd9d 100755 --- a/src/io/pidir.cpp +++ b/src/io/pidir.cpp @@ -274,9 +274,9 @@ PIVector PIDir::entries() { # endif for (int i = 0; i < cnt; ++i) { l << PIFile::fileInfo(dp + PIString(list[i]->d_name)); - delete list[i]; + free(list[i]); } - delete list; + free(list); #endif return l; } diff --git a/src/io/piethernet.cpp b/src/io/piethernet.cpp index 048585ad..2851bef4 100755 --- a/src/io/piethernet.cpp +++ b/src/io/piethernet.cpp @@ -532,13 +532,14 @@ int PIEthernet::read(void * read_to, int max_size) { case UDP: memset(&PRIVATE->raddr_, 0, sizeof(PRIVATE->raddr_)); rs = ethRecvfrom(sock, read_to, max_size, 0, (sockaddr*)&PRIVATE->raddr_); - //piCout << "eth" << path() << "read return" << rs << errorString(); if (rs > 0) { port_r = ntohs(PRIVATE->raddr_.sin_port); ip_r = PIStringAscii(inet_ntoa(PRIVATE->raddr_.sin_addr)); + //piCoutObj << "read from" << ip_r << ":" << port_r << rs << "bytes"; //piCout << "received from" << lastReadAddress(); received(read_to, rs); } + //else piCoutObj << "read returt" << rs << ", error" << ethErrorString(); return rs; //return ::read(sock, read_to, max_size); default: break; @@ -582,7 +583,7 @@ int PIEthernet::write(const void * data, int max_size) { /*if (params[PIEthernet::Broadcast]) PRIVATE->saddr_.sin_addr.s_addr = INADDR_BROADCAST; else*/ PRIVATE->saddr_.sin_addr.s_addr = inet_addr(ip_s.dataAscii()); PRIVATE->saddr_.sin_family = AF_INET; - //piCout << "[PIEth] write to" << ip_s << ":" << port_s << "socket" << sock_s << max_size << "bytes ..."; + //piCoutObj << "write to" << ip_s << ":" << port_s << "socket" << sock_s << max_size << "bytes ..."; return ethSendto(sock_s, data, max_size, 0, (sockaddr * )&PRIVATE->saddr_, sizeof(PRIVATE->saddr_)); //piCout << "[PIEth] write to" << ip_s << ":" << port_s << "ok"; case TCP_Client: @@ -819,7 +820,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() { ifc.ifc_buf = new char[ifc.ifc_len]; if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { piCout << "[PIEthernet] Can`t get interfaces:" << errorString(); - delete ifc.ifc_buf; + delete[] ifc.ifc_buf; return il; } int icnt = ifc.ifc_len / sizeof(ifreq); diff --git a/src/io/pifile.cpp b/src/io/pifile.cpp index d1d1ec0f..4ac9bbd3 100755 --- a/src/io/pifile.cpp +++ b/src/io/pifile.cpp @@ -487,7 +487,7 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info) if (!info.perm_user.write) attr |= FILE_ATTRIBUTE_READONLY; if (SetFileAttributes((LPCTSTR)(fp.data()), attr) == 0) { piCout << "[PIFile] applyFileInfo: \"SetFileAttributes\" error:" << errorString(); - return false; + //return false; } HANDLE hFile = 0; if ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { @@ -515,11 +515,11 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info) if (info.perm_other.exec) mode |= S_IXOTH; if (chmod(fp.data(), mode) != 0) { piCout << "[PIFile] applyFileInfo: \"chmod\" error:" << errorString(); - return false; + //return false; } if (chown(fp.data(), info.id_user, info.id_group) != 0) { piCout << "[PIFile] applyFileInfo: \"chown\" error:" << errorString(); - return false; + //return false; } struct timeval tm[2]; PISystemTime st = info.time_access.toSystemTime(); @@ -530,7 +530,7 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info) tm[1].tv_usec = st.nanoseconds / 1000; if (utimes(fp.data(), tm) != 0) { piCout << "[PIFile] applyFileInfo: \"utimes\" error:" << errorString(); - return false; + //return false; } #endif return true; diff --git a/src/io/piiodevice.cpp b/src/io/piiodevice.cpp index 9675ac52..0041d45d 100755 --- a/src/io/piiodevice.cpp +++ b/src/io/piiodevice.cpp @@ -243,7 +243,7 @@ PIByteArray PIIODevice::readForTime(double timeout_ms) { if (ret <= 0) msleep(1); else str.append(td, ret); } - delete td; + delete[] td; return str; } diff --git a/src/io/pipeer.cpp b/src/io/pipeer.cpp index a668d781..f182dd34 100755 --- a/src/io/pipeer.cpp +++ b/src/io/pipeer.cpp @@ -43,7 +43,9 @@ PIPeer::PeerData::PeerData(const PIString & n): PIObject(n) { PIPeer::PeerData::~PeerData() { - t.stop(); + dt_in.stop(); + dt_out.stop(); + t.stop(true); } @@ -55,6 +57,7 @@ void PIPeer::PeerData::dtThread() { bool PIPeer::PeerData::send(const PIByteArray & d) { + piCout << "send ..." << t.isRunning(); if (t.isRunning()) return false; data = d; t.startOnce(); @@ -335,7 +338,7 @@ bool PIPeer::sendInternal(const PIString & to, const PIByteArray & data) { } PIByteArray ba; ba << int(4) << self_info.name << to << int(0) << data; - //piCoutObj << "sendInternal" << to << data.size_s() << int(data.front()); + piCoutObj << "sendInternal to" << to << data.size_s() << int(data.front()); if (!sendToNeighbour(dp, ba)) return false; return true; } @@ -355,7 +358,7 @@ bool PIPeer::dataRead(uchar * readed, int size) { PIString from, to; ba >> type; PIMutexLocker locker(eth_mutex); - //piCout << "[PIPeer \"" + name_ + "\"] Received packet" << type; + piCoutObj << "received data from" << from << "packet" << type; if (type == 5) { // ping request PIString addr; PISystemTime time; @@ -456,7 +459,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) { if (type <= 0 || type >= 4) return true; PeerInfo pi; ba >> pi.name; - //piCout << "read type" << type << "from" << pi.name; + piCoutObj << "received mb from" << pi.name << "packet" << type; if (pi.name == self_info.name) return true; PIMutexLocker locker(mc_mutex); diag_s.received(size); @@ -743,6 +746,11 @@ void PIPeer::checkNetwork() { PIEthernet::InterfaceList ifaces = PIEthernet::interfaces(); if (prev_ifaces == ifaces) return; prev_ifaces = ifaces; + reinit(); +} + + +void PIPeer::reinit() { PIMutexLocker mbl(mc_mutex); PIMutexLocker ethl(eth_mutex); PIMutexLocker pl(peers_mutex); diff --git a/src/io/pipeer.h b/src/io/pipeer.h index 20d5a8f2..be5cd4b7 100755 --- a/src/io/pipeer.h +++ b/src/io/pipeer.h @@ -129,6 +129,7 @@ public: const PeerInfo & selfInfo() const {return self_info;} const PIMap > & _peerMap() const {return addresses_map;} + void reinit(); void lock() {peers_mutex.lock();} void unlock() {peers_mutex.unlock();} diff --git a/src/thread/pithread.cpp b/src/thread/pithread.cpp index d39e287c..c2c8660c 100755 --- a/src/thread/pithread.cpp +++ b/src/thread/pithread.cpp @@ -123,19 +123,18 @@ bool PIThread::start(int timer_delay) { pthread_attr_t attr; pthread_attr_init(&attr); # ifndef ANDROID - pthread_attr_setschedparam(&attr, &sparam); + //pthread_attr_setschedparam(&attr, &sparam); # endif - if (pthread_create(&thread, &attr, thread_function, this) == 0) { - setPriority(priority_); - running_ = true; - return true; + int ret = pthread_create(&thread, &attr, thread_function, this); + pthread_attr_destroy(&attr); + if (ret == 0) { #else thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0); if (thread != 0) { +#endif setPriority(priority_); running_ = true; return true; -#endif } else { thread = 0; piCoutObj << "Error: Can`t start new thread:" << errorString(); @@ -151,19 +150,18 @@ bool PIThread::startOnce() { pthread_attr_t attr; pthread_attr_init(&attr); # ifndef ANDROID - pthread_attr_setschedparam(&attr, &sparam); + //pthread_attr_setschedparam(&attr, &sparam); # endif - if (pthread_create(&thread, &attr, thread_function_once, this) == 0) { - setPriority(priority_); - running_ = true; - return true; + int ret = pthread_create(&thread, &attr, thread_function_once, this); + pthread_attr_destroy(&attr); + if (ret == 0) { #else thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0); if (thread != 0) { +#endif setPriority(priority_); running_ = true; - return false; -#endif + return true; } else { thread = 0; piCoutObj << "Error: Can`t start new thread:" << errorString(); @@ -180,7 +178,9 @@ void PIThread::terminate() { pthread_kill(thread, SIGKILL); # else //pthread_kill(thread, SIGKILL); + void * ret(0); pthread_cancel(thread); + pthread_join(thread, &ret); # endif #else TerminateThread(thread, 0); @@ -230,6 +230,9 @@ void * PIThread::thread_function(void * t) { if (ct.lockRun) ct.mutex_.unlock(); ct.terminating = ct.running_ = false; //cout << "thread " << t << " exiting ... " << endl; +#ifndef WINDOWS + pthread_detach(ct.thread); +#endif ct.thread = 0; #ifndef WINDOWS pthread_exit(0); @@ -261,6 +264,9 @@ void * PIThread::thread_function_once(void * t) { ct.end(); ct.terminating = ct.running_ = false; //cout << "thread " << t << " exiting ... " << endl; +#ifndef WINDOWS + pthread_detach(ct.thread); +#endif ct.thread = 0; #ifndef WINDOWS pthread_exit(0); @@ -303,7 +309,7 @@ int PIThread::priority2System(PIThread::Priority p) { void PIThread::setPriority(PIThread::Priority prior) { priority_ = prior; #ifndef WINDOWS - if (!running_ && thread != 0) return; + if (!running_ && (thread != 0)) return; pthread_getschedparam(thread, &policy_, &sparam); sparam. # ifndef LINUX @@ -314,7 +320,7 @@ void PIThread::setPriority(PIThread::Priority prior) { = priority2System(priority_); pthread_setschedparam(thread, policy_, &sparam); #else - if (!running_ && thread != 0) return; + if (!running_ && (thread != 0)) return; SetThreadPriority(thread, priority2System(priority_)); #endif } diff --git a/src/thread/pitimer.cpp b/src/thread/pitimer.cpp index ad95b794..a845e3fa 100755 --- a/src/thread/pitimer.cpp +++ b/src/thread/pitimer.cpp @@ -53,6 +53,8 @@ _PITimerBase::_PITimerBase() { interval_ = deferred_delay = 0.; running_ = deferred_ = deferred_mode = false; + tfunc = 0; + parent = 0; } @@ -188,6 +190,10 @@ bool _PITimerImp_Thread::threadFunc() { return false; } st_time += st_inc; + if (!parent->isPIObject()) { + piCout << "Achtung! PITimer \"parent\" is not PIObject!"; + return false; + } tfunc(parent); return true; } diff --git a/utils/system_daemon/daemon.cpp b/utils/system_daemon/daemon.cpp index b950a5fb..f0888f18 100644 --- a/utils/system_daemon/daemon.cpp +++ b/utils/system_daemon/daemon.cpp @@ -19,7 +19,7 @@ Daemon::Remote::Remote(const PIString & n): PIThread() { Daemon::Remote::~Remote() { ft.stop(); - waitForFinish(1000); + stop(true); } @@ -283,6 +283,7 @@ void Daemon::fillInfoTile(const Daemon::HostInfo & hi) { void Daemon::tileEvent(PIScreenTile * t, TileEvent e) { + PIMutexLocker ml(remote_mutex); if (t == list_daemons) { if (e.type == TileList::RowPressed) { connectToDaemon(list_daemons->content[e.data.toInt()].first); @@ -332,6 +333,7 @@ void Daemon::keyEvent(PIKbdListener::KeyEvent key) { void Daemon::fmKeyEvent(PIKbdListener::KeyEvent key) { PIByteArray ba; + PIMutexLocker ml(remote_mutex); //Remote * r = remotes.value(conn_name); //piCout << key.key << key.modifiers; switch (key.key) { @@ -448,6 +450,7 @@ void Daemon::peerConnected(const PIString & name) { CONNECTU(r, receiveFinished, this, closeFileDialog) CONNECTU(r, sendFinished, this, closeFileDialog) CONNECTU(r, removeFinished, this, filesRemoved) + PIMutexLocker ml(remote_mutex); remotes.insert(name, r); } @@ -456,6 +459,7 @@ void Daemon::peerDisconnected(const PIString & name) { if (name == conn_name) { disconnect(); } + PIMutexLocker ml(remote_mutex); Remote * dt = remotes.value(name, 0); if (!dt) return; if (tile_file_progress->ft == &(dt->ft)) { @@ -472,6 +476,7 @@ void Daemon::filesReceived(const PIString & name, bool ok) { void Daemon::filesRemoved(const PIString & name, const PIString & dir) { + PIMutexLocker ml(remote_mutex); Remote * r = remotes.value(name, 0); if (!r) return; if (r->dir_my.absolutePath() != dir) return; @@ -500,8 +505,9 @@ void Daemon::closeFileDialog(const PIString & name, bool ok) { void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { //if (conn_name != from) return; if (data.size() < 4) return; + PIMutexLocker ml(remote_mutex); PIByteArray ba(data), rba; - Remote * r(0); + Remote * r = remotes.value(from); PIString dir; int type; ba >> type; //piCout << "rec from " << from << type; @@ -511,7 +517,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { rba << int(ReplyHostInfo) << info_my; break; case RequestChangeDir: - r = remotes.value(from); if (!r) break; ba >> dir; r->dir_my.cd(dir); @@ -526,7 +531,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { fillInfoTile(info_other); break; case ReplyChangeDir: - r = remotes.value(from); if (!r) break; { PIVector fil; @@ -538,7 +542,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { } break; case CopyFiles: - r = remotes.value(from); if (!r) return; if (r->isRunning()) return; { @@ -549,7 +552,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { } break; case RemoveFiles: - r = remotes.value(from); if (!r) return; if (r->isRunning()) return; { @@ -561,7 +563,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { } break; case MkDir: - r = remotes.value(from); if (!r) return; { PIString dn; @@ -572,7 +573,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { } break; case FileTransfer: - r = remotes.value(from); if (r) r->received(ba); break; }; @@ -625,6 +625,9 @@ void Daemon::makeOtherHostInfo() { void Daemon::requestChDir(const PIString & d) { if (d.isEmpty()) return; + Remote * r = remotes.value(conn_name, 0); + if (d.isEmpty()) return; + if (!r) return; fm.remoteSaveDir(); fm.readingRemote(); PIByteArray ba; diff --git a/utils/system_daemon/daemon.h b/utils/system_daemon/daemon.h index 15b85b26..7f3fe2b0 100644 --- a/utils/system_daemon/daemon.h +++ b/utils/system_daemon/daemon.h @@ -137,6 +137,7 @@ private: mutable PIStringList available_daemons; PITimer timer; PIString conn_name; + PIMutex remote_mutex; PIMap dnames; PIMap remotes; PISystemMonitor sys_mon_other; diff --git a/utils/system_daemon/file_manager.cpp b/utils/system_daemon/file_manager.cpp index 3910ba69..46b194f1 100644 --- a/utils/system_daemon/file_manager.cpp +++ b/utils/system_daemon/file_manager.cpp @@ -15,6 +15,7 @@ FileManager::TileDir::TileDir(): TileList() { PIStringList FileManager::TileDir::selectedNames() const { PIStringList ret; + PIMutexLocker ml(e_mutex); PIVector sind = selected.toVector(); piForeachC (int i, sind) ret << entries[i].name(); @@ -67,6 +68,9 @@ bool FileManager::TileDir::keyEvent(PIKbdListener::KeyEvent key) { pass = true; break; case PIKbdListener::Return: + { + e_mutex.lock(); + bool ud = false; if (cur < entries.size_s() && cur >= 0) { if (!remote) { //piCout << entries[cur]; @@ -77,11 +81,14 @@ bool FileManager::TileDir::keyEvent(PIKbdListener::KeyEvent key) { cur = cp.first; offset = cp.second; selected.clear(); - updateDir(); + ud = true; } } pass = true; } + e_mutex.unlock(); + if (ud) updateDir(); + } break; } if (remote && pass) { @@ -103,6 +110,7 @@ void FileManager::TileDir::unlock() { void FileManager::TileDir::showReading() { + PIMutexLocker ml(e_mutex); cur = -1; offset = 0; entries.clear(); @@ -112,6 +120,7 @@ void FileManager::TileDir::showReading() { void FileManager::TileDir::setContent(const PIVector & l) { + PIMutexLocker ml(e_mutex); PIVector el = dir.entries(), fl, dl; entries.clear(); if (l.isEmpty()) { @@ -155,6 +164,7 @@ void FileManager::TileDir::updateDir() { void FileManager::TileDir::buildNames() { //if (!enabled) return; lock(); + PIMutexLocker ml(e_mutex); content.clear(); PIChar t; CharFlags cf = 0; diff --git a/utils/system_daemon/file_manager.h b/utils/system_daemon/file_manager.h index 7da9fabb..cf0344bc 100644 --- a/utils/system_daemon/file_manager.h +++ b/utils/system_daemon/file_manager.h @@ -54,6 +54,7 @@ private: PIVector entries; PIDir dir; PIMap > prev_pos; + mutable PIMutex e_mutex; bool resized, remote, remote_mode; void * fm, * key_func; EVENT3(actionRequest, bool, remote_tile, FileManager::Action, type, PIVariant, data) diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index 374412e0..0effdea6 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -90,6 +90,7 @@ public: ret->content << TileList::Row("Local file manager", CellFormat()); ret->content << TileList::Row("Connect to another daemon", CellFormat()); ret->content << TileList::Row("Peer info", CellFormat()); + ret->content << TileList::Row("Peer reinit", CellFormat()); ret->content << TileList::Row("Exit", CellFormat()); ret->selection_mode = TileList::NoSelection; return ret; @@ -136,21 +137,19 @@ public: ", " + PIString::fromNumber(cur_peer) , CellFormat()); piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers()) - peers_tl->content << TileList::Row(p.name + " | " + PIString::fromNumber(p.dist) + - " | " + PIString::fromNumber(p.ping()) + - " | " + PIString::fromNumber(p.addresses.size_s()) + - " | " + PIString::fromBool(p.isNeighbour()) - , CellFormat()); + peers_tl->content << TileList::Row(p.name + " | d = " + PIString::fromNumber(p.dist) + + " | p = " + PIString::fromNumber(p.ping()) + + " | n = " + PIString::fromBool(p.isNeighbour()) + , CellFormat()); PIPeer::PeerInfo pi = daemon_.selfInfo(); if (cur_peer >= 0 && cur_peer < daemon_.allPeers().size_s()) pi = daemon_.allPeers()[cur_peer]; peerinfo_tl->content << TileSimple::Row("Name: " + pi.name, CellFormat()); peerinfo_tl->content << TileSimple::Row("Addreses: " + PIString::fromNumber(pi.addresses.size()), CellFormat()); peerinfo_tl->content << TileSimple::Row("Neighbours: " + pi.neighbours.join(", "), CellFormat()); piForeachC(PIPeer::PeerInfo::Address &a , pi.addresses) - addrs_tl->content << TileList::Row(a.address + " | " + a.netmask + - " | " + PIString::fromNumber(a.ping) + - " | " + PIString::fromNumber(a.last_ping.toSeconds()) + - " | " + PIString::fromBool(a.isAvailable()), CellFormat()); + addrs_tl->content << TileList::Row(a.address + + " | p = " + PIString::fromNumber(a.ping) + + " | a = " + PIString::fromBool(a.isAvailable()), CellFormat()); typedef PIPair > PeerPair; PIStringList peermap; piForeachC(PeerPair &p , daemon_._peerMap()) { @@ -182,7 +181,8 @@ public: case 1: tfm->show(); break; case 2: daemon_.showMainList(); tdaemon->show(); break; case 3: tpeer->show(); peers_tl->setFocus(); break; - case 4: PIKbdListener::exiting = true; break; + case 4: daemon_.reinit(); tmenu->show(); break; + case 5: PIKbdListener::exiting = true; break; } } return;