2002-08-02 19:57:46 +00: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 14:13:28 +00:00
|
|
|
#include "CClient.h"
|
2002-07-10 20:18:32 +00:00
|
|
|
#include "CServerProxy.h"
|
2003-09-02 22:05:47 +00:00
|
|
|
#include "CScreen.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CClipboard.h"
|
2004-02-15 17:32:11 +00:00
|
|
|
#include "CPacketStreamFilter.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CProtocolUtil.h"
|
2001-10-08 19:24:46 +00:00
|
|
|
#include "ProtocolTypes.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSynergy.h"
|
2002-07-30 18:31:00 +00:00
|
|
|
#include "IDataSocket.h"
|
|
|
|
#include "ISocketFactory.h"
|
|
|
|
#include "IStreamFilterFactory.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CLog.h"
|
2004-02-15 17:32:11 +00:00
|
|
|
#include "IEventQueue.h"
|
|
|
|
#include "TMethodEventJob.h"
|
2001-11-19 00:33:36 +00:00
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
//
|
|
|
|
// CClient
|
|
|
|
//
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
CEvent::Type CClient::s_connectedEvent = CEvent::kUnknown;
|
|
|
|
CEvent::Type CClient::s_connectionFailedEvent = CEvent::kUnknown;
|
|
|
|
CEvent::Type CClient::s_disconnectedEvent = CEvent::kUnknown;
|
|
|
|
|
|
|
|
CClient::CClient(const CString& name, const CNetworkAddress& address,
|
|
|
|
ISocketFactory* socketFactory,
|
|
|
|
IStreamFilterFactory* streamFilterFactory,
|
|
|
|
CScreen* screen) :
|
|
|
|
m_name(name),
|
|
|
|
m_serverAddress(address),
|
|
|
|
m_socketFactory(socketFactory),
|
|
|
|
m_streamFilterFactory(streamFilterFactory),
|
|
|
|
m_screen(screen),
|
|
|
|
m_stream(NULL),
|
|
|
|
m_timer(NULL),
|
2002-07-10 20:18:32 +00:00
|
|
|
m_server(NULL),
|
2004-02-15 17:32:11 +00:00
|
|
|
|
|
|
|
m_active(false)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
assert(m_socketFactory != NULL);
|
|
|
|
assert(m_screen != NULL);
|
|
|
|
|
2001-10-14 18:29:43 +00:00
|
|
|
// do nothing
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CClient::~CClient()
|
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
cleanupTimer();
|
|
|
|
cleanupScreen();
|
|
|
|
cleanupConnecting();
|
|
|
|
cleanupConnection();
|
2002-07-30 18:31:00 +00:00
|
|
|
delete m_socketFactory;
|
|
|
|
delete m_streamFilterFactory;
|
2001-10-14 18:29:43 +00:00
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::connect()
|
2002-07-30 18:31:00 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
if (m_stream != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2002-07-30 18:31:00 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
try {
|
|
|
|
IDataSocket* socket = m_socketFactory->create();
|
2002-07-30 18:31:00 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
// filter socket messages, including a packetizing filter
|
|
|
|
m_stream = socket;
|
|
|
|
if (m_streamFilterFactory != NULL) {
|
|
|
|
m_stream = m_streamFilterFactory->create(m_stream, true);
|
|
|
|
}
|
|
|
|
m_stream = new CPacketStreamFilter(m_stream, true);
|
2002-07-10 20:18:32 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
// connect
|
|
|
|
LOG((CLOG_DEBUG1 "connecting to server"));
|
|
|
|
setupConnecting();
|
|
|
|
setupTimer();
|
|
|
|
socket->connect(m_serverAddress);
|
|
|
|
}
|
|
|
|
catch (XBase& e) {
|
|
|
|
delete m_stream;
|
|
|
|
m_stream = NULL;
|
|
|
|
sendConnectionFailedEvent(e.what());
|
|
|
|
return;
|
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::disconnect(const char* msg)
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
cleanupTimer();
|
|
|
|
cleanupScreen();
|
|
|
|
cleanupConnection();
|
|
|
|
if (msg != NULL) {
|
|
|
|
sendConnectionFailedEvent(msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendEvent(getDisconnectedEvent(), NULL);
|
|
|
|
}
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-03-13 17:16:24 +00:00
|
|
|
void
|
|
|
|
CClient::handshakeComplete()
|
|
|
|
{
|
|
|
|
m_ready = true;
|
|
|
|
m_screen->enable();
|
|
|
|
sendEvent(getConnectedEvent(), NULL);
|
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
bool
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::isConnected() const
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return (m_server != NULL);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
bool
|
|
|
|
CClient::isConnecting() const
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return (m_timer != NULL);
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
CEvent::Type
|
|
|
|
CClient::getConnectedEvent()
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return CEvent::registerTypeOnce(s_connectedEvent,
|
|
|
|
"CClient::connected");
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
CEvent::Type
|
|
|
|
CClient::getConnectionFailedEvent()
|
2003-03-12 22:34:07 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return CEvent::registerTypeOnce(s_connectionFailedEvent,
|
|
|
|
"CClient::failed");
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
CEvent::Type
|
|
|
|
CClient::getDisconnectedEvent()
|
2002-07-16 16:52:26 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return CEvent::registerTypeOnce(s_disconnectedEvent,
|
|
|
|
"CClient::disconnected");
|
2002-07-16 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void*
|
|
|
|
CClient::getEventTarget() const
|
2002-07-10 21:22:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return m_screen->getEventTarget();
|
2002-07-10 21:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::getClipboard(ClipboardID id, IClipboard* clipboard) const
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
return m_screen->getClipboard(id, clipboard);
|
2002-06-21 17:55:47 +00:00
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
|
2001-10-14 18:29:43 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
m_screen->getShape(x, y, w, h);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::getCursorPos(SInt32& x, SInt32& y) const
|
2002-06-08 21:48:00 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
m_screen->getCursorPos(x, y);
|
2002-06-08 21:48:00 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-07-11 13:13:37 +00:00
|
|
|
CClient::enter(SInt32 xAbs, SInt32 yAbs, UInt32, KeyModifierMask mask, bool)
|
2001-11-25 18:32:41 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
m_active = true;
|
2003-09-02 22:05:47 +00:00
|
|
|
m_screen->mouseMove(xAbs, yAbs);
|
2004-03-21 20:01:41 +00:00
|
|
|
m_screen->enter(mask);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
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
|
|
|
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-07-11 13:13:37 +00:00
|
|
|
|
|
|
|
return true;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::setClipboard(ClipboardID id, const IClipboard* clipboard)
|
2002-05-24 17:54:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
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
|
|
|
m_screen->grabClipboard(id);
|
2004-02-15 17:32:11 +00:00
|
|
|
m_ownClipboard[id] = false;
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
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
|
2003-04-27 17:01:14 +00:00
|
|
|
CClient::keyDown(KeyID id, KeyModifierMask mask, KeyButton button)
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2003-04-27 17:01:14 +00:00
|
|
|
m_screen->keyDown(id, mask, button);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
2003-04-27 17:01:14 +00:00
|
|
|
CClient::keyRepeat(KeyID id, KeyModifierMask mask,
|
|
|
|
SInt32 count, KeyButton button)
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2003-04-27 17:01:14 +00:00
|
|
|
m_screen->keyRepeat(id, mask, count, button);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
void
|
2003-04-27 17:01:14 +00:00
|
|
|
CClient::keyUp(KeyID id, KeyModifierMask mask, KeyButton button)
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2003-04-27 17:01:14 +00:00
|
|
|
m_screen->keyUp(id, mask, button);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
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
|
2002-07-13 22:00:38 +00:00
|
|
|
CClient::screensaver(bool activate)
|
2002-07-10 20:18:32 +00:00
|
|
|
{
|
2002-07-13 22:00:38 +00:00
|
|
|
m_screen->screensaver(activate);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2002-06-26 16:31:48 +00:00
|
|
|
|
2002-12-23 13:55:21 +00:00
|
|
|
void
|
|
|
|
CClient::resetOptions()
|
|
|
|
{
|
|
|
|
m_screen->resetOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::setOptions(const COptionsList& options)
|
|
|
|
{
|
|
|
|
m_screen->setOptions(options);
|
|
|
|
}
|
|
|
|
|
2002-07-10 20:18:32 +00:00
|
|
|
CString
|
|
|
|
CClient::getName() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
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;
|
2004-02-15 17:32:11 +00:00
|
|
|
m_server->onClipboardChanged(id, &clipboard);
|
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
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::sendEvent(CEvent::Type type, void* data)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
EVENTQUEUE->addEvent(CEvent(type, getEventTarget(), data));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::sendConnectionFailedEvent(const char* msg)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
CFailInfo* info = (CFailInfo*)malloc(sizeof(CFailInfo) + strlen(msg));
|
|
|
|
info->m_retry = true;
|
|
|
|
strcpy(info->m_what, msg);
|
|
|
|
sendEvent(getConnectionFailedEvent(), info);
|
|
|
|
}
|
2002-07-16 16:52:26 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::setupConnecting()
|
|
|
|
{
|
|
|
|
assert(m_stream != NULL);
|
|
|
|
|
|
|
|
EVENTQUEUE->adoptHandler(IDataSocket::getConnectedEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleConnected));
|
|
|
|
EVENTQUEUE->adoptHandler(IDataSocket::getConnectionFailedEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleConnectionFailed));
|
2002-05-24 17:54:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2004-02-15 17:32:11 +00:00
|
|
|
CClient::setupConnection()
|
2002-05-24 17:54:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
assert(m_stream != NULL);
|
2003-07-12 17:57:31 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
EVENTQUEUE->adoptHandler(ISocket::getDisconnectedEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleDisconnected));
|
|
|
|
EVENTQUEUE->adoptHandler(IStream::getInputReadyEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleHello));
|
|
|
|
EVENTQUEUE->adoptHandler(IStream::getOutputErrorEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleOutputError));
|
|
|
|
EVENTQUEUE->adoptHandler(IStream::getInputShutdownEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleDisconnected));
|
|
|
|
EVENTQUEUE->adoptHandler(IStream::getOutputShutdownEvent(),
|
|
|
|
m_stream->getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleDisconnected));
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::setupScreen()
|
|
|
|
{
|
|
|
|
assert(m_server == NULL);
|
|
|
|
|
|
|
|
m_ready = false;
|
|
|
|
m_server = new CServerProxy(this, m_stream);
|
|
|
|
EVENTQUEUE->adoptHandler(IScreen::getShapeChangedEvent(),
|
|
|
|
getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleShapeChanged));
|
|
|
|
EVENTQUEUE->adoptHandler(IScreen::getClipboardGrabbedEvent(),
|
|
|
|
getEventTarget(),
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleClipboardGrabbed));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::setupTimer()
|
|
|
|
{
|
|
|
|
assert(m_timer == NULL);
|
|
|
|
|
|
|
|
m_timer = EVENTQUEUE->newOneShotTimer(15.0, NULL);
|
|
|
|
EVENTQUEUE->adoptHandler(CEvent::kTimer, m_timer,
|
|
|
|
new TMethodEventJob<CClient>(this,
|
|
|
|
&CClient::handleConnectTimeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::cleanupConnecting()
|
|
|
|
{
|
|
|
|
if (m_stream != NULL) {
|
|
|
|
EVENTQUEUE->removeHandler(IDataSocket::getConnectedEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(IDataSocket::getConnectionFailedEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::cleanupConnection()
|
|
|
|
{
|
|
|
|
if (m_stream != NULL) {
|
|
|
|
EVENTQUEUE->removeHandler(IStream::getInputReadyEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(IStream::getOutputErrorEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(IStream::getInputShutdownEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(IStream::getOutputShutdownEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(ISocket::getDisconnectedEvent(),
|
|
|
|
m_stream->getEventTarget());
|
|
|
|
delete m_stream;
|
|
|
|
m_stream = NULL;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::cleanupScreen()
|
|
|
|
{
|
|
|
|
if (m_server != NULL) {
|
2004-03-13 17:16:24 +00:00
|
|
|
if (m_ready) {
|
2004-02-15 17:32:11 +00:00
|
|
|
m_screen->disable();
|
|
|
|
m_ready = false;
|
2003-03-12 22:34:07 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
EVENTQUEUE->removeHandler(IScreen::getShapeChangedEvent(),
|
|
|
|
getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(IScreen::getClipboardGrabbedEvent(),
|
|
|
|
getEventTarget());
|
|
|
|
delete m_server;
|
|
|
|
m_server = NULL;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::cleanupTimer()
|
|
|
|
{
|
|
|
|
if (m_timer != NULL) {
|
|
|
|
EVENTQUEUE->removeHandler(CEvent::kTimer, m_timer);
|
|
|
|
EVENTQUEUE->deleteTimer(m_timer);
|
|
|
|
m_timer = NULL;
|
2003-07-12 17:57:31 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
}
|
2003-07-12 17:57:31 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleConnected(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG1 "connected; wait for hello"));
|
|
|
|
cleanupConnecting();
|
|
|
|
setupConnection();
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
// reset clipboard state
|
|
|
|
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
|
|
|
m_ownClipboard[id] = false;
|
|
|
|
m_timeClipboard[id] = 0;
|
2001-11-25 18:32:41 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleConnectionFailed(const CEvent& event, void*)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-02-15 17:32:11 +00:00
|
|
|
IDataSocket::CConnectionFailedInfo* info =
|
|
|
|
reinterpret_cast<IDataSocket::CConnectionFailedInfo*>(event.getData());
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
cleanupTimer();
|
|
|
|
cleanupConnecting();
|
|
|
|
delete m_stream;
|
|
|
|
m_stream = NULL;
|
|
|
|
LOG((CLOG_DEBUG1 "connection failed"));
|
|
|
|
sendConnectionFailedEvent(info->m_what);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleConnectTimeout(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
cleanupTimer();
|
|
|
|
cleanupConnecting();
|
|
|
|
delete m_stream;
|
|
|
|
m_stream = NULL;
|
|
|
|
LOG((CLOG_DEBUG1 "connection timed out"));
|
|
|
|
sendConnectionFailedEvent("Timed out");
|
|
|
|
}
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleOutputError(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
cleanupTimer();
|
|
|
|
cleanupScreen();
|
|
|
|
cleanupConnection();
|
|
|
|
LOG((CLOG_WARN "error sending to server"));
|
|
|
|
sendEvent(getDisconnectedEvent(), NULL);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleDisconnected(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
cleanupTimer();
|
|
|
|
cleanupScreen();
|
|
|
|
cleanupConnection();
|
|
|
|
LOG((CLOG_DEBUG1 "disconnected"));
|
|
|
|
sendEvent(getDisconnectedEvent(), NULL);
|
|
|
|
}
|
2002-06-26 13:48:08 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleShapeChanged(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
LOG((CLOG_DEBUG "resolution changed"));
|
|
|
|
m_server->onInfoChanged();
|
|
|
|
}
|
2002-05-23 14:56:03 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
void
|
|
|
|
CClient::handleClipboardGrabbed(const CEvent& event, void*)
|
|
|
|
{
|
|
|
|
const IScreen::CClipboardInfo* info =
|
|
|
|
reinterpret_cast<const IScreen::CClipboardInfo*>(event.getData());
|
|
|
|
|
|
|
|
// grab ownership
|
|
|
|
m_server->onGrabClipboard(info->m_id);
|
|
|
|
|
|
|
|
// we now own the clipboard and it has not been sent to the server
|
|
|
|
m_ownClipboard[info->m_id] = true;
|
|
|
|
m_timeClipboard[info->m_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(info->m_id);
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClient::handleHello(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
SInt16 major, minor;
|
|
|
|
if (!CProtocolUtil::readf(m_stream, kMsgHello, &major, &minor)) {
|
|
|
|
sendConnectionFailedEvent("Protocol error from server");
|
|
|
|
cleanupTimer();
|
|
|
|
cleanupConnection();
|
|
|
|
return;
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2004-02-15 17:32:11 +00:00
|
|
|
|
|
|
|
// check versions
|
|
|
|
LOG((CLOG_DEBUG1 "got hello version %d.%d", major, minor));
|
|
|
|
if (major < kProtocolMajorVersion ||
|
|
|
|
(major == kProtocolMajorVersion && minor < kProtocolMinorVersion)) {
|
|
|
|
sendConnectionFailedEvent(XIncompatibleClient(major, minor).what());
|
|
|
|
cleanupTimer();
|
|
|
|
cleanupConnection();
|
|
|
|
return;
|
2002-07-10 20:18:32 +00:00
|
|
|
}
|
2002-05-31 18:18:29 +00:00
|
|
|
|
2004-02-15 17:32:11 +00:00
|
|
|
// say hello back
|
|
|
|
LOG((CLOG_DEBUG1 "say hello version %d.%d", kProtocolMajorVersion, kProtocolMinorVersion));
|
|
|
|
CProtocolUtil::writef(m_stream, kMsgHelloBack,
|
|
|
|
kProtocolMajorVersion,
|
|
|
|
kProtocolMinorVersion, &m_name);
|
|
|
|
|
|
|
|
// now connected but waiting to complete handshake
|
|
|
|
setupScreen();
|
|
|
|
cleanupTimer();
|
|
|
|
|
|
|
|
// make sure we process any remaining messages later. we won't
|
|
|
|
// receive another event for already pending messages so we fake
|
|
|
|
// one.
|
|
|
|
if (m_stream->isReady()) {
|
|
|
|
EVENTQUEUE->addEvent(CEvent(IStream::getInputReadyEvent(),
|
|
|
|
m_stream->getEventTarget()));
|
|
|
|
}
|
2002-05-23 14:56:03 +00:00
|
|
|
}
|