1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
/*
* 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.
*/
#include "CClient.h"
#include "CServerProxy.h"
#include "CScreen.h"
#include "CClipboard.h"
#include "CPacketStreamFilter.h"
#include "CProtocolUtil.h"
#include "ProtocolTypes.h"
#include "XSynergy.h"
#include "IDataSocket.h"
#include "ISocketFactory.h"
#include "IStreamFilterFactory.h"
#include "CLog.h"
#include "IEventQueue.h"
#include "TMethodEventJob.h"
#include <cstdlib>
#include <cstring>
//
// CClient
//
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),
m_server(NULL),
m_ready(false),
m_active(false),
m_suspended(false),
m_connectOnResume(false)
{
assert(m_socketFactory != NULL);
assert(m_screen != NULL);
// register suspend/resume event handlers
EVENTQUEUE->adoptHandler(IScreen::getSuspendEvent(),
getEventTarget(),
new TMethodEventJob<CClient>(this,
&CClient::handleSuspend));
EVENTQUEUE->adoptHandler(IScreen::getResumeEvent(),
getEventTarget(),
new TMethodEventJob<CClient>(this,
&CClient::handleResume));
}
CClient::~CClient()
{
EVENTQUEUE->removeHandler(IScreen::getSuspendEvent(),
getEventTarget());
EVENTQUEUE->removeHandler(IScreen::getResumeEvent(),
getEventTarget());
cleanupTimer();
cleanupScreen();
cleanupConnecting();
cleanupConnection();
delete m_socketFactory;
delete m_streamFilterFactory;
}
void
CClient::connect()
{
if (m_stream != NULL) {
return;
}
if (m_suspended) {
m_connectOnResume = true;
return;
}
try {
// resolve the server hostname. do this every time we connect
// in case we couldn't resolve the address earlier or the address
// has changed (which can happen frequently if this is a laptop
// being shuttled between various networks). patch by Brent
// Priddy.
m_serverAddress.resolve();
// create the socket
IDataSocket* socket = m_socketFactory->create();
// 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);
// connect
LOG((CLOG_DEBUG1 "connecting to server"));
setupConnecting();
setupTimer();
socket->connect(m_serverAddress);
}
catch (XBase& e) {
cleanupTimer();
cleanupConnecting();
delete m_stream;
m_stream = NULL;
LOG((CLOG_DEBUG1 "connection failed"));
sendConnectionFailedEvent(e.what());
return;
}
}
void
CClient::disconnect(const char* msg)
{
m_connectOnResume = false;
cleanupTimer();
cleanupScreen();
cleanupConnecting();
cleanupConnection();
if (msg != NULL) {
sendConnectionFailedEvent(msg);
}
else {
sendEvent(getDisconnectedEvent(), NULL);
}
}
void
CClient::handshakeComplete()
{
m_ready = true;
m_screen->enable();
sendEvent(getConnectedEvent(), NULL);
}
bool
CClient::isConnected() const
{
return (m_server != NULL);
}
bool
CClient::isConnecting() const
{
return (m_timer != NULL);
}
CNetworkAddress
CClient::getServerAddress() const
{
return m_serverAddress;
}
CEvent::Type
CClient::getConnectedEvent()
{
return CEvent::registerTypeOnce(s_connectedEvent,
"CClient::connected");
}
CEvent::Type
CClient::getConnectionFailedEvent()
{
return CEvent::registerTypeOnce(s_connectionFailedEvent,
"CClient::failed");
}
CEvent::Type
CClient::getDisconnectedEvent()
{
return CEvent::registerTypeOnce(s_disconnectedEvent,
"CClient::disconnected");
}
void*
CClient::getEventTarget() const
{
return m_screen->getEventTarget();
}
bool
CClient::getClipboard(ClipboardID id, IClipboard* clipboard) const
{
return m_screen->getClipboard(id, clipboard);
}
void
CClient::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
{
m_screen->getShape(x, y, w, h);
}
void
CClient::getCursorPos(SInt32& x, SInt32& y) const
{
m_screen->getCursorPos(x, y);
}
void
CClient::enter(SInt32 xAbs, SInt32 yAbs, UInt32, KeyModifierMask mask, bool)
{
m_active = true;
m_screen->mouseMove(xAbs, yAbs);
m_screen->enter(mask);
}
bool
CClient::leave()
{
m_screen->leave();
m_active = false;
// send clipboards that we own and that have changed
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
if (m_ownClipboard[id]) {
sendClipboard(id);
}
}
return true;
}
void
CClient::setClipboard(ClipboardID id, const IClipboard* clipboard)
{
m_screen->setClipboard(id, clipboard);
m_ownClipboard[id] = false;
m_sentClipboard[id] = false;
}
void
CClient::grabClipboard(ClipboardID id)
{
m_screen->grabClipboard(id);
m_ownClipboard[id] = false;
m_sentClipboard[id] = false;
}
void
CClient::setClipboardDirty(ClipboardID, bool)
{
assert(0 && "shouldn't be called");
}
void
CClient::keyDown(KeyID id, KeyModifierMask mask, KeyButton button)
{
m_screen->keyDown(id, mask, button);
}
void
CClient::keyRepeat(KeyID id, KeyModifierMask mask,
SInt32 count, KeyButton button)
{
m_screen->keyRepeat(id, mask, count, button);
}
void
CClient::keyUp(KeyID id, KeyModifierMask mask, KeyButton button)
{
m_screen->keyUp(id, mask, button);
}
void
CClient::mouseDown(ButtonID id)
{
m_screen->mouseDown(id);
}
void
CClient::mouseUp(ButtonID id)
{
m_screen->mouseUp(id);
}
void
CClient::mouseMove(SInt32 x, SInt32 y)
{
m_screen->mouseMove(x, y);
}
void
CClient::mouseRelativeMove(SInt32 dx, SInt32 dy)
{
m_screen->mouseRelativeMove(dx, dy);
}
void
CClient::mouseWheel(SInt32 xDelta, SInt32 yDelta)
{
m_screen->mouseWheel(xDelta, yDelta);
}
void
CClient::screensaver(bool activate)
{
m_screen->screensaver(activate);
}
void
CClient::resetOptions()
{
m_screen->resetOptions();
}
void
CClient::setOptions(const COptionsList& options)
{
m_screen->setOptions(options);
}
CString
CClient::getName() const
{
return m_name;
}
void
CClient::sendClipboard(ClipboardID id)
{
// note -- m_mutex must be locked on entry
assert(m_screen != NULL);
assert(m_server != NULL);
// 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();
// save and send data if different or not yet sent
if (!m_sentClipboard[id] || data != m_dataClipboard[id]) {
m_sentClipboard[id] = true;
m_dataClipboard[id] = data;
m_server->onClipboardChanged(id, &clipboard);
}
}
}
void
CClient::sendEvent(CEvent::Type type, void* data)
{
EVENTQUEUE->addEvent(CEvent(type, getEventTarget(), data));
}
void
CClient::sendConnectionFailedEvent(const char* msg)
{
CFailInfo* info = (CFailInfo*)malloc(sizeof(CFailInfo) + strlen(msg));
info->m_retry = true;
strcpy(info->m_what, msg);
sendEvent(getConnectionFailedEvent(), info);
}
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));
}
void
CClient::setupConnection()
{
assert(m_stream != NULL);
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));
}
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;
}
}
void
CClient::cleanupScreen()
{
if (m_server != NULL) {
if (m_ready) {
m_screen->disable();
m_ready = false;
}
EVENTQUEUE->removeHandler(IScreen::getShapeChangedEvent(),
getEventTarget());
EVENTQUEUE->removeHandler(IScreen::getClipboardGrabbedEvent(),
getEventTarget());
delete m_server;
m_server = NULL;
}
}
void
CClient::cleanupTimer()
{
if (m_timer != NULL) {
EVENTQUEUE->removeHandler(CEvent::kTimer, m_timer);
EVENTQUEUE->deleteTimer(m_timer);
m_timer = NULL;
}
}
void
CClient::handleConnected(const CEvent&, void*)
{
LOG((CLOG_DEBUG1 "connected; wait for hello"));
cleanupConnecting();
setupConnection();
// reset clipboard state
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
m_ownClipboard[id] = false;
m_sentClipboard[id] = false;
m_timeClipboard[id] = 0;
}
}
void
CClient::handleConnectionFailed(const CEvent& event, void*)
{
IDataSocket::CConnectionFailedInfo* info =
reinterpret_cast<IDataSocket::CConnectionFailedInfo*>(event.getData());
cleanupTimer();
cleanupConnecting();
delete m_stream;
m_stream = NULL;
LOG((CLOG_DEBUG1 "connection failed"));
sendConnectionFailedEvent(info->m_what);
}
void
CClient::handleConnectTimeout(const CEvent&, void*)
{
cleanupTimer();
cleanupConnecting();
cleanupConnection();
delete m_stream;
m_stream = NULL;
LOG((CLOG_DEBUG1 "connection timed out"));
sendConnectionFailedEvent("Timed out");
}
void
CClient::handleOutputError(const CEvent&, void*)
{
cleanupTimer();
cleanupScreen();
cleanupConnection();
LOG((CLOG_WARN "error sending to server"));
sendEvent(getDisconnectedEvent(), NULL);
}
void
CClient::handleDisconnected(const CEvent&, void*)
{
cleanupTimer();
cleanupScreen();
cleanupConnection();
LOG((CLOG_DEBUG1 "disconnected"));
sendEvent(getDisconnectedEvent(), NULL);
}
void
CClient::handleShapeChanged(const CEvent&, void*)
{
LOG((CLOG_DEBUG "resolution changed"));
m_server->onInfoChanged();
}
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_sentClipboard[info->m_id] = false;
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);
}
}
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;
}
// 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;
}
// 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()));
}
}
void
CClient::handleSuspend(const CEvent&, void*)
{
LOG((CLOG_INFO "suspend"));
m_suspended = true;
bool wasConnected = isConnected();
disconnect(NULL);
m_connectOnResume = wasConnected;
}
void
CClient::handleResume(const CEvent&, void*)
{
LOG((CLOG_INFO "resume"));
m_suspended = false;
if (m_connectOnResume) {
m_connectOnResume = false;
connect();
}
}
|