diff --git a/src/lib/client/ServerProxy.cpp b/src/lib/client/ServerProxy.cpp index f970ad59..1576ca8f 100644 --- a/src/lib/client/ServerProxy.cpp +++ b/src/lib/client/ServerProxy.cpp @@ -556,38 +556,18 @@ ServerProxy::setClipboard() { // parse static String dataCached; - static size_t expectedSize; ClipboardID id; - UInt32 seqNum; - size_t mark = 0; - String data; - ProtocolUtil::readf(m_stream, kMsgDClipboard + 4, &id, &seqNum, &mark, &data); + UInt32 seq; + + int r = ClipboardChunk::assemble(m_stream, dataCached, id, seq); - if (mark == kDataStart) { - expectedSize = synergy::string::stringToSizeType(data); - LOG((CLOG_DEBUG "start receiving clipboard data")); - dataCached.clear(); - } - else if (mark == kDataChunk) { - dataCached.append(data); - } - else if (mark == kDataEnd) { + if (r == kFinish) { LOG((CLOG_DEBUG "received clipboard %d size=%d", id, dataCached.size())); - - // validate - if (id >= kClipboardEnd) { - return; - } - else if (expectedSize != dataCached.size()) { - LOG((CLOG_ERR "corrupted clipboard data, expected size=%d actual size=%d", expectedSize, dataCached.size())); - return; - } - + // forward Clipboard clipboard; clipboard.unmarshall(dataCached, 0); m_client->setClipboard(id, &clipboard); - expectedSize = 0; } } diff --git a/src/lib/server/ClientProxy1_6.cpp b/src/lib/server/ClientProxy1_6.cpp index eb2c86af..2e359e8e 100644 --- a/src/lib/server/ClientProxy1_6.cpp +++ b/src/lib/server/ClientProxy1_6.cpp @@ -100,47 +100,24 @@ ClientProxy1_6::recvClipboard() { // parse message static String dataCached; - static size_t expectedSize; ClipboardID id; - UInt8 mark; - UInt32 seqNum; - String data; - if (!ProtocolUtil::readf(getStream(), - kMsgDClipboard + 4, &id, &seqNum, &mark, &data)) { - return false; - } - - if (mark == kDataStart) { - expectedSize = synergy::string::stringToSizeType(data); - LOG((CLOG_DEBUG "start receiving clipboard data")); - dataCached.clear(); - } - else if (mark == kDataChunk) { - dataCached.append(data); - } - else if (mark == kDataEnd) { - LOG((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getName().c_str(), id, seqNum, dataCached.size())); - - // validate - if (id >= kClipboardEnd) { - return false; - } - else if (expectedSize != dataCached.size()) { - LOG((CLOG_ERR "corrupted clipboard data, expected size=%d actual size=%d", expectedSize, dataCached.size())); - return false; - } - + UInt32 seq; + + int r = ClipboardChunk::assemble(getStream(), dataCached, id, seq); + + if (r == kFinish) { + LOG((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getName().c_str(), id, seq, dataCached.size())); // save clipboard m_clipboard[id].m_clipboard.unmarshall(dataCached, 0); - m_clipboard[id].m_sequenceNumber = seqNum; + m_clipboard[id].m_sequenceNumber = seq; // notify ClipboardInfo* info = new ClipboardInfo; info->m_id = id; - info->m_sequenceNumber = seqNum; + info->m_sequenceNumber = seq; m_events->addEvent(Event(m_events->forClipboard().clipboardChanged(), getEventTarget(), info)); } - + return true; } diff --git a/src/lib/synergy/ClipboardChunk.cpp b/src/lib/synergy/ClipboardChunk.cpp index 9d43953c..53b97203 100644 --- a/src/lib/synergy/ClipboardChunk.cpp +++ b/src/lib/synergy/ClipboardChunk.cpp @@ -17,7 +17,10 @@ #include "synergy/ClipboardChunk.h" +#include "synergy/ProtocolUtil.h" #include "synergy/protocol_types.h" +#include "io/IStream.h" +#include "base/Log.h" ClipboardChunk::ClipboardChunk(size_t size) : Chunk(size) @@ -79,3 +82,42 @@ ClipboardChunk::end(ClipboardID id, UInt32 sequence) return end; } + +int +ClipboardChunk::assemble(synergy::IStream* stream, + String& dataCached, + ClipboardID& id, + UInt32& sequence) +{ + static size_t expectedSize; + UInt8 mark; + String data; + + if (!ProtocolUtil::readf(stream, kMsgDClipboard + 4, &id, &sequence, &mark, &data)) { + return kError; + } + + if (mark == kDataStart) { + expectedSize = synergy::string::stringToSizeType(data); + LOG((CLOG_DEBUG "start receiving clipboard data")); + dataCached.clear(); + return kNotFinish; + } + else if (mark == kDataChunk) { + dataCached.append(data); + return kNotFinish; + } + else if (mark == kDataEnd) { + // validate + if (id >= kClipboardEnd) { + return kError; + } + else if (expectedSize != dataCached.size()) { + LOG((CLOG_ERR "corrupted clipboard data, expected size=%d actual size=%d", expectedSize, dataCached.size())); + return kError; + } + return kFinish; + } + + return kError; +} diff --git a/src/lib/synergy/ClipboardChunk.h b/src/lib/synergy/ClipboardChunk.h index b3f199fd..f534f4c6 100644 --- a/src/lib/synergy/ClipboardChunk.h +++ b/src/lib/synergy/ClipboardChunk.h @@ -24,6 +24,16 @@ #define CLIPBOARD_CHUNK_META_SIZE 7 +enum EAssembleResult { + kNotFinish, + kFinish, + kError +}; + +namespace synergy { +class IStream; +}; + class ClipboardChunk : public Chunk { public: ClipboardChunk(size_t size); @@ -40,4 +50,10 @@ public: const String& data); static ClipboardChunk* end(ClipboardID id, UInt32 sequence); + + static int assemble( + synergy::IStream* stream, + String& dataCached, + ClipboardID& id, + UInt32& sequence); };