#4740 Forced delay on heartbeat rather than each clipboard chunk

This commit is contained in:
Xinyu Hou 2016-09-13 11:06:06 -04:00 committed by Andrew Nelless
parent d14d907ac1
commit 8ab69a22ae
1 changed files with 38 additions and 41 deletions

View File

@ -33,10 +33,9 @@
#include <fstream> #include <fstream>
#define SEND_THRESHOLD 0.005f
using namespace std; using namespace std;
#define FAKE_HEARTBEAT_THRESHOLD 3
#define CHUNK_SIZE 512 * 1024; // 512kb #define CHUNK_SIZE 512 * 1024; // 512kb
bool StreamChunker::s_isChunkingFile = false; bool StreamChunker::s_isChunkingFile = false;
@ -70,8 +69,8 @@ StreamChunker::sendFile(
// send chunk messages with a fixed chunk size // send chunk messages with a fixed chunk size
size_t sentLength = 0; size_t sentLength = 0;
size_t chunkSize = CHUNK_SIZE; size_t chunkSize = CHUNK_SIZE;
Stopwatch sendStopwatch; Stopwatch fakeHeartbeatStopwatch;
sendStopwatch.start(); fakeHeartbeatStopwatch.start();
file.seekg (0, std::ios::beg); file.seekg (0, std::ios::beg);
while (true) { while (true) {
@ -81,30 +80,29 @@ StreamChunker::sendFile(
break; break;
} }
if (sendStopwatch.getTime() > SEND_THRESHOLD) { if (fakeHeartbeatStopwatch.getTime() > FAKE_HEARTBEAT_THRESHOLD) {
events->addEvent(Event(events->forFile().keepAlive(), eventTarget)); events->addEvent(Event(events->forFile().keepAlive(), eventTarget));
fakeHeartbeatStopwatch.reset();
}
// make sure we don't read too much from the mock data.
if (sentLength + chunkSize > size) {
chunkSize = size - sentLength;
}
// make sure we don't read too much from the mock data. char* chunkData = new char[chunkSize];
if (sentLength + chunkSize > size) { file.read(chunkData, chunkSize);
chunkSize = size - sentLength; UInt8* data = reinterpret_cast<UInt8*>(chunkData);
} FileChunk* fileChunk = FileChunk::data(data, chunkSize);
delete[] chunkData;
char* chunkData = new char[chunkSize]; events->addEvent(Event(events->forFile().fileChunkSending(), eventTarget, fileChunk));
file.read(chunkData, chunkSize);
UInt8* data = reinterpret_cast<UInt8*>(chunkData);
FileChunk* fileChunk = FileChunk::data(data, chunkSize);
delete[] chunkData;
events->addEvent(Event(events->forFile().fileChunkSending(), eventTarget, fileChunk)); sentLength += chunkSize;
file.seekg (sentLength, std::ios::beg);
sentLength += chunkSize; if (sentLength == size) {
file.seekg (sentLength, std::ios::beg); break;
if (sentLength == size) {
break;
}
sendStopwatch.reset();
} }
} }
@ -136,29 +134,28 @@ StreamChunker::sendClipboard(
// send clipboard chunk with a fixed size // send clipboard chunk with a fixed size
size_t sentLength = 0; size_t sentLength = 0;
size_t chunkSize = CHUNK_SIZE; size_t chunkSize = CHUNK_SIZE;
Stopwatch sendStopwatch; Stopwatch fakeHeartbeatStopwatch;
sendStopwatch.start(); fakeHeartbeatStopwatch.start();
while (true) { while (true) {
if (sendStopwatch.getTime() > SEND_THRESHOLD) { if (fakeHeartbeatStopwatch.getTime() > FAKE_HEARTBEAT_THRESHOLD) {
events->addEvent(Event(events->forFile().keepAlive(), eventTarget)); events->addEvent(Event(events->forFile().keepAlive(), eventTarget));
fakeHeartbeatStopwatch.reset();
}
// make sure we don't read too much from the mock data.
if (sentLength + chunkSize > size) {
chunkSize = size - sentLength;
}
// make sure we don't read too much from the mock data. String chunk(data.substr(sentLength, chunkSize).c_str(), chunkSize);
if (sentLength + chunkSize > size) { ClipboardChunk* dataChunk = ClipboardChunk::data(id, sequence, chunk);
chunkSize = size - sentLength;
} events->addEvent(Event(events->forClipboard().clipboardSending(), eventTarget, dataChunk));
String chunk(data.substr(sentLength, chunkSize).c_str(), chunkSize); sentLength += chunkSize;
ClipboardChunk* dataChunk = ClipboardChunk::data(id, sequence, chunk); if (sentLength == size) {
break;
events->addEvent(Event(events->forClipboard().clipboardSending(), eventTarget, dataChunk));
sentLength += chunkSize;
if (sentLength == size) {
break;
}
sendStopwatch.reset();
} }
} }