• Main Page
  • Classes
  • Files
  • File List

CClientProxy1_0.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2002 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "CClientProxy1_0.h"
00020 #include "CProtocolUtil.h"
00021 #include "XSynergy.h"
00022 #include "IStream.h"
00023 #include "CLog.h"
00024 #include "IEventQueue.h"
00025 #include "TMethodEventJob.h"
00026 #include <cstring>
00027 
00028 //
00029 // CClientProxy1_0
00030 //
00031 
00032 CClientProxy1_0::CClientProxy1_0(const CString& name, synergy::IStream* stream, IEventQueue* eventQueue) :
00033     CClientProxy(name, stream),
00034     m_heartbeatTimer(NULL),
00035     m_parser(&CClientProxy1_0::parseHandshakeMessage),
00036     m_eventQueue(eventQueue)
00037 {
00038     // install event handlers
00039     m_eventQueue->adoptHandler(stream->getInputReadyEvent(),
00040                             stream->getEventTarget(),
00041                             new TMethodEventJob<CClientProxy1_0>(this,
00042                                 &CClientProxy1_0::handleData, NULL));
00043     m_eventQueue->adoptHandler(stream->getOutputErrorEvent(),
00044                             stream->getEventTarget(),
00045                             new TMethodEventJob<CClientProxy1_0>(this,
00046                                 &CClientProxy1_0::handleWriteError, NULL));
00047     m_eventQueue->adoptHandler(stream->getInputShutdownEvent(),
00048                             stream->getEventTarget(),
00049                             new TMethodEventJob<CClientProxy1_0>(this,
00050                                 &CClientProxy1_0::handleDisconnect, NULL));
00051     m_eventQueue->adoptHandler(stream->getOutputShutdownEvent(),
00052                             stream->getEventTarget(),
00053                             new TMethodEventJob<CClientProxy1_0>(this,
00054                                 &CClientProxy1_0::handleWriteError, NULL));
00055     m_eventQueue->adoptHandler(CEvent::kTimer, this,
00056                             new TMethodEventJob<CClientProxy1_0>(this,
00057                                 &CClientProxy1_0::handleFlatline, NULL));
00058 
00059     setHeartbeatRate(kHeartRate, kHeartRate * kHeartBeatsUntilDeath);
00060 
00061     LOG((CLOG_DEBUG1 "querying client \"%s\" info", getName().c_str()));
00062     CProtocolUtil::writef(getStream(), kMsgQInfo);
00063 }
00064 
00065 CClientProxy1_0::~CClientProxy1_0()
00066 {
00067     removeHandlers();
00068 }
00069 
00070 void
00071 CClientProxy1_0::disconnect()
00072 {
00073     removeHandlers();
00074     getStream()->close();
00075     m_eventQueue->addEvent(CEvent(getDisconnectedEvent(), getEventTarget()));
00076 }
00077 
00078 void
00079 CClientProxy1_0::removeHandlers()
00080 {
00081     // uninstall event handlers
00082     m_eventQueue->removeHandler(getStream()->getInputReadyEvent(),
00083                             getStream()->getEventTarget());
00084     m_eventQueue->removeHandler(getStream()->getOutputErrorEvent(),
00085                             getStream()->getEventTarget());
00086     m_eventQueue->removeHandler(getStream()->getInputShutdownEvent(),
00087                             getStream()->getEventTarget());
00088     m_eventQueue->removeHandler(getStream()->getOutputShutdownEvent(),
00089                             getStream()->getEventTarget());
00090     m_eventQueue->removeHandler(CEvent::kTimer, this);
00091 
00092     // remove timer
00093     removeHeartbeatTimer();
00094 }
00095 
00096 void
00097 CClientProxy1_0::addHeartbeatTimer()
00098 {
00099     if (m_heartbeatAlarm > 0.0) {
00100         m_heartbeatTimer = m_eventQueue->newOneShotTimer(m_heartbeatAlarm, this);
00101     }
00102 }
00103 
00104 void
00105 CClientProxy1_0::removeHeartbeatTimer()
00106 {
00107     if (m_heartbeatTimer != NULL) {
00108         m_eventQueue->deleteTimer(m_heartbeatTimer);
00109         m_heartbeatTimer = NULL;
00110     }
00111 }
00112 
00113 void
00114 CClientProxy1_0::resetHeartbeatTimer()
00115 {
00116     // reset the alarm
00117     removeHeartbeatTimer();
00118     addHeartbeatTimer();
00119 }
00120 
00121 void
00122 CClientProxy1_0::resetHeartbeatRate()
00123 {
00124     setHeartbeatRate(kHeartRate, kHeartRate * kHeartBeatsUntilDeath);
00125 }
00126 
00127 void
00128 CClientProxy1_0::setHeartbeatRate(double, double alarm)
00129 {
00130     m_heartbeatAlarm = alarm;
00131 }
00132 
00133 void
00134 CClientProxy1_0::handleData(const CEvent&, void*)
00135 {
00136     // handle messages until there are no more.  first read message code.
00137     UInt8 code[4];
00138     UInt32 n = getStream()->read(code, 4);
00139     while (n != 0) {
00140         // verify we got an entire code
00141         if (n != 4) {
00142             LOG((CLOG_ERR "incomplete message from \"%s\": %d bytes", getName().c_str(), n));
00143             disconnect();
00144             return;
00145         }
00146 
00147         // parse message
00148         LOG((CLOG_DEBUG2 "msg from \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3]));
00149         if (!(this->*m_parser)(code)) {
00150             LOG((CLOG_ERR "invalid message from client \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3]));
00151             disconnect();
00152             return;
00153         }
00154 
00155         // next message
00156         n = getStream()->read(code, 4);
00157     }
00158 
00159     // restart heartbeat timer
00160     resetHeartbeatTimer();
00161 }
00162 
00163 bool
00164 CClientProxy1_0::parseHandshakeMessage(const UInt8* code)
00165 {
00166     if (memcmp(code, kMsgCNoop, 4) == 0) {
00167         // discard no-ops
00168         LOG((CLOG_DEBUG2 "no-op from", getName().c_str()));
00169         return true;
00170     }
00171     else if (memcmp(code, kMsgDInfo, 4) == 0) {
00172         // future messages get parsed by parseMessage
00173         m_parser = &CClientProxy1_0::parseMessage;
00174         if (recvInfo()) {
00175             m_eventQueue->addEvent(CEvent(getReadyEvent(), getEventTarget()));
00176             addHeartbeatTimer();
00177             return true;
00178         }
00179     }
00180     return false;
00181 }
00182 
00183 bool
00184 CClientProxy1_0::parseMessage(const UInt8* code)
00185 {
00186     if (memcmp(code, kMsgDInfo, 4) == 0) {
00187         if (recvInfo()) {
00188             m_eventQueue->addEvent(
00189                             CEvent(getShapeChangedEvent(), getEventTarget()));
00190             return true;
00191         }
00192         return false;
00193     }
00194     else if (memcmp(code, kMsgCNoop, 4) == 0) {
00195         // discard no-ops
00196         LOG((CLOG_DEBUG2 "no-op from", getName().c_str()));
00197         return true;
00198     }
00199     else if (memcmp(code, kMsgCClipboard, 4) == 0) {
00200         return recvGrabClipboard();
00201     }
00202     else if (memcmp(code, kMsgDClipboard, 4) == 0) {
00203         return recvClipboard();
00204     }
00205     return false;
00206 }
00207 
00208 void
00209 CClientProxy1_0::handleDisconnect(const CEvent&, void*)
00210 {
00211     LOG((CLOG_NOTE "client \"%s\" has disconnected", getName().c_str()));
00212     disconnect();
00213 }
00214 
00215 void
00216 CClientProxy1_0::handleWriteError(const CEvent&, void*)
00217 {
00218     LOG((CLOG_WARN "error writing to client \"%s\"", getName().c_str()));
00219     disconnect();
00220 }
00221 
00222 void
00223 CClientProxy1_0::handleFlatline(const CEvent&, void*)
00224 {
00225     // didn't get a heartbeat fast enough.  assume client is dead.
00226     LOG((CLOG_NOTE "client \"%s\" is dead", getName().c_str()));
00227     disconnect();
00228 }
00229 
00230 bool
00231 CClientProxy1_0::getClipboard(ClipboardID id, IClipboard* clipboard) const
00232 {
00233     CClipboard::copy(clipboard, &m_clipboard[id].m_clipboard);
00234     return true;
00235 }
00236 
00237 void
00238 CClientProxy1_0::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
00239 {
00240     x = m_info.m_x;
00241     y = m_info.m_y;
00242     w = m_info.m_w;
00243     h = m_info.m_h;
00244 }
00245 
00246 void
00247 CClientProxy1_0::getCursorPos(SInt32& x, SInt32& y) const
00248 {
00249     // note -- this returns the cursor pos from when we last got client info
00250     x = m_info.m_mx;
00251     y = m_info.m_my;
00252 }
00253 
00254 void
00255 CClientProxy1_0::enter(SInt32 xAbs, SInt32 yAbs,
00256                 UInt32 seqNum, KeyModifierMask mask, bool)
00257 {
00258     LOG((CLOG_DEBUG1 "send enter to \"%s\", %d,%d %d %04x", getName().c_str(), xAbs, yAbs, seqNum, mask));
00259     CProtocolUtil::writef(getStream(), kMsgCEnter,
00260                                 xAbs, yAbs, seqNum, mask);
00261 }
00262 
00263 bool
00264 CClientProxy1_0::leave()
00265 {
00266     LOG((CLOG_DEBUG1 "send leave to \"%s\"", getName().c_str()));
00267     CProtocolUtil::writef(getStream(), kMsgCLeave);
00268 
00269     // we can never prevent the user from leaving
00270     return true;
00271 }
00272 
00273 void
00274 CClientProxy1_0::setClipboard(ClipboardID id, const IClipboard* clipboard)
00275 {
00276     // ignore if this clipboard is already clean
00277     if (m_clipboard[id].m_dirty) {
00278         // this clipboard is now clean
00279         m_clipboard[id].m_dirty = false;
00280         CClipboard::copy(&m_clipboard[id].m_clipboard, clipboard);
00281 
00282         CString data = m_clipboard[id].m_clipboard.marshall();
00283         LOG((CLOG_DEBUG "send clipboard %d to \"%s\" size=%d", id, getName().c_str(), data.size()));
00284         CProtocolUtil::writef(getStream(), kMsgDClipboard, id, 0, &data);
00285     }
00286 }
00287 
00288 void
00289 CClientProxy1_0::grabClipboard(ClipboardID id)
00290 {
00291     LOG((CLOG_DEBUG "send grab clipboard %d to \"%s\"", id, getName().c_str()));
00292     CProtocolUtil::writef(getStream(), kMsgCClipboard, id, 0);
00293 
00294     // this clipboard is now dirty
00295     m_clipboard[id].m_dirty = true;
00296 }
00297 
00298 void
00299 CClientProxy1_0::setClipboardDirty(ClipboardID id, bool dirty)
00300 {
00301     m_clipboard[id].m_dirty = dirty;
00302 }
00303 
00304 void
00305 CClientProxy1_0::keyDown(KeyID key, KeyModifierMask mask, KeyButton)
00306 {
00307     LOG((CLOG_DEBUG1 "send key down to \"%s\" id=%d, mask=0x%04x", getName().c_str(), key, mask));
00308     CProtocolUtil::writef(getStream(), kMsgDKeyDown1_0, key, mask);
00309 }
00310 
00311 void
00312 CClientProxy1_0::keyRepeat(KeyID key, KeyModifierMask mask,
00313                 SInt32 count, KeyButton)
00314 {
00315     LOG((CLOG_DEBUG1 "send key repeat to \"%s\" id=%d, mask=0x%04x, count=%d", getName().c_str(), key, mask, count));
00316     CProtocolUtil::writef(getStream(), kMsgDKeyRepeat1_0, key, mask, count);
00317 }
00318 
00319 void
00320 CClientProxy1_0::keyUp(KeyID key, KeyModifierMask mask, KeyButton)
00321 {
00322     LOG((CLOG_DEBUG1 "send key up to \"%s\" id=%d, mask=0x%04x", getName().c_str(), key, mask));
00323     CProtocolUtil::writef(getStream(), kMsgDKeyUp1_0, key, mask);
00324 }
00325 
00326 void
00327 CClientProxy1_0::mouseDown(ButtonID button)
00328 {
00329     LOG((CLOG_DEBUG1 "send mouse down to \"%s\" id=%d", getName().c_str(), button));
00330     CProtocolUtil::writef(getStream(), kMsgDMouseDown, button);
00331 }
00332 
00333 void
00334 CClientProxy1_0::mouseUp(ButtonID button)
00335 {
00336     LOG((CLOG_DEBUG1 "send mouse up to \"%s\" id=%d", getName().c_str(), button));
00337     CProtocolUtil::writef(getStream(), kMsgDMouseUp, button);
00338 }
00339 
00340 void
00341 CClientProxy1_0::mouseMove(SInt32 xAbs, SInt32 yAbs)
00342 {
00343     LOG((CLOG_DEBUG2 "send mouse move to \"%s\" %d,%d", getName().c_str(), xAbs, yAbs));
00344     CProtocolUtil::writef(getStream(), kMsgDMouseMove, xAbs, yAbs);
00345 }
00346 
00347 void
00348 CClientProxy1_0::mouseRelativeMove(SInt32, SInt32)
00349 {
00350     // ignore -- not supported in protocol 1.0
00351 }
00352 
00353 void
00354 CClientProxy1_0::mouseWheel(SInt32, SInt32 yDelta)
00355 {
00356     // clients prior to 1.3 only support the y axis
00357     LOG((CLOG_DEBUG2 "send mouse wheel to \"%s\" %+d", getName().c_str(), yDelta));
00358     CProtocolUtil::writef(getStream(), kMsgDMouseWheel1_0, yDelta);
00359 }
00360 
00361 void
00362 CClientProxy1_0::gameDeviceButtons(GameDeviceID, GameDeviceButton)
00363 {
00364     // ignore -- not supported in protocol 1.0
00365     LOG((CLOG_DEBUG "gameDeviceButtons not supported"));
00366 }
00367 
00368 void
00369 CClientProxy1_0::gameDeviceSticks(GameDeviceID, SInt16, SInt16, SInt16, SInt16)
00370 {
00371     // ignore -- not supported in protocol 1.0
00372     LOG((CLOG_DEBUG "gameDeviceSticks not supported"));
00373 }
00374 
00375 void
00376 CClientProxy1_0::gameDeviceTriggers(GameDeviceID, UInt8, UInt8)
00377 {
00378     // ignore -- not supported in protocol 1.0
00379     LOG((CLOG_DEBUG "gameDeviceTriggers not supported"));
00380 }
00381 
00382 void
00383 CClientProxy1_0::gameDeviceTimingReq()
00384 {
00385     // ignore -- not supported in protocol 1.0
00386     LOG((CLOG_DEBUG "gameDeviceTimingReq not supported"));
00387 }
00388 
00389 void
00390 CClientProxy1_0::cryptoIv(const UInt8* iv)
00391 {
00392     // ignore -- not supported in protocol 1.0
00393     LOG((CLOG_DEBUG "cryptoIv not supported"));
00394 }
00395 
00396 void
00397 CClientProxy1_0::screensaver(bool on)
00398 {
00399     LOG((CLOG_DEBUG1 "send screen saver to \"%s\" on=%d", getName().c_str(), on ? 1 : 0));
00400     CProtocolUtil::writef(getStream(), kMsgCScreenSaver, on ? 1 : 0);
00401 }
00402 
00403 void
00404 CClientProxy1_0::resetOptions()
00405 {
00406     LOG((CLOG_DEBUG1 "send reset options to \"%s\"", getName().c_str()));
00407     CProtocolUtil::writef(getStream(), kMsgCResetOptions);
00408 
00409     // reset heart rate and death
00410     resetHeartbeatRate();
00411     removeHeartbeatTimer();
00412     addHeartbeatTimer();
00413 }
00414 
00415 void
00416 CClientProxy1_0::setOptions(const COptionsList& options)
00417 {
00418     LOG((CLOG_DEBUG1 "send set options to \"%s\" size=%d", getName().c_str(), options.size()));
00419     CProtocolUtil::writef(getStream(), kMsgDSetOptions, &options);
00420 
00421     // check options
00422     for (UInt32 i = 0, n = (UInt32)options.size(); i < n; i += 2) {
00423         if (options[i] == kOptionHeartbeat) {
00424             double rate = 1.0e-3 * static_cast<double>(options[i + 1]);
00425             if (rate <= 0.0) {
00426                 rate = -1.0;
00427             }
00428             setHeartbeatRate(rate, rate * kHeartBeatsUntilDeath);
00429             removeHeartbeatTimer();
00430             addHeartbeatTimer();
00431         }
00432     }
00433 }
00434 
00435 bool
00436 CClientProxy1_0::recvInfo()
00437 {
00438     // parse the message
00439     SInt16 x, y, w, h, dummy1, mx, my;
00440     if (!CProtocolUtil::readf(getStream(), kMsgDInfo + 4,
00441                             &x, &y, &w, &h, &dummy1, &mx, &my)) {
00442         return false;
00443     }
00444     LOG((CLOG_DEBUG "received client \"%s\" info shape=%d,%d %dx%d at %d,%d", getName().c_str(), x, y, w, h, mx, my));
00445 
00446     // validate
00447     if (w <= 0 || h <= 0) {
00448         return false;
00449     }
00450     if (mx < x || mx >= x + w || my < y || my >= y + h) {
00451         mx = x + w / 2;
00452         my = y + h / 2;
00453     }
00454 
00455     // save
00456     m_info.m_x  = x;
00457     m_info.m_y  = y;
00458     m_info.m_w  = w;
00459     m_info.m_h  = h;
00460     m_info.m_mx = mx;
00461     m_info.m_my = my;
00462 
00463     // acknowledge receipt
00464     LOG((CLOG_DEBUG1 "send info ack to \"%s\"", getName().c_str()));
00465     CProtocolUtil::writef(getStream(), kMsgCInfoAck);
00466     return true;
00467 }
00468 
00469 bool
00470 CClientProxy1_0::recvClipboard()
00471 {
00472     // parse message
00473     ClipboardID id;
00474     UInt32 seqNum;
00475     CString data;
00476     if (!CProtocolUtil::readf(getStream(),
00477                             kMsgDClipboard + 4, &id, &seqNum, &data)) {
00478         return false;
00479     }
00480     LOG((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getName().c_str(), id, seqNum, data.size()));
00481 
00482     // validate
00483     if (id >= kClipboardEnd) {
00484         return false;
00485     }
00486 
00487     // save clipboard
00488     m_clipboard[id].m_clipboard.unmarshall(data, 0);
00489     m_clipboard[id].m_sequenceNumber = seqNum;
00490 
00491     // notify
00492     CClipboardInfo* info   = new CClipboardInfo;
00493     info->m_id             = id;
00494     info->m_sequenceNumber = seqNum;
00495     m_eventQueue->addEvent(CEvent(getClipboardChangedEvent(),
00496                             getEventTarget(), info));
00497 
00498     return true;
00499 }
00500 
00501 bool
00502 CClientProxy1_0::recvGrabClipboard()
00503 {
00504     // parse message
00505     ClipboardID id;
00506     UInt32 seqNum;
00507     if (!CProtocolUtil::readf(getStream(), kMsgCClipboard + 4, &id, &seqNum)) {
00508         return false;
00509     }
00510     LOG((CLOG_DEBUG "received client \"%s\" grabbed clipboard %d seqnum=%d", getName().c_str(), id, seqNum));
00511 
00512     // validate
00513     if (id >= kClipboardEnd) {
00514         return false;
00515     }
00516 
00517     // notify
00518     CClipboardInfo* info   = new CClipboardInfo;
00519     info->m_id             = id;
00520     info->m_sequenceNumber = seqNum;
00521     m_eventQueue->addEvent(CEvent(getClipboardGrabbedEvent(),
00522                             getEventTarget(), info));
00523 
00524     return true;
00525 }
00526 
00527 
00528 //
00529 // CClientProxy1_0::CClientClipboard
00530 //
00531 
00532 CClientProxy1_0::CClientClipboard::CClientClipboard() :
00533     m_clipboard(),
00534     m_sequenceNumber(0),
00535     m_dirty(true)
00536 {
00537     // do nothing
00538 }

Generated on Fri May 24 2013 00:00:03 for Synergy by  doxygen 1.7.1