2001-10-06 18:13:28 +04:00
|
|
|
#include "CClient.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CClipboard.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CInputPacketStream.h"
|
|
|
|
#include "COutputPacketStream.h"
|
|
|
|
#include "CProtocolUtil.h"
|
2001-10-08 23:24:46 +04:00
|
|
|
#include "ISecondaryScreen.h"
|
|
|
|
#include "ProtocolTypes.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XScreen.h"
|
|
|
|
#include "XSynergy.h"
|
|
|
|
#include "XSocket.h"
|
2001-11-25 21:32:41 +03:00
|
|
|
#include "CLock.h"
|
2001-10-14 22:29:43 +04:00
|
|
|
#include "CThread.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CTimerThread.h"
|
2002-06-02 22:49:35 +04:00
|
|
|
#include "XThread.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CLog.h"
|
|
|
|
#include "TMethodJob.h"
|
2001-10-08 23:24:46 +04:00
|
|
|
#include <memory>
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// hack to work around operator=() bug in STL in g++ prior to v3
|
|
|
|
#if defined(__GNUC__) && (__GNUC__ < 3)
|
|
|
|
#define assign(_dst, _src, _type) _dst.reset(_src)
|
|
|
|
#else
|
|
|
|
#define assign(_dst, _src, _type) _dst = std::auto_ptr<_type >(_src)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
//
|
|
|
|
// CClient
|
|
|
|
//
|
|
|
|
|
2002-06-17 17:31:21 +04:00
|
|
|
CClient::CClient(const CString& clientName) :
|
2002-06-11 02:06:45 +04:00
|
|
|
m_name(clientName),
|
|
|
|
m_input(NULL),
|
|
|
|
m_output(NULL),
|
|
|
|
m_screen(NULL),
|
|
|
|
m_camp(false),
|
|
|
|
m_active(false),
|
|
|
|
m_seqNum(0),
|
|
|
|
m_ignoreMove(false)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 22:29:43 +04:00
|
|
|
// do nothing
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CClient::~CClient()
|
|
|
|
{
|
2001-10-14 22:29:43 +04:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CClient::camp(bool on)
|
2002-06-10 02:20:28 +04:00
|
|
|
{
|
|
|
|
m_camp = on;
|
|
|
|
}
|
|
|
|
|
2002-06-21 21:55:47 +04:00
|
|
|
bool
|
|
|
|
CClient::open()
|
|
|
|
{
|
|
|
|
// open the screen
|
|
|
|
try {
|
|
|
|
log((CLOG_INFO "opening screen"));
|
|
|
|
openSecondaryScreen();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (XScreenOpenFailure&) {
|
|
|
|
// can't open screen yet. wait a few seconds to retry.
|
|
|
|
CThread::sleep(3.0);
|
|
|
|
log((CLOG_INFO "failed to open screen"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CClient::run(const CNetworkAddress& serverAddress)
|
2001-10-14 22:29:43 +04:00
|
|
|
{
|
2002-06-21 21:55:47 +04:00
|
|
|
// check preconditions
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
}
|
|
|
|
|
2002-05-22 20:42:48 +04:00
|
|
|
CThread* thread = NULL;
|
2001-11-19 03:33:36 +03:00
|
|
|
try {
|
|
|
|
log((CLOG_NOTE "starting client"));
|
|
|
|
|
|
|
|
// start server interactions
|
|
|
|
m_serverAddress = &serverAddress;
|
|
|
|
thread = new CThread(new TMethodJob<CClient>(this, &CClient::runSession));
|
|
|
|
|
|
|
|
// handle events
|
|
|
|
log((CLOG_DEBUG "starting event handling"));
|
|
|
|
m_screen->run();
|
|
|
|
|
|
|
|
// clean up
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_NOTE "stopping client"));
|
2001-11-19 03:33:36 +03:00
|
|
|
thread->cancel();
|
2002-06-10 02:20:28 +04:00
|
|
|
void* result = thread->getResult();
|
2001-11-19 03:33:36 +03:00
|
|
|
delete thread;
|
|
|
|
closeSecondaryScreen();
|
2002-06-10 02:20:28 +04:00
|
|
|
return (result != NULL);
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-11-25 21:32:41 +03:00
|
|
|
log((CLOG_ERR "client error: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// clean up
|
2002-06-02 22:49:35 +04:00
|
|
|
log((CLOG_NOTE "stopping client"));
|
|
|
|
if (thread != NULL) {
|
|
|
|
thread->cancel();
|
|
|
|
thread->wait();
|
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
closeSecondaryScreen();
|
2002-06-10 02:20:28 +04:00
|
|
|
return true;
|
2002-06-02 22:49:35 +04:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
// clean up
|
|
|
|
log((CLOG_NOTE "stopping client"));
|
2002-05-22 20:42:48 +04:00
|
|
|
if (thread != NULL) {
|
|
|
|
thread->cancel();
|
|
|
|
thread->wait();
|
|
|
|
delete thread;
|
|
|
|
}
|
2002-06-21 21:55:47 +04:00
|
|
|
closeSecondaryScreen();
|
2002-06-02 22:49:35 +04:00
|
|
|
throw;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
log((CLOG_DEBUG "unknown client error"));
|
|
|
|
|
|
|
|
// clean up
|
2002-06-02 22:49:35 +04:00
|
|
|
log((CLOG_NOTE "stopping client"));
|
2002-05-22 20:42:48 +04:00
|
|
|
if (thread != NULL) {
|
|
|
|
thread->cancel();
|
|
|
|
thread->wait();
|
|
|
|
delete thread;
|
|
|
|
}
|
2002-06-21 21:55:47 +04:00
|
|
|
closeSecondaryScreen();
|
2001-11-19 03:33:36 +03:00
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::quit()
|
2002-06-09 01:48:00 +04:00
|
|
|
{
|
|
|
|
m_screen->stop();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CClient::onClipboardChanged(ClipboardID id)
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
2002-04-27 18:19:53 +04:00
|
|
|
log((CLOG_DEBUG "sending clipboard %d changed", id));
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_output != NULL) {
|
|
|
|
// m_output can be NULL if the screen calls this method
|
|
|
|
// before we've gotten around to connecting to the server.
|
2002-04-29 17:31:44 +04:00
|
|
|
CProtocolUtil::writef(m_output, kMsgCClipboard, id, m_seqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we now own the clipboard and it has not been sent to the server
|
|
|
|
m_ownClipboard[id] = true;
|
|
|
|
m_timeClipboard[id] = 0;
|
|
|
|
|
|
|
|
// if we're not the active screen then send the clipboard now,
|
2002-05-24 21:54:28 +04:00
|
|
|
// otherwise we'll wait until we leave.
|
2002-04-29 17:31:44 +04:00
|
|
|
if (!m_active) {
|
|
|
|
// get clipboard
|
|
|
|
CClipboard clipboard;
|
|
|
|
m_screen->getClipboard(id, &clipboard);
|
|
|
|
|
|
|
|
// save new time
|
|
|
|
m_timeClipboard[id] = clipboard.getTime();
|
|
|
|
|
|
|
|
// marshall the data
|
|
|
|
CString data = clipboard.marshall();
|
|
|
|
|
|
|
|
// send data
|
|
|
|
log((CLOG_DEBUG "sending clipboard %d seqnum=%d, size=%d", id, m_seqNum, data.size()));
|
2002-04-30 20:23:03 +04:00
|
|
|
if (m_output != NULL) {
|
|
|
|
// FIXME -- will we send the clipboard when we connect?
|
|
|
|
CProtocolUtil::writef(m_output, kMsgDClipboard, id, m_seqNum, &data);
|
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onResolutionChanged()
|
2002-05-24 21:54:28 +04:00
|
|
|
{
|
|
|
|
log((CLOG_DEBUG "resolution changed"));
|
|
|
|
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// start ignoring mouse movement until we get an acknowledgment
|
|
|
|
m_ignoreMove = true;
|
|
|
|
|
|
|
|
// send notification of resolution change
|
|
|
|
onQueryInfoNoLock();
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
#include "CTCPSocket.h" // FIXME
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::runSession(void*)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "starting client \"%s\"", m_name.c_str()));
|
|
|
|
|
2002-06-17 16:02:26 +04:00
|
|
|
std::auto_ptr<IDataSocket> socket;
|
2001-10-06 18:13:28 +04:00
|
|
|
std::auto_ptr<IInputStream> input;
|
|
|
|
std::auto_ptr<IOutputStream> output;
|
|
|
|
try {
|
2002-06-10 02:20:28 +04:00
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
// allow connect this much time to succeed
|
|
|
|
// FIXME -- timeout in member
|
|
|
|
CTimerThread timer(m_camp ? -1.0 : 30.0);
|
|
|
|
|
|
|
|
// create socket and attempt to connect to server
|
|
|
|
log((CLOG_DEBUG1 "connecting to server"));
|
2002-06-17 16:02:26 +04:00
|
|
|
assign(socket, new CTCPSocket(), IDataSocket); // FIXME -- use factory
|
2002-06-10 02:20:28 +04:00
|
|
|
socket->connect(*m_serverAddress);
|
|
|
|
log((CLOG_INFO "connected to server"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (XSocketConnect&) {
|
|
|
|
// failed to connect. if not camping then rethrow.
|
|
|
|
if (!m_camp) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're camping. wait a bit before retrying
|
|
|
|
CThread::sleep(5.0);
|
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// get the input and output streams
|
|
|
|
IInputStream* srcInput = socket->getInputStream();
|
|
|
|
IOutputStream* srcOutput = socket->getOutputStream();
|
|
|
|
|
|
|
|
// attach the encryption layer
|
2001-10-21 04:21:21 +04:00
|
|
|
bool own = false;
|
2001-10-06 18:13:28 +04:00
|
|
|
/* FIXME -- implement ISecurityFactory
|
|
|
|
if (m_securityFactory != NULL) {
|
2001-10-21 04:21:21 +04:00
|
|
|
input.reset(m_securityFactory->createInputFilter(srcInput, own));
|
|
|
|
output.reset(m_securityFactory->createOutputFilter(srcOutput, own));
|
2001-10-06 18:13:28 +04:00
|
|
|
srcInput = input.get();
|
|
|
|
srcOutput = output.get();
|
2001-10-21 04:21:21 +04:00
|
|
|
own = true;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2002-06-10 02:20:28 +04:00
|
|
|
// give handshake some time
|
|
|
|
CTimerThread timer(30.0);
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
// attach the packetizing filters
|
2001-11-19 03:33:36 +03:00
|
|
|
assign(input, new CInputPacketStream(srcInput, own), IInputStream);
|
|
|
|
assign(output, new COutputPacketStream(srcOutput, own), IOutputStream);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// wait for hello from server
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "wait for hello"));
|
|
|
|
SInt16 major, minor;
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::readf(input.get(), "Synergy%2i%2i", &major, &minor);
|
|
|
|
|
|
|
|
// check versions
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "got hello version %d.%d", major, minor));
|
2002-06-04 15:06:26 +04:00
|
|
|
if (major < kProtocolMajorVersion ||
|
|
|
|
(major == kProtocolMajorVersion && minor < kProtocolMinorVersion)) {
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XIncompatibleClient(major, minor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// say hello back
|
2002-06-04 15:06:26 +04:00
|
|
|
log((CLOG_DEBUG1 "say hello version %d.%d", kProtocolMajorVersion, kProtocolMinorVersion));
|
2001-10-06 18:13:28 +04:00
|
|
|
CProtocolUtil::writef(output.get(), "Synergy%2i%2i%s",
|
2002-06-04 15:06:26 +04:00
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion, &m_name);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// record streams in a more useful place
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
m_input = input.get();
|
|
|
|
m_output = output.get();
|
|
|
|
}
|
|
|
|
catch (XIncompatibleClient& e) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "server has incompatible version %d.%d", e.getMajor(), e.getMinor()));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->stop();
|
2002-06-10 02:20:28 +04:00
|
|
|
CThread::exit(NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "connection timed out"));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->stop();
|
2001-10-06 18:13:28 +04:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "connection failed: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->stop();
|
2002-06-10 02:20:28 +04:00
|
|
|
CThread::exit(NULL);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
log((CLOG_ERR "connection failed: <unknown error>"));
|
|
|
|
m_screen->stop();
|
|
|
|
CThread::exit(NULL);
|
2001-10-08 23:24:46 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-06-10 03:08:18 +04:00
|
|
|
bool fail = false;
|
2001-10-06 18:13:28 +04:00
|
|
|
try {
|
2001-10-08 23:24:46 +04:00
|
|
|
// handle messages from server
|
2001-10-06 18:13:28 +04:00
|
|
|
for (;;) {
|
|
|
|
// wait for reply
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG2 "waiting for message"));
|
2001-10-06 18:13:28 +04:00
|
|
|
UInt8 code[4];
|
|
|
|
UInt32 n = input->read(code, 4);
|
|
|
|
|
|
|
|
// verify we got an entire code
|
|
|
|
if (n == 0) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_NOTE "server disconnected"));
|
2001-10-06 18:13:28 +04:00
|
|
|
// server hungup
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n != 4) {
|
|
|
|
// client sent an incomplete message
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "incomplete message from server"));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse message
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG2 "msg from server: %c%c%c%c", code[0], code[1], code[2], code[3]));
|
2001-10-06 18:13:28 +04:00
|
|
|
if (memcmp(code, kMsgDMouseMove, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onMouseMove();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDMouseWheel, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onMouseWheel();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDKeyDown, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onKeyDown();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDKeyUp, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onKeyUp();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDMouseDown, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onMouseDown();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDMouseUp, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onMouseUp();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgDKeyRepeat, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onKeyRepeat();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgCEnter, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onEnter();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgCLeave, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onLeave();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgCClipboard, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onGrabClipboard();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgCScreenSaver, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onScreenSaver();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgQInfo, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onQueryInfo();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-05-24 21:54:28 +04:00
|
|
|
else if (memcmp(code, kMsgCInfoAck, 4) == 0) {
|
|
|
|
onInfoAcknowledgment();
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
else if (memcmp(code, kMsgDClipboard, 4) == 0) {
|
2001-10-08 23:24:46 +04:00
|
|
|
onSetClipboard();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgCClose, 4) == 0) {
|
|
|
|
// server wants us to hangup
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv close"));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-05-23 18:56:03 +04:00
|
|
|
else if (memcmp(code, kMsgEIncompatible, 4) == 0) {
|
|
|
|
onErrorIncompatible();
|
2002-06-10 03:08:18 +04:00
|
|
|
fail = true;
|
2002-05-23 18:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (memcmp(code, kMsgEBusy, 4) == 0) {
|
|
|
|
onErrorBusy();
|
2002-06-10 03:08:18 +04:00
|
|
|
fail = true;
|
2002-05-23 18:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-05-31 22:18:29 +04:00
|
|
|
else if (memcmp(code, kMsgEUnknown, 4) == 0) {
|
|
|
|
onErrorUnknown();
|
2002-06-10 03:08:18 +04:00
|
|
|
fail = true;
|
2002-05-31 22:18:29 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-05-23 18:56:03 +04:00
|
|
|
else if (memcmp(code, kMsgEBad, 4) == 0) {
|
|
|
|
onErrorBad();
|
2002-06-10 03:08:18 +04:00
|
|
|
fail = true;
|
2002-05-23 18:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
else {
|
|
|
|
// unknown message
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "unknown message from server"));
|
2001-10-06 18:13:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_ERR "error: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->stop();
|
2002-06-10 02:20:28 +04:00
|
|
|
CThread::exit(reinterpret_cast<void*>(1));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2001-10-08 23:24:46 +04:00
|
|
|
|
|
|
|
// done with socket
|
2001-10-14 18:37:41 +04:00
|
|
|
log((CLOG_DEBUG "disconnecting from server"));
|
2001-10-08 23:24:46 +04:00
|
|
|
socket->close();
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// exit event loop
|
|
|
|
m_screen->stop();
|
2002-06-10 02:20:28 +04:00
|
|
|
|
2002-06-10 03:08:18 +04:00
|
|
|
CThread::exit(fail ? NULL : reinterpret_cast<void*>(1));
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME -- use factory to create screen
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 03:33:36 +03:00
|
|
|
#include "CMSWindowsSecondaryScreen.h"
|
2002-06-19 15:23:49 +04:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 03:33:36 +03:00
|
|
|
#include "CXWindowsSecondaryScreen.h"
|
|
|
|
#endif
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::openSecondaryScreen()
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
|
|
|
assert(m_screen == NULL);
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// not active
|
|
|
|
m_active = false;
|
|
|
|
|
|
|
|
// reset last sequence number
|
|
|
|
m_seqNum = 0;
|
|
|
|
|
|
|
|
// reset clipboard state
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
m_timeClipboard[id] = 0;
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// open screen
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "creating secondary screen"));
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen = new CMSWindowsSecondaryScreen;
|
2002-06-19 15:23:49 +04:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen = new CXWindowsSecondaryScreen;
|
|
|
|
#endif
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "opening secondary screen"));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->open(this);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::closeSecondaryScreen()
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
|
|
|
|
// close the secondary screen
|
|
|
|
try {
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "closing secondary screen"));
|
2001-11-19 03:33:36 +03:00
|
|
|
m_screen->close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "destroying secondary screen"));
|
2001-11-19 03:33:36 +03:00
|
|
|
delete m_screen;
|
|
|
|
m_screen = NULL;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onEnter()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt16 x, y;
|
2002-04-30 21:48:11 +04:00
|
|
|
UInt16 mask;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-04-30 21:48:11 +04:00
|
|
|
CProtocolUtil::readf(m_input, kMsgCEnter + 4, &x, &y, &m_seqNum, &mask);
|
2002-04-29 17:31:44 +04:00
|
|
|
m_active = true;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-04-30 21:48:11 +04:00
|
|
|
log((CLOG_DEBUG1 "recv enter, %d,%d %d %04x", x, y, m_seqNum, mask));
|
|
|
|
m_screen->enter(x, y, static_cast<KeyModifierMask>(mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onLeave()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv leave"));
|
|
|
|
|
2002-04-29 17:31:44 +04:00
|
|
|
// tell screen we're leaving
|
2001-10-06 18:13:28 +04:00
|
|
|
m_screen->leave();
|
2002-04-29 17:31:44 +04:00
|
|
|
|
|
|
|
// no longer the active screen
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_active = false;
|
|
|
|
|
|
|
|
// send clipboards that we own and that have changed
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
if (m_ownClipboard[id]) {
|
|
|
|
// get clipboard data. set the clipboard time to the last
|
|
|
|
// clipboard time before getting the data from the screen
|
|
|
|
// as the screen may detect an unchanged clipboard and
|
|
|
|
// avoid copying the data.
|
|
|
|
CClipboard clipboard;
|
|
|
|
if (clipboard.open(m_timeClipboard[id]))
|
|
|
|
clipboard.close();
|
|
|
|
m_screen->getClipboard(id, &clipboard);
|
|
|
|
|
|
|
|
// check time
|
|
|
|
if (m_timeClipboard[id] == 0 ||
|
|
|
|
clipboard.getTime() != m_timeClipboard[id]) {
|
|
|
|
// save new time
|
|
|
|
m_timeClipboard[id] = clipboard.getTime();
|
|
|
|
|
|
|
|
// marshall the data
|
|
|
|
CString data = clipboard.marshall();
|
|
|
|
|
2002-05-24 21:54:28 +04:00
|
|
|
// save and send data if different
|
|
|
|
if (data != m_dataClipboard[id]) {
|
|
|
|
log((CLOG_DEBUG "sending clipboard %d seqnum=%d, size=%d", id, m_seqNum, data.size()));
|
|
|
|
m_dataClipboard[id] = data;
|
|
|
|
CProtocolUtil::writef(m_output,
|
2002-04-29 17:31:44 +04:00
|
|
|
kMsgDClipboard, id, m_seqNum, &data);
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onGrabClipboard()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 18:19:53 +04:00
|
|
|
ClipboardID id;
|
2002-04-29 17:31:44 +04:00
|
|
|
UInt32 seqNum;
|
2002-04-27 18:19:53 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-04-29 17:31:44 +04:00
|
|
|
CProtocolUtil::readf(m_input, kMsgCClipboard + 4, &id, &seqNum);
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG "recv grab clipboard %d", id));
|
2002-04-29 18:25:24 +04:00
|
|
|
|
|
|
|
// validate
|
|
|
|
if (id >= kClipboardEnd) {
|
|
|
|
return;
|
|
|
|
}
|
2002-04-29 17:31:44 +04:00
|
|
|
|
|
|
|
// we no longer own the clipboard
|
|
|
|
m_ownClipboard[id] = false;
|
2002-04-27 18:19:53 +04:00
|
|
|
}
|
|
|
|
m_screen->grabClipboard(id);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onScreenSaver()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt8 on;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgCScreenSaver + 4, &on);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv screen saver on=%d", on));
|
2002-06-22 23:20:21 +04:00
|
|
|
m_screen->screenSaver(on != 0);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onQueryInfo()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-05-24 21:54:28 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
onQueryInfoNoLock();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onQueryInfoNoLock()
|
2002-05-24 21:54:28 +04:00
|
|
|
{
|
2002-06-19 21:03:29 +04:00
|
|
|
SInt32 mx, my, x, y, w, h;
|
|
|
|
m_screen->getMousePos(mx, my);
|
|
|
|
m_screen->getShape(x, y, w, h);
|
2001-10-06 18:13:28 +04:00
|
|
|
SInt32 zoneSize = m_screen->getJumpZoneSize();
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-06-19 21:03:29 +04:00
|
|
|
log((CLOG_DEBUG1 "sending info shape=%d,%d %dx%d zone=%d pos=%d,%d", x, y, w, h, zoneSize, mx, my));
|
|
|
|
CProtocolUtil::writef(m_output, kMsgDInfo, x, y, w, h, zoneSize, mx, my);
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onInfoAcknowledgment()
|
2002-05-24 21:54:28 +04:00
|
|
|
{
|
|
|
|
log((CLOG_DEBUG1 "recv info acknowledgment"));
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
2002-05-24 21:54:28 +04:00
|
|
|
m_ignoreMove = false;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onSetClipboard()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 18:19:53 +04:00
|
|
|
ClipboardID id;
|
2001-11-25 21:32:41 +03:00
|
|
|
CString data;
|
|
|
|
{
|
|
|
|
// parse message
|
|
|
|
UInt32 seqNum;
|
|
|
|
CLock lock(&m_mutex);
|
2002-04-27 18:19:53 +04:00
|
|
|
CProtocolUtil::readf(m_input, kMsgDClipboard + 4, &id, &seqNum, &data);
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG "recv clipboard %d size=%d", id, data.size()));
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-04-29 18:25:24 +04:00
|
|
|
// validate
|
|
|
|
if (id >= kClipboardEnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-25 21:32:41 +03:00
|
|
|
// unmarshall
|
|
|
|
CClipboard clipboard;
|
2002-04-29 17:31:44 +04:00
|
|
|
clipboard.unmarshall(data, 0);
|
2001-11-25 21:32:41 +03:00
|
|
|
|
|
|
|
// set screen's clipboard
|
2002-04-27 18:19:53 +04:00
|
|
|
m_screen->setClipboard(id, &clipboard);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onKeyDown()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-30 20:23:03 +04:00
|
|
|
UInt16 id, mask;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDKeyDown + 4, &id, &mask);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv key down id=%d, mask=0x%04x", id, mask));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->keyDown(static_cast<KeyID>(id),
|
2001-10-08 23:24:46 +04:00
|
|
|
static_cast<KeyModifierMask>(mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onKeyRepeat()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-30 20:23:03 +04:00
|
|
|
UInt16 id, mask, count;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDKeyRepeat + 4, &id, &mask, &count);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv key repeat id=%d, mask=0x%04x, count=%d", id, mask, count));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->keyRepeat(static_cast<KeyID>(id),
|
2001-10-08 23:24:46 +04:00
|
|
|
static_cast<KeyModifierMask>(mask),
|
2001-10-06 18:13:28 +04:00
|
|
|
count);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onKeyUp()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-30 20:23:03 +04:00
|
|
|
UInt16 id, mask;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDKeyUp + 4, &id, &mask);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv key up id=%d, mask=0x%04x", id, mask));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->keyUp(static_cast<KeyID>(id),
|
2001-10-08 23:24:46 +04:00
|
|
|
static_cast<KeyModifierMask>(mask));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onMouseDown()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt8 id;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDMouseDown + 4, &id);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv mouse down id=%d", id));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->mouseDown(static_cast<ButtonID>(id));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onMouseUp()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt8 id;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDMouseUp + 4, &id);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG1 "recv mouse up id=%d", id));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->mouseUp(static_cast<ButtonID>(id));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onMouseMove()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-05-24 21:54:28 +04:00
|
|
|
bool ignore;
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt16 x, y;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDMouseMove + 4, &x, &y);
|
2002-05-24 21:54:28 +04:00
|
|
|
ignore = m_ignoreMove;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG2 "recv mouse move %d,%d", x, y));
|
2002-05-24 21:54:28 +04:00
|
|
|
if (!ignore) {
|
|
|
|
m_screen->mouseMove(x, y);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onMouseWheel()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-04-27 22:06:40 +04:00
|
|
|
SInt16 delta;
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgDMouseWheel + 4, &delta);
|
|
|
|
}
|
2002-04-30 20:23:03 +04:00
|
|
|
log((CLOG_DEBUG2 "recv mouse wheel %+d", delta));
|
2001-10-26 01:40:29 +04:00
|
|
|
m_screen->mouseWheel(delta);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onErrorIncompatible()
|
2002-05-23 18:56:03 +04:00
|
|
|
{
|
|
|
|
SInt32 major, minor;
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
CProtocolUtil::readf(m_input, kMsgEIncompatible + 4, &major, &minor);
|
|
|
|
log((CLOG_ERR "server has incompatible version %d.%d", major, minor));
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onErrorBusy()
|
2002-05-23 18:56:03 +04:00
|
|
|
{
|
|
|
|
log((CLOG_ERR "server already has a connected client with name \"%s\"", m_name.c_str()));
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onErrorUnknown()
|
2002-05-31 22:18:29 +04:00
|
|
|
{
|
|
|
|
log((CLOG_ERR "server refused client with name \"%s\"", m_name.c_str()));
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::onErrorBad()
|
2002-05-23 18:56:03 +04:00
|
|
|
{
|
|
|
|
log((CLOG_ERR "server disconnected due to a protocol error"));
|
|
|
|
}
|