2001-10-06 14:13:28 +00:00
|
|
|
#include "CServerProtocol1_0.h"
|
|
|
|
#include "CServer.h"
|
2001-11-25 18:32:41 +00:00
|
|
|
#include "CClipboard.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CProtocolUtil.h"
|
|
|
|
#include "ProtocolTypes.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSynergy.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "IInputStream.h"
|
2002-06-01 10:52:02 +00:00
|
|
|
#include "IOutputStream.h"
|
2002-05-31 18:09:43 +00:00
|
|
|
#include "CThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
2002-06-26 16:31:48 +00:00
|
|
|
#include "CStopwatch.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstring>
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CServerProtocol1_0
|
|
|
|
//
|
|
|
|
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::CServerProtocol1_0(CServer* server, const CString& client,
|
|
|
|
IInputStream* input, IOutputStream* output) :
|
2002-06-10 22:06:45 +00:00
|
|
|
CServerProtocol(server, client, input, output)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CServerProtocol1_0::~CServerProtocol1_0()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::run()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-06-26 16:31:48 +00:00
|
|
|
// handle messages until the client hangs up or stops sending heartbeats
|
|
|
|
CStopwatch heartTimer;
|
2001-10-06 14:13:28 +00:00
|
|
|
for (;;) {
|
2002-05-31 18:09:43 +00:00
|
|
|
CThread::testCancel();
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// wait for a message
|
|
|
|
UInt8 code[4];
|
2002-06-26 16:31:48 +00:00
|
|
|
UInt32 n = getInputStream()->read(code, 4, kHeartRate);
|
2002-05-31 18:09:43 +00:00
|
|
|
CThread::testCancel();
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-26 16:31:48 +00:00
|
|
|
// check if client hungup
|
2001-10-06 14:13:28 +00:00
|
|
|
if (n == 0) {
|
2001-10-14 18:29:43 +00:00
|
|
|
log((CLOG_NOTE "client \"%s\" disconnected", getClient().c_str()));
|
2001-10-06 14:13:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
|
|
|
|
// check if client has stopped sending heartbeats
|
|
|
|
if (n == (UInt32)-1) {
|
2002-07-01 13:01:16 +00:00
|
|
|
/* FIXME -- disabled to avoid masking bugs
|
2002-06-26 16:31:48 +00:00
|
|
|
if (heartTimer.getTime() > kHeartDeath) {
|
|
|
|
log((CLOG_NOTE "client \"%s\" is dead", getClient().c_str()));
|
|
|
|
return;
|
|
|
|
}
|
2002-07-01 13:01:16 +00:00
|
|
|
*/
|
2002-06-26 16:31:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// got a message so reset heartbeat monitor
|
|
|
|
heartTimer.reset();
|
|
|
|
|
|
|
|
// verify we got an entire code
|
2001-10-06 14:13:28 +00:00
|
|
|
if (n != 4) {
|
2001-10-14 18:29:43 +00:00
|
|
|
log((CLOG_ERR "incomplete message from \"%s\": %d bytes", getClient().c_str(), n));
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// client sent an incomplete message
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse message
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG2 "msg from \"%s\": %c%c%c%c", getClient().c_str(), code[0], code[1], code[2], code[3]));
|
2001-10-06 14:13:28 +00:00
|
|
|
if (memcmp(code, kMsgDInfo, 4) == 0) {
|
|
|
|
recvInfo();
|
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
else if (memcmp(code, kMsgCNoop, 4) == 0) {
|
|
|
|
// discard no-ops
|
|
|
|
continue;
|
|
|
|
}
|
2001-11-25 18:32:41 +00:00
|
|
|
else if (memcmp(code, kMsgCClipboard, 4) == 0) {
|
|
|
|
recvGrabClipboard();
|
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDClipboard, 4) == 0) {
|
|
|
|
recvClipboard();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
// FIXME -- more message here
|
|
|
|
else {
|
2002-05-23 14:56:03 +00:00
|
|
|
log((CLOG_ERR "invalid message from client \"%s\"", getClient().c_str()));
|
2001-10-14 18:29:43 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// unknown message
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::queryInfo()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "querying client \"%s\" info", getClient().c_str()));
|
2001-10-14 14:37:41 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
// send request
|
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgQInfo);
|
|
|
|
|
|
|
|
// wait for and verify reply
|
|
|
|
UInt8 code[4];
|
2002-06-26 16:31:48 +00:00
|
|
|
for (;;) {
|
|
|
|
UInt32 n = getInputStream()->read(code, 4, -1.0);
|
|
|
|
if (n == 4) {
|
|
|
|
if (memcmp(code, kMsgCNoop, 4) == 0) {
|
|
|
|
// discard heartbeats
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (memcmp(code, kMsgDInfo, 4) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle reply
|
|
|
|
recvInfo();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::sendClose()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send close to \"%s\"", getClient().c_str()));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCClose);
|
2002-06-01 10:52:02 +00:00
|
|
|
|
|
|
|
// force the close to be sent before we return
|
|
|
|
getOutputStream()->flush();
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendEnter(SInt32 xAbs, SInt32 yAbs,
|
|
|
|
UInt32 seqNum, KeyModifierMask mask)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-30 17:48:11 +00:00
|
|
|
log((CLOG_DEBUG1 "send enter to \"%s\", %d,%d %d %04x", getClient().c_str(), xAbs, yAbs, seqNum, mask));
|
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCEnter,
|
|
|
|
xAbs, yAbs, seqNum, mask);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::sendLeave()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send leave to \"%s\"", getClient().c_str()));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCLeave);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendClipboard(ClipboardID id, const CString& data)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG "send clipboard %d to \"%s\" size=%d", id, getClient().c_str(), data.size()));
|
2002-04-27 14:19:53 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDClipboard, id, 0, &data);
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendGrabClipboard(ClipboardID id)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG "send grab clipboard %d to \"%s\"", id, getClient().c_str()));
|
2002-04-29 13:31:44 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCClipboard, id, 0);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendScreenSaver(bool on)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-30 16:23:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send screen saver to \"%s\" on=%d", getClient().c_str(), on ? 1 : 0));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCScreenSaver, on ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::sendInfoAcknowledgment()
|
2002-05-24 17:54:28 +00:00
|
|
|
{
|
|
|
|
log((CLOG_DEBUG1 "send info ack to \"%s\"", getClient().c_str()));
|
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCInfoAck);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendKeyDown(KeyID key, KeyModifierMask mask)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send key down to \"%s\" id=%d, mask=0x%04x", getClient().c_str(), key, mask));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyDown, key, mask);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendKeyRepeat(KeyID key, KeyModifierMask mask, SInt32 count)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-30 16:23:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send key repeat to \"%s\" id=%d, mask=0x%04x, count=%d", getClient().c_str(), key, mask, count));
|
2001-11-25 18:32:41 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyRepeat, key, mask, count);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendKeyUp(KeyID key, KeyModifierMask mask)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send key up to \"%s\" id=%d, mask=0x%04x", getClient().c_str(), key, mask));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyUp, key, mask);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendMouseDown(ButtonID button)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send mouse down to \"%s\" id=%d", getClient().c_str(), button));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseDown, button);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendMouseUp(ButtonID button)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "send mouse up to \"%s\" id=%d", getClient().c_str(), button));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseUp, button);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendMouseMove(SInt32 xAbs, SInt32 yAbs)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG2 "send mouse move to \"%s\" %d,%d", getClient().c_str(), xAbs, yAbs));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseMove, xAbs, yAbs);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CServerProtocol1_0::sendMouseWheel(SInt32 delta)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-04-30 16:23:03 +00:00
|
|
|
log((CLOG_DEBUG2 "send mouse wheel to \"%s\" %+d", getClient().c_str(), delta));
|
2001-10-06 14:13:28 +00:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseWheel, delta);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::recvInfo()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// parse the message
|
2002-06-19 17:03:29 +00:00
|
|
|
SInt16 x, y, w, h, zoneInfo, mx, my;
|
2002-05-24 17:54:28 +00:00
|
|
|
CProtocolUtil::readf(getInputStream(), kMsgDInfo + 4,
|
2002-06-19 17:03:29 +00:00
|
|
|
&x, &y, &w, &h, &zoneInfo, &mx, &my);
|
|
|
|
log((CLOG_DEBUG "received client \"%s\" info shape=%d,%d %dx%d, zone=%d, pos=%d,%d", getClient().c_str(), x, y, w, h, zoneInfo, mx, my));
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// validate
|
2002-04-29 14:25:24 +00:00
|
|
|
if (w <= 0 || h <= 0 || zoneInfo < 0) {
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XBadClient();
|
|
|
|
}
|
2002-06-19 17:03:29 +00:00
|
|
|
if (mx < x || my < y || mx >= x + w || my >= y + h) {
|
2002-05-24 17:54:28 +00:00
|
|
|
throw XBadClient();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// tell server of change
|
2002-06-19 17:03:29 +00:00
|
|
|
getServer()->setInfo(getClient(), x, y, w, h, zoneInfo, mx, my);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::recvClipboard()
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-29 14:25:24 +00:00
|
|
|
// parse message
|
2002-04-27 14:19:53 +00:00
|
|
|
ClipboardID id;
|
2001-11-25 18:32:41 +00:00
|
|
|
UInt32 seqNum;
|
|
|
|
CString data;
|
2002-04-27 14:19:53 +00:00
|
|
|
CProtocolUtil::readf(getInputStream(), kMsgDClipboard + 4, &id, &seqNum, &data);
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getClient().c_str(), id, seqNum, data.size()));
|
2002-04-29 14:25:24 +00:00
|
|
|
|
|
|
|
// validate
|
|
|
|
if (id >= kClipboardEnd) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// send update
|
2002-04-27 14:19:53 +00:00
|
|
|
getServer()->setClipboard(id, seqNum, data);
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CServerProtocol1_0::recvGrabClipboard()
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-04-29 14:25:24 +00:00
|
|
|
// parse message
|
2002-04-27 14:19:53 +00:00
|
|
|
ClipboardID id;
|
2002-04-29 13:31:44 +00:00
|
|
|
UInt32 seqNum;
|
|
|
|
CProtocolUtil::readf(getInputStream(), kMsgCClipboard + 4, &id, &seqNum);
|
|
|
|
log((CLOG_DEBUG "received client \"%s\" grabbed clipboard %d seqnum=%d", getClient().c_str(), id, seqNum));
|
2002-04-29 14:25:24 +00:00
|
|
|
|
|
|
|
// validate
|
|
|
|
if (id >= kClipboardEnd) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// send update
|
2002-04-29 13:31:44 +00:00
|
|
|
getServer()->grabClipboard(id, seqNum, getClient());
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|