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
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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
};