2001-10-06 14:13:28 +00:00
|
|
|
#include "CClient.h"
|
2002-07-10 20:18:32 +00:00
|
|
|
#include "CServerProxy.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CClipboard.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CInputPacketStream.h"
|
|
|
|
#include "COutputPacketStream.h"
|
|
|
|
#include "CProtocolUtil.h"
|
2001-10-08 19:24:46 +00:00
|
|
|
#include "ISecondaryScreen.h"
|
2002-07-10 20:18:32 +00:00
|
|
|
#include "IServer.h"
|
2001-10-08 19:24:46 +00:00
|
|
|
#include "ProtocolTypes.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XScreen.h"
|
|
|
|
#include "XSynergy.h"
|
|
|
|
#include "XSocket.h"
|
2001-11-25 18:32:41 +00:00
|
|
|
#include "CLock.h"
|
2001-10-14 18:29:43 +00:00
|
|
|
#include "CThread.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CTimerThread.h"
|
2002-06-02 18:49:35 +00:00
|
|
|
#include "XThread.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 "TMethodJob.h"
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
//
|
|
|
|
// CClient
|
|
|
|
//
|
|
|
|
|
2002-06-17 13:31:21 +00:00
|
|
|
CClient::CClient(const CString& clientName) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_name(clientName),
|
|
|
|
m_screen(NULL),
|
2002-07-10 20:18:32 +00:00
|
|
|
m_server(NULL),
|
2002-06-10 22:06:45 +00:00
|
|
|
m_camp(false),
|
|
|
|
m_active(false),
|
2002-07-10 20:18:32 +00:00
|
|
|
m_rejected(true)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2001-10-14 18:29:43 +00:00
|
|
|
// do nothing
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CClient::~CClient()
|
|
|
|
{
|
2001-10-14 18:29:43 +00:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CClient::camp(bool on)
|
2002-06-09 22:20:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
CLock lock(&m_mutex);
|
2002-06-09 22:20:28 +00:00
|
|
|
m_camp = on;
|
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::setAddress(const CNetworkAddress& serverAddress)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_serverAddress = serverAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::quit()
|
|
|
|
{
|
|
|
|
m_screen->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CClient::wasRejected() const
|
|
|
|
{
|
|
|
|
return m_rejected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-10 21:22:28 +00:00
|
|
|
CClient::onInfoChanged(const CClientInfo& info)
|
|
|
|
{
|
|
|
|
log((CLOG_DEBUG "resolution changed"));
|
|
|
|
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
if (m_server != NULL) {
|
|
|
|
m_server->onInfoChanged(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CClient::onGrabClipboard(ClipboardID id)
|
2002-07-10 20:18:32 +00: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-10 21:22:28 +00:00
|
|
|
return false;
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// grab ownership
|
2002-07-10 21:22:28 +00:00
|
|
|
m_server->onGrabClipboard(id);
|
2002-07-10 20:18:32 +00: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-10 21:22:28 +00:00
|
|
|
|
|
|
|
return true;
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-10 21:22:28 +00:00
|
|
|
CClient::onClipboardChanged(ClipboardID, const CString&)
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2002-07-10 21:22:28 +00:00
|
|
|
// ignore -- we'll check the clipboard when we leave
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 17:55:47 +00: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-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::run()
|
2001-10-14 18:29:43 +00:00
|
|
|
{
|
2002-06-21 17:55:47 +00:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 20:18:32 +00:00
|
|
|
|
|
|
|
// check preconditions
|
2002-06-21 17:55:47 +00:00
|
|
|
assert(m_screen != NULL);
|
2002-07-10 20:18:32 +00:00
|
|
|
assert(m_server == NULL);
|
|
|
|
|
|
|
|
// connection starts as unsuccessful
|
|
|
|
m_rejected = true;
|
2002-06-21 17:55:47 +00:00
|
|
|
}
|
|
|
|
|
2002-05-22 16:42:48 +00:00
|
|
|
CThread* thread = NULL;
|
2001-11-19 00:33:36 +00:00
|
|
|
try {
|
2002-07-10 20:18:32 +00:00
|
|
|
log((CLOG_NOTE "starting client \"%s\"", m_name.c_str()));
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
// start server interactions
|
2002-07-10 20:18:32 +00:00
|
|
|
thread = new CThread(new TMethodJob<CClient>(
|
|
|
|
this, &CClient::runSession));
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
// handle events
|
|
|
|
log((CLOG_DEBUG "starting event handling"));
|
|
|
|
m_screen->run();
|
2002-07-10 20:18:32 +00:00
|
|
|
log((CLOG_DEBUG "stopped event handling"));
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
// clean up
|
2002-07-10 20:18:32 +00:00
|
|
|
deleteSession(thread);
|
|
|
|
log((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
2001-11-25 18:32:41 +00:00
|
|
|
log((CLOG_ERR "client error: %s", e.what()));
|
2001-11-19 00:33:36 +00:00
|
|
|
|
|
|
|
// clean up
|
2002-07-10 20:18:32 +00:00
|
|
|
log((CLOG_DEBUG "stopped event handling"));
|
|
|
|
deleteSession(thread);
|
|
|
|
log((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_rejected = false;
|
2002-06-02 18:49:35 +00:00
|
|
|
}
|
|
|
|
catch (XThread&) {
|
|
|
|
// clean up
|
2002-07-10 20:18:32 +00:00
|
|
|
log((CLOG_DEBUG "stopped event handling"));
|
|
|
|
deleteSession(thread);
|
|
|
|
log((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2002-06-02 18:49:35 +00:00
|
|
|
throw;
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
log((CLOG_DEBUG "unknown client error"));
|
|
|
|
|
|
|
|
// clean up
|
2002-07-10 20:18:32 +00:00
|
|
|
log((CLOG_DEBUG "stopped event handling"));
|
|
|
|
deleteSession(thread);
|
|
|
|
log((CLOG_NOTE "stopping client \"%s\"", m_name.c_str()));
|
2001-11-19 00:33:36 +00:00
|
|
|
throw;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::close()
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
closeSecondaryScreen();
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::enter(SInt32 xAbs, SInt32 yAbs,
|
|
|
|
UInt32 seqNum, KeyModifierMask mask, bool)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_active = true;
|
2002-04-29 13:31:44 +00:00
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
m_screen->enter(xAbs, yAbs, mask);
|
|
|
|
}
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
bool
|
|
|
|
CClient::leave()
|
|
|
|
{
|
|
|
|
m_screen->leave();
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_active = false;
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-07-10 20:18:32 +00: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 16:23:03 +00:00
|
|
|
}
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::setClipboard(ClipboardID id, const CString& data)
|
2002-05-24 17:54:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
// unmarshall
|
|
|
|
CClipboard clipboard;
|
|
|
|
clipboard.unmarshall(data, 0);
|
|
|
|
|
|
|
|
// set screen's clipboard
|
|
|
|
m_screen->setClipboard(id, &clipboard);
|
2002-05-24 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::grabClipboard(ClipboardID id)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
// we no longer own the clipboard
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
}
|
2002-06-09 22:20:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
m_screen->grabClipboard(id);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::setClipboardDirty(ClipboardID, bool)
|
|
|
|
{
|
|
|
|
assert(0 && "shouldn't be called");
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::keyDown(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
m_screen->keyDown(id, mask);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::keyRepeat(KeyID id, KeyModifierMask mask, SInt32 count)
|
|
|
|
{
|
|
|
|
m_screen->keyRepeat(id, mask, count);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::keyUp(KeyID id, KeyModifierMask mask)
|
|
|
|
{
|
|
|
|
m_screen->keyUp(id, mask);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::mouseDown(ButtonID id)
|
|
|
|
{
|
|
|
|
m_screen->mouseDown(id);
|
|
|
|
}
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::mouseUp(ButtonID id)
|
|
|
|
{
|
|
|
|
m_screen->mouseUp(id);
|
|
|
|
}
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::mouseMove(SInt32 x, SInt32 y)
|
|
|
|
{
|
|
|
|
m_screen->mouseMove(x, y);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::mouseWheel(SInt32 delta)
|
|
|
|
{
|
|
|
|
m_screen->mouseWheel(delta);
|
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::screenSaver(bool activate)
|
|
|
|
{
|
|
|
|
m_screen->screenSaver(activate);
|
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
CString
|
|
|
|
CClient::getName() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
|
|
|
|
{
|
|
|
|
m_screen->getShape(x, y, w, h);
|
|
|
|
}
|
2001-10-08 19:24:46 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::getCenter(SInt32&, SInt32&) const
|
|
|
|
{
|
|
|
|
assert(0 && "shouldn't be called");
|
|
|
|
}
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
|
|
|
CClient::getMousePos(SInt32& x, SInt32& y) const
|
|
|
|
{
|
|
|
|
m_screen->getMousePos(x, y);
|
|
|
|
}
|
2002-06-09 22:20:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
SInt32
|
|
|
|
CClient::getJumpZoneSize() const
|
|
|
|
{
|
|
|
|
return m_screen->getJumpZoneSize();
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME -- use factory to create screen
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
#include "CMSWindowsSecondaryScreen.h"
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2001-11-19 00:33:36 +00:00
|
|
|
#include "CXWindowsSecondaryScreen.h"
|
|
|
|
#endif
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClient::openSecondaryScreen()
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
|
|
|
assert(m_screen == NULL);
|
|
|
|
|
2002-04-29 13:31:44 +00:00
|
|
|
// not active
|
|
|
|
m_active = false;
|
|
|
|
|
|
|
|
// reset clipboard state
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
m_timeClipboard[id] = 0;
|
|
|
|
}
|
|
|
|
|
2001-11-19 00:33:36 +00:00
|
|
|
// open screen
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "creating secondary screen"));
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-07-10 21:22:28 +00:00
|
|
|
m_screen = new CMSWindowsSecondaryScreen(this);
|
2002-06-19 11:23:49 +00:00
|
|
|
#elif UNIX_LIKE
|
2002-07-10 21:22:28 +00:00
|
|
|
m_screen = new CXWindowsSecondaryScreen(this);
|
2001-11-19 00:33:36 +00:00
|
|
|
#endif
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "opening secondary screen"));
|
2002-07-10 21:22:28 +00:00
|
|
|
m_screen->open();
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CClient::closeSecondaryScreen()
|
2001-11-19 00:33:36 +00:00
|
|
|
{
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
|
|
|
|
// close the secondary screen
|
|
|
|
try {
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "closing secondary screen"));
|
2001-11-19 00:33:36 +00:00
|
|
|
m_screen->close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "destroying secondary screen"));
|
2001-11-19 00:33:36 +00:00
|
|
|
delete m_screen;
|
|
|
|
m_screen = NULL;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-26 13:48:08 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::sendClipboard(ClipboardID id)
|
2002-06-26 13:48:08 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
// note -- m_mutex must be locked on entry
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
assert(m_server != NULL);
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00: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 18:32:41 +00:00
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
m_screen->getClipboard(id, &clipboard);
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// check time
|
|
|
|
if (m_timeClipboard[id] == 0 ||
|
|
|
|
clipboard.getTime() != m_timeClipboard[id]) {
|
|
|
|
// save new time
|
|
|
|
m_timeClipboard[id] = clipboard.getTime();
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// marshall the data
|
|
|
|
CString data = clipboard.marshall();
|
2002-04-29 13:31:44 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// save and send data if different
|
|
|
|
if (data != m_dataClipboard[id]) {
|
|
|
|
m_dataClipboard[id] = data;
|
2002-07-10 21:22:28 +00:00
|
|
|
m_server->onClipboardChanged(id, data);
|
2002-04-29 13:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::runSession(void*)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
try {
|
|
|
|
log((CLOG_DEBUG "starting server proxy"));
|
|
|
|
runServer();
|
|
|
|
m_screen->stop();
|
|
|
|
log((CLOG_DEBUG "stopping server proxy"));
|
2002-04-27 14:19:53 +00:00
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
catch (...) {
|
|
|
|
m_screen->stop();
|
|
|
|
log((CLOG_DEBUG "stopping server proxy"));
|
|
|
|
throw;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::deleteSession(CThread* thread)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
if (thread != NULL) {
|
|
|
|
thread->cancel();
|
|
|
|
thread->wait();
|
|
|
|
delete thread;
|
|
|
|
}
|
2002-05-24 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
#include "CTCPSocket.h" // FIXME
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-10 20:18:32 +00:00
|
|
|
CClient::runServer()
|
2002-05-24 17:54:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
IDataSocket* socket = NULL;
|
|
|
|
CServerProxy* proxy = NULL;
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
// allow connect this much time to succeed
|
|
|
|
// FIXME -- timeout in member
|
|
|
|
CTimerThread timer(m_camp ? -1.0 : 30.0);
|
2001-11-25 18:32:41 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// create socket and attempt to connect to server
|
|
|
|
log((CLOG_DEBUG1 "connecting to server"));
|
|
|
|
socket = new CTCPSocket; // FIXME -- use factory
|
|
|
|
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;
|
|
|
|
}
|
2002-05-24 17:54:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// we're camping. wait a bit before retrying
|
|
|
|
CThread::sleep(5.0);
|
|
|
|
}
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// create proxy
|
|
|
|
log((CLOG_DEBUG1 "negotiating with server"));
|
|
|
|
proxy = handshakeServer(socket);
|
2001-11-25 18:32:41 +00:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 20:18:32 +00:00
|
|
|
m_server = proxy;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
catch (XThread&) {
|
|
|
|
log((CLOG_ERR "connection timed out"));
|
|
|
|
delete socket;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
log((CLOG_ERR "connection failed: %s", e.what()));
|
|
|
|
log((CLOG_DEBUG "disconnecting from server"));
|
|
|
|
delete socket;
|
2002-04-29 14:25:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
catch (...) {
|
|
|
|
log((CLOG_ERR "connection failed: <unknown error>"));
|
|
|
|
log((CLOG_DEBUG "disconnecting from server"));
|
|
|
|
delete socket;
|
|
|
|
return;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
try {
|
|
|
|
// process messages
|
|
|
|
bool rejected = true;
|
|
|
|
if (proxy != NULL) {
|
|
|
|
log((CLOG_DEBUG1 "communicating with server"));
|
|
|
|
rejected = !proxy->run();
|
|
|
|
}
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// clean up
|
2001-11-25 18:32:41 +00:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 20:18:32 +00:00
|
|
|
m_rejected = rejected;
|
|
|
|
m_server = NULL;
|
|
|
|
delete proxy;
|
|
|
|
log((CLOG_DEBUG "disconnecting from server"));
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
catch (...) {
|
2001-11-25 18:32:41 +00:00
|
|
|
CLock lock(&m_mutex);
|
2002-07-10 20:18:32 +00:00
|
|
|
m_rejected = false;
|
|
|
|
m_server = NULL;
|
|
|
|
delete proxy;
|
|
|
|
log((CLOG_DEBUG "disconnecting from server"));
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
throw;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
CServerProxy*
|
|
|
|
CClient::handshakeServer(IDataSocket* socket)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-07-10 20:18:32 +00:00
|
|
|
// get the input and output streams
|
|
|
|
IInputStream* input = socket->getInputStream();
|
|
|
|
IOutputStream* output = socket->getOutputStream();
|
|
|
|
bool own = false;
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// attach the encryption layer
|
|
|
|
/* FIXME -- implement ISecurityFactory
|
|
|
|
if (m_securityFactory != NULL) {
|
|
|
|
input = m_securityFactory->createInputFilter(input, own);
|
|
|
|
output = m_securityFactory->createOutputFilter(output, own);
|
|
|
|
own = true;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2002-07-10 20:18:32 +00:00
|
|
|
*/
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// attach the packetizing filters
|
|
|
|
input = new CInputPacketStream(input, own);
|
|
|
|
output = new COutputPacketStream(output, own);
|
|
|
|
own = true;
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
CServerProxy* proxy = NULL;
|
|
|
|
try {
|
|
|
|
// give handshake some time
|
|
|
|
CTimerThread timer(30.0);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// wait for hello from server
|
|
|
|
log((CLOG_DEBUG1 "wait for hello"));
|
|
|
|
SInt16 major, minor;
|
|
|
|
CProtocolUtil::readf(input, "Synergy%2i%2i", &major, &minor);
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// check versions
|
|
|
|
log((CLOG_DEBUG1 "got hello version %d.%d", major, minor));
|
|
|
|
if (major < kProtocolMajorVersion ||
|
|
|
|
(major == kProtocolMajorVersion && minor < kProtocolMinorVersion)) {
|
|
|
|
throw XIncompatibleClient(major, minor);
|
2002-06-26 13:48:08 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// say hello back
|
|
|
|
log((CLOG_DEBUG1 "say hello version %d.%d", kProtocolMajorVersion, kProtocolMinorVersion));
|
|
|
|
CProtocolUtil::writef(output, "Synergy%2i%2i%s",
|
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion, &m_name);
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// create server proxy
|
|
|
|
proxy = new CServerProxy(this, input, output);
|
2002-05-23 14:56:03 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// negotiate
|
|
|
|
// FIXME
|
2002-05-23 14:56:03 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
catch (XIncompatibleClient& e) {
|
|
|
|
log((CLOG_ERR "server has incompatible version %d.%d", e.getMajor(), e.getMinor()));
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
log((CLOG_WARN "error communicating with server: %s", e.what()));
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// probably timed out
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
|
|
|
else if (own) {
|
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
2002-05-23 14:56:03 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
// failed
|
|
|
|
if (proxy != NULL) {
|
|
|
|
delete proxy;
|
|
|
|
}
|
|
|
|
else if (own) {
|
|
|
|
delete input;
|
|
|
|
delete output;
|
|
|
|
}
|
2002-05-31 18:18:29 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
return NULL;
|
2002-05-23 14:56:03 +00:00
|
|
|
}
|