2002-08-02 23:57:46 +04:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CClient.h"
|
2002-07-11 00:18:32 +04:00
|
|
|
#include "CServerProxy.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "ISecondaryScreenFactory.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"
|
2002-07-14 02:00:38 +04:00
|
|
|
#include "CSecondaryScreen.h"
|
2002-07-11 00:18:32 +04:00
|
|
|
#include "IServer.h"
|
2001-10-08 23:24:46 +04:00
|
|
|
#include "ProtocolTypes.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XScreen.h"
|
|
|
|
#include "XSynergy.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "IDataSocket.h"
|
|
|
|
#include "ISocketFactory.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XSocket.h"
|
2002-07-30 22:31:00 +04:00
|
|
|
#include "IStreamFilterFactory.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-10-16 02:01:41 +04:00
|
|
|
#include "XMT.h"
|
2002-06-02 22:49:35 +04:00
|
|
|
#include "XThread.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CLog.h"
|
2002-06-26 20:31:48 +04:00
|
|
|
#include "CStopwatch.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "TMethodJob.h"
|
2001-11-19 03:33:36 +03:00
|
|
|
|
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_screen(NULL),
|
2002-07-11 00:18:32 +04:00
|
|
|
m_server(NULL),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_camp(false),
|
2002-07-30 22:31:00 +04:00
|
|
|
m_screenFactory(NULL),
|
|
|
|
m_socketFactory(NULL),
|
|
|
|
m_streamFilterFactory(NULL),
|
2002-07-16 20:52:26 +04:00
|
|
|
m_session(NULL),
|
2002-06-11 02:06:45 +04:00
|
|
|
m_active(false),
|
2002-07-11 00:18:32 +04:00
|
|
|
m_rejected(true)
|
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()
|
|
|
|
{
|
2002-07-30 22:31:00 +04:00
|
|
|
delete m_screenFactory;
|
|
|
|
delete m_socketFactory;
|
|
|
|
delete m_streamFilterFactory;
|
2001-10-14 22:29:43 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
CLock lock(&m_mutex);
|
2002-06-10 02:20:28 +04:00
|
|
|
m_camp = on;
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::setAddress(const CNetworkAddress& serverAddress)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_serverAddress = serverAddress;
|
|
|
|
}
|
|
|
|
|
2002-07-30 22:31:00 +04:00
|
|
|
void
|
|
|
|
CClient::setScreenFactory(ISecondaryScreenFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_screenFactory;
|
|
|
|
m_screenFactory = adopted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::setSocketFactory(ISocketFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_socketFactory;
|
|
|
|
m_socketFactory = adopted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::setStreamFilterFactory(IStreamFilterFactory* adopted)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
delete m_streamFilterFactory;
|
|
|
|
m_streamFilterFactory = adopted;
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-30 19:17:44 +04:00
|
|
|
CClient::exitMainLoop()
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-30 19:17:44 +04:00
|
|
|
m_screen->exitMainLoop();
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CClient::wasRejected() const
|
|
|
|
{
|
|
|
|
return m_rejected;
|
|
|
|
}
|
|
|
|
|
2002-07-16 20:52:26 +04:00
|
|
|
void
|
|
|
|
CClient::onError()
|
|
|
|
{
|
|
|
|
// close down session but don't wait too long
|
|
|
|
deleteSession(3.0);
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-11 01:22:28 +04:00
|
|
|
CClient::onInfoChanged(const CClientInfo& info)
|
|
|
|
{
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "resolution changed"));
|
2002-07-11 01:22:28 +04:00
|
|
|
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_server != NULL) {
|
|
|
|
m_server->onInfoChanged(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CClient::onGrabClipboard(ClipboardID id)
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_server == NULL) {
|
|
|
|
// m_server can be NULL if the screen calls this method
|
|
|
|
// before we've gotten around to connecting to the server.
|
|
|
|
// we simply ignore the clipboard change in that case.
|
2002-07-11 01:22:28 +04:00
|
|
|
return false;
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// grab ownership
|
2002-07-11 01:22:28 +04:00
|
|
|
m_server->onGrabClipboard(id);
|
2002-07-11 00:18:32 +04:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
// otherwise we'll wait until we leave.
|
|
|
|
if (!m_active) {
|
|
|
|
sendClipboard(id);
|
|
|
|
}
|
2002-07-11 01:22:28 +04:00
|
|
|
|
|
|
|
return true;
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-11 01:22:28 +04:00
|
|
|
CClient::onClipboardChanged(ClipboardID, const CString&)
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-11 01:22:28 +04:00
|
|
|
// ignore -- we'll check the clipboard when we leave
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
|
2002-07-31 16:39:34 +04:00
|
|
|
void
|
2002-06-21 21:55:47 +04:00
|
|
|
CClient::open()
|
|
|
|
{
|
|
|
|
// open the screen
|
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "opening screen"));
|
2002-06-21 21:55:47 +04:00
|
|
|
openSecondaryScreen();
|
|
|
|
}
|
|
|
|
catch (XScreenOpenFailure&) {
|
2002-07-31 16:39:34 +04:00
|
|
|
// can't open screen
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "failed to open screen"));
|
2002-07-31 16:39:34 +04:00
|
|
|
throw;
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-30 19:17:44 +04:00
|
|
|
CClient::mainLoop()
|
2001-10-14 22:29:43 +04:00
|
|
|
{
|
2002-06-21 21:55:47 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 00:18:32 +04:00
|
|
|
|
|
|
|
// check preconditions
|
2002-06-21 21:55:47 +04:00
|
|
|
assert(m_screen != NULL);
|
2002-07-11 00:18:32 +04:00
|
|
|
assert(m_server == NULL);
|
|
|
|
|
|
|
|
// connection starts as unsuccessful
|
|
|
|
m_rejected = true;
|
2002-06-21 21:55:47 +04:00
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "starting client \"%s\"", m_name.c_str()));
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// start server interactions
|
2002-07-16 20:52:26 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_session = new CThread(new TMethodJob<CClient>(
|
2002-07-11 00:18:32 +04:00
|
|
|
this, &CClient::runSession));
|
2002-07-16 20:52:26 +04:00
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// handle events
|
2002-07-30 19:17:44 +04:00
|
|
|
m_screen->mainLoop();
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// clean up
|
2002-07-16 20:52:26 +04:00
|
|
|
deleteSession();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
2002-10-16 02:01:41 +04:00
|
|
|
catch (XMT& e) {
|
|
|
|
LOG((CLOG_ERR "client error: %s", e.what()));
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
deleteSession();
|
|
|
|
LOG((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
|
|
|
throw;
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
catch (XBase& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "client error: %s", e.what()));
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// clean up
|
2002-07-16 20:52:26 +04:00
|
|
|
deleteSession();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2002-07-11 00:18:32 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_rejected = false;
|
2002-06-02 22:49:35 +04:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
// clean up
|
2002-07-16 20:52:26 +04:00
|
|
|
deleteSession();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2002-06-02 22:49:35 +04:00
|
|
|
throw;
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "unknown client error"));
|
2001-11-19 03:33:36 +03:00
|
|
|
|
|
|
|
// clean up
|
2002-07-16 20:52:26 +04:00
|
|
|
deleteSession();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
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
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::close()
|
2002-06-09 01:48:00 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
closeSecondaryScreen();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "closed screen"));
|
2002-06-09 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-11 17:13:37 +04:00
|
|
|
CClient::enter(SInt32 xAbs, SInt32 yAbs, UInt32, KeyModifierMask mask, bool)
|
2001-11-25 21:32:41 +03:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_active = true;
|
2002-04-29 17:31:44 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
m_screen->enter(xAbs, yAbs, mask);
|
|
|
|
}
|
2002-04-29 17:31:44 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
bool
|
|
|
|
CClient::leave()
|
|
|
|
{
|
|
|
|
m_screen->leave();
|
2002-04-29 17:31:44 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_active = false;
|
2002-04-29 17:31:44 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// send clipboards that we own and that have changed
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
if (m_ownClipboard[id]) {
|
|
|
|
sendClipboard(id);
|
2002-04-30 20:23:03 +04:00
|
|
|
}
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-07-11 17:13:37 +04:00
|
|
|
|
|
|
|
return true;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::setClipboard(ClipboardID id, const CString& data)
|
2002-05-24 21:54:28 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
// unmarshall
|
|
|
|
CClipboard clipboard;
|
|
|
|
clipboard.unmarshall(data, 0);
|
|
|
|
|
|
|
|
// set screen's clipboard
|
|
|
|
m_screen->setClipboard(id, &clipboard);
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::grabClipboard(ClipboardID id)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
// we no longer own the clipboard
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
}
|
2002-06-10 02:20:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
m_screen->grabClipboard(id);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::setClipboardDirty(ClipboardID, bool)
|
|
|
|
{
|
|
|
|
assert(0 && "shouldn't be called");
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::keyDown(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
m_screen->keyDown(id, mask);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::keyRepeat(KeyID id, KeyModifierMask mask, SInt32 count)
|
|
|
|
{
|
|
|
|
m_screen->keyRepeat(id, mask, count);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::keyUp(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
m_screen->keyUp(id, mask);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::mouseDown(ButtonID id)
|
|
|
|
{
|
|
|
|
m_screen->mouseDown(id);
|
|
|
|
}
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::mouseUp(ButtonID id)
|
|
|
|
{
|
|
|
|
m_screen->mouseUp(id);
|
|
|
|
}
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::mouseMove(SInt32 x, SInt32 y)
|
|
|
|
{
|
|
|
|
m_screen->mouseMove(x, y);
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
|
|
|
CClient::mouseWheel(SInt32 delta)
|
|
|
|
{
|
|
|
|
m_screen->mouseWheel(delta);
|
|
|
|
}
|
2002-06-26 20:31:48 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CClient::screensaver(bool activate)
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
m_screen->screensaver(activate);
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
2002-06-26 20:31:48 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
CString
|
|
|
|
CClient::getName() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
SInt32
|
|
|
|
CClient::getJumpZoneSize() const
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
return m_screen->getJumpZoneSize();
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
2001-10-08 23:24:46 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CClient::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
m_screen->getShape(x, y, w, h);
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
void
|
2002-07-14 02:00:38 +04:00
|
|
|
CClient::getCursorPos(SInt32& x, SInt32& y) const
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
m_screen->getCursorPos(x, y);
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
2002-06-10 02:20:28 +04:00
|
|
|
|
2002-07-14 02:00:38 +04:00
|
|
|
void
|
|
|
|
CClient::getCursorCenter(SInt32&, SInt32&) const
|
2002-07-11 00:18:32 +04:00
|
|
|
{
|
2002-07-14 02:00:38 +04:00
|
|
|
assert(0 && "shouldn't be called");
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
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 clipboard state
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
m_timeClipboard[id] = 0;
|
|
|
|
}
|
|
|
|
|
2002-07-30 22:31:00 +04:00
|
|
|
// create screen
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "creating secondary screen"));
|
2002-07-31 16:39:34 +04:00
|
|
|
if (m_screenFactory != NULL) {
|
|
|
|
m_screen = m_screenFactory->create(this);
|
|
|
|
}
|
2002-07-30 22:31:00 +04:00
|
|
|
if (m_screen == NULL) {
|
|
|
|
throw XScreenOpenFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// open screen
|
2002-07-24 17:01:18 +04:00
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "opening secondary screen"));
|
2002-07-24 17:01:18 +04:00
|
|
|
m_screen->open();
|
|
|
|
}
|
|
|
|
catch (...) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "destroying secondary screen"));
|
2002-07-24 17:01:18 +04:00
|
|
|
delete m_screen;
|
|
|
|
m_screen = NULL;
|
|
|
|
throw;
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CClient::closeSecondaryScreen()
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
|
|
|
// close the secondary screen
|
|
|
|
try {
|
2002-07-31 16:39:34 +04:00
|
|
|
if (m_screen != NULL) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "closing secondary screen"));
|
2002-07-31 16:39:34 +04:00
|
|
|
m_screen->close();
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2002-10-16 01:29:44 +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-26 17:48:08 +04:00
|
|
|
void
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::sendClipboard(ClipboardID id)
|
2002-06-26 17:48:08 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
// note -- m_mutex must be locked on entry
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
assert(m_server != NULL);
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// 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();
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
m_screen->getClipboard(id, &clipboard);
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// check time
|
|
|
|
if (m_timeClipboard[id] == 0 ||
|
|
|
|
clipboard.getTime() != m_timeClipboard[id]) {
|
|
|
|
// save new time
|
|
|
|
m_timeClipboard[id] = clipboard.getTime();
|
2002-04-29 17:31:44 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// marshall the data
|
|
|
|
CString data = clipboard.marshall();
|
2002-04-29 17:31:44 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// save and send data if different
|
|
|
|
if (data != m_dataClipboard[id]) {
|
|
|
|
m_dataClipboard[id] = data;
|
2002-07-11 01:22:28 +04:00
|
|
|
m_server->onClipboardChanged(id, data);
|
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
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::runSession(void*)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
try {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "starting server proxy"));
|
2002-07-11 00:18:32 +04:00
|
|
|
runServer();
|
2002-07-30 19:17:44 +04:00
|
|
|
m_screen->exitMainLoop();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "stopping server proxy"));
|
2002-04-27 18:19:53 +04:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (...) {
|
2002-07-30 19:17:44 +04:00
|
|
|
m_screen->exitMainLoop();
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "stopping server proxy"));
|
2002-07-11 00:18:32 +04:00
|
|
|
throw;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-16 20:52:26 +04:00
|
|
|
CClient::deleteSession(double timeout)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-16 20:52:26 +04:00
|
|
|
// get session thread object
|
|
|
|
CThread* thread;
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
thread = m_session;
|
|
|
|
m_session = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// shut it down
|
2002-07-11 00:18:32 +04:00
|
|
|
if (thread != NULL) {
|
|
|
|
thread->cancel();
|
2002-07-16 20:52:26 +04:00
|
|
|
thread->wait(timeout);
|
2002-07-11 00:18:32 +04:00
|
|
|
delete thread;
|
|
|
|
}
|
2002-05-24 21:54:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-07-11 00:18:32 +04:00
|
|
|
CClient::runServer()
|
2002-05-24 21:54:28 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
IDataSocket* socket = NULL;
|
|
|
|
CServerProxy* proxy = NULL;
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
// allow connect this much time to succeed
|
2002-09-02 21:30:04 +04:00
|
|
|
CTimerThread timer(15.0);
|
2001-11-25 21:32:41 +03:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// create socket and attempt to connect to server
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "connecting to server"));
|
2002-07-30 22:31:00 +04:00
|
|
|
if (m_socketFactory != NULL) {
|
|
|
|
socket = m_socketFactory->create();
|
|
|
|
}
|
|
|
|
assert(socket != NULL);
|
2002-07-11 00:18:32 +04:00
|
|
|
socket->connect(m_serverAddress);
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_INFO "connected to server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-07-24 17:01:18 +04:00
|
|
|
catch (XSocketConnect& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "failed to connect to server: %s", e.getErrstr()));
|
2002-07-24 17:01:18 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// failed to connect. if not camping then rethrow.
|
|
|
|
if (!m_camp) {
|
|
|
|
throw;
|
|
|
|
}
|
2002-05-24 21:54:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// we're camping. wait a bit before retrying
|
2002-09-02 21:30:04 +04:00
|
|
|
CThread::sleep(15.0);
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// create proxy
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "negotiating with server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
proxy = handshakeServer(socket);
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 00:18:32 +04:00
|
|
|
m_server = proxy;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (XThread&) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "connection timed out"));
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "connection failed: %s", e.what()));
|
|
|
|
LOG((CLOG_DEBUG "disconnecting from server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
2002-04-29 18:25:24 +04:00
|
|
|
return;
|
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (...) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "connection failed: <unknown error>"));
|
|
|
|
LOG((CLOG_DEBUG "disconnecting from server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
delete socket;
|
|
|
|
return;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
try {
|
|
|
|
// process messages
|
|
|
|
bool rejected = true;
|
|
|
|
if (proxy != NULL) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "communicating with server"));
|
2002-07-30 19:17:44 +04:00
|
|
|
rejected = !proxy->mainLoop();
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// clean up
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 00:18:32 +04:00
|
|
|
m_rejected = rejected;
|
|
|
|
m_server = NULL;
|
|
|
|
delete proxy;
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "disconnecting from server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
socket->close();
|
|
|
|
delete socket;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2002-07-11 00:18:32 +04:00
|
|
|
catch (...) {
|
2001-11-25 21:32:41 +03:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-11 00:18:32 +04:00
|
|
|
m_rejected = false;
|
|
|
|
m_server = NULL;
|
|
|
|
delete proxy;
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG "disconnecting from server"));
|
2002-07-11 00:18:32 +04:00
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
throw;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
CServerProxy*
|
|
|
|
CClient::handshakeServer(IDataSocket* socket)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-07-11 00:18:32 +04:00
|
|
|
// get the input and output streams
|
|
|
|
IInputStream* input = socket->getInputStream();
|
|
|
|
IOutputStream* output = socket->getOutputStream();
|
|
|
|
bool own = false;
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-30 22:31:00 +04:00
|
|
|
// attach filters
|
|
|
|
if (m_streamFilterFactory != NULL) {
|
|
|
|
input = m_streamFilterFactory->createInput(input, own);
|
|
|
|
output = m_streamFilterFactory->createOutput(output, own);
|
2002-07-11 00:18:32 +04:00
|
|
|
own = true;
|
2001-11-25 21:32:41 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// attach the packetizing filters
|
|
|
|
input = new CInputPacketStream(input, own);
|
|
|
|
output = new COutputPacketStream(output, own);
|
|
|
|
own = true;
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
CServerProxy* proxy = NULL;
|
|
|
|
try {
|
|
|
|
// give handshake some time
|
|
|
|
CTimerThread timer(30.0);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// wait for hello from server
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "wait for hello"));
|
2002-07-11 00:18:32 +04:00
|
|
|
SInt16 major, minor;
|
2002-07-30 22:31:00 +04:00
|
|
|
CProtocolUtil::readf(input, kMsgHello, &major, &minor);
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// check versions
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "got hello version %d.%d", major, minor));
|
2002-07-11 00:18:32 +04:00
|
|
|
if (major < kProtocolMajorVersion ||
|
|
|
|
(major == kProtocolMajorVersion && minor < kProtocolMinorVersion)) {
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2002-06-26 17:48:08 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// say hello back
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_DEBUG1 "say hello version %d.%d", kProtocolMajorVersion, kProtocolMinorVersion));
|
2002-07-30 22:31:00 +04:00
|
|
|
CProtocolUtil::writef(output, kMsgHelloBack,
|
2002-07-11 00:18:32 +04:00
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion, &m_name);
|
2002-06-26 17:48:08 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// create server proxy
|
|
|
|
proxy = new CServerProxy(this, input, output);
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// negotiate
|
|
|
|
// FIXME
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
catch (XIncompatibleClient& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_ERR "server has incompatible version %d.%d", e.getMajor(), e.getMinor()));
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2002-10-16 01:29:44 +04:00
|
|
|
LOG((CLOG_WARN "error communicating with server: %s", e.what()));
|
2002-07-11 00:18:32 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// probably timed out
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
|
|
|
else if (own) {
|
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
2002-05-23 18:56:03 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
// failed
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
|
|
|
else if (own) {
|
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
2002-05-31 22:18:29 +04:00
|
|
|
|
2002-07-11 00:18:32 +04:00
|
|
|
return NULL;
|
2002-05-23 18:56:03 +04:00
|
|
|
}
|