Read from sockets using larger, dynamic buffers.

1024 is too small for downloading things.
This commit is contained in:
Unknown W. Brackets 2014-12-31 09:36:00 -08:00
parent b891268d60
commit 48cbcc9a5e
3 changed files with 33 additions and 13 deletions

View File

@ -159,27 +159,43 @@ bool Buffer::FlushSocket(uintptr_t sock) {
return true;
}
bool Buffer::ReadAll(int fd) {
char buf[1024];
bool Buffer::ReadAll(int fd, int hintSize) {
std::vector<char> buf;
if (hintSize >= 65536 * 16) {
buf.resize(65536);
} else if (hintSize >= 1024 * 16) {
buf.resize(hintSize / 16);
} else {
buf.resize(1024);
}
while (true) {
int retval = recv(fd, buf, sizeof(buf), 0);
if (retval == 0)
return true;
else if (retval < 0) {
int retval = recv(fd, buf.data(), (int)buf.size(), 0);
if (retval == 0) {
break;
} else if (retval < 0) {
ELOG("Error reading from buffer: %i", retval);
return false;
}
char *p = Append((size_t)retval);
memcpy(p, buf, retval);
memcpy(p, buf.data(), retval);
}
return true;
}
bool Buffer::ReadAllWithProgress(int fd, int knownSize, float *progress) {
char buf[1024];
std::vector<char> buf;
if (knownSize >= 65536 * 16) {
buf.resize(65536);
} else if (knownSize >= 1024 * 16) {
buf.resize(knownSize / 16);
} else {
buf.resize(1024);
}
int total = 0;
while (true) {
int retval = recv(fd, buf, sizeof(buf), 0);
int retval = recv(fd, buf.data(), (int)buf.size(), 0);
if (retval == 0) {
return true;
} else if (retval < 0) {
@ -187,7 +203,7 @@ bool Buffer::ReadAllWithProgress(int fd, int knownSize, float *progress) {
return false;
}
char *p = Append((size_t)retval);
memcpy(p, buf, retval);
memcpy(p, buf.data(), retval);
total += retval;
*progress = (float)total / (float)knownSize;
}

View File

@ -66,7 +66,7 @@ class Buffer {
bool FlushToFile(const char *filename);
bool FlushSocket(uintptr_t sock); // Windows portability
bool ReadAll(int fd);
bool ReadAll(int fd, int hintSize = 0);
bool ReadAllWithProgress(int fd, int knownSize, float *progress);
// < 0: error

View File

@ -215,7 +215,11 @@ int Client::SendRequestWithData(const char *method, const char *resource, const
"%s"
"\r\n";
buffer.Printf(tpl, method, resource, httpVersion_, host_.c_str(), userAgent_, otherHeaders ? otherHeaders : "");
buffer.Printf(tpl,
method, resource, httpVersion_,
host_.c_str(),
userAgent_,
otherHeaders ? otherHeaders : "");
buffer.Append(data);
bool flushed = buffer.FlushSocket(sock());
if (!flushed) {
@ -297,7 +301,7 @@ int Client::ReadResponseEntity(Buffer *readbuf, const std::vector<std::string> &
if (!contentLength || !progress) {
// No way to know how far along we are. Let's just not update the progress counter.
if (!readbuf->ReadAll(sock()))
if (!readbuf->ReadAll(sock(), contentLength))
return -1;
} else {
// Let's read in chunks, updating progress between each.