forked from ArcticaProject/nx-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannel.cpp
2125 lines (1697 loc) · 52.3 KB
/
Channel.cpp
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2010 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. */
/* */
/**************************************************************************/
#include "Channel.h"
#include "List.h"
#include "Proxy.h"
#include "Statistics.h"
#include "StaticCompressor.h"
#include "NXalert.h"
extern Proxy *proxy;
//
// Set the verbosity level.
//
#define PANIC
#define WARNING
#undef TEST
#undef DEBUG
#undef DUMP
//
// Log the operations related to splits.
//
#undef SPLIT
#undef COUNT
#define COUNT_MAJOR_OPCODE 154
#undef MONITOR
#define MONITOR_MAJOR_OPCODE 154
#define MONITOR_MINOR_OPCODE 23
#undef CLEAR
#define CLEAR_MAJOR_OPCODE 154
#define CLEAR_MINOR_OPCODE 23
//
// Define this to know how many messages
// are allocated and deallocated.
//
#undef REFERENCES
//
// Set to the descriptor of the first X
// channel successfully connected.
//
int Channel::firstClient_ = -1;
//
// Port used for font server connections.
//
int Channel::fontPort_ = -1;
//
// This is used for reference count.
//
#ifdef REFERENCES
int Channel::references_ = 0;
#endif
Channel::Channel(Transport *transport, StaticCompressor *compressor)
: transport_(transport), compressor_(compressor)
{
fd_ = transport_ -> fd();
finish_ = 0;
closing_ = 0;
drop_ = 0;
congestion_ = 0;
priority_ = 0;
alert_ = 0;
firstRequest_ = 1;
firstReply_ = 1;
enableCache_ = 1;
enableSplit_ = 1;
enableSave_ = 1;
enableLoad_ = 1;
//
// Must be set by proxy.
//
opcodeStore_ = NULL;
clientStore_ = NULL;
serverStore_ = NULL;
clientCache_ = NULL;
serverCache_ = NULL;
#ifdef REFERENCES
*logofs << "Channel: Created new Channel at "
<< this << " out of " << ++references_
<< " allocated references.\n" << logofs_flush;
#endif
}
Channel::~Channel()
{
if (firstClient_ == fd_)
{
firstClient_ = -1;
}
#ifdef REFERENCES
*logofs << "Channel: Deleted Channel at "
<< this << " out of " << --references_
<< " allocated references.\n" << logofs_flush;
#endif
}
int Channel::handleEncode(EncodeBuffer &encodeBuffer, ChannelCache *channelCache,
MessageStore *store, const unsigned char opcode,
const unsigned char *buffer, const unsigned int size)
{
#ifdef MONITOR
static float totalMessages = 0;
static float totalBits = 0;
int bits;
int diff;
bits = encodeBuffer.getBits();
#endif
//
// Check if message can be differentially
// encoded using a similar message in the
// message store.
//
#ifdef COUNT
if (*(buffer) == COUNT_MAJOR_OPCODE)
{
if (*(buffer) < 128)
{
*logofs << "handleEncode: Handling OPCODE#" << (unsigned int) *(buffer)
<< ".\n" << logofs_flush;
}
else
{
*logofs << "handleEncode: Handling OPCODE#" << (unsigned int) *(buffer)
<< " MINOR#" << (unsigned int) *(buffer + 1) << ".\n"
<< logofs_flush;
}
}
#endif
#ifdef CLEAR
if (*(buffer) == CLEAR_MAJOR_OPCODE &&
(CLEAR_MINOR_OPCODE == -1 || *(buffer + 1) == CLEAR_MINOR_OPCODE))
{
*((unsigned char *) buffer) = X_NoOperation;
*((unsigned char *) buffer + 1) = '\0';
CleanData((unsigned char *) buffer + 4, size - 4);
}
#endif
if (handleEncodeCached(encodeBuffer, channelCache,
store, buffer, size) == 1)
{
#ifdef MONITOR
diff = encodeBuffer.getBits() - bits;
if (*(buffer) == MONITOR_MAJOR_OPCODE &&
(MONITOR_MINOR_OPCODE == -1 || *(buffer + 1) == MONITOR_MINOR_OPCODE))
{
totalMessages++;
totalBits += diff;
*logofs << "handleEncode: Handled cached OPCODE#" << (unsigned int) *(buffer)
<< " MINOR#" << (unsigned int) *(buffer + 1) << ". " << size
<< " bytes in, " << diff << " bits (" << ((float) diff) / 8
<< " bytes) out. Average " << totalBits / totalMessages
<< "/1.\n" << logofs_flush;
}
#endif
//
// Let the channel update the split store
// and notify the agent in the case of a
// cache hit.
//
if (store -> enableSplit)
{
handleSplit(encodeBuffer, store, store -> lastAction,
store -> lastHit, opcode, buffer, size);
}
return 1;
}
//
// A similar message could not be found in
// cache or message must be discarded. Must
// transmit the message using the field by
// field differential encoding.
//
handleEncodeIdentity(encodeBuffer, channelCache,
store, buffer, size, bigEndian_);
//
// Check if message has a distinct data part.
//
if (store -> enableData)
{
//
// If message split was requested by agent then send data
// out-of-band, dividing it in small chunks. Until message
// is completely transferred, keep in the split store a
// dummy version of the message, with data replaced with a
// pattern.
//
// While data is being transferred, agent should have put
// the resource (for example its client) asleep. It can
// happen, though, that a different client would reference
// the same message. We cannot issue a cache hit for images
// being split (such images are put in store in 'incomplete'
// state), so we need to handle this case.
//
if (store -> enableSplit == 1)
{
//
// Let the channel decide what to do with the
// message. If the split can't take place be-
// cause the split store is full, the channel
// will tell the remote side that the data is
// going to follow.
//
if (handleSplit(encodeBuffer, store, store -> lastAction,
(store -> lastAction == IS_ADDED ? store -> lastAdded : 0),
opcode, buffer, size) == 1)
{
#ifdef MONITOR
diff = encodeBuffer.getBits() - bits;
if (*(buffer) == MONITOR_MAJOR_OPCODE &&
(MONITOR_MINOR_OPCODE == -1 || *(buffer + 1) == MONITOR_MINOR_OPCODE))
{
totalMessages++;
totalBits += diff;
*logofs << "handleEncode: Handled split OPCODE#" << (unsigned int) *(buffer)
<< " MINOR#" << (unsigned int) *(buffer + 1) << ". " << size
<< " bytes in, " << diff << " bits (" << ((float) diff) / 8
<< " bytes) out. Average " << totalBits / totalMessages
<< "/1.\n" << logofs_flush;
}
#endif
return 0;
}
}
//
// The split did not take place and we are going
// to transfer the data part. Check if the static
// compression of the data section is enabled.
// This is the case of all messages not having a
// special differential encoding or messages that
// we want to store in cache in compressed form.
//
unsigned int offset = store -> identitySize(buffer, size);
if (store -> enableCompress)
{
unsigned char *data = NULL;
unsigned int dataSize = 0;
int compressed = handleCompress(encodeBuffer, opcode, offset,
buffer, size, data, dataSize);
if (compressed < 0)
{
return -1;
}
else if (compressed > 0)
{
//
// Update the size of the message according
// to the result of the data compression.
//
handleUpdate(store, size - offset, dataSize);
}
}
else
{
handleCopy(encodeBuffer, opcode, offset, buffer, size);
}
}
#ifdef MONITOR
diff = encodeBuffer.getBits() - bits;
if (*(buffer) == MONITOR_MAJOR_OPCODE &&
(MONITOR_MINOR_OPCODE == -1 || *(buffer + 1) == MONITOR_MINOR_OPCODE))
{
totalMessages++;
totalBits += diff;
*logofs << "handleEncode: Handled OPCODE#" << (unsigned int) *(buffer)
<< " MINOR#" << (unsigned int) *(buffer + 1) << ". " << size
<< " bytes in, " << diff << " bits (" << ((float) diff) / 8
<< " bytes) out. Average " << totalBits / totalMessages
<< "/1.\n" << logofs_flush;
}
#endif
return 0;
}
int Channel::handleDecode(DecodeBuffer &decodeBuffer, ChannelCache *channelCache,
MessageStore *store, unsigned char &opcode,
unsigned char *&buffer, unsigned int &size)
{
//
// Check first if the message is in the
// message store.
//
unsigned int split = 0;
if (handleDecodeCached(decodeBuffer, channelCache,
store, buffer, size) == 1)
{
//
// Let the channel update the split store
// in the case of a message being cached.
//
if (store -> enableSplit == 1)
{
if (control -> isProtoStep7() == 1)
{
#ifdef DEBUG
*logofs << "handleDecode: " << store -> name()
<< ": Checking if the message was split.\n"
<< logofs_flush;
#endif
decodeBuffer.decodeBoolValue(split);
}
if (split == 1)
{
handleSplit(decodeBuffer, store, store -> lastAction,
store -> lastHit, opcode, buffer, size);
handleCleanAndNullRequest(opcode, buffer, size);
}
}
return 1;
}
//
// Decode the full identity.
//
handleDecodeIdentity(decodeBuffer, channelCache, store, buffer,
size, bigEndian_, &writeBuffer_);
//
// Check if the message has a distinct
// data part.
//
if (store -> enableData)
{
//
// Check if message has been split.
//
if (store -> enableSplit)
{
#ifdef DEBUG
*logofs << "handleDecode: " << store -> name()
<< ": Checking if the message was split.\n"
<< logofs_flush;
#endif
decodeBuffer.decodeBoolValue(split);
if (split == 1)
{
//
// If the message was added to the store,
// create the entry without the data part.
//
handleSaveSplit(store, buffer, size);
handleSplit(decodeBuffer, store, store -> lastAction,
(store -> lastAction == IS_ADDED ? store -> lastAdded : 0),
opcode, buffer, size);
handleCleanAndNullRequest(opcode, buffer, size);
return 0;
}
}
//
// Decode the data part.
//
unsigned int offset = store -> identitySize(buffer, size);
if (store -> enableCompress)
{
const unsigned char *data = NULL;
unsigned int dataSize = 0;
int decompressed = handleDecompress(decodeBuffer, opcode, offset,
buffer, size, data, dataSize);
if (decompressed < 0)
{
return -1;
}
else if (decompressed > 0)
{
//
// The message has been transferred
// in compressed format.
//
handleSave(store, buffer, size, data, dataSize);
if (store -> enableSplit)
{
if (split == 1)
{
handleSplit(decodeBuffer, store, store -> lastAction,
(store -> lastAction == IS_ADDED ? store -> lastAdded : 0),
opcode, buffer, size);
handleCleanAndNullRequest(opcode, buffer, size);
}
}
return 0;
}
}
else
{
//
// Static compression of the data part
// was not enabled for this message.
//
handleCopy(decodeBuffer, opcode, offset, buffer, size);
}
}
//
// The message doesn't have a data part
// or the data was not compressed.
//
handleSave(store, buffer, size);
if (store -> enableSplit)
{
if (split == 1)
{
handleSplit(decodeBuffer, store, store -> lastAction,
(store -> lastAction == IS_ADDED ? store -> lastAdded : 0),
opcode, buffer, size);
handleCleanAndNullRequest(opcode, buffer, size);
}
}
return 0;
}
int Channel::handleEncodeCached(EncodeBuffer &encodeBuffer, ChannelCache *channelCache,
MessageStore *store, const unsigned char *buffer,
const unsigned int size)
{
if (control -> LocalDeltaCompression == 0 ||
enableCache_ == 0 || store -> enableCache == 0)
{
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(is_discarded,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(is_discarded,
store -> lastActionCacheCompat);
}
store -> lastAction = is_discarded;
return 0;
}
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name()
<< ": Going to handle a new message of this class.\n"
<< logofs_flush;
#endif
//
// Check if the estimated size of cache is greater
// than the requested limit. If it is the case make
// some room by deleting one or more messages.
//
int position;
while (mustCleanStore(store) == 1 && canCleanStore(store) == 1)
{
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name()
<< ": Trying to reduce size of message store.\n"
<< logofs_flush;
#endif
position = store -> clean(use_checksum);
if (position == nothing)
{
#ifdef TEST
*logofs << "handleEncodeCached: " << store -> name()
<< ": WARNING! No message found to be "
<< "actually removed.\n" << logofs_flush;
#endif
break;
}
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name()
<< ": Message at position " << position
<< " will be removed.\n" << logofs_flush;
#endif
//
// Encode the position of message to
// be discarded.
//
store -> lastRemoved = position;
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(is_removed, store -> lastRemoved,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(is_removed,
store -> lastActionCacheCompat);
encodeBuffer.encodePositionValueCompat(store -> lastRemoved,
store -> lastRemovedCacheCompat);
}
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name() << ": Going to "
<< "clean up message at position " << position << ".\n"
<< logofs_flush;
#endif
store -> remove(position, use_checksum, discard_data);
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name() << ": There are "
<< store -> getSize() << " messages in the store out of "
<< store -> cacheSlots << " slots.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of store is " << store -> getLocalStorageSize()
<< " bytes locally and " << store -> getRemoteStorageSize()
<< " bytes remotely.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of total cache is " << store -> getLocalTotalStorageSize()
<< " bytes locally and " << store -> getRemoteTotalStorageSize()
<< " bytes remotely.\n" << logofs_flush;
#endif
}
#ifdef DEBUG
if (mustCleanStore(store) == 1 && canCleanStore(store) == 0)
{
*logofs << "handleEncodeCached: " << store -> name()
<< ": Store would need a clean but operation will be delayed.\n"
<< logofs_flush;
*logofs << "handleEncodeCached: " << store -> name() << ": There are "
<< store -> getSize() << " messages in the store out of "
<< store -> cacheSlots << " slots.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of store is " << store -> getLocalStorageSize()
<< " bytes locally and " << store -> getRemoteStorageSize()
<< " bytes remotely.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of total cache is " << store -> getLocalTotalStorageSize()
<< " bytes locally and " << store -> getRemoteTotalStorageSize()
<< " bytes remotely.\n" << logofs_flush;
}
#endif
//
// If 'on the wire' size of message exceeds the
// allowed limit then avoid to store it in the
// cache.
//
if (store -> validateMessage(buffer, size) == 0)
{
#ifdef TEST
*logofs << "handleEncodeCached: " << store -> name()
<< ": Message with size " << size << " ignored.\n"
<< logofs_flush;
#endif
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(is_discarded,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(is_discarded,
store -> lastActionCacheCompat);
}
store -> lastAction = is_discarded;
return 0;
}
//
// Fill the message object with the
// received data.
//
Message *message = store -> getTemporary();
if (message == NULL)
{
#ifdef PANIC
*logofs << "handleEncodeCached: " << store -> name()
<< ": PANIC! Can't allocate memory for "
<< "a new message.\n" << logofs_flush;
#endif
cerr << "Error" << ": Can't allocate memory for "
<< "a new message in context [D].\n";
HandleCleanup();
}
//
// As we are at encoding side, it is enough to store the
// checksum for the object while data can be erased. Both
// the identity and the data will never be sent through
// the wire again as long as they are stored in the cache
// at the decoding side. The split parameter is always
// set to 0 as the data will not be stored in any case.
//
store -> parse(message, 0, buffer, size, use_checksum,
discard_data, bigEndian_);
#ifdef DUMP
store -> dump(message);
#endif
//
// Search the object in the message
// store. If found get the position.
//
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name()
<< ": Searching object of size " << size
<< " in the cache.\n" << logofs_flush;
#endif
int added;
int locked;
position = store -> findOrAdd(message, use_checksum,
discard_data, added, locked);
if (position == nothing)
{
#ifdef WARNING
*logofs << "handleEncodeCached: " << store -> name()
<< ": WARNING! Can't store object in the cache.\n"
<< logofs_flush;
#endif
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(is_discarded,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(is_discarded,
store -> lastActionCacheCompat);
}
store -> lastAction = is_discarded;
return 0;
}
else if (locked == 1)
{
//
// We can't issue a cache hit. Encoding identity
// differences while message it's being split
// would later result in agent to commit a wrong
// version of message.
//
#ifdef WARNING
*logofs << "handleEncodeCached: " << store -> name()
<< ": WARNING! Message of size " << store -> plainSize(position)
<< " at position " << position << " is locked.\n"
<< logofs_flush;
#endif
cerr << "Warning" << ": Message of size " << store -> plainSize(position)
<< " at position " << position << " is locked.\n";
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(is_discarded,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(is_discarded,
store -> lastActionCacheCompat);
}
store -> lastAction = is_discarded;
return 0;
}
else if (added == 1)
{
store -> resetTemporary();
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name() << ": Message of size "
<< store -> plainSize(position) << " has been stored at position "
<< position << ".\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name() << ": There are "
<< store -> getSize() << " messages in the store out of "
<< store -> cacheSlots << " slots.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of store is " << store -> getLocalStorageSize()
<< " bytes locally and " << store -> getRemoteStorageSize()
<< " bytes remotely.\n" << logofs_flush;
*logofs << "handleEncodeCached: " << store -> name()
<< ": Size of total cache is " << store -> getLocalTotalStorageSize()
<< " bytes locally and " << store -> getRemoteTotalStorageSize()
<< " bytes remotely.\n" << logofs_flush;
#endif
//
// Inform the decoding side that message
// must be inserted in cache and encode
// the position where the insertion took
// place.
//
store -> lastAction = IS_ADDED;
store -> lastAdded = position;
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(IS_ADDED, store -> lastAdded,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(IS_ADDED,
store -> lastActionCacheCompat);
encodeBuffer.encodePositionValueCompat(store -> lastAdded,
store -> lastAddedCacheCompat);
}
return 0;
}
else
{
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name()
<< ": Cache hit. Found object at position "
<< position << ".\n" << logofs_flush;
#endif
//
// Must abort the connection if the
// the position is invalid.
//
Message *cachedMessage = store -> get(position);
//
// Increase the rating of the cached
// message.
//
store -> touch(cachedMessage);
#ifdef DEBUG
*logofs << "handleEncodeCached: " << store -> name() << ": Hits for "
<< "object at position " << position << " are now "
<< store -> getTouches(position) << ".\n"
<< logofs_flush;
#endif
//
// Send to the decoding side position
// where object can be found in cache.
//
store -> lastAction = IS_HIT;
store -> lastHit = position;
if (control -> isProtoStep7() == 1)
{
encodeBuffer.encodeActionValue(IS_HIT, store -> lastHit,
store -> lastActionCache);
}
else
{
encodeBuffer.encodeActionValueCompat(IS_HIT,
store -> lastActionCacheCompat);
encodeBuffer.encodePositionValueCompat(store -> lastHit,
store -> lastHitCacheCompat);
}
//
// Send the field by field differences in
// respect to the original message stored
// in cache.
//
store -> updateIdentity(encodeBuffer, message, cachedMessage, channelCache);
return 1;
}
}
void Channel::handleUpdateAdded(MessageStore *store, unsigned int dataSize,
unsigned int compressedDataSize)
{
#ifdef TEST
if (store -> lastAction != IS_ADDED)
{
#ifdef PANIC
*logofs << "handleUpdateAdded: " << store -> name()
<< ": PANIC! Function called for action '"
<< store -> lastAction << "'.\n"
<< logofs_flush;
#endif
cerr << "Error" << ": Update function called for "
<< "store '" << store -> name() << "' with "
<< "action '" << store -> lastAction
<< "'.\n";
HandleCleanup();
}
#endif
#ifdef DEBUG
*logofs << "handleUpdateAdded: " << store -> name() << ": Updating "
<< "object at position " << store -> lastAdded << " of size "
<< store -> plainSize(store -> lastAdded) << " (" << dataSize
<< "/" << compressedDataSize << ").\n" << logofs_flush;
#endif
store -> updateData(store -> lastAdded, dataSize, compressedDataSize);
#ifdef DEBUG
*logofs << "handleUpdateAdded: " << store -> name() << ": There are "
<< store -> getSize() << " messages in the store out of "
<< store -> cacheSlots << " slots.\n" << logofs_flush;
*logofs << "handleUpdateAdded: " << store -> name()
<< ": Size of store is " << store -> getLocalStorageSize()
<< " bytes locally and " << store -> getRemoteStorageSize()
<< " bytes remotely.\n" << logofs_flush;
*logofs << "handleUpdateAdded: " << store -> name()
<< ": Size of total cache is " << store -> getLocalTotalStorageSize()
<< " bytes locally and " << store -> getRemoteTotalStorageSize()
<< " bytes remotely.\n" << logofs_flush;
#endif
}
int Channel::handleDecodeCached(DecodeBuffer &decodeBuffer, ChannelCache *channelCache,
MessageStore *store, unsigned char *&buffer,
unsigned int &size)
{
//
// Create a new message object and
// fill it with received data.
//
#ifdef DEBUG
*logofs << "handleDecodeCached: " << store -> name()
<< ": Going to handle a new message of this class.\n"
<< logofs_flush;
#endif
//
// Decode bits telling how to handle
// this message.
//
unsigned char action;
unsigned short int position;
if (control -> isProtoStep7() == 1)