2001-10-06 18:13:28 +04:00
|
|
|
#include "CServerProtocol1_0.h"
|
|
|
|
#include "CServer.h"
|
|
|
|
#include "CProtocolUtil.h"
|
|
|
|
#include "ProtocolTypes.h"
|
|
|
|
#include "IInputStream.h"
|
2001-10-14 18:37:41 +04:00
|
|
|
#include "CLog.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CServerProtocol1_0
|
|
|
|
//
|
|
|
|
|
|
|
|
CServerProtocol1_0::CServerProtocol1_0(CServer* server, const CString& client,
|
|
|
|
IInputStream* input, IOutputStream* output) :
|
|
|
|
CServerProtocol(server, client, input, output)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CServerProtocol1_0::~CServerProtocol1_0()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::run()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// handle messages until the client hangs up
|
|
|
|
for (;;) {
|
|
|
|
// wait for a message
|
|
|
|
UInt8 code[4];
|
|
|
|
UInt32 n = getInputStream()->read(code, 4);
|
|
|
|
|
|
|
|
// verify we got an entire code
|
|
|
|
if (n == 0) {
|
2001-10-14 22:29:43 +04:00
|
|
|
log((CLOG_NOTE "client \"%s\" disconnected", getClient().c_str()));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// client hungup
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (n != 4) {
|
2001-10-14 22:29:43 +04:00
|
|
|
log((CLOG_ERR "incomplete message from \"%s\": %d bytes", getClient().c_str(), n));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// client sent an incomplete message
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse message
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "msg from \"%s\": %c%c%c%c", getClient().c_str(), code[0], code[1], code[2], code[3]));
|
2001-10-06 18:13:28 +04:00
|
|
|
if (memcmp(code, kMsgDInfo, 4) == 0) {
|
|
|
|
recvInfo();
|
|
|
|
}
|
|
|
|
// FIXME -- more message here
|
|
|
|
else {
|
2001-10-14 22:29:43 +04:00
|
|
|
log((CLOG_ERR "unknown message from client \"%s\"", getClient().c_str()));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// unknown message
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::queryInfo()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "querying client \"%s\" info", getClient().c_str()));
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// send request
|
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgQInfo);
|
|
|
|
|
|
|
|
// wait for and verify reply
|
|
|
|
UInt8 code[4];
|
|
|
|
UInt32 n = getInputStream()->read(code, 4);
|
|
|
|
if (n != 4 && memcmp(code, kMsgDInfo, 4) != 0) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle reply
|
|
|
|
recvInfo();
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::sendClose()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send close to \"%s\"", getClient().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendEnter(
|
2001-10-14 20:58:01 +04:00
|
|
|
SInt32 xAbs, SInt32 yAbs)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send enter to \"%s\", %d,%d", getClient().c_str(), xAbs, yAbs));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCEnter, xAbs, yAbs);
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::sendLeave()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send leave to \"%s\"", getClient().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCLeave);
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::sendGrabClipboard()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send grab clipboard to \"%s\"", getClient().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCClipboard);
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::sendQueryClipboard()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "query clipboard to \"%s\"", getClient().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgQClipboard);
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::sendScreenSaver(bool on)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send screen saver to \"%s\"", getClient().c_str()));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgCScreenSaver, on ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendKeyDown(
|
2001-10-14 20:58:01 +04:00
|
|
|
KeyID key, KeyModifierMask mask)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send key down to \"%s\" id=%d, mask=0x%04x", getClient().c_str(), key, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyDown, key, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendKeyRepeat(
|
2001-10-14 20:58:01 +04:00
|
|
|
KeyID key, KeyModifierMask mask)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send key repeat to \"%s\" id=%d, mask=0x%04x", getClient().c_str(), key, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyRepeat, key, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendKeyUp(
|
2001-10-14 20:58:01 +04:00
|
|
|
KeyID key, KeyModifierMask mask)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send key up to \"%s\" id=%d, mask=0x%04x", getClient().c_str(), key, mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDKeyUp, key, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendMouseDown(
|
2001-10-14 20:58:01 +04:00
|
|
|
ButtonID button)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send mouse down to \"%s\" id=%d", getClient().c_str(), button));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseDown, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendMouseUp(
|
2001-10-14 20:58:01 +04:00
|
|
|
ButtonID button)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send mouse up to \"%s\" id=%d", getClient().c_str(), button));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseUp, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendMouseMove(
|
2001-10-14 20:58:01 +04:00
|
|
|
SInt32 xAbs, SInt32 yAbs)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send mouse move to \"%s\" %d,%d", getClient().c_str(), xAbs, yAbs));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseMove, xAbs, yAbs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerProtocol1_0::sendMouseWheel(
|
2001-10-14 20:58:01 +04:00
|
|
|
SInt32 delta)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "send mouse wheel to \"%s\" %+d", getClient().c_str(), delta));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(getOutputStream(), kMsgDMouseWheel, delta);
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CServerProtocol1_0::recvInfo()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// parse the message
|
|
|
|
SInt32 w, h, zoneInfo;
|
|
|
|
CProtocolUtil::readf(getInputStream(), kMsgDInfo + 4, &w, &h, &zoneInfo);
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_INFO "received client \"%s\" info size=%dx%d, zone=%d", getClient().c_str(), w, h, zoneInfo));
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// validate
|
|
|
|
if (w == 0 || h == 0) {
|
|
|
|
throw XBadClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell server of change
|
|
|
|
getServer()->setInfo(getClient(), w, h, zoneInfo);
|
|
|
|
}
|
|
|
|
|