aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/ClientChannel.h
blob: 6c907bf356f9efd29d0436a18edfbf3e459c4a06 (plain)
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
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2009 NoMachine, http://www.nomachine.com/.         */
/*                                                                        */
/* NXCOMP, NX protocol compression and NX extensions to this software     */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/**************************************************************************/

#ifndef ClientChannel_H
#define ClientChannel_H

#include "List.h"
#include "Channel.h"

#include "SequenceQueue.h"

#include "ClientReadBuffer.h"

//
// Set the verbosity level.
//

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

//
// If defined, the client channel will
// have the chance of suppressing more
// opcodes for test purposes.
//

#undef  LAME

//
// Define this to log a line when a
// channel is created or destroyed.
//

#undef  REFERENCES

//
// This class implements the X client
// side compression of the protocol.
//

class ClientChannel : public Channel
{
  public:

  ClientChannel(Transport *transport, StaticCompressor *compressor);

  virtual ~ClientChannel();

  virtual int handleRead(EncodeBuffer &encodeBuffer, const unsigned char *message,
                             unsigned int length);

  virtual int handleWrite(const unsigned char *message, unsigned int length);

  virtual int handleSplit(EncodeBuffer &encodeBuffer, MessageStore *store,
                              T_store_action action, int position, const unsigned char opcode,
                                  const unsigned char *buffer, const unsigned int size);

  virtual int handleSplit(DecodeBuffer &decodeBuffer, MessageStore *store,
                              T_store_action action, int position, unsigned char &opcode,
                                  unsigned char *&buffer, unsigned int &size)
  {
    return 0;
  }

  virtual int handleSplit(EncodeBuffer &encodeBuffer);

  virtual int handleSplit(DecodeBuffer &decodeBuffer)
  {
    return 0;
  }

  virtual int handleSplitEvent(EncodeBuffer &encodeBuffer, Split *split);

  virtual int handleSplitEvent(DecodeBuffer &decodeBuffer);

  virtual int handleMotion(EncodeBuffer &encodeBuffer)
  {
    return 0;
  }

  virtual int handleCompletion(EncodeBuffer &encodeBuffer)
  {
    return 0;
  }

  virtual int handleConfiguration();

  virtual int handleFinish();

  virtual int handleAsyncEvents()
  {
    return 0;
  }

  virtual int needSplit() const
  {
    #if defined(TEST) || defined(SPLIT)
    *logofs << "needSplit: SPLIT! Returning pending split "
            << "flag " << splitState_.pending << " with "
            << clientStore_ -> getSplitTotalSize()
            << " splits in the split stores.\n"
            << logofs_flush;
    #endif

    return splitState_.pending;
  }

  virtual int needMotion() const
  {
    return 0;
  }

  virtual T_channel_type getType() const
  {
    return channel_x11;
  }

  int setBigEndian(int flag);

  //
  // Initialize the static members.
  //

  static int setReferences();

  private:

  int handleFastReadRequest(EncodeBuffer &encodeBuffer, const unsigned char &opcode,
                                const unsigned char *&buffer, const unsigned int &size);

  int handleFastWriteReply(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                               unsigned char *&buffer, unsigned int &size);

  int handleFastWriteEvent(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                               unsigned char *&buffer, unsigned int &size);

  //
  // Intercept the request before the opcode
  // is encoded.
  //

  int handleTaintRequest(unsigned char &opcode, const unsigned char *&buffer,
                             unsigned int &size)
  {
    if (control -> isProtoStep7() == 0)
    {
      if (opcode == X_NXFreeSplit || opcode == X_NXAbortSplit ||
              opcode == X_NXFinishSplit)
      {
        return handleTaintSplitRequest(opcode, buffer, size);
      }
      else if (opcode == X_NXSetCacheParameters)
      {
        return handleTaintCacheRequest(opcode, buffer, size);
      }
      else if (opcode == X_NXGetFontParameters)
      {
        return handleTaintFontRequest(opcode, buffer, size);
      }
    }

    if (control -> TaintReplies > 0 &&
            opcode == X_GetInputFocus)
    {
      return handleTaintSyncRequest(opcode, buffer, size);
    }

    #ifdef LAME

    return handleTaintLameRequest(opcode, buffer, size);

    #endif

    return 0;
  }

  int handleTaintCacheRequest(unsigned char &opcode, const unsigned char *&buffer,
                                  unsigned int &size);

  int handleTaintFontRequest(unsigned char &opcode, const unsigned char *&buffer,
                                 unsigned int &size);

  int handleTaintSplitRequest(unsigned char &opcode, const unsigned char *&buffer,
                                  unsigned int &size);

  int handleTaintLameRequest(unsigned char &opcode, const unsigned char *&buffer,
                                 unsigned int &size);

  int handleTaintSyncRequest(unsigned char &opcode, const unsigned char *&buffer,
                                 unsigned int &size);

  int handleTaintSyncError(unsigned char opcode);

  //
  // How to handle sequence counter
  // in notification event.
  //

  enum T_sequence_mode
  {
    sequence_immediate,
    sequence_deferred
  };

  //
  // Send split notifications to the
  // agent.
  //

  int handleRestart(T_sequence_mode mode, int resource);

  int handleNotify(T_notification_type type, T_sequence_mode mode,
                       int resource, int request, int position);

  //
  // Other utility functions used in
  // handling of the image streaming.
  //

  int mustSplitMessage(int resource)
  {
    return (clientStore_ -> getSplitStore(resource) ->
                getSize() != 0);
  }

  int canSplitMessage(T_split_mode mode, unsigned int size)
  {
    return ((int) size >= control -> SplitDataThreshold &&
                (clientStore_ -> getSplitTotalStorageSize() < control ->
                     SplitTotalStorageSize && clientStore_ ->
                         getSplitTotalSize() < control -> SplitTotalSize));
  }

  int canSendSplit(Split *split)
  {
    return (split -> getMode() != split_sync ||
                split -> getState() == split_missed ||
                    split -> getState() == split_loaded);
  }

  int handleSplitSend(EncodeBuffer &encodeBuffer, int resource,
                          int &total, int &bytes);

  Split *handleSplitFind(T_checksum checksum, int resource);

  int handleSplitChecksum(EncodeBuffer &encodeBuffer, T_checksum checksum);

  void handleSplitEnable()
  {
    if (control -> isProtoStep7() == 0)
    {
      #if defined(TEST) || defined(SPLIT)
      *logofs << "handleSplitEnable: WARNING! Disabling split "
              << "with an old proxy version.\n"
              << logofs_flush;
      #endif

      enableSplit_ = 0;
    }
  }

  void handleSplitPending(int resource)
  {
    if (splitState_.pending == 0)
    {
      if (clientStore_ -> getSplitStore(resource) != NULL &&
              clientStore_ -> getSplitStore(resource) ->
                  getFirstSplit() != NULL)
      {
        splitState_.pending = canSendSplit(clientStore_ ->
            getSplitStore(resource) -> getFirstSplit());

        #if defined(TEST) || defined(SPLIT)
        *logofs << "handleSplitPending: SPLIT! Set the pending "
                << "split flag to " << splitState_.pending
                << " with " << clientStore_ -> getSplitTotalSize()
                << " splits in the split stores.\n"
                << logofs_flush;
        #endif
      }
    }
  }

  //
  // Scan all the split stores to find
  // if there is any split to send.
  //

  void handleSplitPending();

  //
  // Handle the MIT-SHM initialization
  // messages exchanged with the remote
  // proxy.
  //

  int handleShmemRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                             const unsigned char *buffer, const unsigned int size);

  int handleShmemReply(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                           unsigned char *&buffer, unsigned int &size);

  //
  // Query the port used to tunnel
  // the font server connections.
  //

  int handleFontRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                            const unsigned char *buffer, const unsigned int size);

  int handleFontReply(DecodeBuffer &decodeBuffer, unsigned char &opcode,
                          unsigned char *&buffer, unsigned int &size);

  //
  // Let the agent set the cache
  // policy for image requests.
  //

  int handleCacheRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                             const unsigned char *buffer, const unsigned int size);

  //
  // Encode the start and end split
  // requests.
  //

  int handleStartSplitRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                                  const unsigned char *buffer, const unsigned int size);

  int handleEndSplitRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                                const unsigned char *buffer, const unsigned int size);

  //
  // Empty a split store and send the
  // restart event.
  //

  int handleAbortSplitRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                                  const unsigned char *buffer, const unsigned int size);

  //
  // Force the proxy to finalize all
  // the pending split operations and
  // restart a resource.
  //
  
  int handleFinishSplitRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                                   const unsigned char *buffer, const unsigned int size);

  //
  // Tell the remote peer to send the
  // split requests to the X server.
  //

  int handleCommitSplitRequest(EncodeBuffer &encodeBuffer, const unsigned char opcode,
                                   const unsigned char *buffer, const unsigned int size);

  //
  // Other utilities.
  //

  void handleDecodeCharInfo(DecodeBuffer &, unsigned char *);

  //
  // Own read buffer. It is able to identify
  // full messages read from the X descriptor.
  //

  ClientReadBuffer readBuffer_;

  //
  // Sequence number of last request coming
  // from the X client or the X server.
  //

  unsigned int clientSequence_;
  unsigned int serverSequence_;

  //
  // Last sequence number known by client. It can
  // be the real sequence generated by server or
  // the one of the last auto-generated event.
  //

  unsigned int lastSequence_;

  //
  // Used to identify replies based on sequence
  // number of original request.
  //

  SequenceQueue sequenceQueue_;

  //
  // This is used to test the synchronous flush
  // in the proxy.
  //

  int lastRequest_;

  //
  // Current resource id selected as target and
  // other information related to the image split.
  // The pending and abort flags are set when we
  // want the proxy to give us a chance to send
  // more split data. We also save the position
  // of the last commit operation performed by
  // channel so we can differentially encode the
  // position of next message to commit.
  //

  typedef struct
  {
    int resource;
    int pending;
    int commit;
    T_split_mode mode;

  } T_split_state;

  T_split_state splitState_;

  //
  // List of agent resources.
  //

  List splitResources_;

  //
  // How many sync requests we
  // have tainted so far.
  //

  int taintCounter_;

  private:

  //
  // Keep track of object 
  // creation and deletion.
  //

  #ifdef REFERENCES

  static int references_;

  #endif
};

#endif /* ClientChannel_H */