-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WinMsgComm.pas
2543 lines (2160 loc) · 106 KB
/
WinMsgComm.pas
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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
WinMsgComm
Small library for interprocess communication based on Windows message queue
system (but also works on Linux). Intended only for exchange of small data
and/or for notifications.
As there is no Windows-like messaging system in Linux (obviously),
SimpleMessages library is used there instead. Refer to that library for
potential limitations.
NOTE - SimpleMessages can also be used on Windows OS if you undefine
symbol UseWindowsMessages.
With one exception (see further), it is necessary to repeatedly call method
Update, as it manages processing of incoming messages/data and their
dispatching to events, and is also responsible for internal workings of the
communication. Without this, the communication object might become
unresponsive and even block other threads that are communicating with it.
Only when Windows messaging system is used for communication and when the
object is running in a main thread of an GUI application, the message
processing and dispatching is done automatically by the Application object.
Version 2.0.1 (2024-05-03)
Last change 2024-09-09
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.WinMsgComm
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BitVector - github.com/TheLazyTomcat/Lib.BitVector
CRC32 - github.com/TheLazyTomcat/Lib.CRC32
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
SharedMemoryStream - github.com/TheLazyTomcat/Lib.SharedMemoryStream
* SimpleMessages - github.com/TheLazyTomcat/Lib.SimpleMessages
StrRect - github.com/TheLazyTomcat/Lib.StrRect
* UtilityWindow - github.com/TheLazyTomcat/Lib.UtilityWindow
Library AuxExceptions is required only when rebasing local exception classes
(see symbol WinMsgComm_UseAuxExceptions for details).
Library SimpleMessages is required only when compiling for Linux OS or when
UseWindowsMessages symbol is not defined.
Library UtilityWindow is required only when compiling for Windows OS and
symbol UseWindowsMessages is defined.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
LinSyncObjs - github.com/TheLazyTomcat/Lib.LinSyncObjs
ListSorters - github.com/TheLazyTomcat/Lib.ListSorters
MemVector - github.com/TheLazyTomcat/Lib.MemVector
MulticastEvent - github.com/TheLazyTomcat/Lib.MulticastEvent
NamedSharedItems - github.com/TheLazyTomcat/Lib.NamedSharedItems
SHA1 - github.com/TheLazyTomcat/Lib.SHA1
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
SimpleFutex - github.com/TheLazyTomcat/Lib.SimpleFutex
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
WinSyncObjs - github.com/TheLazyTomcat/Lib.WinSyncObjs
WndAlloc - github.com/TheLazyTomcat/Lib.WndAlloc
===============================================================================}
unit WinMsgComm;
{
WinMsgComm_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
WinMsgComm_UseAuxExceptions to achieve this.
}
{$IF Defined(WinMsgComm_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(CPU64) or Defined(CPU64BITS)}
{$DEFINE CPU64bit}
{$ELSEIF Defined(CPU16)}
{$MESSAGE FATAL 'Unsupported CPU.'}
{$ELSE}
{$DEFINE CPU32bit}
{$IFEND}
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$MODESWITCH ClassicProcVars+}
{$INLINE ON}
{$DEFINE CanInline}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+}
//------------------------------------------------------------------------------
{
UseWindowsMessages
When defined, windows messaging system (SendMessage, GetMessage, ...) is used
to send and receive messages whenever it is available - that is, on Windows
operating system.
When not defined, the data are being sent using SimpleMessages library.
SimpleMessages is also used when compiling for operating system other than
Windows.
Has no effect when compiling for non-Windows operating systems.
By default enabled.
To disable/undefine this symbol in a project without changing this library,
define project-wide symbol WinMsgComm_UseWindowsMessages_Off.
}
{$DEFINE UseWindowsMessages}
{$IFDEF WinMsgComm_UseWindowsMessages_Off}
{$UNDEF UseWindowsMessages}
{$ENDIF}
{
ConserveMemory
Normally, communication objects are using large lookup table to directly
translate connection ID to its index in a list of known connections. This
table reguires about 256KiB of memory, and such memory consumption for each
object migh be undesirable - if so, define this symbol and the table will be
omitted.
But be warned, the memory footprint goes down, but the communication will
become slower, especially when large number of endpoints is connected.
By default undefined.
To enable/define this symbol in a project without changing this library,
define project-wide symbol WinMsgComm_ConserveMemory_On.
}
{$UNDEF ConserveMemory}
{$IFDEF WinMsgComm_ConserveMemory_On}
{$DEFINE ConserveMemory}
{$ENDIF}
{
SimpleMessagesHigherLimits
When SimpleMessages library is used for communication, it is done using its
default limits. These limits can be too low for some uses. You can define
this symbol in such cases to increase the limits (to 1024 clients and 10240
messages).
Has no effect when Windows messages are used for communication.
By default undefined.
To enable/define this symbol in a project without changing this library,
define project-wide symbol WinMsgComm_SimpleMessagesHigherLimits_On.
}
{$UNDEF SimpleMessagesHigherLimits}
{$IFDEF WinMsgComm_SimpleMessagesHigherLimits_On}
{$DEFINE SimpleMessagesHigherLimits}
{$ENDIF}
{
SimpleMessagesNoLimits
Similar to SimpleMessagesHigherLimits, but it sets client limit to its
technical maximum (65534) and messages limit to 655340.
Note that, if both SimpleMessagesHigherLimits and SimpleMessagesNoLimits are
defined, then this symbol takes precendence.
WARNING - when this symbol is defined, then this library allocates over
35MiB of global shared memory for each used domain.
By default undefined.
To enable/define this symbol in a project without changing this library,
define project-wide symbol WinMsgComm_SimpleMessagesNoLimits_On.
}
{$UNDEF SimpleMessagesNoLimits}
{$IFDEF WinMsgComm_SimpleMessagesNoLimits_On}
{$DEFINE SimpleMessagesNoLimits}
{$ENDIF}
//------------------------------------------------------------------------------
// do not touch following...
{$IFNDEF Windows}
{$UNDEF UseWindowsMessages}
{$ENDIF}
interface
uses
{$IFDEF UseWindowsMessages}Windows, Messages,{$ENDIF} SysUtils,
AuxTypes, AuxClasses, BitVector, CRC32, SharedMemoryStream,
{$IFDEF UseWindowsMessages}UtilityWindow{$ELSE}SimpleMessages{$ENDIF}
{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EWMCException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EWMCInvalidConnection = class(EWMCException);
EWMCInvalidOperation = class(EWMCException);
EWMCInvalidValue = class(EWMCException);
EWMCIndexOutOfBounds = class(EWMCException);
EWMCOutOfResources = class(EWMCException);
EWMCTooMuchData = class(EWMCException);
EWMCServerExists = class(EWMCException);
{===============================================================================
--------------------------------------------------------------------------------
TWinMsgComm
--------------------------------------------------------------------------------
===============================================================================}
type
TWMCConnectionID = UInt16; // used in internal types, so it must be up here
{===============================================================================
TWinMsgComm - internal types
===============================================================================}
type
TWMCMessagePayload = UInt64;
TWMCSharedDataID = UInt32;
TWMCSystemID = {$IFDEF UseWindowsMessages}HWND{$ELSE}TSMClientID{$ENDIF};
TWMCGlobalData = packed record
Flags: UInt32;
Counter: UInt32; // only interlocked access
IDPool: array[0..Pred(Succ(Integer(High(TWMCConnectionID))) div 8){8191}] of UInt8;
end;
PWMCGlobalData = ^TWMCGlobalData;
{$IFNDEF ConserveMemory}
TWMCIDToIndexTable = array[TWMCConnectionID] of Integer;
{$ENDIF}
TWMCTransaction = record
DataPtr: Pointer;
DataSize: TMemSize;
Position: TMemSize;
CheckSum: TCRC32;
end;
TWMCConnectionData = record
ConnectionID: TWMCConnectionID;
Is32bit: Boolean;
SystemID: TWMCSystemID;
Transacting: Boolean;
Transaction: TWMCTransaction;
end;
{===============================================================================
TWinMsgComm - public types
===============================================================================}
type
TWMCMessageCode = UInt8;
TWMCUserData = Int8;
TWMCConnection = record
ConnectionID: TWMCConnectionID;
Is32Bit: Boolean;
Transacting: Boolean;
end;
TWMCValueType = (mvtBool,mvtUInt8,mvtInt8,mvtUInt16,mvtInt16,mvtUInt32,
mvtInt32,mvtUInt64,mvtInt64,mvtFloat32,mvtFloat64,mvtString,
mvtData);
TWMCValue = record
UserData: TWMCUserData;
StringValue: String;
case ValueType: TWMCValueType of
mvtBool: (BoolValue: ByteBool);
mvtUInt8: (UInt8Value: UInt8);
mvtInt8: (Int8Value: Int8);
mvtUInt16: (UInt16Value: UInt16);
mvtInt16: (Int16Value: Int16);
mvtUInt32: (UInt32Value: UInt32);
mvtInt32: (Int32Value: Int32);
mvtUInt64: (UInt64Value: UInt64);
mvtInt64: (Int64Value: Int64);
mvtFloat32: (Float32Value: Float32);
mvtFloat64: (Float64Value: Float64);
mvtData: (DataPtr: Pointer;
DataSize: TMemSize)
end;
TWMCValueEvent = procedure(Sender: TObject; SenderID: TWMCConnectionID; Value: TWMCValue; Sent: Boolean) of object;
TWMCValueCallback = procedure(Sender: TObject; SenderID: TWMCConnectionID; Value: TWMCValue; Sent: Boolean);
const
WMC_BROADCAST = TWMCConnectionID(High(TWMCConnectionID));
{===============================================================================
TWinMsgComm - class declaration
===============================================================================}
type
TWinMsgComm = class(TCustomListObject)
protected
fLargeDataThreshold: TMemSize;
fDomainName: String;
fGlobalDataShrdMem: TSharedMemory;
fIDPoolVector: TBitVector;
fConnectionID: TWMCConnectionID;
fConnections: array of TWMCConnectionData;
fConnectionCount: Integer;
{$IFNDEF ConserveMemory}
fIDToIndexTable: TWMCIDToIndexTable;
{$ENDIF}
{$IFDEF UseWindowsMessages}
fWindowsMessageID: UINT;
fOwnsRecevingWindow: Boolean;
fReceivingWindow: TUtilityWindow;
{$ELSE}
fMessagesClient: TSimpleMessagesClient;
{$ENDIF}
fSystemID: TWMCSystemID;
fOnIncomingValueCallback: TWMCValueCallback;
fOnIncomingValueEvent: TWMCValueEvent;
fOnConnectionCallback: TNotifyCallback;
fOnConnectionEvent: TNotifyEvent;
// getters, setters
Function GetConnection(Index: Integer): TWMCConnection; virtual;
// list methods
Function GetCapacity: Integer; override;
procedure SetCapacity(Value: Integer); override;
Function GetCount: Integer; override;
procedure SetCount(Value: Integer); override;
Function ConnectionAdd(ConnectionID: TWMCConnectionID; Is32bit: Boolean; SystemID: TWMCSystemID): Integer; virtual;
Function ConnectionRemove(ConnectionID: TWMCConnectionID): Integer; virtual;
procedure ConnectionDelete(Index: Integer); virtual;
// incoming transaction
Function TransactionStart(Sender: TWMCConnectionID; DataSize: TMemSize): Boolean; virtual;
Function TransactionBuff(Sender: TWMCConnectionID; Payload: TWMCMessagePayload; Size: TMemSize): Boolean; virtual;
Function TransactionEnd(Sender: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; virtual;
// incoming messages
class Function DataToValue(var Value: TWMCValue; MessageCode: TWMCMessageCode; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
{$IFDEF UseWindowsMessages}
procedure HandleMessage(var Msg: TMessage; var Handled: Boolean; Sent: Boolean); virtual;
Function ProcessCopyData(CopyDataStruct: TCopyDataStruct; Sent: Boolean): Boolean; virtual;
{$ELSE}
procedure HandleMessage(Sender: TObject; var Msg: TSMMessage; var Flags: TSMDispatchFlags); virtual;
{$ENDIF}
Function ProcessSharedData(Sender: TWMCConnectionID; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; virtual;
Function ProcessMessage(Sender: TWMCConnectionID; MessageCode: TWMCMessageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; virtual;
// outgoing messages
Function SysSendSinglecast(SystemID: TWMCSystemID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Integer; virtual;
Function SysSendBroadcast(MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Integer; virtual;
Function SysPostSinglecast(SystemID: TWMCSystemID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function SysPostBroadcast(MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function SendMessageIdx(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function PostMessageIdx(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function SendMessageAll(MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function PostMessageAll(MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function SendMessageRcp(Recipient: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function PostMessageRcp(Recipient: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function Send8ByteQuantity(Recipient: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
Function Post8ByteQuantity(Recipient: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload): Boolean; virtual;
// note that there is, for various reasons, no data posting
{$IFDEF UseWindowsMessages}
Function SendCopyData(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
{$ENDIF}
Function SendSharedData(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
Function SendTransactedData(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
Function TrySendTinyDataIdx(Index: Integer; IsString: Boolean; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize; out SendResult: Boolean): Boolean; virtual;
Function TryPostTinyDataIdx(Index: Integer; IsString: Boolean; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize; out SendResult: Boolean): Boolean; virtual;
Function TrySendTinyDataRcp(Recipient: TWMCConnectionID; IsString: Boolean; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize; out SendResult: Boolean): Boolean; virtual;
Function TryPostTinyDataRcp(Recipient: TWMCConnectionID; IsString: Boolean; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize; out SendResult: Boolean): Boolean; virtual;
Function SendDataIdx(Index: Integer; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
Function SendDataAll(MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
Function SendDataRcp(Recipient: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; DataPtr: Pointer; DataSize: TMemSize): Boolean; virtual;
// events firing
procedure DoIncomingValue(SenderID: TWMCConnectionID; Value: TWMCValue; Sent: Boolean); virtual;
procedure DoConnectionChange; virtual;
// init/final
procedure Initialize(const DomainName: String{$IFDEF UseWindowsMessages}; ReceivingWindow: TUtilityWindow{$ENDIF}); virtual;
procedure Finalize; virtual;
// some utilities
class Function SysParamUsableSize(RecipientIs32Bit: Boolean): TMemSize; virtual;
public
class Function MaxDataSize: TMemSize; virtual;
class Function ValueTypeStr(ValueType: TWMCValueType): String; virtual;
constructor Create(const DomainName: String = ''{$IFDEF UseWindowsMessages}; ReceivingWindow: TUtilityWindow = nil{$ENDIF});
destructor Destroy; override;
Function LowIndex: Integer; override;
Function HighIndex: Integer; override;
Function ConnectionIndexOf(ConnectionID: TWMCConnectionID): Integer; virtual;
Function ConnectionFind(ConnectionID: TWMCConnectionID; out Index: Integer): Boolean; virtual;
Function ConnectionsCheck: Boolean; virtual;
procedure Update(WaitForValue: Boolean = False); virtual;
Function SendBool(Recipient: TWMCConnectionID; Value: Boolean; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostBool(Recipient: TWMCConnectionID; Value: Boolean; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendUInt8(Recipient: TWMCConnectionID; Value: UInt8; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostUInt8(Recipient: TWMCConnectionID; Value: UInt8; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendInt8(Recipient: TWMCConnectionID; Value: Int8; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostInt8(Recipient: TWMCConnectionID; Value: Int8; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendUInt16(Recipient: TWMCConnectionID; Value: UInt16; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostUInt16(Recipient: TWMCConnectionID; Value: UInt16; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendInt16(Recipient: TWMCConnectionID; Value: Int16; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostInt16(Recipient: TWMCConnectionID; Value: Int16; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendUInt32(Recipient: TWMCConnectionID; Value: UInt32; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostUInt32(Recipient: TWMCConnectionID; Value: UInt32; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendInt32(Recipient: TWMCConnectionID; Value: Int32; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostInt32(Recipient: TWMCConnectionID; Value: Int32; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendUInt64(Recipient: TWMCConnectionID; Value: UInt64; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostUInt64(Recipient: TWMCConnectionID; Value: UInt64; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendInt64(Recipient: TWMCConnectionID; Value: Int64; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostInt64(Recipient: TWMCConnectionID; Value: Int64; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendInteger(Recipient: TWMCConnectionID; Value: Integer; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostInteger(Recipient: TWMCConnectionID; Value: Integer; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendFloat32(Recipient: TWMCConnectionID; Value: Float32; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostFloat32(Recipient: TWMCConnectionID; Value: Float32; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendFloat64(Recipient: TWMCConnectionID; Value: Float64; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostFloat64(Recipient: TWMCConnectionID; Value: Float64; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendFloat(Recipient: TWMCConnectionID; Value: Float64; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostFloat(Recipient: TWMCConnectionID; Value: Float64; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendString(Recipient: TWMCConnectionID; const Value: String; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostString(Recipient: TWMCConnectionID; const Value: String; UserData: TWMCUserData = 0): Boolean; virtual;
Function SendData(Recipient: TWMCConnectionID; const Data; Size: TMemSize; UserData: TWMCUserData = 0): Boolean; virtual;
Function PostData(Recipient: TWMCConnectionID; const Data; Size: TMemSize; UserData: TWMCUserData = 0): Boolean; virtual;
property LargeDataThreshold: TMemSize read fLargeDataThreshold write fLargeDataThreshold;
property DomainName: String read fDomainName;
property ConnectionID: TWMCConnectionID read fConnectionID;
property Connections[Index: Integer]: TWMCConnection read GetConnection; default;
property OnIncomingValueCallback: TWMCValueCallback read fOnIncomingValueCallback write fOnIncomingValueCallback;
property OnIncomingValueEvent: TWMCValueEvent read fOnIncomingValueEvent write fOnIncomingValueEvent;
property OnIncomingValue: TWMCValueEvent read fOnIncomingValueEvent write fOnIncomingValueEvent;
property OnConnectionChangeCallback: TNotifyCallback read fOnConnectionCallback write fOnConnectionCallback;
property OnConnectionChangeEvent: TNotifyEvent read fOnConnectionEvent write fOnConnectionEvent;
property OnConnectionChange: TNotifyEvent read fOnConnectionEvent write fOnConnectionEvent;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWinMsgCommPeer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWinMsgCommPeer - class declaration
===============================================================================}
type
TWinMsgCommPeer = class(TWinMsgComm)
protected
Function ProcessMessage(Sender: TWMCConnectionID; MessageCode: TWMCMessageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; override;
procedure Initialize(const DomainName: String{$IFDEF UseWindowsMessages}; ReceivingWindow: TUtilityWindow{$ENDIF}); override;
procedure Finalize; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWinMsgCommServer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWinMsgCommServer - class declaration
===============================================================================}
type
TWinMsgCommServer = class(TWinMsgComm)
protected
fInitComplete: Boolean;
Function ProcessMessage(Sender: TWMCConnectionID; MessageCode: TWMCMessageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; override;
procedure Initialize(const DomainName: String{$IFDEF UseWindowsMessages}; ReceivingWindow: TUtilityWindow{$ENDIF}); override;
procedure Finalize; override;
public
class Function ServerPresentOnDomain(const DomainName: String = ''): Boolean; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWinMsgCommClient
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWinMsgCommClient - class declaration
===============================================================================}
type
TWinMsgCommClient = class(TWinMsgComm)
protected
fServerID: TWMCConnectionID;
Function ProcessMessage(Sender: TWMCConnectionID; MessageCode: TWMCMessageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean; override;
procedure Initialize(const DomainName: String{$IFDEF UseWindowsMessages}; ReceivingWindow: TUtilityWindow{$ENDIF}); override;
procedure Finalize; override;
public
Function ServerOnline: Boolean;
Function SendBool(Value: Boolean; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostBool(Value: Boolean; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendUInt8(Value: UInt8; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostUInt8(Value: UInt8; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendInt8(Value: Int8; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostInt8(Value: Int8; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendUInt16(Value: UInt16; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostUInt16(Value: UInt16; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendInt16(Value: Int16; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostInt16(Value: Int16; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendUInt32(Value: UInt32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostUInt32(Value: UInt32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendInt32(Value: Int32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostInt32(Value: Int32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendUInt64(Value: UInt64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostUInt64(Value: UInt64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendInt64(Value: Int64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostInt64(Value: Int64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendInteger(Value: Integer; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostInteger(Value: Integer; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendFloat32(Value: Float32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostFloat32(Value: Float32; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendFloat64(Value: Float64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostFloat64(Value: Float64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendFloat(Value: Float64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostFloat(Value: Float64; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendString(const Value: String; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostString(const Value: String; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function SendData(const Data; Size: TMemSize; UserData: TWMCUserData = 0): Boolean; reintroduce;
Function PostData(const Data; Size: TMemSize; UserData: TWMCUserData = 0): Boolean; reintroduce;
end;
implementation
uses
Math,
StrRect, InterlockedOps;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$DEFINE W5028:={$WARN 5028 OFF}} // Local $1 "$2" is not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TWinMsgComm
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWinMsgComm - implementation types and constants
===============================================================================}
type
TWMCSharedDataHeader = packed record
MessageCode: TWMCMEssageCode;
DataSize: UInt64;
Payload: record end; // zero-size field
end;
PWMCSharedDataHeader = ^TWMCSharedDataHeader;
const
WMC_MAXDATASIZE = 512 * 1024 * 1024; // 512 MiB
WMC_NAMEPREFIX_GLBDATA = 'wmc_glbdata_';
WMC_NAMEPREFIX_MSGNAME = 'wmc_msgname_';
WMC_NAMEPREFIX_SHRDATA = 'wmc_shrdata_';
WMC_FLAG_INITIALIZED = UInt32($00000001);
WMC_FLAG_WINDOWSMSGS = UInt32($00000002);
WMC_FLAG_SERVERPRESENT = UInt32($00000004);
WMC_USERDATA_FLAG_32BIT = Int8($01);
WMC_MSGRES_ERR = 0;
WMC_MSGRES_OK = 1;
{-------------------------------------------------------------------------------
TWinMsgComm - message codes
-------------------------------------------------------------------------------}
{
sync whether the message is sent (+) or posted (-)
ret returned value (message result)
sender valid sender
(!) user data contains some additional info
? can be both sent or posted
R returns WMC_MSGRES_OK after successful processing (sent messages only)
/ returned value is ignored
}
//------------------------------------|------------- payload -----------|- sync -|- ret -|- sender -|- notes -
const
WMC_MSG_PING = $00; // sender system ID | + | R | any | used to check connection
WMC_MSG_SERVERONLINE = $01; // server system ID (!) | + | / | server | broadcasted when server connects
WMC_MSG_SERVEROFFLINE = $02; // server system ID | - | / | server | broadcasted when server is disconnecting
WMC_MSG_SERVER = $03; // server system ID (!) | + | / | server | sent in response to WMC_MSG_CLIENTONLINE
WMC_MSG_CLIENTONLINE = $04; // client system ID (!) | + | / | client | broadcasted when client connects
WMC_MSG_CLIENTOFFLINE = $05; // client system ID | - | / | client | sent to server (if any is connected) when client is disconnecting
WMC_MSG_CLIENT = $06; // client system ID (!) | + | / | client | sent in response to WMC_MSG_SERVERONLINE
WMC_MSG_PEERONLINE = $07; // peer system ID (!) | + | / | peer | broadcasted when peer connects
WMC_MSG_PEEROFFLINE = $08; // peer system ID | - | / | peer | broadcasted when peer is disconnecting
WMC_MSG_PEER = $09; // peer system ID (!) | + | / | peer | sent in response to WMC_MSG_PEERONLINE
WMC_MSG_VAL_BOOL = $10; // 8bit boolean value (ByteBool) | ? | / | any |
WMC_MSG_VAL_UINT8 = $11; // 8bit unsigned integer value | ? | / | any |
WMC_MSG_VAL_INT8 = $12; // 8bit signed integer value | ? | / | any |
WMC_MSG_VAL_UINT16 = $13; // 16bit unsigned integer value | ? | / | any |
WMC_MSG_VAL_INT16 = $14; // 16bit signed integer value | ? | / | any |
WMC_MSG_VAL_UINT32 = $15; // 32bit unsigned integer value | ? | / | any |
WMC_MSG_VAL_INT32 = $16; // 32bit signed integer value | ? | / | any |
WMC_MSG_VAL_UINT64 = $17; // 64bit unsigned integer value | ? | / | any |
WMC_MSG_VAL_INT64 = $18; // 64bit signed integer value | ? | / | any |
WMC_MSG_VAL_FLOAT32 = $19; // 32bit floating point value | ? | / | any |
WMC_MSG_VAL_FLOAT64 = $1A; // 64bit floating point value | ? | / | any |
WMC_MSG_STRING0 = $20; // 0-length string | ? | / | any |
//WMC_MSG_STRING1 = $21; // string of length 1 | ? | / | any |
//WMC_MSG_STRING2 = $22; // string of length 2 | ? | / | any |
//WMC_MSG_STRING3 = $23; // string of length 3 | ? | / | any |
//WMC_MSG_STRING4 = $24; // string of length 4 | ? | / | any |
//WMC_MSG_STRING5 = $25; // string of length 5 | ? | / | any |
//WMC_MSG_STRING6 = $26; // string of length 6 | ? | / | any |
//WMC_MSG_STRING7 = $27; // string of length 7 | ? | / | any |
WMC_MSG_STRING8 = $28; // string of length 8 | ? | / | any |
WMC_MSG_DATA0 = $30; // 0 bytes of data | ? | / | any |
//WMC_MSG_DATA1 = $31; // 1 byte of data | ? | / | any |
//WMC_MSG_DATA2 = $32; // 2 bytes of data | ? | / | any |
//WMC_MSG_DATA3 = $33; // 3 bytes of data | ? | / | any |
//WMC_MSG_DATA4 = $34; // 4 bytes of data | ? | / | any |
//WMC_MSG_DATA5 = $35; // 5 bytes of data | ? | / | any |
//WMC_MSG_DATA6 = $36; // 6 bytes of data | ? | / | any |
//WMC_MSG_DATA7 = $37; // 7 bytes of data | ? | / | any |
WMC_MSG_DATA8 = $38; // 8 bytes of data | ? | / | any |
WMC_MSG_DATA = $39; // shared data size and ID | + | / | any |
WMC_MSG_TRANS_START = $40; // size of sent data | + | R | any | transaction start
WMC_MSG_TRANS_END_UINT64 = $41; // data checksum | + | R | any | also used in elsewhere to identify data type
WMC_MSG_TRANS_END_INT64 = $42; // data checksum | + | R | any | -//-
WMC_MSG_TRANS_END_FLOAT64 = $43; // data checksum | + | R | any | -//-
WMC_MSG_TRANS_END_STRING = $44; // data checksum | + | R | any | -//-
WMC_MSG_TRANS_END_DATA = $45; // data checksum | + | R | any | -//-
WMC_MSG_TRANS_BUFF0 = $50; // 0 bytes of data | + | R | any | normally unused
//WMC_MSG_TRANS_BUFF1 = $51; // 1 byte of data | + | R | any |
//WMC_MSG_TRANS_BUFF2 = $52; // 2 bytes of data | + | R | any |
//WMC_MSG_TRANS_BUFF3 = $53; // 3 bytes of data | + | R | any |
//WMC_MSG_TRANS_BUFF4 = $54; // 4 bytes of data | + | R | any |
//WMC_MSG_TRANS_BUFF5 = $55; // 5 bytes of data | + | R | any |
//WMC_MSG_TRANS_BUFF6 = $56; // 6 bytes of data | + | R | any |
//WMC_MSG_TRANS_BUFF7 = $57; // 7 bytes of data | + | R | any |
WMC_MSG_TRANS_BUFF8 = $58; // 8 bytes of data | + | R | any |
{===============================================================================
TWinMsgComm - auxiliary functions
===============================================================================}
type
TWMCMsgMetaData = packed record
ConnectionID: TWMCConnectionID;
MessageCode: TWMCMessageCode;
UserData: TWMCUserData;
end;
TWMCMsgSharedData = packed record
DataSize: UInt32; // do not use TMemSize as it changes size depending on platform
SharedDataID: TWMCSharedDataID
end;
//------------------------------------------------------------------------------
Function GetConnectionID(MetaData: TWMCMessagePayload): TWMCConnectionID;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := TWMCMsgMetaData(UInt32(MetaData)).ConnectionID;
end;
//------------------------------------------------------------------------------
Function GetMessageCode(MetaData: TWMCMessagePayload): TWMCMessageCode;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := TWMCMsgMetaData(UInt32(MetaData)).MessageCode;
end;
//------------------------------------------------------------------------------
Function GetUserData(MetaData: TWMCMessagePayload): TWMCUserData;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := TWMCMsgMetaData(UInt32(MetaData)).UserData;
end;
//------------------------------------------------------------------------------
Function GetMessageMetaData(ConnectionID: TWMCConnectionID; MessageCode: TWMCMessageCode; UserData: TWMCUserData): TWMCMessagePayload;{$IFDEF CanInline} inline;{$ENDIF}
var
MetaData: TWMCMsgMetaData;
begin
MetaData.ConnectionID := ConnectionID;
MetaData.MessageCode := MessageCode;
MetaData.UserData := UserData;
Result := TWMCMessagePayload(UInt32(MetaData));
end;
//------------------------------------------------------------------------------
Function GetSharedDataSize(MetaData: TWMCMessagePayload): TMemSize;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := TMemSize(TWMCMsgSharedData(MetaData).DataSize);
end;
//------------------------------------------------------------------------------
Function GetSharedDataID(MetaData: TWMCMessagePayload): TWMCSharedDataID;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := TWMCMsgSharedData(MetaData).SharedDataID;
end;
//------------------------------------------------------------------------------
Function GetSharedPayload(DataSize: TMemSize; SharedDataID: TWMCSharedDataID): TWMCMessagePayload;{$IFDEF CanInline} inline;{$ENDIF}
var
TempPayload: TWMCMsgSharedData;
begin
TempPayload.DataSize := UInt32(DataSize);
TempPayload.SharedDataID := SharedDataID;
Result := TWMCMessagePayload(TempPayload);
end;
//------------------------------------------------------------------------------
Function BoolToInt(Value: Boolean): Integer;{$IFDEF CanInline} inline;{$ENDIF}
begin
If Value then
Result := 1
else
Result := 0;
end;
//------------------------------------------------------------------------------
Function IntToBool(Value: Integer): Boolean;{$IFDEF CanInline} inline;{$ENDIF}
begin
Result := Value <> 0;
end;
//------------------------------------------------------------------------------
Function TransactionEndCodeToValueCode(Code: Integer): Integer;
begin
case Code of
WMC_MSG_TRANS_END_UINT64: Result := WMC_MSG_VAL_UINT64;
WMC_MSG_TRANS_END_INT64: Result := WMC_MSG_VAL_INT64;
WMC_MSG_TRANS_END_FLOAT64: Result := WMC_MSG_VAL_FLOAT64;
else
raise EWMCInvalidValue.CreateFmt('TransactionEndCodeToValueCode: Invalid transaction end code (%d).',[Code]);
end;
end;
{===============================================================================
TWinMsgComm - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TWinMsgComm - protected methods
-------------------------------------------------------------------------------}
Function TWinMsgComm.GetConnection(Index: Integer): TWMCConnection;
begin
If CheckIndex(Index) then
begin
Result.ConnectionID := fConnections[Index].ConnectionID;
Result.Is32Bit := fConnections[Index].Is32bit;
Result.Transacting := fConnections[Index].Transacting;
end
else raise EWMCIndexOutOfBounds.CreateFmt('TWinMsgComm.GetConnection: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.GetCapacity: Integer;
begin
Result := Length(fConnections);
end;
//------------------------------------------------------------------------------
procedure TWinMsgComm.SetCapacity(Value: Integer);
begin
SetLength(fConnections,Value);
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.GetCount: Integer;
begin
Result := fConnectionCount;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TWinMsgComm.SetCount(Value: Integer);
begin
// do nothing
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
Function TWinMsgComm.ConnectionAdd(ConnectionID: TWMCConnectionID; Is32bit: Boolean; SystemID: TWMCSystemID): Integer;
begin
If ConnectionID <> WMC_BROADCAST then
begin
If not ConnectionFind(ConnectionID,Result) then
begin
Grow;
Result := fConnectionCount;
fConnections[Result].ConnectionID := ConnectionID;
fConnections[Result].Is32bit := Is32Bit;
fConnections[Result].SystemID := SystemID;
fConnections[Result].Transacting := False;
fConnections[Result].Transaction.DataPtr := nil;
fConnections[Result].Transaction.DataSize := 0;
fConnections[Result].Transaction.Position := 0;
fConnections[Result].Transaction.CheckSum := ZeroCRC32;
Inc(fConnectionCount);
{$IFNDEF ConserveMemory}
fIDToIndexTable[ConnectionID] := Result;
{$ENDIF}
DoConnectionChange;
end
else raise EWMCInvalidConnection.CreateFmt('TWinMsgComm.ConnectionAdd: Connection (%u) already exists.',[ConnectionID]);
end
else raise EWMCInvalidConnection.CreateFmt('TWinMsgComm.ConnectionAdd: Invalid connection ID (%u)',[ConnectionID]);
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.ConnectionRemove(ConnectionID: TWMCConnectionID): Integer;
begin
If ConnectionID <> WMC_BROADCAST then
begin
If ConnectionFind(ConnectionID,Result) then
ConnectionDelete(Result);
end
else raise EWMCInvalidConnection.CreateFmt('TWinMsgComm.ConnectionRemove: Invalid connection ID (%u)',[ConnectionID]);
end;
//------------------------------------------------------------------------------
procedure TWinMsgComm.ConnectionDelete(Index: Integer);
var
i: Integer;
begin
If CheckIndex(Index) then
begin
{$IFNDEF ConserveMemory}
fIDToIndexTable[Connections[Index].ConnectionID] := -1;
{$ENDIF}
If fConnections[Index].Transacting then
with fConnections[Index].Transaction do
FreeMem(DataPtr,DataSize);
For i := Index to Pred(HighIndex) do
begin
fConnections[i] := fConnections[i + 1];
{$IFNDEF ConserveMemory}
fIDToIndexTable[Connections[i].ConnectionID] := i;
{$ENDIF}
end;
Dec(fConnectionCount);
Shrink;
DoConnectionChange;
end
else raise EWMCIndexOutOfBounds.CreateFmt('TWinMsgComm.ConnectionDelete: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.TransactionStart(Sender: TWMCConnectionID; DataSize: TMemSize): Boolean;
var
Index: Integer;
begin
Result := False;
If ConnectionFind(Sender,Index) then
If not fConnections[Index].Transacting then
begin
fConnections[Index].Transacting := True;
fConnections[Index].Transaction.DataPtr := AllocMem(DataSize);
fConnections[Index].Transaction.DataSize := DataSize;
fConnections[Index].Transaction.Position := 0;
fConnections[Index].Transaction.CheckSum := InitialCRC32;
Result := True;
end;
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.TransactionBuff(Sender: TWMCConnectionID; Payload: TWMCMessagePayload; Size: TMemSize): Boolean;
var
Index: Integer;
begin
Result := False;
If ConnectionFind(Sender,Index) then
with fConnections[Index] do
If Transacting and (Transaction.Position + Size <= Transaction.DataSize) then
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
Move(Payload,Pointer(PtrUInt(Transaction.DataPtr) + PtrUInt(Transaction.Position))^,Size);
{$IFDEF FPCDWM}{$POP}{$ENDIF}
Transaction.CheckSum := BufferCRC32(Transaction.CheckSum,Payload,Size);
Inc(Transaction.Position,Size);
Result := True;
end;
end;
//------------------------------------------------------------------------------
Function TWinMsgComm.TransactionEnd(Sender: TWMCConnectionID; MessageCode: TWMCMEssageCode; UserData: TWMCUserData; Payload: TWMCMessagePayload; Sent: Boolean): Boolean;
var
Index: Integer;
TempValue: TWMCValue;
begin
Result := False;
If ConnectionFind(Sender,Index) then
with fConnections[Index] do
If Transacting and (Transaction.Position = Transaction.DataSize) and
SameCRC32(Transaction.CheckSum,TCRC32(UInt32(Payload))) then
begin
TempValue.UserData := UserData;
TempValue.StringValue := '';
If DataToValue(TempValue,MessageCode,Transaction.DataPtr,Transaction.DataSize) then
begin
DoIncomingValue(Sender,TempValue,Sent);
Result := True;
end;
Transacting := False;
FreeMem(Transaction.DataPtr,Transaction.DataSize);
end;
end;
//------------------------------------------------------------------------------
class Function TWinMsgComm.DataToValue(var Value: TWMCValue; MessageCode: TWMCMessageCode; DataPtr: Pointer; DataSize: TMemSize): Boolean;
var
TempStr: UTF8String;
begin
Result := False;
case MessageCode of
WMC_MSG_TRANS_END_UINT64:
If DataSize = 8 then
begin
Value.ValueType := mvtUInt64;
Move(DataPtr^,Value.UInt64Value,DataSize);
Result := True;
end;
WMC_MSG_TRANS_END_INT64:
If DataSize = 8 then
begin
Value.ValueType := mvtInt64;
Move(DataPtr^,Value.Int64Value,DataSize);
Result := True;
end;
WMC_MSG_TRANS_END_FLOAT64:
If DataSize = 8 then
begin
Value.ValueType := mvtFloat64;