Compute progress % during downloads

This commit is contained in:
Henrik Rydgård
2013-11-29 16:31:19 +01:00
parent ca6b5b67d5
commit 197d010189
4 changed files with 62 additions and 9 deletions
+25 -2
View File
@@ -175,11 +175,34 @@ bool Buffer::ReadAll(int fd) {
return true;
}
size_t Buffer::Read(int fd, size_t sz) {
bool Buffer::ReadAllWithProgress(int fd, int knownSize, float *progress) {
char buf[1024];
int total = 0;
while (true) {
int retval = recv(fd, buf, sizeof(buf), 0);
if (retval == 0) {
return true;
} else if (retval < 0) {
ELOG("Error reading from buffer: %i", retval);
return false;
}
char *p = Append((size_t)retval);
memcpy(p, buf, retval);
total += retval;
*progress = (float)total / (float)knownSize;
ILOG("Progress: %f", *progress);
}
return true;
}
int Buffer::Read(int fd, size_t sz) {
char buf[1024];
int retval;
size_t received = 0;
while ((retval = recv(fd, buf, std::min(sz, sizeof(buf)), 0)) > 0) {
if (retval < 0) {
return retval;
}
char *p = Append((size_t)retval);
memcpy(p, buf, retval);
sz -= retval;
@@ -193,4 +216,4 @@ size_t Buffer::Read(int fd, size_t sz) {
void Buffer::PeekAll(std::string *dest) {
dest->resize(data_.size());
memcpy(&(*dest)[0], &data_[0], data_.size());
}
}