fix PIFileTransfer
git-svn-id: svn://db.shs.com.ru/pip@73 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -64,14 +64,14 @@ bool PIFileTransfer::send(PIVector<PIFile::FileInfo> entries) {
|
||||
bool PIFileTransfer::sendFiles(const PIVector<PFTFileInfo> &files) {
|
||||
files_ = files;
|
||||
PIStringList names;
|
||||
// piCoutObj << "prepare to send" << files_.size() << "files";
|
||||
// piCoutObj << "prepare to send" << files_.size() << "files";
|
||||
for(int i=0; i<files_.size_s(); i++) {
|
||||
// files_[i].path = dir.relative(files_[i].path);
|
||||
// files_[i].path = dir.relative(files_[i].path);
|
||||
if (names.contains(files_[i].path)) {files_.remove(i); i--;}
|
||||
else names << files_[i].path;
|
||||
if (files_[i].isDir()) files_[i].size = 0;
|
||||
// piCout << "prepare" << i << files_[i].path << files_[i].dest_path << files_[i].name();
|
||||
}
|
||||
// piCout << "prepare" << i << files_[i].path << files_[i].dest_path << files_[i].name();
|
||||
}
|
||||
srand(PISystemTime::current().toMilliseconds());
|
||||
pftheader.session_id = rand();
|
||||
sendFilesStarted();
|
||||
@@ -82,25 +82,25 @@ bool PIFileTransfer::sendFiles(const PIVector<PFTFileInfo> &files) {
|
||||
if (!send_process()) return false;
|
||||
pftheader.step = pft_Data;
|
||||
PIVector<Part> pts;
|
||||
for (int i=0; i<files_.size_s(); i++)
|
||||
for (int i=0; i<files_.size_s(); i++) {
|
||||
pts << Part(i+1, files_[i].size);
|
||||
}
|
||||
buildSession(pts);
|
||||
bool ok = send_process();
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
|
||||
piCout << "processFile" << id << files_.size();
|
||||
// piCout << "processFile" << id << files_.size();
|
||||
PFTFileInfo fi = files_[id-1];
|
||||
bytes_file_all = fi.size;
|
||||
bytes_file_cur = start;
|
||||
cur_file_string = fi.dest_path;
|
||||
PIString path = dir.absolutePath() + dir.separator + fi.dest_path;
|
||||
piCout << "receive" << path << fi.size << data.size();
|
||||
// piCout << "receive" << path << fi.size << data.size();
|
||||
if (fi.isDir()) {
|
||||
// piCoutObj << "make dir" << fi.entry.path;
|
||||
// piCoutObj << "make dir" << fi.entry.path;
|
||||
if (!PIDir::make(path)) {
|
||||
cur_file_string = "ERROR! while create directory " + path;
|
||||
piCoutObj << cur_file_string;
|
||||
@@ -113,22 +113,22 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
|
||||
work_file.close();
|
||||
if (!work_file.open(path, PIIODevice::ReadWrite)) {
|
||||
cur_file_string = "ERROR! while open file " + path;
|
||||
piCoutObj << cur_file_string << "," << errorString();
|
||||
piCoutObj << cur_file_string;
|
||||
stopReceive();
|
||||
return;
|
||||
}
|
||||
if (work_file.size() > fi.size) {
|
||||
// piCoutObj << "error size" << work_file.size() << fi.size;
|
||||
// piCoutObj << "error size" << work_file.size() << fi.size;
|
||||
work_file.clear();
|
||||
work_file.resize(fi.size);
|
||||
// piCoutObj << "correct size" << work_file.size() << fi.size;
|
||||
// piCoutObj << "correct size" << work_file.size() << fi.size;
|
||||
}
|
||||
}
|
||||
// piCoutObj << "write file" << path << work_file.path() << work_file.size() << fi.entry.size << work_file.pos() << fi.fstart << fi.fsize;
|
||||
// piCoutObj << "write file" << path << work_file.path() << work_file.size() << fi.entry.size << work_file.pos() << fi.fstart << fi.fsize;
|
||||
if (work_file.size() < (llong)start) {
|
||||
// piCoutObj << "error pos size" << work_file.pos() << fi.fstart;
|
||||
// piCoutObj << "error pos size" << work_file.pos() << fi.fstart;
|
||||
work_file.resize(start);
|
||||
// piCoutObj << "correct pos size" << work_file.pos() << fi.fstart;
|
||||
// piCoutObj << "correct pos size" << work_file.pos() << fi.fstart;
|
||||
}
|
||||
if (work_file.size() > fi.size) {
|
||||
piCoutObj << "****** error size" << work_file.size() << fi.size;
|
||||
@@ -136,7 +136,7 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
|
||||
work_file.resize(fi.size);
|
||||
piCoutObj << "****** correct size" << work_file.size() << fi.size;
|
||||
}
|
||||
// if (fi.fstart != work_file.pos()) piCoutObj << "error pos" << work_file.pos() << fi.fstart;
|
||||
// if (fi.fstart != work_file.pos()) piCoutObj << "error pos" << work_file.pos() << fi.fstart;
|
||||
work_file.seek(start);
|
||||
int rs = work_file.write(data.data(), data.size());
|
||||
if (rs != data.size_s()) {
|
||||
@@ -150,51 +150,50 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
|
||||
|
||||
|
||||
PIByteArray PIFileTransfer::buildPacket(Part p) {
|
||||
// piCoutObj << "Step" << pftheader.step;
|
||||
// piCoutObj << "session id" << pftheader.session_id;
|
||||
// piCoutObj << "Step" << pftheader.step;
|
||||
// piCoutObj << "session id" << pftheader.session_id;
|
||||
PIByteArray ba;
|
||||
switch(pftheader.step) {
|
||||
case pft_None:
|
||||
stopSend();
|
||||
case pft_None:
|
||||
stopSend();
|
||||
return PIByteArray();
|
||||
case pft_Description:
|
||||
ba.resize(p.size);
|
||||
memcpy(ba.data(), desc.data(p.start), p.size);
|
||||
case pft_Description:
|
||||
ba.resize(p.size);
|
||||
memcpy(ba.data(), desc.data(p.start), p.size);
|
||||
return ba;
|
||||
case pft_Data:
|
||||
// piCout << "send data" << p.id << files_.size();
|
||||
PIFile::FileInfo fi = files_[p.id-1];
|
||||
if (fi.isFile()) {
|
||||
// piCout << "send file" << fi.name() << fi.size;
|
||||
PIString path = fi.path;
|
||||
if (work_file.path() != path || !work_file.isOpened()) {
|
||||
if (!work_file.open(path, PIIODevice::ReadOnly)) {
|
||||
case pft_Data:
|
||||
// piCout << "send data" << p.id << files_.size();
|
||||
PIFile::FileInfo fi = files_[p.id-1];
|
||||
if (fi.isFile()) {
|
||||
// piCout << "send file" << fi.name() << fi.size;
|
||||
PIString path = fi.path;
|
||||
if (work_file.path() != path || !work_file.isOpened()) {
|
||||
if (!work_file.open(path, PIIODevice::ReadOnly)) {
|
||||
break_ = true;
|
||||
cur_file_string = "ERROR! while open file " + fi.path;
|
||||
piCoutObj << cur_file_string;
|
||||
stopSend();
|
||||
return PIByteArray();
|
||||
}
|
||||
}
|
||||
work_file.seek(p.start);
|
||||
ba.resize(p.size);
|
||||
int rs = work_file.read(ba.data(), ba.size());
|
||||
if ((llong)rs != (llong)p.size) {
|
||||
break_ = true;
|
||||
cur_file_string = "ERROR! while open file " + fi.path;
|
||||
piCoutObj << cur_file_string << "," << errorString();
|
||||
cur_file_string = "ERROR! while read file " + fi.path + " (must " + PIString::fromNumber(p.size) + ", but read " + PIString::fromNumber(rs) + ")";
|
||||
piCoutObj << cur_file_string;
|
||||
stopSend();
|
||||
return PIByteArray();
|
||||
}
|
||||
}
|
||||
work_file.seek(p.start);
|
||||
ba.resize(p.size);
|
||||
int rs = work_file.read(ba.data(), ba.size());
|
||||
piCout << rs << p.size;
|
||||
if ((llong)rs != (llong)p.size) {
|
||||
break_ = true;
|
||||
cur_file_string = "ERROR! while read file " + fi.path + " (must " + PIString::fromNumber(p.size) + ", but read " + PIString::fromNumber(rs) + ")";
|
||||
piCoutObj << cur_file_string;
|
||||
stopSend();
|
||||
return PIByteArray();
|
||||
}
|
||||
}
|
||||
// if (fi.isDir()) {
|
||||
// piCout << "create dir" << fi.path;
|
||||
// dir.make(fi.path);
|
||||
// }
|
||||
cur_file_string = fi.path;
|
||||
bytes_file_all = fi.size;
|
||||
bytes_file_cur = p.start;
|
||||
// if (fi.isDir()) {
|
||||
// piCout << "create dir" << fi.path;
|
||||
// dir.make(fi.path);
|
||||
// }
|
||||
cur_file_string = fi.path;
|
||||
bytes_file_all = fi.size;
|
||||
bytes_file_cur = p.start;
|
||||
return ba;
|
||||
}
|
||||
return PIByteArray();
|
||||
@@ -203,9 +202,9 @@ PIByteArray PIFileTransfer::buildPacket(Part p) {
|
||||
|
||||
void PIFileTransfer::receivePart(PIBaseTransfer::Part fi, PIByteArray ba, PIByteArray pheader) {
|
||||
PFTHeader h;
|
||||
// piCout << pheader.size() << sizeof(PFTHeader);
|
||||
// piCout << pheader.size() << sizeof(PFTHeader);
|
||||
pheader >> h;
|
||||
// piCout << h.session_id;
|
||||
// piCout << h.session_id;
|
||||
StepType st = (StepType)h.step;
|
||||
pftheader.step = st;
|
||||
if (!h.check_sig()) {
|
||||
@@ -215,20 +214,20 @@ void PIFileTransfer::receivePart(PIBaseTransfer::Part fi, PIByteArray ba, PIByte
|
||||
return;
|
||||
}
|
||||
switch(st) {
|
||||
case pft_None:
|
||||
break;
|
||||
case pft_Description:
|
||||
pftheader.session_id = h.session_id;
|
||||
if (desc.size() < fi.start + fi.size) desc.resize(fi.start + fi.size);
|
||||
memcpy(desc.data(fi.start), ba.data(), ba.size_s());
|
||||
break;
|
||||
case pft_Data:
|
||||
if (h.session_id == pftheader.session_id)
|
||||
processFile(fi.id, fi.start, ba);
|
||||
else stopReceive();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case pft_None:
|
||||
break;
|
||||
case pft_Description:
|
||||
pftheader.session_id = h.session_id;
|
||||
if (desc.size() < fi.start + fi.size) desc.resize(fi.start + fi.size);
|
||||
memcpy(desc.data(fi.start), ba.data(), ba.size_s());
|
||||
break;
|
||||
case pft_Data:
|
||||
if (h.session_id == pftheader.session_id)
|
||||
processFile(fi.id, fi.start, ba);
|
||||
else stopReceive();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,13 +235,14 @@ void PIFileTransfer::receivePart(PIBaseTransfer::Part fi, PIByteArray ba, PIByte
|
||||
PIByteArray PIFileTransfer::customHeader() {
|
||||
PIByteArray ba;
|
||||
ba << pftheader;
|
||||
return ba;
|
||||
return ba;
|
||||
}
|
||||
|
||||
|
||||
void PIFileTransfer::receive_started() {
|
||||
if (pftheader.step == pft_None) {
|
||||
files_.clear();
|
||||
// piCoutObj << "start receive";
|
||||
receiveFilesStarted();
|
||||
}
|
||||
}
|
||||
@@ -252,10 +252,11 @@ void PIFileTransfer::receive_finished(bool ok) {
|
||||
if (pftheader.step == pft_Description) {
|
||||
bool user_ok = true;
|
||||
if (ok) {
|
||||
// piCoutObj << desc.size() << PICoutManipulators::Hex << desc;
|
||||
desc >> files_;
|
||||
// piCoutObj << files_;
|
||||
PIStringList files;
|
||||
piForeachC(PFTFileInfo &fi, files_) files << fi.dest_path;
|
||||
//piCout << files;
|
||||
receiveFilesRequest(files, bytesAll(), &user_ok);
|
||||
}
|
||||
if (!ok || !user_ok) {
|
||||
|
||||
Reference in New Issue
Block a user