Added changes for StreamChunker that is left from last commit #4601
This commit is contained in:
parent
c674642181
commit
6e7b3d87c5
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
#include "synergy/StreamChunker.h"
|
#include "synergy/StreamChunker.h"
|
||||||
|
|
||||||
|
#include "synergy/FileChunk.h"
|
||||||
|
#include "synergy/ClipboardChunk.h"
|
||||||
#include "synergy/protocol_types.h"
|
#include "synergy/protocol_types.h"
|
||||||
#include "base/EventTypes.h"
|
#include "base/EventTypes.h"
|
||||||
#include "base/Event.h"
|
#include "base/Event.h"
|
||||||
|
@ -24,10 +26,10 @@
|
||||||
#include "base/EventTypes.h"
|
#include "base/EventTypes.h"
|
||||||
#include "base/Log.h"
|
#include "base/Log.h"
|
||||||
#include "base/Stopwatch.h"
|
#include "base/Stopwatch.h"
|
||||||
|
#include "base/String.h"
|
||||||
#include "common/stdexcept.h"
|
#include "common/stdexcept.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#define PAUSE_TIME_HACK 0.1
|
#define PAUSE_TIME_HACK 0.1
|
||||||
|
|
||||||
|
@ -36,7 +38,10 @@ using namespace std;
|
||||||
const size_t StreamChunker::m_chunkSize = 512 * 1024; // 512kb
|
const size_t StreamChunker::m_chunkSize = 512 * 1024; // 512kb
|
||||||
|
|
||||||
void
|
void
|
||||||
StreamChunker::sendFileChunks(char* filename, IEventQueue* events, void* eventTarget)
|
StreamChunker::sendFile(
|
||||||
|
char* filename,
|
||||||
|
IEventQueue* events,
|
||||||
|
void* eventTarget)
|
||||||
{
|
{
|
||||||
std::fstream file(reinterpret_cast<char*>(filename), std::ios::in | std::ios::binary);
|
std::fstream file(reinterpret_cast<char*>(filename), std::ios::in | std::ios::binary);
|
||||||
|
|
||||||
|
@ -49,14 +54,9 @@ StreamChunker::sendFileChunks(char* filename, IEventQueue* events, void* eventTa
|
||||||
size_t size = (size_t)file.tellg();
|
size_t size = (size_t)file.tellg();
|
||||||
|
|
||||||
// send first message (file size)
|
// send first message (file size)
|
||||||
String fileSize = intToString(size);
|
String fileSize = synergy::string::intToString(size);
|
||||||
size_t sizeLength = fileSize.size();
|
FileChunk* sizeMessage = FileChunk::start(fileSize);
|
||||||
Chunk* sizeMessage = new Chunk(sizeLength + 2);
|
|
||||||
char* chunkData = sizeMessage->m_chunk;
|
|
||||||
|
|
||||||
chunkData[0] = kDataStart;
|
|
||||||
memcpy(&chunkData[1], fileSize.c_str(), sizeLength);
|
|
||||||
chunkData[sizeLength + 1] = '\0';
|
|
||||||
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, sizeMessage));
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, sizeMessage));
|
||||||
|
|
||||||
// send chunk messages with a fixed chunk size
|
// send chunk messages with a fixed chunk size
|
||||||
|
@ -72,13 +72,12 @@ StreamChunker::sendFileChunks(char* filename, IEventQueue* events, void* eventTa
|
||||||
chunkSize = size - sentLength;
|
chunkSize = size - sentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// for fileChunk->m_chunk, the first byte is the chunk mark, last is \0
|
char* chunkData = new char[chunkSize];
|
||||||
Chunk* fileChunk = new Chunk(chunkSize + 2);
|
file.read(chunkData, chunkSize);
|
||||||
char* chunkData = fileChunk->m_chunk;
|
UInt8* data = reinterpret_cast<UInt8*>(chunkData);
|
||||||
|
FileChunk* fileChunk = FileChunk::data(data, chunkSize);
|
||||||
|
delete[] chunkData;
|
||||||
|
|
||||||
chunkData[0] = kDataChunk;
|
|
||||||
file.read(&chunkData[1], chunkSize);
|
|
||||||
chunkData[chunkSize + 1] = '\0';
|
|
||||||
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, fileChunk));
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, fileChunk));
|
||||||
|
|
||||||
sentLength += chunkSize;
|
sentLength += chunkSize;
|
||||||
|
@ -93,21 +92,58 @@ StreamChunker::sendFileChunks(char* filename, IEventQueue* events, void* eventTa
|
||||||
}
|
}
|
||||||
|
|
||||||
// send last message
|
// send last message
|
||||||
Chunk* transferFinished = new Chunk(2);
|
FileChunk* end = FileChunk::end();
|
||||||
chunkData = transferFinished->m_chunk;
|
|
||||||
|
|
||||||
chunkData[0] = kDataEnd;
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, end));
|
||||||
chunkData[1] = '\0';
|
|
||||||
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, transferFinished));
|
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
String
|
void
|
||||||
StreamChunker::intToString(size_t i)
|
StreamChunker::sendClipboard(
|
||||||
|
String& data,
|
||||||
|
size_t size,
|
||||||
|
ClipboardID id,
|
||||||
|
UInt32 sequence,
|
||||||
|
IEventQueue* events,
|
||||||
|
void* eventTarget)
|
||||||
{
|
{
|
||||||
//TODO: this should be in string
|
// send first message (data size)
|
||||||
stringstream ss;
|
String dataSize = synergy::string::intToString(size);
|
||||||
ss << i;
|
ClipboardChunk* sizeMessage = ClipboardChunk::start(id, sequence, dataSize);
|
||||||
return ss.str();
|
|
||||||
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, sizeMessage));
|
||||||
|
|
||||||
|
// send clipboard chunk with a fixed size
|
||||||
|
// TODO: 4096 fails and this shouldn't a magic number
|
||||||
|
size_t sentLength = 0;
|
||||||
|
size_t chunkSize = 2048;
|
||||||
|
Stopwatch stopwatch;
|
||||||
|
stopwatch.start();
|
||||||
|
while (true) {
|
||||||
|
if (stopwatch.getTime() > 0.1f) {
|
||||||
|
// make sure we don't read too much from the mock data.
|
||||||
|
if (sentLength + chunkSize > size) {
|
||||||
|
chunkSize = size - sentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
String chunk(data.substr(sentLength, chunkSize).c_str(), chunkSize);
|
||||||
|
ClipboardChunk* dataChunk = ClipboardChunk::data(id, sequence, chunk);
|
||||||
|
|
||||||
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, dataChunk));
|
||||||
|
|
||||||
|
sentLength += chunkSize;
|
||||||
|
|
||||||
|
if (sentLength == size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
stopwatch.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send last message
|
||||||
|
ClipboardChunk* end = ClipboardChunk::end(id, sequence);
|
||||||
|
|
||||||
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, end));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue