Refactored duplicated code #4601

This commit is contained in:
Jerry (Xinyu Hou) 2015-05-21 16:04:50 -07:00
parent 342a345d1f
commit 1c28444cf6
4 changed files with 72 additions and 57 deletions

View File

@ -556,38 +556,18 @@ ServerProxy::setClipboard()
{ {
// parse // parse
static String dataCached; static String dataCached;
static size_t expectedSize;
ClipboardID id; ClipboardID id;
UInt32 seqNum; UInt32 seq;
size_t mark = 0;
String data;
ProtocolUtil::readf(m_stream, kMsgDClipboard + 4, &id, &seqNum, &mark, &data);
if (mark == kDataStart) { int r = ClipboardChunk::assemble(m_stream, dataCached, id, seq);
expectedSize = synergy::string::stringToSizeType(data);
LOG((CLOG_DEBUG "start receiving clipboard data")); if (r == kFinish) {
dataCached.clear();
}
else if (mark == kDataChunk) {
dataCached.append(data);
}
else if (mark == kDataEnd) {
LOG((CLOG_DEBUG "received clipboard %d size=%d", id, dataCached.size())); 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 // forward
Clipboard clipboard; Clipboard clipboard;
clipboard.unmarshall(dataCached, 0); clipboard.unmarshall(dataCached, 0);
m_client->setClipboard(id, &clipboard); m_client->setClipboard(id, &clipboard);
expectedSize = 0;
} }
} }

View File

@ -100,44 +100,21 @@ ClientProxy1_6::recvClipboard()
{ {
// parse message // parse message
static String dataCached; static String dataCached;
static size_t expectedSize;
ClipboardID id; ClipboardID id;
UInt8 mark; UInt32 seq;
UInt32 seqNum;
String data;
if (!ProtocolUtil::readf(getStream(),
kMsgDClipboard + 4, &id, &seqNum, &mark, &data)) {
return false;
}
if (mark == kDataStart) { int r = ClipboardChunk::assemble(getStream(), dataCached, id, seq);
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;
}
if (r == kFinish) {
LOG((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getName().c_str(), id, seq, dataCached.size()));
// save clipboard // save clipboard
m_clipboard[id].m_clipboard.unmarshall(dataCached, 0); m_clipboard[id].m_clipboard.unmarshall(dataCached, 0);
m_clipboard[id].m_sequenceNumber = seqNum; m_clipboard[id].m_sequenceNumber = seq;
// notify // notify
ClipboardInfo* info = new ClipboardInfo; ClipboardInfo* info = new ClipboardInfo;
info->m_id = id; info->m_id = id;
info->m_sequenceNumber = seqNum; info->m_sequenceNumber = seq;
m_events->addEvent(Event(m_events->forClipboard().clipboardChanged(), m_events->addEvent(Event(m_events->forClipboard().clipboardChanged(),
getEventTarget(), info)); getEventTarget(), info));
} }

View File

@ -17,7 +17,10 @@
#include "synergy/ClipboardChunk.h" #include "synergy/ClipboardChunk.h"
#include "synergy/ProtocolUtil.h"
#include "synergy/protocol_types.h" #include "synergy/protocol_types.h"
#include "io/IStream.h"
#include "base/Log.h"
ClipboardChunk::ClipboardChunk(size_t size) : ClipboardChunk::ClipboardChunk(size_t size) :
Chunk(size) Chunk(size)
@ -79,3 +82,42 @@ ClipboardChunk::end(ClipboardID id, UInt32 sequence)
return end; 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;
}

View File

@ -24,6 +24,16 @@
#define CLIPBOARD_CHUNK_META_SIZE 7 #define CLIPBOARD_CHUNK_META_SIZE 7
enum EAssembleResult {
kNotFinish,
kFinish,
kError
};
namespace synergy {
class IStream;
};
class ClipboardChunk : public Chunk { class ClipboardChunk : public Chunk {
public: public:
ClipboardChunk(size_t size); ClipboardChunk(size_t size);
@ -40,4 +50,10 @@ public:
const String& data); const String& data);
static ClipboardChunk* static ClipboardChunk*
end(ClipboardID id, UInt32 sequence); end(ClipboardID id, UInt32 sequence);
static int assemble(
synergy::IStream* stream,
String& dataCached,
ClipboardID& id,
UInt32& sequence);
}; };