Repository navigation
Expand file tree
/
Copy pathP2PeerTarget.cpp
More file actions
3193 lines (2947 loc) · 104 KB
/
Copy pathP2PeerTarget.cpp
File metadata and controls
3193 lines (2947 loc) · 104 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-2009, 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.
//
//
// P2PeerTarget implementation.
// NOTES: All objects within a virtual P2Peer network must derive
// from P2PeerTarget
//
#include "stdafx.h"
#include "P2PeerHub.h"
#include "P2PeerTarget.h"
#include "P2Pwin32.h"
#include "Msgexception.h"
//
// Construction and destruction
//
//
// Parameters: P2PeerTarget *pTargetParent
// Parent of this target. Refer RegisterMsgTarget()
// for further details
//
// short nPriority
// Priority applied to message map routing through
// passed object
// NOTES: Refer RegisterTarget() for further details
//
P2PeerTarget::P2PeerTarget ( )
{
m_dwAddrTag = (DWORD_PTR)this;
m_uiValidObject = (UINT_PTR)this;
}
P2PeerTarget::P2PeerTarget ( P2PeerTarget *pTargetParent
, P2Pri_t nPriority )
{
// Firstly
m_dwAddrTag = (DWORD_PTR)this;
m_uiValidObject = (UINT_PTR)this;
//m_pTargetParent = 0;
//m_pTargetChildHi = 0;
//m_pTargetChildLo = 0;
//m_pTargetPrev = 0;
//m_pTargetNext = 0;
//m_nPriority = nPriority;
//m_pP2PmsgSinkIDmap = 0;
// Associate with parent
if ( pTargetParent )
pTargetParent -> RegisterTarget ( this, nPriority );
}
P2PeerTarget::~P2PeerTarget(void)
{
// Object validity flag.
m_uiValidObject = 0;
// Timers
// NOTES: Target specific timers first. Then globally cancel
// all timers outstanding for this object.
// : Targeting specific timers is best handled in derived
// class
// P2PmsgSink's
// NOTES: Recover those localised to this object
if ( m_pP2PmsgSinkIDmap )
{
while ( m_pP2PmsgSinkIDmap->size() > 0 )
{
P2PmsgSinkIDmap::iterator it = m_pP2PmsgSinkIDmap->begin();
P2PmsgSinkClose ( it->second );
m_pP2PmsgSinkIDmap -> erase(it);
}
delete m_pP2PmsgSinkIDmap;
m_pP2PmsgSinkIDmap = nullptr;
}
// Drop parent message map registration
if ( m_pTargetParent )
m_pTargetParent -> RemoveTarget ( this );
// Drop hi-priority target registrations
while ( m_pTargetChildHi )
RemoveTarget ( m_pTargetChildHi );
// Drop lo-priority target registrations
while ( m_pTargetChildLo )
RemoveTarget ( m_pTargetChildLo );
}
///////////////////////////////////////////////////////////////////////
// P2PmsgPump management
// NOTES: Started using default P2PeerTarget::SpawnTarget() or
// CreateTarget() implementations
typedef struct
{
P2PumpID *pnPumpID;
P2PeerTarget *pTarget;
P2Paddr oP2Paddr;
RUN_PUMP pfnRunPump;
} P2PumpContext;
//
// Creates P2PmsgPump and commences pumping
// NOTES: Thread processing may be stopped via Close()
//
//
// Parameters: LPTHREAD_START_ROUTINE pfnThreadProc
// The thread procedure of the new thread. Refer
// win32 CreateThread() for further details
//
// RUN_HUB pfnRunHub = 0
// Operational method of the new thread
//
// P2PumpID *pnPumpID
// Pointer to pump identification code. Retained
// and cleared upon P2PmsgPump closure.
//
// Returns: HANDLE
// Returns the handle to the newly created thread
// or NULL on failure.
//
HANDLE
P2PeerTarget::SpawnPump ( LPTHREAD_START_ROUTINE pfnThreadProc
, RUN_PUMP pfnRunPump
, LPCTNAM lpszName
, P2PumpID *pnP2PumpID )
{
// Preparation
if ( pfnThreadProc == NULL )
pfnThreadProc = P2PeerTarget::ProcPump;
if ( pfnRunPump == NULL )
pfnRunPump = RUN_PUMP_cast(&P2PeerTarget::RunPump);
ASSERT(pfnRunPump);
// Pass context through
P2PumpContext oContext;
oContext.pnPumpID = pnP2PumpID;
oContext.pTarget = this;
oContext.oP2Paddr = lpszName;
oContext.pfnRunPump = pfnRunPump;
HANDLE hThread = CreateThread ( 0, 0
, pfnThreadProc, &oContext //pvParam
, 0, pnP2PumpID );
if ( !hThread )
return FALSE;
// Confirm operation
// NOTES: Contracted for P2PmsgPump operation upon return
DWORD dwExitCode;
UINT uSpins = 0;
while ( !P2PmsgPumpExists(*pnP2PumpID) )
{
if ( !GetExitCodeThread(hThread,&dwExitCode) ||
dwExitCode != STILL_ACTIVE )
return 0; // Operational failure
YieldForP2PmsgPump ( uSpins ); // Yield (escalating - see header)
}
// Tidy up and
return P2PmsgPumpExists(*pnP2PumpID) ? hThread : 0;
}
//
// Closes or stops pump operation
// NOTES: Pump operation may be re-started via
// P2PeerTarget::SpawnPump() or P2PeerTarget::CreatePump()
//
// Parameters: P2PumpID nPumpID
// Identification code of pump to be closed
void
P2PeerTarget::ClosePump ( P2PumpID nPumpID )
{
// Close and confirm
SignalP2PmsgPump ( nPumpID, P2PsigPump_CLOSE );
UINT uSpins = 0;
while ( P2PmsgPumpExists(nPumpID) )
YieldForP2PmsgPump ( uSpins );
}
//
// Default thread starting address nominated in CreateThread()
// NOTES: Provide alternative implementation to override default
// action. Follows P2PeerPump::ProcVanilla() base class pattern
//
//
// Parameters: void *pvContext
// Thread data passed via CreateThread()
//
// Returns: DWORD
// Completion code
//
DWORD WINAPI
P2PeerTarget::ProcPump ( void *pvContext )
{
// Because this is all problematic we
try
{
// Introduce locals
// NOTES: P2PumpContext is assumed not to persist
P2PumpContext *pContext = static_cast<P2PumpContext *>(pvContext);
P2PeerTarget *pTarget = pContext -> pTarget;
P2PumpID *pnPumpID = pContext -> pnPumpID;
//P2PmsgHubID nHubID = pTarget -> GetHubID ( );
RUN_PUMP pfnRunPump = pContext -> pfnRunPump;
pTarget -> P2PeerTarget::AssertValid ( );
// Mandatory P2PeerPump thread environment
// NOTES: Sequence contains mandatory P2PeerPump and P2Pmsg
// life cycle management sequences
CreateP2PmsgPump ( pTarget->GetHubID()
, pContext->oP2Paddr.c_name(), pTarget );
(pTarget->*pfnRunPump) ( );
if ( pnPumpID && *pnPumpID == GetCurrentThreadId() )
*pnPumpID = 0; // Flags pump closure
CloseP2PmsgPump ( );
}
// Exceptions
catch_pP2Pevent_Cancel
catch_pCException_Cancel
catch_ALL_Cancel
// Tidy up, and
return 0;
}
//
// Plain network P2PmsgPump implementation
// NOTES: Simply pumps P2PeerSys, P2PeerCon and P2PeerMsg objects
// through the P2PeerTarget base class
// : Usually P2PeerMsg's are swapped to the context of this
// thread via ContextSwap() and processed independantly in
// the fullness of time
// : It's possible to have processing delays in this context.
// But keep in mind P2PeerMsg's may keep building up
//
void
P2PeerTarget::RunPump ( )
{
// Locals
DWORD dwResult;
P2PsigID nSigID;
// Because this is all problematic we
try
{
// Latencies
SetP2PmsgPumpFunc ( 0, T__FUNCTION__ );
ThrowP2Pevent();
// Pump messages through the P2PeerSys, P2PeerCon and
// P2PeerMsg_MAP's until terminated and exhausted
while ( (dwResult=PumpP2Pmsg(8000,nSigID)) != 0 )
{
// Immediate closure signalled
if ( nSigID == P2PsigPump_CLOSE )
break;
// Idle closure signalled
if ( nSigID == P2PsigPump_CLOSEONIDLE )
break;
// Wakeup signalled
if ( nSigID == P2PsigPump_WAKEUP )
continue;
}
}
// Exceptions
catch ( P2Pevent *pEVT)
{
pEVT->Advice("P2PmsgPump() has terminated" )
->Cancel();
}
catch ( ... )
{
EVERR->MODULE
->Message("Last resort exception of unknown type, "
"P2PmsgPump has terminated" )
->Cancel();
}
// Tidy up, and
}
//
// Performs post P2PmsgHub destruction processing
// NOTES: Specialise for external notifications etc
//
void
P2PeerTarget::PostDestroyPump ( P2PumpID )
{
}
///////////////////////////////////////////////////////////////////////
// Message manufacture
//
// Manufactures P2PeerMsg object
// NOTES: Message will be tagged and flagged for
// reflection back to this P2PeerTarget instance
//
//
// Parameters: P2PeerID strDestin
// Message destination address
//
// P2Pmsg_t nMsg
// Message type
//
// const void *pvData
// Optional message data copied directly into
// manufactured message. NULL pointer negates copy
// operation
//
// short nDataSize
// Message data length
//
// Returns: P2PeerMsg*
// Manufactured message. Client becomes responsible
// for life cycle
//
P2PeerMsg*
P2PeerTarget::P2PeerMsgFactory ( P2PaddrSTR pDestinSTR
, P2PmsgID sMsg
, const void *pvData, short nDataSize )
{
// Manufacture
P2PeerMsg *pMsg = new P2PeerMsg ( GetP2PaddrHub(), pDestinSTR
, sMsg
, pvData, nDataSize );
// Address tag
// NOTES: For instances when similar P2PeerMsg's originate
// from multiple P2PeerTarget's for the same P2Peer.
// This tag is used to route possible P2PeerMsg exceptions
// and reflections back to this P2PeerTarget
pMsg -> SetAddrTag ( &m_dwAddrTag, sizeof(m_dwAddrTag) );
// Tidy up and
return pMsg;
}
//
// Assigns message ownership to this target
//
//
// Parameters: P2PeerMsg *pMsg
// Message whose ownership is to be assigned to this
// object.
//
void
P2PeerTarget::AssignOwnership ( P2PeerMsg *pMsg )
{
//TODO:LJM Check message is not being pumped etc
// Simply
pMsg -> SetSource ( GetP2PaddrHub() );
pMsg -> SetAddrTag ( &m_dwAddrTag, sizeof(m_dwAddrTag) );
}
BOOL
P2PeerTarget::HasOwnership ( P2PeerMsg *pMsg )
{
return pMsg->AddrTag() == m_dwAddrTag ? TRUE : FALSE;
}
///////////////////////////////////////////////////////////////////////
// P2Pump context
// NOTES: P2PeerContext() is used to check current P2Pump context
// and the P2PeerContextSwap() implementations are used to
// swap context.
// : When context swapping occurs the P2PeerCon or P2PeerMsg
// objects will be re-routed to the same handler but in
// the context of the nominated thread.
// : An exception is generated if not used in the context of
// P2PeerCon_MAP, P2PeerMsg_MAP and P2PeerSys_MAP handlers
//
// Confirms P2PmsgHub or P2PmsgPump processing context
// NOTES: Usually called from P2PeerCon notification or P2PeerMsg
// handlers to confirm processing is occuring in the correct
// thread context
// : Under some circumstances P2PeerCon and P2PeerMsg objects
// may be managed in one context and processed in another
//
//
// Parameters: P2PumpID nPumpID
// Pump whose context the P2PeerCon or P2PeerMsg
// object is to be processed
//
// Returns: bool
// TRUE... Process in current context
// FALSE.. Requires P2PeerContextSwap()
//
bool
P2PeerTarget::P2PeerContext ( P2PumpID nPumpID )
{
// Compare current with passed context
if ( GetCurrentThreadId() == nPumpID )
return TRUE;
// Post to other context
return FALSE;
}
//
// Swaps the processing context for the current P2Pmsg object to
// the nominated pump
// NOTES: Context swap details are always thrown as as exceptions
// : The exception will be intercepted, context swapped and
// implemented directly through this P2PeerTarget instance
//
// Parameters: P2PmsgPump nPumpID
// Identifier of pump in which context the P2PeerCon
// or P2PeerMsg object is to be processed
//
// Returns: mapRESULT
// Context swap object (msgSWAP)
//
mapRESULT
P2PeerTarget::P2PeerContextSwap ( P2PumpID nPumpID )
{
// Delegate
return SwapP2PmsgContext ( this, nPumpID
, (P2PeerCon *)0, (P2PeerMsg *)0 );
}
//
// Swaps the processing context for the current P2Pmsg object to
// the nominated window
// NOTES: Context swaps MUST be immediately propagated back up through
// the stack for implementation. Outcome is undefined when
// attempting further processing on a P2PeerMsg flagged for
// a context swap.
//
//
// Parameters: HWND hWnd
// Window containing nWM_APP message handler that
// subsequently delegates implementation via
// pContext->pTarget->On_P2P(...)
//
// UINT nWM_APP
// WM_APP+offset message
// NOTES: Usually the WM_APP_P2PeerTarget definition is
// specified. Should this definition conflict the
// option is available to specify an alternative.
// : Nominated window must support handler for this
// message type. This handler will subsequently
// delegate implementation to OnP2PeerTarget()
//
// Returns: mapRESULT
// Context swap object (msgSWAP)
//
mapRESULT
P2PeerTarget::P2PeerContextSwap ( HWND hWnd, UINT nWM_APP )
{
// Delegate
return SwapP2PmsgContext ( this, AfxGetApp()->m_nThreadID
, (P2PeerCon *)0, (P2PeerMsg *)0
, hWnd, nWM_APP );
}
//
// Swaps the processing context for the current P2PeerCon object
// NOTES: Context swaps MUST be immediately propagated back up through
// the stack for implementation. Outcome is undefined when
// attempting further processing on a P2PeerMsg flagged for
// a context swap.
//
//
// Parameters: P2PeerCon *pCon
// Connection object to which current P2PeerMsg is to be
// queued
//
// Returns: mapRESULT
// Context swap object (msgSWAP)
//
mapRESULT
P2PeerTarget::P2PeerContextSwap ( P2PeerCon *pCon, P2PeerMsg *pMsg )
{
// Throw change in P2Pmsg context exception
return SwapP2PmsgContext ( this
, 0, pCon, pMsg );
}
//
// Swaps the processing context with respect to the current P2PeerMsg object
// NOTES: Context swaps MUST be immediately propagated back up through
// the stack for implementation. Outcome is undefined when
// attempting further processing on a P2PeerMsg flagged for
// a context swap.
//
// Returns: mapRESULT
// Context swap object (msgSWAP)
//
mapRESULT
P2PeerTarget::P2PeerContextSwap ( P2PeerMsg *pMsg )
{
// Throw change in P2Pmsg context exception
return SwapP2PmsgContext ( this
, 0, (P2PeerCon *)0, pMsg );
}
///////////////////////////////////////////////////////////////////////
// Timers
// NOTES: Timers are managed under the context of the P2PmsgPump in
// which they are created
//
// Sets PIT for this P2PeerTarget object
// NOTES: Outstanding timers must be cancelled in destructor for
// this object
//
//
// Parameters: P2Pmsecs_t uMSecDelay
// Specifies the time-out value, in milliseconds
//
// DWORD dwUserKey
// User defined data key supplied to timeout handler
//
// Returns: PITimerID
// Identifier of the new timer if successful. An
// application passes this value to the KillTimer member
// function to kill the timer.
//
PITimerID
P2PeerTarget::SetPITimer ( P2Pmsecs_t uMSecDelay, DWORD dwUserKey )
{
// Delegate
// NOTES: Handler for this object always called
return SetP2PmsgTimer ( 0, this, uMSecDelay, dwUserKey );
}
//
// Sets PIT for connection
// NOTES: Outstanding timers must be cancelled in destructor for
// this object
//
//
// Parameters: P2PeerMsg *pMsg
// Timer message
//
// P2PeerTime_t uMSecDelay
// Specifies the time-out value, in milliseconds
//
// DWORD dwUserKey
// User defined data key supplied to timeout handler
//
// Returns: PITimerID
// Identifier of the new timer if successful.
//
PITimerID
P2PeerTarget::PostPITimer ( P2PeerMsg *pMsg
, P2Pmsecs_t uMSecDelay, DWORD dwUserKey )
{
// Locals
P2PeerMsg *pMsgTimer = 0;
Msg_P2PeerTimer oP2PeerTimer;
oP2PeerTimer.dwUserKey = dwUserKey;
// Manufacture
if ( pMsg )
pMsgTimer = pMsg -> WrapFactory ( P2Pmsg_Timer
, &oP2PeerTimer, sizeof(Msg_P2PeerTimer) );
else
pMsgTimer = new P2PeerMsg ( 0, 0, P2Pmsg_Timer
,&oP2PeerTimer, sizeof(Msg_P2PeerTimer) );
pMsgTimer -> SetSource ( GetP2PaddrHub() );
pMsgTimer -> SetDestin ( pMsgTimer->GetSource() );
// Deliver
return SetP2PmsgTimer ( pMsgTimer->GetSource(), CN_P2PeerMsg, P2P_PITimer
,(P2PeerCon *)0, pMsgTimer
, uMSecDelay, 0 );
}
//
// Default PIT handler for P2PeerTarget
// NOTES: Override for specialisation
//
//
// Parameters: bool bCancel
// Cancellation flag. Refer CancelPITimer() for further
// details
// true... Cancelled
// false.. Triggered
//
// PITimerID nPITimerID
// Timer identification. Refer SetPITimer() for
// further details
//
// DWORD dwUserKey
// Timer user key
//
void
P2PeerTarget::On_PITimer ( bool bCancel
, PITimerID nPITimerID, DWORD dwUserKey )
{
// Unknown timer
// NOTES: Report and ignore
EVERR->MODULE
->AFP(bCancel)->AFP(nPITimerID)->AFP(dwUserKey)
->Message(_T("PITimer not handled [P2PeerHub=%s]")
, GetP2PaddrHub().c_wstr() )
->Advice_T ("Ignored, bug (SNHappen)")
->Cancel ();
}
///////////////////////////////////////////////////////////////////////
// P2Pevent's
//
// Dedicated P2Pevent handler
// NOTES: Processed in the context of the P2PeerTarget that created
// the notifications sink
//
//
// Parameters: P2PeventSinkID
// Identification code of the Event sink for which the
// notification sink is being performed.
//
// P2PumpID nPumpID
// Originating P2Pevent pump,
//
// const P2Pevent *pEvent
// Posted event
//
//
evtRESULT
P2PeerTarget::On_P2Pevent( P2PeventSinkID nSinkID, P2PumpID nPumpID
, P2Pevent *pEvent )
{
UNREFERENCED_PARAMETER(nSinkID);
UNREFERENCED_PARAMETER(nPumpID);
// Default implementation is to acknowledge
// NOTES: Negate notifications to negate possible recursive feed
// back look
pEvent -> Display ( (HWND)0 );
// Tidy up, and
return evtHANDLED;
}
///////////////////////////////////////////////////////////////////////
// P2PeerCon integration
// NOTES: The state of a P2PeerCon object is managed by the
// asynchronous exchange between P2PeerCon_MAP handlers
// and the P2PeerHub
//
// Unhandled P2PeerCon notification
// NOTES: Generates and cancels error event
//
//
// Parameters: P2PeerCon *pCon
// Connection for which notification not handled
//
// P2Pmsg_t nMsg
// Message type
//
void
P2PeerTarget::NotHandled ( P2PeerCon *pCon, P2Pmsg_t nMsg )
{
// Explaination
P2Pevent *pEVT =
EVERR->MODULE->AFPcon(pCon)->AFP(nMsg)
->Message(L"ON_P2PeerCon_%s(%s,...) not handled"
, EncodeP2Pmsg_t(nMsg)
, (P2PaddrSTR)pCon->GetP2Paddress() )
->Advice (L"Version problem, bug, connection dropped" )
->Group (L"P2P");
// Tidy up, and
//DropP2PmsgCon ( pCon );
pCon -> Drop ( pEVT->Isolate() );
pCon -> Destroy ( );
}
//
// Post P2PeerCon object to linked parent P2PeerHub
// NOTES: Control over lifecycle of passed P2PeerCon is assumed
// : Altimately P2PeerCon objects may only ever be posted
// to individual P2PeerHub's.
//
//
// Parameters: P2PeerCon *pCon
// Connection object to be posted.
//
// Returns: P2PeerCon*
// Connection posted flag
// 0.. Posted
//
P2PeerCon*
P2PeerTarget::PostP2PeerCon ( P2PeerCon *pCon )
{
// Nothing to pick up P2PeerMsg
if ( m_pTargetParent == NULL )
{
EVERR->MODULE->AFPcon(pCon)
->Message("No linked P2PeerHub" )
->Advice ("Linked P2PeerTarget's must be terminated in P2PeerHub" )
->Advice ("Application bug (SNHappen)" )
->Cancel ( );
return pCon;
}
// Simply
// NOTES: Altimately parent P2PeerTarget will provide
// an implementation
return m_pTargetParent -> PostP2PeerCon ( pCon );
}
//
// Default P2PeerCon handler
//
//
// Parameters: P2PeerCon *pCon
// Connection object for which state transition
// is to be routed
//
// int nCode
// Operational code
//
// P2Pmsg_t nMsg
// State transition message to be routed.
//
// void *pExtra
// Extra information. Context sensitive
//
// P2P_CONHANDLERINFO *pHandlerInfo
//
// Returns: BOOL
// TRUE...
// FALSE..
BOOL
P2PeerTarget::On_P2PeerCon ( const P2Paddr& oP2Paddr, UINT nCode, P2Pmsg_t nMsg
, P2PeerMsg *pMsg, P2PeerCon *pCon
, P2P_CONHANDLERINFO* pHandlerInfo )
{
// Introduce locals
P2PeerTarget *pTarget;
BOOL bResult = FALSE;
BOOL bMapcast = FALSE;
// determine the message number and code (packed into nCode)
const P2P_CONMAP *pP2P_ConMap;
const P2P_CONMAP_ENTRY *pEntry;
// Hi-priority child delegation is always first
// NOTES: All these priorities will be -ve. Work backwards
// through the list. A priority of -1 will be
// processed before -2 etc
for ( pTarget = m_pTargetChildHi;
( pTarget &&
( !bResult||bMapcast ) );
pTarget = pTarget->m_pTargetNext )
bResult |= pTarget -> On_P2PeerCon ( oP2Paddr, nCode, nMsg
, pMsg, pCon
, pHandlerInfo );
// Traverse through message map to see if it applies to us
// NOTES: Top down search. Derived class first then down
// through the base classes.
// : If there exists a desire to optimise then place
// frequent messages at the top of the message map
// and less frequent messages at the end of the list
// : Traverse continues until P2PeerCon flagged as
// handled.
// : The default set of handlers in this P2PeerTarget are
// not traversed in this instance. Refer On_DefaultCon()
// processing for further details
for ( pP2P_ConMap = GetP2PeerConMap();
( pP2P_ConMap &&
pP2P_ConMap->pBaseMap &&
!bResult );
pP2P_ConMap = pP2P_ConMap->pBaseMap )
{
// Trap BEGIN_P2PeerCon_MAP(CMyClass, CMyClass)!
VERIFY(pP2P_ConMap != pP2P_ConMap->pBaseMap);
// Search for handler in P2PeerCon map
// NOTES:
pEntry = FindP2PeerConEntry ( pP2P_ConMap->lpEntries
, nMsg, nCode, pCon, pMsg );
if ( pEntry == NULL)
continue;
// Dispatch message
// NOTES: P2PeerCon's are at most only ever handled once
// per map
bResult = DispatchP2PeerCon ( this
, pCon, nCode, nMsg
, pEntry->pfn, pMsg
, pEntry->nSig, pHandlerInfo );
}
// Lo-priority child delegation is always last
// NOTES: All these priorities will be +ve. Work forewards
// through the list. A priority of +1 will be
// processed before +2 etc
for ( pTarget = m_pTargetChildLo;
( pTarget &&
( !bResult||bMapcast ) );
pTarget = pTarget->m_pTargetNext )
bResult |= pTarget -> On_P2PeerCon ( oP2Paddr, nCode, nMsg
, pMsg, pCon
, pHandlerInfo );
// Tidy up and
return bResult; // Handled summary
}
//
// Default P2PeerCon handler
// NOTES: Provides default processing services for pumped P2PeerCon
// states changes that have not been intercepted and handled
// via a P2PeerCon_MAP() entry
// : The default processing services are sufficient for most pumped
// P2PeerCon state changes.
//
//
// Parameters: P2PeerCon *pCon
// Connection object for which state transition
// is to be routed
//
// int nCode
// Operational code
//
// P2Pmsg_t nMsg
// State transition message to be routed.
//
// void *pExtra
// Extra information. Context sensitive
//
// P2P_CONHANDLERINFO *pHandlerInfo
//
// Returns: conRESULT
// TRUE...
// FALSE..
BOOL
P2PeerTarget::On_DefaultCon ( UINT nCode, P2Pmsg_t nMsg
, P2PeerMsg *pMsg, P2PeerCon *pCon
, P2P_CONHANDLERINFO* pHandlerInfo )
{
// Introduce locals
BOOL bResult = FALSE;
// Determine the message number and code (packed into nCode)
const P2P_CONMAP *pP2P_ConMap = P2PeerTarget::GetP2PeerConMap();
const P2P_CONMAP_ENTRY *pEntry;
// Search the base class only
// NOTES: Isolates default processing in the P2PeerTarget assigned to
// the P2PeerPump
// : Access to these handlers is negated in the primary search
// and act as last resort defaults
VERIFY(pP2P_ConMap&&pP2P_ConMap->pBaseMap==NULL);
// Search for handler in P2PeerCon map
// NOTES: Ideally there's always an entry
pEntry = FindP2PeerConEntry ( pP2P_ConMap->lpEntries
, nMsg, nCode, pCon, pMsg );
// Dispatch message
// NOTES: P2PeerCon's are at most only ever handled once
// per map
if ( pEntry )
bResult = DispatchP2PeerCon ( this
, pCon, nCode, nMsg
, pEntry->pfn, pMsg
, pEntry->nSig, pHandlerInfo );
// Tidy up and
return bResult; // Handled summary
}
//
// Pre-destruction handler for P2PeerCon objects
// NOTES: Called as part of the P2PeerCon management procedures
// immediately prior to destruction
// : Override this default implementation for special processing,
// resource recovery etc
// : Specialisation should delegate to this base class
// implementation to ensure propagation through the P2PeerTarget
// heirachy
//
//
// Parameters: P2PeerCon *pCon
// Completed processing message
//
// conRESULT bConResult
// Permutations
// conHANDLED... Handled
// conCONTINUE.. Not handled
// conDROP...... Handled, drop P2PeerCon object
//
void
P2PeerTarget::PreDestroyP2PeerCon ( P2PeerCon *pCon
, conRESULT bConResult )
{
// Stops compiler warnings
pCon = pCon;
bConResult = bConResult;
// Hi-priority child delegation is always first
// NOTES: All these priorities will be -ve. Work backwards
// through the list. A priority of -1 will be
// processed before -2 etc
for ( P2PeerTarget *pTarget = m_pTargetChildHi; pTarget;
pTarget = pTarget->m_pTargetNext )
pTarget -> PreDestroyP2PeerCon ( pCon, bConResult );
// Lo-priority child delegation is always last
// NOTES: All these priorities will be +ve. Work forewards
// through the list. A priority of +1 will be
// processed before +2 etc
for ( P2PeerTarget *pTarget = m_pTargetChildLo; pTarget;
pTarget = pTarget->m_pTargetNext )
pTarget -> PreDestroyP2PeerCon ( pCon, bConResult );
}
//
// Exposes P2PeerHub to which this P2PeerTarget is ultimately linked
// NOTES: RegisterTarget() and RemoveTarget() for managing
// hierarchy's or P2PeerTarget objects
//
//
// Returns P2PeerHub*
// Pointer to linked P2PeerHub in the P2Peer hierarchy
//
P2PeerHub*
P2PeerTarget::GetP2PeerHub ( )
{
// Nothing to pick up implementation
if ( m_pTargetParent == NULL )
EVERR->MODULE
->Message("No linked P2PeerHub" )
->Advice ("Linked P2PeerTarget's must be terminated in P2PeerHub" )
->Advice ("Application bug (SNHappen)" )
->Throw();
// Delegate
// NOTES: Altimately the linked P2PeerHub will provide an
// implementation
return m_pTargetParent -> GetP2PeerHub ( );
}
//
// Exposes P2PmsgHubID to which this P2PeerTarget is ultimately linked
// NOTES: RegisterTarget() and RemoveTarget() for managing
// hierarchy's or P2PeerTarget objects
//
//
// Returns P2PmsgHubID
// Identification code of linked P2PeerHub in the
// P2Peer hierarchy
//
P2PmsgHubID
P2PeerTarget::GetHubID ( ) const
{
// Nothing to pick up implementation
if ( m_pTargetParent == NULL )
EVERR->MODULE
->Message("No linked P2PeerHub" )
->Advice ("Linked P2PeerTarget's must be terminated in P2PeerHub" )
->Advice ("Application bug (SNHappen)" )
->Throw();
// Delegate
// NOTES: Altimately the linked P2PeerHub will provide an
// implementation
return m_pTargetParent -> GetHubID ( );
}
///////////////////////////////////////////////////////////////////////
// P2PeerTarget hierarchy management
//
// Attaches P2PeerMsg map of the passed child P2PeerTarget to the
// end of P2PeerMsg map for this the parent P2PeerTarget
//
//
// Parameters: P2PeerTarget *pTargetChild
// Registered as child of this parent
//
// short nPriority
// Priority applied to message map routing through
// passed object
// -ve.. Messages are routed through pTargetChild