2013-07-24 16:41:12 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2014-11-02 12:12:05 +00:00
|
|
|
* Copyright (C) 2013 Synergy Si Ltd.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-05-18 17:17:22 +00:00
|
|
|
#include "synergy/StreamChunker.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2015-05-19 16:37:15 +00:00
|
|
|
#include "synergy/FileChunk.h"
|
|
|
|
#include "synergy/ClipboardChunk.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "synergy/protocol_types.h"
|
|
|
|
#include "base/EventTypes.h"
|
|
|
|
#include "base/Event.h"
|
|
|
|
#include "base/IEventQueue.h"
|
|
|
|
#include "base/EventTypes.h"
|
|
|
|
#include "base/Log.h"
|
|
|
|
#include "base/Stopwatch.h"
|
2015-05-19 16:37:15 +00:00
|
|
|
#include "base/String.h"
|
2014-03-14 20:33:18 +00:00
|
|
|
#include "common/stdexcept.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2013-07-24 16:41:12 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2013-07-26 14:10:06 +00:00
|
|
|
#define PAUSE_TIME_HACK 0.1
|
|
|
|
|
2013-07-24 16:41:12 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2015-05-18 17:17:22 +00:00
|
|
|
const size_t StreamChunker::m_chunkSize = 512 * 1024; // 512kb
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
void
|
2015-05-19 16:37:15 +00:00
|
|
|
StreamChunker::sendFile(
|
|
|
|
char* filename,
|
|
|
|
IEventQueue* events,
|
|
|
|
void* eventTarget)
|
2013-07-24 16:41:12 +00:00
|
|
|
{
|
|
|
|
std::fstream file(reinterpret_cast<char*>(filename), std::ios::in | std::ios::binary);
|
|
|
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
throw runtime_error("failed to open file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// check file size
|
|
|
|
file.seekg (0, std::ios::end);
|
|
|
|
size_t size = (size_t)file.tellg();
|
|
|
|
|
|
|
|
// send first message (file size)
|
2015-05-19 21:23:43 +00:00
|
|
|
String fileSize = synergy::string::sizeTypeToString(size);
|
2015-05-19 16:37:15 +00:00
|
|
|
FileChunk* sizeMessage = FileChunk::start(fileSize);
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, sizeMessage));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
// send chunk messages with a fixed chunk size
|
|
|
|
size_t sentLength = 0;
|
|
|
|
size_t chunkSize = m_chunkSize;
|
2014-11-11 13:51:47 +00:00
|
|
|
Stopwatch stopwatch;
|
2013-07-26 14:10:06 +00:00
|
|
|
stopwatch.start();
|
2013-07-24 16:41:12 +00:00
|
|
|
file.seekg (0, std::ios::beg);
|
|
|
|
while (true) {
|
2013-07-26 14:10:06 +00:00
|
|
|
if (stopwatch.getTime() > PAUSE_TIME_HACK) {
|
|
|
|
// make sure we don't read too much from the mock data.
|
|
|
|
if (sentLength + chunkSize > size) {
|
|
|
|
chunkSize = size - sentLength;
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:15 +00:00
|
|
|
char* chunkData = new char[chunkSize];
|
|
|
|
file.read(chunkData, chunkSize);
|
|
|
|
UInt8* data = reinterpret_cast<UInt8*>(chunkData);
|
|
|
|
FileChunk* fileChunk = FileChunk::data(data, chunkSize);
|
|
|
|
delete[] chunkData;
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, fileChunk));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2013-07-26 14:10:06 +00:00
|
|
|
sentLength += chunkSize;
|
|
|
|
file.seekg (sentLength, std::ios::beg);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2013-07-26 14:10:06 +00:00
|
|
|
if (sentLength == size) {
|
|
|
|
break;
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2013-07-26 14:10:06 +00:00
|
|
|
stopwatch.reset();
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send last message
|
2015-05-19 16:37:15 +00:00
|
|
|
FileChunk* end = FileChunk::end();
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2015-05-19 16:37:15 +00:00
|
|
|
events->addEvent(Event(events->forIScreen().fileChunkSending(), eventTarget, end));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:37:15 +00:00
|
|
|
void
|
|
|
|
StreamChunker::sendClipboard(
|
|
|
|
String& data,
|
|
|
|
size_t size,
|
|
|
|
ClipboardID id,
|
|
|
|
UInt32 sequence,
|
|
|
|
IEventQueue* events,
|
|
|
|
void* eventTarget)
|
2013-07-24 16:41:12 +00:00
|
|
|
{
|
2015-05-19 16:37:15 +00:00
|
|
|
// send first message (data size)
|
2015-05-19 21:23:43 +00:00
|
|
|
String dataSize = synergy::string::sizeTypeToString(size);
|
2015-05-19 16:37:15 +00:00
|
|
|
ClipboardChunk* sizeMessage = ClipboardChunk::start(id, sequence, dataSize);
|
|
|
|
|
2015-05-20 18:09:54 +00:00
|
|
|
events->addEvent(Event(events->forClipboard().clipboardSending(), eventTarget, sizeMessage));
|
2015-05-19 16:37:15 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2015-05-20 18:09:54 +00:00
|
|
|
events->addEvent(Event(events->forClipboard().clipboardSending(), eventTarget, dataChunk));
|
2015-05-19 16:37:15 +00:00
|
|
|
|
|
|
|
sentLength += chunkSize;
|
|
|
|
|
|
|
|
if (sentLength == size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopwatch.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send last message
|
|
|
|
ClipboardChunk* end = ClipboardChunk::end(id, sequence);
|
|
|
|
|
2015-05-20 18:09:54 +00:00
|
|
|
events->addEvent(Event(events->forClipboard().clipboardSending(), eventTarget, end));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|