diff --git a/src/io/pibasetransfer.cpp b/src/io/pibasetransfer.cpp index 8e2c846f..72e27f9a 100644 --- a/src/io/pibasetransfer.cpp +++ b/src/io/pibasetransfer.cpp @@ -1,6 +1,6 @@ #include "pibasetransfer.h" -const uint PIBaseTransfer::signature = 0x54444950; +const uint PIBaseTransfer::signature = 0x54424950; PIBaseTransfer::PIBaseTransfer(): crc(standardCRC_16()), diag(false) { header.sig = signature; @@ -235,7 +235,7 @@ void PIBaseTransfer::buildSession(PIVector parts) { bytes_all += parts[i].size; // fi.size = fi.entry.size; fi.start = 0; - int rest = parts[i].size - (packet_size - cur_size); + llong rest = parts[i].size - (packet_size - cur_size); // piCout << i << fi.size << rest << bytes_all; if (rest <= 0) { fi.size = parts[i].size; diff --git a/src/io/pifile.cpp b/src/io/pifile.cpp index eb24251f..d9591c62 100755 --- a/src/io/pifile.cpp +++ b/src/io/pifile.cpp @@ -110,11 +110,13 @@ PIString PIFile::FileInfo::dir() const { PIFile::PIFile(): PIIODevice() { + fdi = -1; setPrecision(5); } PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) { + fdi = -1; setPrecision(5); if (!path.isEmpty()) openDevice(); @@ -122,6 +124,7 @@ PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(p PIFile::PIFile(const PIFile & other) { + fdi = -1; setPrecision(other.prec_); setPath(other.path()); mode_ = other.mode_; @@ -139,12 +142,15 @@ bool PIFile::openDevice() { if (fd != 0) fclose(fd); } } - fd = fopen(p.data(), strType(mode_).data()); + fd = fopen64(p.data(), strType(mode_).data()); opened_ = (fd != 0); + if (opened_) { + fdi = fileno(fd); #ifndef WINDOWS - if (opened_) fcntl(fileno(fd), F_SETFL, O_NONBLOCK); + fcntl(fdi, F_SETFL, O_NONBLOCK); #endif seekToBegin(); + } //piCout << "open file" << fd << opened_; return opened_; } @@ -155,6 +161,7 @@ bool PIFile::closeDevice() { if (!opened_ || fd == 0) return true; bool cs = (fclose(fd) == 0); if (cs) fd = 0; + fdi = -1; //piCout << "closed file" << fd << opened_; return cs; } @@ -210,9 +217,9 @@ PIByteArray PIFile::readAll(bool forceRead) { llong PIFile::size() const { if (!opened_) return -1; llong s, cp = pos(); - fseek(fd, 0, SEEK_END); clearerr(fd); + lseek64(fdi, 0, SEEK_END); clearerr(fd); s = pos(); - fseek(fd, cp, SEEK_SET); clearerr(fd); + lseek64(fdi, cp, SEEK_SET); clearerr(fd); return s; } @@ -237,7 +244,7 @@ void PIFile::resize(llong new_size, uchar fill_) { bool PIFile::isExists(const PIString & path) { - FILE * f = fopen(PIString(path).data(), "r"); + FILE * f = fopen64(PIString(path).data(), "r"); bool ok = (f != 0); if (ok) fclose(f); return ok; @@ -264,6 +271,109 @@ PIString PIFile::strType(const PIIODevice::DeviceMode type) { } +void PIFile::flush() { + if (opened_) fflush(fd); +} + + +void PIFile::seek(llong position) { + if (!opened_) return; + lseek64(fdi, position, SEEK_SET); + clearerr(fd); +} + + +void PIFile::seekToBegin() { + if (!opened_) return; + lseek64(fdi, 0, SEEK_SET); + clearerr(fd); +} + + +void PIFile::seekToEnd() { + if (!opened_) return; + lseek64(fdi, 0, SEEK_END); + clearerr(fd); +} + + +void PIFile::seekToLine(llong line) { + if (!opened_) return; + seekToBegin(); + piForTimes(line) readLine(); + clearerr(fd); +} + + +char PIFile::readChar() { + return (char)fgetc(fd); +} + + +void PIFile::setPath(const PIString & path) { + PIIODevice::setPath(path); + if (opened_) openDevice(); +} + + +llong PIFile::pos() const { + if (!opened_) return -1; + return ftell(fd); +} + + +bool PIFile::isEnd() const { + if (!opened_) return true; + return (feof(fd) || ferror(fd)); +} + + +void PIFile::setPrecision(int prec) { + prec_ = prec; + if (prec_ >= 0) prec_str = "." + PIString::fromNumber(prec_); + else prec_str = ""; +} + + +int PIFile::read(void * read_to, int max_size) { + if (!canRead() || fd == 0) return -1; + return fread(read_to, 1, max_size, fd); +} + + +int PIFile::write(const void * data, int max_size) { + if (!canWrite() || fd == 0) return -1; + return fwrite(data, 1, max_size, fd); +} + + +PIFile & PIFile::writeToBinLog(ushort id, const void * data, int size) { + if (!isWriteable() || fd == 0) return *this; + writeBinary(id).writeBinary((ushort)size); + write(data, size); + flush(); + return *this; +} + + +void PIFile::clear() { + close(); + fd = fopen(path().data(), "w"); + if (fd != 0) fclose(fd); + fd = 0; + opened_ = false; + open(); +} + + +void PIFile::remove() { + close(); + std::remove(path().data()); +} + + + + PIFile::FileInfo PIFile::fileInfo(const PIString & path) { FileInfo ret; if (path.isEmpty()) return ret; diff --git a/src/io/pifile.h b/src/io/pifile.h index ff9f55ac..ce6d991b 100755 --- a/src/io/pifile.h +++ b/src/io/pifile.h @@ -94,23 +94,23 @@ public: //! Immediate write all buffered data to disk - void flush() {if (opened_) fflush(fd);} + void flush(); //! Move read/write position to "position" - void seek(llong position) {if (!opened_) return; fseek(fd, position, SEEK_SET); clearerr(fd);} + void seek(llong position); //! Move read/write position to the begin of the file - void seekToBegin() {if (!opened_) return; fseek(fd, 0, SEEK_SET); clearerr(fd);} + void seekToBegin(); //! Move read/write position to the end of the file - void seekToEnd() {if (!opened_) return; fseek(fd, 0, SEEK_END); clearerr(fd);} + void seekToEnd(); //! Move read/write position to text line number "line" - void seekToLine(llong line) {if (!opened_) return; seekToBegin(); piForTimes (line) readLine(); clearerr(fd);} // line 0 - begin of file + void seekToLine(llong line); //void fill(char c) {stream.fill(c);} //! Read one char and return it - char readChar() {return (char)fgetc(fd);} + char readChar(); //! Read one text line and return it PIString readLine(); @@ -123,16 +123,16 @@ public: //! Set file path to "path" and reopen file if need - void setPath(const PIString & path) {PIIODevice::setPath(path); if (opened_) openDevice();} + void setPath(const PIString & path); //! Returns file size llong size() const; //! Returns read/write position - llong pos() const {if (!opened_) return -1; return ftell(fd);} + llong pos() const; //! Returns if position is at the end of file - bool isEnd() const {if (!opened_) return true; return (feof(fd) || ferror(fd));} + bool isEnd() const; //! Returns if file is empty bool isEmpty() const {return (size() <= 0);} @@ -145,16 +145,16 @@ public: int precision() const {return prec_;} //! Set float numbers write precision to "prec_" digits - void setPrecision(int prec) {prec_ = prec; if (prec_ >= 0) prec_str = "." + PIString::fromNumber(prec_); else prec_str = "";} + void setPrecision(int prec); //! Read from file to "read_to" no more than "max_size" and return readed bytes count - int read(void * read_to, int max_size) {if (!canRead() || fd == 0) return -1; return fread(read_to, 1, max_size, fd);} + int read(void * read_to, int max_size); //! Write to file "data" with size "max_size" and return written bytes count - int write(const void * data, int max_size) {if (!canWrite() || fd == 0) return -1; return fwrite(data, 1, max_size, fd);} + int write(const void * data, int max_size); - PIFile & writeToBinLog(ushort id, const void * data, int size) {if (!isWriteable() || fd == 0) return *this; writeBinary(id).writeBinary((ushort)size); write(data, size); flush(); return *this;} + PIFile & writeToBinLog(ushort id, const void * data, int size); //! Write to file binary content of "v" @@ -188,7 +188,7 @@ public: PIFile & operator <<(const char v) {if (canWrite() && fd != 0) write(&v, 1); return *this;} //PIFile & operator <<(const string & v) {write(v.c_str(), v.size()); return *this;} //! Write to file string "v" - PIFile & operator <<(const PIString & v) {if (canWrite() && fd != 0) write(v.data(), v.lengthAscii()); return *this;} + PIFile & operator <<(const PIString & v) {if (canWrite() && fd != 0) *this << v.toByteArray(); return *this;} //! Write to file text representation of "v" PIFile & operator <<(const PIByteArray & v) {if (canWrite() && fd != 0) write(v.data(), v.size()); return *this;} //! Write to file text representation of "v" @@ -242,8 +242,8 @@ public: PIString constructFullPath() const; - EVENT_HANDLER(void, clear) {close(); fd = fopen(path().data(), "w"); if (fd != 0) fclose(fd); fd = 0; opened_ = false; open();} - EVENT_HANDLER0(void, remove) {close(); std::remove(path().data());} + EVENT_HANDLER(void, clear); + EVENT_HANDLER(void, remove); EVENT_HANDLER1(void, resize, llong, new_size) {resize(new_size, 0);} EVENT_HANDLER2(void, resize, llong, new_size, uchar, fill); @@ -301,7 +301,7 @@ private: PIString strType(const PIIODevice::DeviceMode type); FILE * fd; - int ret, prec_; + int ret, prec_, fdi; PIString prec_str; }; diff --git a/src/io/pifiletransfer.cpp b/src/io/pifiletransfer.cpp index ae2a9199..b62370c5 100644 --- a/src/io/pifiletransfer.cpp +++ b/src/io/pifiletransfer.cpp @@ -82,9 +82,8 @@ bool PIFileTransfer::sendFiles(const PIVector &files) { if (!send_process()) return false; pftheader.step = pft_Data; PIVector pts; - for (int i=0; i> files_; PIStringList files; piForeachC(PFTFileInfo &fi, files_) files << fi.dest_path; -// piCout << files; + //piCout << files; receiveFilesRequest(files, bytesAll(), &user_ok); } if (!ok || !user_ok) { diff --git a/src/io/pifiletransfer.h b/src/io/pifiletransfer.h index 5f77001e..b4c6a5d5 100644 --- a/src/io/pifiletransfer.h +++ b/src/io/pifiletransfer.h @@ -72,10 +72,10 @@ private: bool sendFiles(const PIVector &files); void processFile(int id, ullong start, PIByteArray &data); - virtual void receivePart(Part fi, PIByteArray ba, PIByteArray pheader); - virtual PIByteArray buildPacket(Part fi); - virtual PIByteArray customHeader(); -// EVENT_HANDLER(void, send_started); + virtual void receivePart(Part fi, PIByteArray ba, PIByteArray pheader); + virtual PIByteArray buildPacket(Part fi); + virtual PIByteArray customHeader(); + //EVENT_HANDLER(void, send_started); EVENT_HANDLER(void, receive_started); EVENT_HANDLER1(void, send_finished, bool, ok); EVENT_HANDLER1(void, receive_finished, bool, ok); diff --git a/src/thread/pitimer.cpp b/src/thread/pitimer.cpp index f0731358..57337d7a 100755 --- a/src/thread/pitimer.cpp +++ b/src/thread/pitimer.cpp @@ -330,9 +330,11 @@ void _PITimerImp_Pool::Pool::remove(_PITimerImp_Pool * t) { void _PITimerImp_Pool::Pool::run() { piForeach (_PITimerImp_Pool * t, timers) t->threadFunc(); - piForeach (_PITimerImp_Pool * t, to_remove) - timers.removeOne(t); - to_remove.clear(); + if (!to_remove.isEmpty()) { + piForeach (_PITimerImp_Pool * t, to_remove) + timers.removeOne(t); + to_remove.clear(); + } //while (t->threadFunc()); } diff --git a/utils/system_daemon/daemon.cpp b/utils/system_daemon/daemon.cpp index 401c8d5e..45871514 100644 --- a/utils/system_daemon/daemon.cpp +++ b/utils/system_daemon/daemon.cpp @@ -4,6 +4,18 @@ extern PIScreen screen; + +Daemon::Remote::Remote(const PIString & n): PIThread() { + setName(n); + ft.setName(n); + CONNECTU(&ft, sendRequest, this, ftSendRequest) + CONNECTU(&ft, receiveFinished, this, ftReceived) + dir_my = PIDir::current(); +} + + + + Daemon::Daemon(): PIPeer("_pisd_" + PISystemInfo::instance()->hostname + "_" + PIString(rand() % 100)), fm(this) { setName("Daemon"); timer.setName("__S__Daemon_timer"); @@ -228,8 +240,8 @@ void Daemon::peerConnected(const PIString & name) { mode = 2; conn_name = name;*/ Remote * r = new Remote(name); - CONNECTU(&(r->dt), sendRequest, this, dtSendRequest) - CONNECTU(&(r->dt), receiveFinished, this, dtReceived) + CONNECTU(&(r->ft), sendRequest, this, ftSendRequest) + CONNECTU(&(r->ft), receiveFinished, this, ftReceived) remotes.insert(name, r); } @@ -244,7 +256,7 @@ void Daemon::peerDisconnected(const PIString & name) { } -void Daemon::dtSendRequest(PIByteArray & data) { +void Daemon::ftSendRequest(PIByteArray & data) { PIDataTransfer * dt = (PIDataTransfer*)emitter(); if (!dt) return; PIByteArray hdr; hdr << int(DataTransfer); @@ -253,7 +265,7 @@ void Daemon::dtSendRequest(PIByteArray & data) { } -void Daemon::dtReceived(bool ok) { +void Daemon::ftReceived(bool ok) { if (!ok) return; PIDataTransfer * dt = (PIDataTransfer*)emitter(); if (!dt) return; @@ -304,11 +316,6 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { fm.remoteRestoreDir(); } break; - case DataTransfer: - r = remotes.value(from); - if (r) - r->dt.received(ba); - break; }; if (!rba.isEmpty()) send(from, rba); } diff --git a/utils/system_daemon/daemon.h b/utils/system_daemon/daemon.h index fc9c5725..0f966f5d 100644 --- a/utils/system_daemon/daemon.h +++ b/utils/system_daemon/daemon.h @@ -65,22 +65,28 @@ private: }; class Remote: public PIThread { + PIOBJECT_SUBCLASS(Remote, PIThread) public: - Remote(const PIString & n = PIString()) {dt.setName(n); ft.setName(n); dir_my = PIDir::current();} + Remote(const PIString & n = PIString()); void sendData(const PIByteArray & d) {_d = d; startOnce();} + EVENT_HANDLER1(void, ftSendRequest, PIByteArray &, data) {sendRequest(name(), data);} + //EVENT1(ftReceived, const PIString & , name) + EVENT2(sendRequest, const PIString & , name, PIByteArray &, data) + EVENT1(receiveFinished, bool, ok) + EVENT_HANDLER1(void, received, const PIByteArray & , data) {ft.received(data);} + PIDir dir_my, dir_remote; - PIDataTransfer dt; PIFileTransfer ft; PIByteArray _d; - void run() {dt.send(_d);} + //void run() {ft.send(_d);} }; EVENT_HANDLER2(void, tileEvent, PIScreenTile *, t, PIScreenTypes::TileEvent, e); EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, key); EVENT_HANDLER1(void, fmKeyEvent, PIKbdListener::KeyEvent, key); EVENT_HANDLER2(void, timerEvent, void * , _d, int, delim); - EVENT_HANDLER1(void, dtSendRequest, PIByteArray &, data); - EVENT_HANDLER1(void, dtReceived, bool, ok); + EVENT_HANDLER1(void, ftSendRequest, PIByteArray &, data); + EVENT_HANDLER1(void, ftReceived, bool, ok); EVENT(menuRequest); void hideAll(); void showTile(PIScreenTile * t, const PIString & header = PIString());