-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2PeerMsg.cpp
More file actions
1966 lines (1803 loc) · 61.3 KB
/
Copy pathP2PeerMsg.cpp
File metadata and controls
1966 lines (1803 loc) · 61.3 KB
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 © 2002-2011, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//
// P2PeerMsg implementation
// NOTES: Container that facilitates the asychronous exchange of
// messages between P2PeerHub's and subsequent routing through
// the registered ID_SPIN_LBPeriodsies of derivered P2PeerTarget's
// : These object's are not designed to be thread or context
// safe and as such reference MUST be isolated
//
#include "stdafx.h"
#include "P2PeerMsg.h"
#include "Msgexception.h"
///////////////////////////////////////////////////////////////////////
// Construction and destruction
std::atomic<UINT> g_P2PeerMsgInstances = 0; // atomic ++/-- (TSan Risk #3)
//
// Constructors and destructor
//
// Parameters: P2PeerMsg& rhs
// Copy constructor reference
//
// const void *pvMessage
// Complete message, including P2PeerMsgHdr
//
// short iMsgSize
// Message size
P2PeerMsg::P2PeerMsg ( )
// : P3PmsgBSTR ( VBLock_Addr16, 2048 )
{
// Firstly
RenderThisSafe ( );
Init ( 0, AllocBLOB08(sizeof(P2PeerMsgPrefix)) );
}
P2PeerMsg::P2PeerMsg ( const P2PeerMsg& rhs )
: P3PmsgBSTR( rhs )
{
// Firstly
RenderThisSafe ( );
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
}
P2PeerMsg::P2PeerMsg ( P2PmsgHANDLE hVBList )
: P3PmsgBSTR ( hVBList )
{
// Firstly
RenderThisSafe ( );
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
ASSERT(0);
}
P2PeerMsg::P2PeerMsg ( UCHAR uVBLockAddr, VBLsize nSizeof )
: P3PmsgBSTR ( uVBLockAddr, nSizeof )
{
// Firstly
RenderThisSafe ( );
r_data() = AllocBLOB08(sizeof(P2PeerMsgPrefix));
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
}
/*P2PeerMsg::P2PeerMsg ( const void *pvMsgImage, P2Psize_t iMsgImageSize )
{
// Firstly
RenderThisSafe ( );
//// Allocate memory
//// NOTES: This is a special case since the whole message is
//// being passed. Not just the data component
ASSERT(0);
// Parallel image
//P2PmsgBSTR *pBSTR =(P2PmsgBSTR *)pvMsgImage;
//m_pP2PmsgBSTR_Heap = MakeBSTR ( pBSTR->iSize );
//memcpy ( m_pP2PmsgBSTR_Heap, pvMsgImage, iMsgImageSize );
// Propagation
//Connect ( m_pP2PmsgBSTR_Heap);
//SetFragmented ( );
}*/
P2PeerMsg::P2PeerMsg ( const VBListIOmage& oIOmage )
: P3PmsgBSTR( oIOmage )
{
// Firstly
//RenderThisSafe ( );
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
}
P2PeerMsg::P2PeerMsg ( VBListIOmage *pIOmage )
: P3PmsgBSTR( pIOmage )
{
// Firstly
RenderThisSafe ( );
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
}
P2PeerMsg::P2PeerMsg ( VBListIOmage *pIOmage, VBLsize nBufferLen )
: P3PmsgBSTR( pIOmage, nBufferLen )
{
// The constructor above with the buffer extent supplied, for images that
// came off a wire rather than out of this process. Everything else about
// it is identical; what the length buys is that the two IOMAGE block walks
// run as a GATE -- in every build, refusing rather than asserting -- which
// is what the pointer-only form cannot do because it does not know where
// the image ends. See P2Peerio::RecvP2PeerMsg stage 4.
RenderThisSafe ( );
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
}
P2PeerMsg::P2PeerMsg ( P2PmsgID pszMsgID )
: P3PmsgBSTR( pszMsgID, AllocBLOB08(sizeof(P2PeerMsgPrefix)) )
{
// Firstly
RenderThisSafe();
//Init ( pszMsgID, AllocBLOB08(sizeof(P2PeerMsgPrefix)) );
// Addressing
InitItem ( VBLockBSTR_NET,P3PmsgData() );
// Data
InitItem ( VBLockBSTR_MSG,P3PmsgData() );
}
P2PeerMsg::P2PeerMsg ( P2PaddrSTR oSrcSTR, P2PaddrSTR oDstSTR
, P2PmsgID pszMsg
, const void *pvMsgData, P2Psize_t iMsgDataSize )
: P3PmsgBSTR( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) )
{
// Firstly
RenderThisSafe();
//Init ( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) );
// Addressing
InitItem(VBLockBSTR_NET,P3PmsgData());
SetSource ( P3PmsgField ( TMsg_Src, DataWSTR08(oSrcSTR) ) );
SetDestin ( P3PmsgField ( TMsg_Dst, DataWSTR08(oDstSTR) ) );
// Data
// NOTES: Passed data is placed in the data node header
if ( iMsgDataSize <= 255 )
InitItem ( VBLockBSTR_MSG
, P3PmsgData(pvMsgData,iMsgDataSize,VBLockData_BLOB08) );
else
InitItem ( VBLockBSTR_MSG
, P3PmsgData(pvMsgData,iMsgDataSize,VBLockData_BLOB16) );
}
P2PeerMsg::P2PeerMsg ( const P3PmsgField& oSource, const P3PmsgField& oDestin
, P2PmsgID pszMsg
, const void *pvMsgData, P2Psize_t iMsgDataSize )
: P3PmsgBSTR( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) )
{
// Firstly
RenderThisSafe();
//Init ( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) );
// Addressing
InitItem(VBLockBSTR_NET,P3PmsgData());
SetSource ( oSource );
SetDestin ( oDestin );
// Data
// NOTES: Passed data is placed in the data node header
if ( iMsgDataSize <= 255 )
InitItem ( VBLockBSTR_MSG
, P3PmsgData(pvMsgData,iMsgDataSize,VBLockData_BLOB08) );
else
InitItem ( VBLockBSTR_MSG
, P3PmsgData(pvMsgData,iMsgDataSize,VBLockData_BLOB16) );
}
P2PeerMsg::P2PeerMsg ( P2PaddrSTR pSrcSTR, P2PaddrSTR pDstSTR
, P2PmsgID pszMsg
, LPCTSTR lpszMsgFormat, ... )
: P3PmsgBSTR( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) )
// : P2PmsgItem ( pszMsg, P2PmsgVT_STR, lpszMsgFormat, 0 )
{
// Introduce locals
va_list ap; // Variable argument list.
TCHAR szMsgData[2048];
P2Psize_t iMsgDataSize = 0;
va_start ( ap, lpszMsgFormat );
if ( lpszMsgFormat )
iMsgDataSize = (P2Psize_t)_vstprintf_s ( szMsgData, ARRAYSIZE(szMsgData), lpszMsgFormat, ap );
szMsgData[iMsgDataSize++] = 0;
szMsgData[iMsgDataSize++] = 0;
va_end ( ap );
// Firstly
RenderThisSafe();
//Init ( pszMsg, AllocBLOB08(sizeof(P2PeerMsgPrefix)) );
// Addressing
P3PmsgItem& oItemAddr = InitItem(VBLockBSTR_NET,P3PmsgData());
oItemAddr += P3PmsgField ( TMsg_Src, DataWSTR08(pSrcSTR) );
oItemAddr += P3PmsgField ( TMsg_Dst, DataWSTR08(pDstSTR) );
// Data
// NOTES: Passed data is placed in the data header
if ( iMsgDataSize <= 255 )
InitItem ( VBLockBSTR_MSG
, P3PmsgData(szMsgData,iMsgDataSize,VBLockData_BSTR08) );
else
InitItem ( VBLockBSTR_MSG
, P3PmsgData(szMsgData,iMsgDataSize,VBLockData_BSTR16) );
}
P2PeerMsg::~P2PeerMsg ()
{
// Garbage collection
delete m_pP2PmsgWrap;
delete m_pstrVisualSummary;
g_P2PeerMsgInstances--;
}
void
P2PeerMsg::RenderThisSafe ( )
{
// Attributes
//m_pP2PmsgWrap = 0;
//m_pP2PmsgParent = 0;
//m_pstrVisualSummary = 0;
g_P2PeerMsgInstances++;
// Dedicated VBList
ASSERT(sizeof(P2PeerMsgPrefix)<255);
//r_data() = P3PmsgData ( (void *)0, sizeof(P2PeerMsgPrefix), VBLockData_BLOB08 );
}
void
P2PeerMsg::ResetThisObject ( )
{
// Garbage collection
delete m_pP2PmsgWrap;
m_pP2PmsgWrap = 0;
// Delegation
r_item(0).DESC.Truncate();
// Attributes
m_pP2PmsgParent = 0;
}
///////////////////////////////////////////////////////////////////////
// Overloaded Operators
//
// operator = () overload
//
// Parameters: const P2PeerMsg& rhs
// Right hand side of expression.
//
// Returns: P2PeerMsg&
// Reference to self.
//
P2PeerMsg&
P2PeerMsg::operator = ( const P2PeerMsg& rhs )
{
// Optimise
if ( this == &rhs )
return *this;
ResetThisObject ( );
// Snapshot and base class delegation
(P3PmsgBSTR&)*this = rhs;
// Tidy up and
//SetFragmented ( );
return *this;
}
//
// operator [] overload
// Recursively exposes wrapped messages
//
// Parameters: int e
// Index of recursively wrapped message to be exposed
// NOTES: Throws an exception should recursion level
// not exist
//
// Returns: P2PeerMsg&
// Wrapped message reference
//
P2PeerMsg&
P2PeerMsg::operator [] ( int e )
{
// Self reference
if ( e == 0 )
return *this;
// To be sure, to be sure
if ( e < 0 ||
e > MAX_P2PeerMsg_Index )
EVERR->MODULE->AFP(e)
->Message(_T("Invalid wrapped message index=%i (0 to %i)"), e, MAX_P2PeerMsg_Index )
->Group("P2P")->Throw();
if ( !IsWrapped() )
EVERR->MODULE->AFP(e)
->Message_T("P2PeerMsg not wrapped" )
->Advice_T ("Use IsWrapped() as pre-check")
->Throw();
// Unwrapping
if ( m_pP2PmsgWrap == 0 )
{
VBListIOmage *pIOmage = (VBListIOmage *)r_item(VBLockBSTR_WRP).r_data().c_vBlob();
m_pP2PmsgWrap = new P2PeerMsg ( *pIOmage );
}
// Recursively delegate
return (*m_pP2PmsgWrap)[e-1];
}
///////////////////////////////////////////////////////////////////////
// Factories
// NOTES: All methods return pointers to P2PeerMsg objects for which
// the client becomes responsible for the life cycle
//
// Constructs a response message from the encapsulated message
// NOTES: Clones address properties of the contained message then swaps
// the source and destination addresses.
//
// Parameters: P2PmsgID pszMsg
// Response message type
//
// const void *pvMsgData
// Message data
//
// short iMsgDataSize
// Message data size.
//
// Returns: P2PeerMsg*
// Response P2PeerMsg pointer. Client becomes
// responsible for life cycle.
//
P2PeerMsg*
P2PeerMsg::ResponseFactory ( P2PmsgID pszMsg
, const void *pvMsgData, P2Psize_t iMsgDataSize )
{
// Build the response message
// NOTES: Perform raw prefix copy
P2PeerMsgSP spMsg = new P2PeerMsg ( r_Destin(), r_Source()
, pszMsg, pvMsgData, iMsgDataSize );
ASSERT(!P2PeerMsg_IsPosted(spMsg.p_SafePtr()));
// Data
spMsg -> r_data() = r_data();
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)spMsg->r_data().c_vBlob();
pPrefix->uiState &= ~P2PeerMsgState_Posted;
ASSERT(!P2PeerMsg_IsPosted(spMsg.p_SafePtr()));
// Tidy up, and
return spMsg.Dereference();
}
//
// Builds a redirection message from this message.
//
//
// Parameters: P2PaddrSTR strP2Paddr
// Redirected P2PeerMsg destination address
//
//
// Returns: P2PeerMsg*
// Pointer to redirection message
// NOTES: Client becomes responsible for life cycle
//
P2PeerMsg*
P2PeerMsg::RedirectFactory ( P2PaddrSTR strDestin )
{
// Create an image and assign redirection address.
// NOTES: DESTINATION ONLY. TMsg_Scp - what the origin addressed this to -
// is inherited by the image untouched, and MUST be: overwriting it
// here is precisely the defect this field exists to undo, because
// every caller of this function is re-addressing a copy the origin
// never saw. Refer TMsg_Scp in the header
P2PeerMsg *pMsg = new P2PeerMsg ( *this );
pMsg -> SetDestin ( strDestin );
//LPCTSTR lpszSource=pMsg->GetSource();
//LPCTSTR lpszDestin=pMsg->GetDestin();
//LPCTSTR lpszMsg=pMsg->c_name();
// Then
ASSERT(pMsg->Map_MatchName(c_name()));//DELETE-ME
ASSERT(!IsWrapped()||(*this)[1].Map_MatchName((*pMsg)[1].c_name()));
return pMsg;
}
P2PeerMsg*
P2PeerMsg::RedirectFactory ( P2PaddrSTR strP2PaddrDst
, P2PmsgID pszMsg
, const void *pvMsgData, P2Psize_t iMsgDataSize )
{
// Simply
ASSERT(wcslen(pszMsg)>0);//DELETE-ME
return new P2PeerMsg ( GetSource(), strP2PaddrDst
, pszMsg
, pvMsgData, iMsgDataSize );
}
//
// Builds a wrapped response message from the encapsulated message.
//
//
// Parameters: P2Pmsg_t nMsg
// Type of wrapped response
//
// void *pvMsgData
// Data pointer, Null value handled.
//
// short iDataSize
// Size of data area in wrapped message. Size
// of wrapped message is exclusive to this value.
//
//
// Returns: P2PeerMsg*
// Pointer to wrapped response message
//
P2PeerMsg*
P2PeerMsg::WrappedResponseFactory ( P2PmsgID pszMsg
, const void *pvMsgData, P2Psize_t iDataSize )
{
// Build a wrapped message
// NOTES: Add an extra bit on for the size of this message
// : Message source and destinations are reversed.
// : COPIES, not the pointers the accessors return. GetDestin(),
// GetSource() and c_name() all end in c_wstr()/c_name(), which off
// Windows hands back one of 16 ROTATING thread-local scratch buffers
// - valid only until the next accessor call on this thread. The
// constructor below consumes its arguments one at a time and makes
// several such calls between them, so by the time the second was
// read its slot had been recycled: measured on Linux, the response
// to an undeliverable message was addressed to "Src" - the field
// NAME - instead of to the sender. It was therefore undeliverable
// itself and was dropped, so on that platform an undeliverable
// message was NEVER reported back to whoever sent it. Invisible on
// Windows, where the accessor aims straight at the stored characters
// : Same defect and same fix as P2PeerHub::RouteP2PeerMsg, which
// records the sizes it grew to at P2PeerHub.cpp:715-725. A test
// that asserts the report ARRIVES is what finally reached this one
//Print(stdout);
const CString strDestin = GetDestin ( );
const CString strSource = GetSource ( );
P2PeerMsgSP spMsg = new P2PeerMsg ( (P2PaddrSTR)strDestin, (P2PaddrSTR)strSource
, pszMsg, pvMsgData, iDataSize );
//if ( GetP2Pmsgnn() == VBLock_Addr16 )
// spMsg = new P2PeerMsg16 ( GetDestin(), GetSource(), pszMsg, pvMsgData, iDataSize );
//if ( GetP2Pmsgnn() == VBLock_Addr32 )
// spMsg = new P2PeerMsg32 ( GetDestin(), GetSource(), pszMsg, pvMsgData, iDataSize );
//else
// spMsg = new P2PeerMsg64 ( GetDestin(), GetSource(), pszMsg, pvMsgData, iDataSize );
spMsg -> r_data() = r_data();
P2PeerMsgPrefix *pPrefix0 = (P2PeerMsgPrefix *)(spMsg->r_data().c_vBlob());
pPrefix0 -> uiState &= ~P2PeerMsgState_Posted;
//spMsg -> InitItem ( VBLockBSTR_WRP, P3PmsgData() );
//spMsg -> r_item ( VBLockBSTR_WRP) += r_item ( 0 );
// Copy in details of the wrapped message
// TODO:LJM This can be optimised
// NOTES: BOUNDED. The response embeds the whole of this message, so it is
// always larger than what it reports on - and it is built in a
// 16-bit addressed message heap (65535 bytes). Unbounded, a message
// of about 30 KB - well inside what a stock peer may send, since
// P2Peerio::m_dwMaxRecvSize defaults to 32768 - made the heap throw
// "Internal VBHeapRoot.uVBLockAddr=1 corruption" from
// VBHeapRoot_SetFree() on the routing hub's pump thread, and NO
// report went out at all. See MAX_P2PmsgWrapEmbed for the
// measurements and for why the bound is half of MAX_P2Psize
// : Over the bound the body is ELIDED, not truncated. A truncated
// IOMAGE is a corrupt message, and handing the parser one of those
// is the defect class this tree has spent sessions closing. The
// stub is a VALID message carrying the original name, source and
// destination, so IsWrapped(), UnwrapFactory() and the stock
// On_MsgCatch handler all behave exactly as they did
PrepareP2Piomage ( ~(DWORD)0 );
const UINT nP2PiomageSize = P2PiomageSize ( );
if ( nP2PiomageSize <= (UINT)MAX_P2PmsgWrapEmbed )
{
P3PmsgData oData ( (void *)P2Piomage(), nP2PiomageSize, VBLockData_BLOB16 );
ReleaseP2Piomage ( );
spMsg -> InitItem ( VBLockBSTR_WRP, oData );
}
else
{
ReleaseP2Piomage ( );
// Snapshots again, for the reason given at the top of this function: the
// name and the two addresses are all ring pointers off Windows, and each
// call below spends slots before the previous value has been copied out
const CString strName = c_name ( );
P2PeerMsg oStub ( (P2PmsgID)strName );
oStub.SetSource ( (P2PaddrSTR)strSource );
oStub.SetDestin ( (P2PaddrSTR)strDestin );
oStub.PrepareP2Piomage ( ~(DWORD)0 );
P3PmsgData oData ( (void *)oStub.P2Piomage(), oStub.P2PiomageSize()
, VBLockData_BLOB16 );
oStub.ReleaseP2Piomage ( );
spMsg -> InitItem ( VBLockBSTR_WRP, oData );
// Traced, not raised. A message too big to quote is a routine
// consequence of its size, not an error in its own right - and the
// caller is in the middle of reporting a DIFFERENT failure, so throwing
// here would lose that report as well. (IsEVTRC lives in P2Pwin32.h,
// which this TU deliberately does not pull in; the event is cheap beside
// a >16 KB message and this branch is not on any hot path.)
EVTRC->MODULE
->Message("Response body elided: the wrapped message is %u bytes, "
"over the %u-byte embedding bound"
, nP2PiomageSize, (UINT)MAX_P2PmsgWrapEmbed )
->Advice_T("The response carries the message's name and addresses "
"but not its payload")
->Group("P2P")
->Cancel();
}
P2PeerMsgPrefix *pPrefix1 = (P2PeerMsgPrefix *)((*spMsg)[1].r_data().c_vBlob());
pPrefix1 -> uiState &= ~P2PeerMsgState_Posted;
// Copy in details of the wrapped message
//spMsg -> m_pP2PmsgWrap = new P2PeerMsg ( *this );
// Then
return spMsg.Dereference();
}
//
// Constructs a reflection message from the encapsulated message
// NOTES: Simply clones a copy of the contained message and swaps
// the source and destination addresses.
// : The reflection state of the manufactured message is
// inverted. Normal messages are manufactured from reflected
// messages and reflected state messages are manufactured
// from normal messages
//
// Returns: P2PeerMsg*
// Pointer to reflection message. Client becomes
// responsible for life cycle.
//
P2PeerMsg*
P2PeerMsg::ReflectFactory ( ) const
{
// Build the response message
// NOTES: Source and destination addresses are swapped. Content
// is preserved
P2PeerMsgSP spMsg = new P2PeerMsg ( *this );
spMsg -> SetSource ( r_Destin() );
spMsg -> SetDestin ( r_Source() );
// Swap reflected state
// NOTES: Normal messages become reflected and reflected
// messages become normal
// : Refer ON_P2PeerMsg and ON_P2PeerMsg_CATCH
// for intercepting reflected-reflected (normal) P2PeerMsg's
// : Refer ON_P2PeerMsg_REFLECT and ON_P2PeerMsg_REFLECT_CATCH
// for intercepting reflected-normal P2PeerMsg's
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)spMsg->r_data().c_vBlob();
if ( (pPrefix->nMsg&P2P_Reflected) == P2P_Reflected )
pPrefix -> nMsg ^= P2P_Reflected;
else
pPrefix -> nMsg |= P2P_Reflected;
// Then
return spMsg.Dereference();
}
//
// Constructs a loopback message from the encapsulated message
// NOTES: Simply clones a copy of the contained message and swaps
// the source and destination addresses.
// : Under some circumstances P2PeerHub's may exchange the
// same message (P2PmsgPing or P2PmsgPoll etc) to accomplish
// a particular outcome
//
// Returns: P2PeerMsg*
// Loopback P2PeerMsg pointer. Client becomes
// responsible for life cycle.
//
P2PeerMsg*
P2PeerMsg::LoopbackFactory ( ) const
{
P2PeerMsgSP spMsg = new P2PeerMsg ( *this );
spMsg -> SetSource ( r_Destin() );
spMsg -> SetDestin ( r_Source() );
return spMsg.Dereference();
}
//
// Wraps message for reflection in nominated message type
//
// Parameters: P2Pmsg_t nMsg
// Type of message contents are wrapped into.
// NOTES: This is the outer message that must be
// unwrapped at the destination.
//
// void *pvMsgData
// Data pointer. Null value handled.
//
// short iMsgDataSize
// Size of message data area. This is in addition
// to the wrapped message.
//
// Returns: P2PeerMsg*
// Pointer to unwrapped message. Client becomes
// responsible for life cycle.
//
P2PeerMsg*
P2PeerMsg::WrapFactory ( P2PmsgID szMsg
, const void *pvMsgData, P2Psize_t iDataSize )
{
// Build a wrapped message
// NOTES: Add an extra bit on for the size of this message
// : Message source and destinations are reversed.
P2PeerMsgSP spMsg
= new P2PeerMsg ( GetDestin(), GetSource()
, szMsg
, pvMsgData, iDataSize );
spMsg -> r_data() = r_data();
// Copy in details of the wrapped message
// TODO:LJM This can be optimised
PrepareP2Piomage ( ~(DWORD)0 );
//UINT nSizeIOmage=this->IOmageSize();
P3PmsgData oData ( (void *)P2Piomage(), P2PiomageSize(), VBLockData_BLOB16 );
//UINT nSizeData=oData.Sizeof();
ReleaseP2Piomage ( );
spMsg -> InitItem ( VBLockBSTR_WRP, oData );
// and simply
return spMsg.Dereference();
}
//P2PeerMsg*
//P2PeerMsg::WrapFactory ( P2PmsgID pszMsg
// , const void *pvMsgData, P2Psize_t iDataSize )
//{
// // Build a wrapped message
// // NOTES: Add an extra bit on for the size of this message
// // : Message source and destinations are reversed.
// P2PeerMsg *pMsg
// = new P2PeerMsg ( m_pP2PmsgBSTR -> nDestinID
// , m_pP2PmsgBSTR -> nSourceID
// , pszMsg
// , pvMsgData, iDataSize );
// //pMsg -> m_pMsgHeader -> ucPriority
// // = m_pMsgHeader -> ucPriority;
// pMsg -> m_pP2PmsgBSTR ->ucPriority
// = m_pP2PmsgBSTR-> ucPriority;
// //pMsg -> m_pMsgHeader -> dwAddrTag
// // = m_pMsgHeader -> dwAddrTag;
// pMsg -> m_pP2PmsgBSTR ->dwAddrTag
// = m_pP2PmsgBSTR-> dwAddrTag;
//
// // Copy in details of the wrapped message
// //pMsg -> Allocate_( pvMsgData, iDataSize
// // , m_pMsgHeader_, m_pMsgHeader_->iSize );
// ASSERT(!pMsg->m_pP2PmsgWrap);
// pMsg -> m_pP2PmsgWrap = new P2PeerMsg ( *this );
//
// // and simply
//ASSERT(pMsg->Map_MatchName(pszMsg));
//ASSERT(Map_MatchName((*pMsg)[1].GetName()));//DELETE-ME
// return pMsg;
//}
void
P2PeerMsg::WrapAttach ( P2PeerMsg *pP2Pmsg )
{
// To be sure, to be sure
ASSERT(0);
if ( m_pP2PmsgWrap )
EVERR->MODULE
->Message_T("P2Pmsg already contains wrapped P2Pmsg" )
->Advice_T ("Bug, (SNHappen)")
->Throw();
m_pP2PmsgWrap = pP2Pmsg;
}
//
// Unwraps this message to expose the original wrapped message.
// NOTES: Refer WrapFactory() for wrapping up messages. Use
// IsWrapped() to check wrapped status of a message.
// : Client becomes responsible for life cycle of the
// unwrapped message returned.
//
// Returns: P2PeerMsg*
// Pointer to unwrapped message. Client becomes
// responsible for life cycle.
// 0.. No wrapped message.
//
P2PeerMsg*
P2PeerMsg::UnwrapFactory ( )
{
// Optimisation
if ( m_pP2PmsgWrap )
return new P2PeerMsg(*m_pP2PmsgWrap);
// To be sure, to be sure
if ( !IsWrapped() )
EVERR->MODULE
->Message_T("P2PeerMsg not wrapped" )
->Advice_T ("Use IsWrapped() as pre-check")
->Throw();
// Instanciate
VBListIOmage *pIOmage = (VBListIOmage *)r_item(VBLockBSTR_WRP).r_data().c_vBlob();
return new P2PeerMsg ( *pIOmage );
}
//
// Builds an error response from the passed error message
//
//
// Parameters: P2Pevent *pP2Pevent
// Error message object from which the response
// is to be constructed
//
// Returns: P2PeerMsg*
// Pointer to error response message. Client
// becomes responsible for life cycle.
//
/*P2PeerMsg*
P2PeerMsg::ErrorResponseFactory ( P2Pevent *pP2Pevent )
{
// Introduce locals
P2PeerMsg *pMsg = 0;
// Build response message
EventLogData *pEventLogData
= pP2Pevent -> LogEventialsFactory();
pMsg = WrappedResponseFactory ( P2Pmsg_Error
, pEventLogData
, pEventLogData->nSize );
delete pEventLogData;
// Tidy up and
return pMsg;
}*/
//
// Creates P2Pmsg_Exception from encapsulated P2PeerMsg and passed
// P2Pevent
// NOTES: Clones routing properties of the contained message and
// then swaps the source and destination addresses
// : P2PeerMsg_CATCH handlers are used to intercept posted
// P2Pmsg_Exceptions
//
//
// Parameters: P2Pevent *pEVT
// Event object from which the exception is to be
// constructed
// NOTES: Use the P2PeventFactory() to extract P2Pevent's
// from P2Pmsg_Exception's
//
// Returns: P2PeerMsg*
// Exception P2PeerMsg pointer. Client becomes
// responsible for life cycle.
//
P2PeerMsg*
P2PeerMsg::ExceptionFactory ( P2Pevent *pEVT )
{
// Introduce locals
P2PeerMsgSP spMsg;
// Build response message
spMsg = WrappedResponseFactory ( P2Pmsg_Exception, 0, 0 );
spMsg -> AttachP2Pevent ( pEVT );
// Tidy up and
ASSERT(spMsg->Map_MatchName(P2Pmsg_Exception));//DELETE-ME
ASSERT(spMsg->IsWrapped());//DELETE-ME
ASSERT(AfxCheckMemory());
ASSERT(Map_MatchName( (*spMsg)[1].c_name()));
return spMsg.Dereference();
}
///////////////////////////////////////////////////////////////////////
// P2Pevents
// P2Pevent*
// UnwrapP2Pevent( );
// Attaches copy of passed P2Pevent to message
// NOTES: Any existing P2Pevent is displaced by passed P2Pevent
//
// Parameters: P2Pevent *pEvent
// Attached event
//
// LPCTSTR lpszFieldName = 0
// Name of node under which event is attached
//
void
P2PeerMsg::AttachP2Pevent ( const P2Pevent *pEVT
, LPCTNAM lpszFieldname )
{
// Field confirmation
if ( lpszFieldname == 0 ||
lpszFieldname[0] == 0 )
lpszFieldname = L"P2Pevent";
// Events node may not exist
if ( !Exists(VBLockBSTR_EVT) )
InitItem ( VBLockBSTR_EVT, P3PmsgData() );
// Attachment
if ( r_item(VBLockBSTR_EVT).Exists(lpszFieldname) )
r_item(VBLockBSTR_EVT)[lpszFieldname] = *pEVT;
else
r_item(VBLockBSTR_EVT) += *pEVT;
}
//
// Extracts copy of P2Pevent from message
// NOTES: Refer complimentary AttachP2Pevent() for attachment details
//
//
// Parameters: LPCTSTR lpszFieldName = 0
// Name of field from which P2Pevent is to be extracted
//
// Returns: P2Pevent*
// Extracted instance, client becomes responsible for
// life cycle
// 0.. Nothing attached
P2Pevent*
P2PeerMsg::ExtractP2Pevent( LPCTNAM lpszFieldname )
{
// Node confirmation
if ( lpszFieldname == 0 ||
lpszFieldname[0] == 0 )
lpszFieldname = L"P2Pevent";
// Node extraction
if ( !Exists(VBLockBSTR_EVT) ||
!r_item(VBLockBSTR_EVT).Exists(lpszFieldname) )
return 0;
// Manufacture
// NOTES: Client responsible for life cycle
return new P2Pevent(r_item(VBLockBSTR_EVT).SelectItem(lpszFieldname));
}
///////////////////////////////////////////////////////////////////////
// P2PeerMsg_MAP routing
// NOTES: Used for P2PeerMsg routing and handler assignment
//
// Matches P2PeerMsg state against passed filter
//
//
// Parameters: USHORT uiMsgStateWildbits
// Bit mask filter of allowable states
//
//
// Returns: bool
// Filtered comparison result
// true... Matched
// false.. Failed to match
//
bool
P2PeerMsg::Map_MatchState( USHORT uiMsgStateFilter )
{
P2PeerMsgPrefix *pPrefix = (P2PeerMsgPrefix *)r_data().c_vBlob();
bool bStateMsg = pPrefix -> nMsg&P2P_Reflected ? true : false;
bool bStateFlt = uiMsgStateFilter&P2P_Reflected ? true : false;
// Implementation
return bStateMsg == bStateFlt ? true : false;
}
//
// Description:
//
//
// Parameters: const char *pszMsgWildcard
// Message wildcard
//
// Returns: bool
// true... Message matches wildcard
// false.. No match
//
bool
P2PeerMsg::Map_MatchName ( LPCTNAM pszMsgWildcard ) const
{
// Preparation
bool star = false;
const T_ADDR *pszMsg = c_name ( );
// Implmentation
Top:
const T_ADDR *p, *s;
for ( s = pszMsg, p = pszMsgWildcard; *s; ++s, ++p )
{
switch (*p) {
case '?':
if (*s == '.') goto starCheck;
break;
case '*':
star = TRUE;
pszMsg = s, pszMsgWildcard = p;
if (!*++pszMsgWildcard) return TRUE;
goto Top;
case '#':
if (!isdigit(*s))
goto starCheck;
while (isdigit(*(s + 1)))
s++;
break;
default:
//if (mapCaseTable[*s] != mapCaseTable[*p])
if (*s != *p )
goto starCheck;
break;
} /* endswitch */
} /* endfor */
if (*p == '*') ++p;
return (!*p);
starCheck:
if (!star) return FALSE;
pszMsg++;
goto Top;
}
void
P2PeerMsg::AssertValid ( ) const
{
// Firstly delegate
__super::AssertValid ( );
}
void
P2PeerMsg::Print ( FILE *fd )
{
// Indentation
int nDepth = GetDepth ( );
for ( int i = 0; i < nDepth; i++ )
P3Pmsg_fwprintf ( fd, L" " );
// Encode message header
P3Pmsg_fwprintf ( fd, L"P2PeerMsg[%i] {\n", nDepth );
//m_oBSTR.Print ( fd, 1+nDepth );
P3PmsgBSTR::Print ( fd, 1+nDepth );
if ( IsWrapped() )
(*this)[1].Print ( fd );
// Tidy up
for ( int i = 0; i < nDepth; i++ )
P3Pmsg_fwprintf ( fd, L" " );
P3Pmsg_fwprintf ( fd, L"}\n" );
}
//
// Generates Visual Runtime Summary for P2PeerMsg instance
// FORMAT: [P2PeerMsg Name=MsgName, Src=MsgSource, Dst=MsgDestin]
// : Such brief summaries are used to pass brief P2PeerMsg state
// information to P2Pevent's
//
//
// Returns: LPCTSTR
// Pointer to encoded P2PeerMsg summary
// NOTES: (Re-)Encoded upon each reference
//
LPCTSTR
P2PeerMsg::GetVisualRTSummary ( )
{
// Container
if ( !m_pstrVisualSummary )
m_pstrVisualSummary = new CString ( );
// Simply encode header
m_pstrVisualSummary
-> Format ( L"[Name=%s, Source=%s, Destin=%s]"
, c_name()
, GetSource()
, GetDestin() );
// Tidy up and