source: trunk/synergy/lib/server/CClientProxy1_0.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 12.3 KB
Line 
1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2002 Chris Schoeneman
4 *
5 * This package is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * found in the file COPYING that should have accompanied this file.
8 *
9 * This package is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include "CClientProxy1_0.h"
16#include "CProtocolUtil.h"
17#include "XSynergy.h"
18#include "IStream.h"
19#include "CLog.h"
20#include "IEventQueue.h"
21#include "TMethodEventJob.h"
22#include <cstring>
23
24//
25// CClientProxy1_0
26//
27
28CClientProxy1_0::CClientProxy1_0(const CString& name, IStream* stream) :
29 CClientProxy(name, stream),
30 m_heartbeatTimer(NULL),
31 m_parser(&CClientProxy1_0::parseHandshakeMessage)
32{
33 // install event handlers
34 EVENTQUEUE->adoptHandler(IStream::getInputReadyEvent(),
35 stream->getEventTarget(),
36 new TMethodEventJob<CClientProxy1_0>(this,
37 &CClientProxy1_0::handleData, NULL));
38 EVENTQUEUE->adoptHandler(IStream::getOutputErrorEvent(),
39 stream->getEventTarget(),
40 new TMethodEventJob<CClientProxy1_0>(this,
41 &CClientProxy1_0::handleWriteError, NULL));
42 EVENTQUEUE->adoptHandler(IStream::getInputShutdownEvent(),
43 stream->getEventTarget(),
44 new TMethodEventJob<CClientProxy1_0>(this,
45 &CClientProxy1_0::handleDisconnect, NULL));
46 EVENTQUEUE->adoptHandler(IStream::getOutputShutdownEvent(),
47 stream->getEventTarget(),
48 new TMethodEventJob<CClientProxy1_0>(this,
49 &CClientProxy1_0::handleWriteError, NULL));
50 EVENTQUEUE->adoptHandler(CEvent::kTimer, this,
51 new TMethodEventJob<CClientProxy1_0>(this,
52 &CClientProxy1_0::handleFlatline, NULL));
53
54 setHeartbeatRate(kHeartRate, kHeartRate * kHeartBeatsUntilDeath);
55
56 LOG((CLOG_DEBUG1 "querying client \"%s\" info", getName().c_str()));
57 CProtocolUtil::writef(getStream(), kMsgQInfo);
58}
59
60CClientProxy1_0::~CClientProxy1_0()
61{
62 removeHandlers();
63}
64
65void
66CClientProxy1_0::disconnect()
67{
68 removeHandlers();
69 getStream()->close();
70 EVENTQUEUE->addEvent(CEvent(getDisconnectedEvent(), getEventTarget()));
71}
72
73void
74CClientProxy1_0::removeHandlers()
75{
76 // uninstall event handlers
77 EVENTQUEUE->removeHandler(IStream::getInputReadyEvent(),
78 getStream()->getEventTarget());
79 EVENTQUEUE->removeHandler(IStream::getOutputErrorEvent(),
80 getStream()->getEventTarget());
81 EVENTQUEUE->removeHandler(IStream::getInputShutdownEvent(),
82 getStream()->getEventTarget());
83 EVENTQUEUE->removeHandler(IStream::getOutputShutdownEvent(),
84 getStream()->getEventTarget());
85 EVENTQUEUE->removeHandler(CEvent::kTimer, this);
86
87 // remove timer
88 removeHeartbeatTimer();
89}
90
91void
92CClientProxy1_0::addHeartbeatTimer()
93{
94 if (m_heartbeatAlarm > 0.0) {
95 m_heartbeatTimer = EVENTQUEUE->newOneShotTimer(m_heartbeatAlarm, this);
96 }
97}
98
99void
100CClientProxy1_0::removeHeartbeatTimer()
101{
102 if (m_heartbeatTimer != NULL) {
103 EVENTQUEUE->deleteTimer(m_heartbeatTimer);
104 m_heartbeatTimer = NULL;
105 }
106}
107
108void
109CClientProxy1_0::resetHeartbeatTimer()
110{
111 // reset the alarm
112 removeHeartbeatTimer();
113 addHeartbeatTimer();
114}
115
116void
117CClientProxy1_0::resetHeartbeatRate()
118{
119 setHeartbeatRate(kHeartRate, kHeartRate * kHeartBeatsUntilDeath);
120}
121
122void
123CClientProxy1_0::setHeartbeatRate(double, double alarm)
124{
125 m_heartbeatAlarm = alarm;
126}
127
128void
129CClientProxy1_0::handleData(const CEvent&, void*)
130{
131 // handle messages until there are no more. first read message code.
132 UInt8 code[4];
133 UInt32 n = getStream()->read(code, 4);
134 while (n != 0) {
135 // verify we got an entire code
136 if (n != 4) {
137 LOG((CLOG_ERR "incomplete message from \"%s\": %d bytes", getName().c_str(), n));
138 disconnect();
139 return;
140 }
141
142 // parse message
143 LOG((CLOG_DEBUG2 "msg from \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3]));
144 if (!(this->*m_parser)(code)) {
145 LOG((CLOG_ERR "invalid message from client \"%s\": %c%c%c%c", getName().c_str(), code[0], code[1], code[2], code[3]));
146 disconnect();
147 return;
148 }
149
150 // next message
151 n = getStream()->read(code, 4);
152 }
153
154 // restart heartbeat timer
155 resetHeartbeatTimer();
156}
157
158bool
159CClientProxy1_0::parseHandshakeMessage(const UInt8* code)
160{
161 if (memcmp(code, kMsgCNoop, 4) == 0) {
162 // discard no-ops
163 LOG((CLOG_DEBUG2 "no-op from", getName().c_str()));
164 return true;
165 }
166 else if (memcmp(code, kMsgDInfo, 4) == 0) {
167 // future messages get parsed by parseMessage
168 m_parser = &CClientProxy1_0::parseMessage;
169 if (recvInfo()) {
170 EVENTQUEUE->addEvent(CEvent(getReadyEvent(), getEventTarget()));
171 addHeartbeatTimer();
172 return true;
173 }
174 }
175 return false;
176}
177
178bool
179CClientProxy1_0::parseMessage(const UInt8* code)
180{
181 if (memcmp(code, kMsgDInfo, 4) == 0) {
182 if (recvInfo()) {
183 EVENTQUEUE->addEvent(
184 CEvent(getShapeChangedEvent(), getEventTarget()));
185 return true;
186 }
187 return false;
188 }
189 else if (memcmp(code, kMsgCNoop, 4) == 0) {
190 // discard no-ops
191 LOG((CLOG_DEBUG2 "no-op from", getName().c_str()));
192 return true;
193 }
194 else if (memcmp(code, kMsgCClipboard, 4) == 0) {
195 return recvGrabClipboard();
196 }
197 else if (memcmp(code, kMsgDClipboard, 4) == 0) {
198 return recvClipboard();
199 }
200 return false;
201}
202
203void
204CClientProxy1_0::handleDisconnect(const CEvent&, void*)
205{
206 LOG((CLOG_NOTE "client \"%s\" has disconnected", getName().c_str()));
207 disconnect();
208}
209
210void
211CClientProxy1_0::handleWriteError(const CEvent&, void*)
212{
213 LOG((CLOG_WARN "error writing to client \"%s\"", getName().c_str()));
214 disconnect();
215}
216
217void
218CClientProxy1_0::handleFlatline(const CEvent&, void*)
219{
220 // didn't get a heartbeat fast enough. assume client is dead.
221 LOG((CLOG_NOTE "client \"%s\" is dead", getName().c_str()));
222 disconnect();
223}
224
225bool
226CClientProxy1_0::getClipboard(ClipboardID id, IClipboard* clipboard) const
227{
228 CClipboard::copy(clipboard, &m_clipboard[id].m_clipboard);
229 return true;
230}
231
232void
233CClientProxy1_0::getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h) const
234{
235 x = m_info.m_x;
236 y = m_info.m_y;
237 w = m_info.m_w;
238 h = m_info.m_h;
239}
240
241void
242CClientProxy1_0::getCursorPos(SInt32& x, SInt32& y) const
243{
244 // note -- this returns the cursor pos from when we last got client info
245 x = m_info.m_mx;
246 y = m_info.m_my;
247}
248
249void
250CClientProxy1_0::enter(SInt32 xAbs, SInt32 yAbs,
251 UInt32 seqNum, KeyModifierMask mask, bool)
252{
253 LOG((CLOG_DEBUG1 "send enter to \"%s\", %d,%d %d %04x", getName().c_str(), xAbs, yAbs, seqNum, mask));
254 CProtocolUtil::writef(getStream(), kMsgCEnter,
255 xAbs, yAbs, seqNum, mask);
256}
257
258bool
259CClientProxy1_0::leave()
260{
261 LOG((CLOG_DEBUG1 "send leave to \"%s\"", getName().c_str()));
262 CProtocolUtil::writef(getStream(), kMsgCLeave);
263
264 // we can never prevent the user from leaving
265 return true;
266}
267
268void
269CClientProxy1_0::setClipboard(ClipboardID id, const IClipboard* clipboard)
270{
271 // ignore if this clipboard is already clean
272 if (m_clipboard[id].m_dirty) {
273 // this clipboard is now clean
274 m_clipboard[id].m_dirty = false;
275 CClipboard::copy(&m_clipboard[id].m_clipboard, clipboard);
276
277 CString data = m_clipboard[id].m_clipboard.marshall();
278 LOG((CLOG_DEBUG "send clipboard %d to \"%s\" size=%d", id, getName().c_str(), data.size()));
279 CProtocolUtil::writef(getStream(), kMsgDClipboard, id, 0, &data);
280 }
281}
282
283void
284CClientProxy1_0::grabClipboard(ClipboardID id)
285{
286 LOG((CLOG_DEBUG "send grab clipboard %d to \"%s\"", id, getName().c_str()));
287 CProtocolUtil::writef(getStream(), kMsgCClipboard, id, 0);
288
289 // this clipboard is now dirty
290 m_clipboard[id].m_dirty = true;
291}
292
293void
294CClientProxy1_0::setClipboardDirty(ClipboardID id, bool dirty)
295{
296 m_clipboard[id].m_dirty = dirty;
297}
298
299void
300CClientProxy1_0::keyDown(KeyID key, KeyModifierMask mask, KeyButton)
301{
302 LOG((CLOG_DEBUG1 "send key down to \"%s\" id=%d, mask=0x%04x", getName().c_str(), key, mask));
303 CProtocolUtil::writef(getStream(), kMsgDKeyDown1_0, key, mask);
304}
305
306void
307CClientProxy1_0::keyRepeat(KeyID key, KeyModifierMask mask,
308 SInt32 count, KeyButton)
309{
310 LOG((CLOG_DEBUG1 "send key repeat to \"%s\" id=%d, mask=0x%04x, count=%d", getName().c_str(), key, mask, count));
311 CProtocolUtil::writef(getStream(), kMsgDKeyRepeat1_0, key, mask, count);
312}
313
314void
315CClientProxy1_0::keyUp(KeyID key, KeyModifierMask mask, KeyButton)
316{
317 LOG((CLOG_DEBUG1 "send key up to \"%s\" id=%d, mask=0x%04x", getName().c_str(), key, mask));
318 CProtocolUtil::writef(getStream(), kMsgDKeyUp1_0, key, mask);
319}
320
321void
322CClientProxy1_0::mouseDown(ButtonID button)
323{
324 LOG((CLOG_DEBUG1 "send mouse down to \"%s\" id=%d", getName().c_str(), button));
325 CProtocolUtil::writef(getStream(), kMsgDMouseDown, button);
326}
327
328void
329CClientProxy1_0::mouseUp(ButtonID button)
330{
331 LOG((CLOG_DEBUG1 "send mouse up to \"%s\" id=%d", getName().c_str(), button));
332 CProtocolUtil::writef(getStream(), kMsgDMouseUp, button);
333}
334
335void
336CClientProxy1_0::mouseMove(SInt32 xAbs, SInt32 yAbs)
337{
338 LOG((CLOG_DEBUG2 "send mouse move to \"%s\" %d,%d", getName().c_str(), xAbs, yAbs));
339 CProtocolUtil::writef(getStream(), kMsgDMouseMove, xAbs, yAbs);
340}
341
342void
343CClientProxy1_0::mouseRelativeMove(SInt32, SInt32)
344{
345 // ignore -- not supported in protocol 1.0
346}
347
348void
349CClientProxy1_0::mouseWheel(SInt32, SInt32 yDelta)
350{
351 // clients prior to 1.3 only support the y axis
352 LOG((CLOG_DEBUG2 "send mouse wheel to \"%s\" %+d", getName().c_str(), yDelta));
353 CProtocolUtil::writef(getStream(), kMsgDMouseWheel1_0, yDelta);
354}
355
356void
357CClientProxy1_0::screensaver(bool on)
358{
359 LOG((CLOG_DEBUG1 "send screen saver to \"%s\" on=%d", getName().c_str(), on ? 1 : 0));
360 CProtocolUtil::writef(getStream(), kMsgCScreenSaver, on ? 1 : 0);
361}
362
363void
364CClientProxy1_0::resetOptions()
365{
366 LOG((CLOG_DEBUG1 "send reset options to \"%s\"", getName().c_str()));
367 CProtocolUtil::writef(getStream(), kMsgCResetOptions);
368
369 // reset heart rate and death
370 resetHeartbeatRate();
371 removeHeartbeatTimer();
372 addHeartbeatTimer();
373}
374
375void
376CClientProxy1_0::setOptions(const COptionsList& options)
377{
378 LOG((CLOG_DEBUG1 "send set options to \"%s\" size=%d", getName().c_str(), options.size()));
379 CProtocolUtil::writef(getStream(), kMsgDSetOptions, &options);
380
381 // check options
382 for (UInt32 i = 0, n = options.size(); i < n; i += 2) {
383 if (options[i] == kOptionHeartbeat) {
384 double rate = 1.0e-3 * static_cast<double>(options[i + 1]);
385 if (rate <= 0.0) {
386 rate = -1.0;
387 }
388 setHeartbeatRate(rate, rate * kHeartBeatsUntilDeath);
389 removeHeartbeatTimer();
390 addHeartbeatTimer();
391 }
392 }
393}
394
395bool
396CClientProxy1_0::recvInfo()
397{
398 // parse the message
399 SInt16 x, y, w, h, dummy1, mx, my;
400 if (!CProtocolUtil::readf(getStream(), kMsgDInfo + 4,
401 &x, &y, &w, &h, &dummy1, &mx, &my)) {
402 return false;
403 }
404 LOG((CLOG_DEBUG "received client \"%s\" info shape=%d,%d %dx%d", getName().c_str(), x, y, w, h));
405
406 // validate
407 if (w <= 0 || h <= 0) {
408 return false;
409 }
410
411 // save
412 m_info.m_x = x;
413 m_info.m_y = y;
414 m_info.m_w = w;
415 m_info.m_h = h;
416 m_info.m_mx = mx;
417 m_info.m_my = my;
418
419 // acknowledge receipt
420 LOG((CLOG_DEBUG1 "send info ack to \"%s\"", getName().c_str()));
421 CProtocolUtil::writef(getStream(), kMsgCInfoAck);
422 return true;
423}
424
425bool
426CClientProxy1_0::recvClipboard()
427{
428 // parse message
429 ClipboardID id;
430 UInt32 seqNum;
431 CString data;
432 if (!CProtocolUtil::readf(getStream(),
433 kMsgDClipboard + 4, &id, &seqNum, &data)) {
434 return false;
435 }
436 LOG((CLOG_DEBUG "received client \"%s\" clipboard %d seqnum=%d, size=%d", getName().c_str(), id, seqNum, data.size()));
437
438 // validate
439 if (id >= kClipboardEnd) {
440 return false;
441 }
442
443 // save clipboard
444 m_clipboard[id].m_clipboard.unmarshall(data, 0);
445 m_clipboard[id].m_sequenceNumber = seqNum;
446
447 // notify
448 CClipboardInfo* info = new CClipboardInfo;
449 info->m_id = id;
450 info->m_sequenceNumber = seqNum;
451 EVENTQUEUE->addEvent(CEvent(getClipboardChangedEvent(),
452 getEventTarget(), info));
453
454 return true;
455}
456
457bool
458CClientProxy1_0::recvGrabClipboard()
459{
460 // parse message
461 ClipboardID id;
462 UInt32 seqNum;
463 if (!CProtocolUtil::readf(getStream(), kMsgCClipboard + 4, &id, &seqNum)) {
464 return false;
465 }
466 LOG((CLOG_DEBUG "received client \"%s\" grabbed clipboard %d seqnum=%d", getName().c_str(), id, seqNum));
467
468 // validate
469 if (id >= kClipboardEnd) {
470 return false;
471 }
472
473 // notify
474 CClipboardInfo* info = new CClipboardInfo;
475 info->m_id = id;
476 info->m_sequenceNumber = seqNum;
477 EVENTQUEUE->addEvent(CEvent(getClipboardGrabbedEvent(),
478 getEventTarget(), info));
479
480 return true;
481}
482
483
484//
485// CClientProxy1_0::CClientClipboard
486//
487
488CClientProxy1_0::CClientClipboard::CClientClipboard() :
489 m_clipboard(),
490 m_sequenceNumber(0),
491 m_dirty(true)
492{
493 // do nothing
494}
Note: See TracBrowser for help on using the repository browser.