void TcpCommander::ADownload(std::string args, SOCKET client) {
json r;
std::vector<std::uint8_t> package_data;
std::basic_ifstream<std::uint8_t> package_download;
std::string req = std::string("download ");
req.append(args);
try {
package_download.open(args.c_str(), std::ifstream::binary);
if (package_download.good() && package_download.is_open()) {
package_download.seekg(0, ifstream::end);
unsigned int length = (unsigned int)package_download.tellg();
package_download.seekg(0, ifstream::beg);
unsigned int pkg_count = length / PKG_SIZE;
unsigned int lpkg_size = length % PKG_SIZE;
if (lpkg_size < 1 && length <= PKG_SIZE)
lpkg_size = length;
if (pkg_count < 1 && lpkg_size > 0) {
package_download.read(&package_data[0], lpkg_size * sizeof(std::uint8_t));
r["result"] = {
{"req", req},
{"code", 0},
{"pkg", {
{"id", pkg_count},
{"count", pkg_count},
{"size", lpkg_size},
{"buffer", json::binary_t(package_data)}
}}
};
this->SendCommand(client, r.dump());
r.clear();
package_data.clear();
package_data.shrink_to_fit();
}
else if (lpkg_size > 0) {
for (unsigned int i = 0; i < pkg_count; i++) {
unsigned int pkg_id = i + 1;
unsigned int pkg_size = 0;
if (pkg_id < pkg_count) {
pkg_size = PKG_SIZE;
}
else if (lpkg_size > 0) {
pkg_size = lpkg_size;
}
package_data.resize((size_t)pkg_size * sizeof(unsigned char));
package_download.read(&package_data[0], pkg_size * sizeof(std::uint8_t));
r["result"] = {
{"req", req},
{"code", 0},
{"pkg", {
{"id", pkg_id},
{"count", pkg_count},
{"size", pkg_size},
{"buffer", json::binary_t(package_data)}
}}
};
if (!this->SendCommand(client, r.dump()))
i = pkg_count;
r.clear();
package_data.clear();
package_data.shrink_to_fit();
std::this_thread::sleep_for(std::chrono::milliseconds(DOWNLOAD_DELAY));
}
}
r.clear();
package_data.clear();
package_data.shrink_to_fit();
package_download.close();
}
else {
package_download.close();
r["result"] = {
{"req", req},
{"code", GetLastError()}
};
}
}
catch (exception ex) {
json j_ex;
j_ex["result"] = {
{"req", ex.what()},
{"code", GetLastError()}
};
this->SendCommand(client, j_ex.dump());
j_ex.clear();
}
}