-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassReplicationRelayClient.pas
556 lines (481 loc) · 15.9 KB
/
ClassReplicationRelayClient.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
{
mysql-replication-relay
Copyright (C) 2023 Freedelity
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
}
unit ClassReplicationRelayClient;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$UNDEF REPDEBUG}
interface
uses
ReplicationRelayStructures,Declarations, ClassModule, Dateutils, Classes, SysUtils, ClassDebug, IdTCPClient, IdIOHandler, IdGlobal, StrUtils,HFNV1A;
const
CONST_NOTHING=0;
type
TReplicationRelayClient = class
protected
FTCPClient: TIdTCPClient;
FHBufferStream: TMemoryStream;
FBufferStream: TMemoryStream;
FServerHost: String;
FServerPort: Integer;
FClientID: Integer;
FClientToken: Integer;
public
ClientDisconnected: Boolean;
constructor Create;
destructor Destroy; override;
function Connect(Host: String;Port : Integer): Integer;
procedure SetClientID(AClientID: Integer; AToken: DWord); overload;
procedure SetClientID(AClientID: Integer; ASecret: String); overload;
function SendCommand(ACommand,ASubcommand: Integer): Integer;
function GetReply(AStream: TMemoryStream):Integer;
function CommandPing(): Integer;
function CommandAuth(AClientName: String; AAuthKey: QWord): Integer;
function CommandReqFilter(AReqType: Byte; ASchemaName,ATableName: String;ADiscardType: Byte; AQueueLimit: Integer): Integer;
function CommandPoll(): TQueueItem;
function CommandPollV2(): TQueueItem;
published
end;
implementation
{******************************************************************************}
function PopStringFromStream(AStream:TMemoryStream): String;
var
i,len: DWord;
begin
Result:='';
len:=AStream.ReadDWord();
{$IFDEF REPDEBUG}
AddToLog(llWarning,'strlen = '+IntToStr(len));
{$ENDIF}
if(len>1) then // 0 size string has size 1 (null terminator)
for i:=0 to len-2 do
Result:=Result+Char(AStream.ReadByte());
AStream.ReadByte(); // Read $00
{$IFDEF REPDEBUG}
AddToLog(llWarning,'PopStringFromStream read ['+Result+']');
{$ENDIF}
end;
{******************************************************************************}
{ TReplicationRelayClient }
{******************************************************************************}
procedure PushStringToStream(AStream:TMemoryStream;AString: String);
var
len: DWord;
begin
len:=Length(AString)+1;
AStream.WriteDWord(len);
AStream.Write(AString[1],Length(AString));
AStream.WriteByte(0);
end;
{******************************************************************************}
constructor TReplicationRelayClient.Create;
begin
FClientID:=0;
FClientToken:=0;
ClientDisconnected:=true;
FTCPClient:=nil;
FHBufferStream:=TMemoryStream.Create;
FBufferStream:=TMemoryStream.Create;
end;
{******************************************************************************}
destructor TReplicationRelayClient.Destroy;
begin
FreeAndNil(FBufferStream);
FreeAndNil(FHBufferStream);
inherited;
end;
{******************************************************************************}
procedure TReplicationRelayClient.SetClientID(AClientID: Integer; AToken: DWord); overload;
begin
FClientID:=AClientID;
FClientToken:=AToken;
end;
{******************************************************************************}
procedure TReplicationRelayClient.SetClientID(AClientID: Integer; ASecret: String); overload;
begin
FClientID:=AClientID;
FClientToken:=FNV1A(ASecret);
end;
{******************************************************************************}
function TReplicationRelayClient.Connect(Host: String;Port : Integer): Integer;
var
tmp: String;
resu,i: Integer;
begin
Result:=0;
AddToLog(llWarning,'TReplicationRelayClient.Init');
FServerHost:=Host;
FServerPort:=Port;
try
if(FTCPClient<>nil) then
begin
AddToLog(llWarning,'Reconnect Detected');
FreeAndNil(FTCPClient); // This is a reconnect
end;
FTCPClient:=TIdTCPClient.Create;
FTCPClient.Host:=FServerHost;
FTCPClient.Port:=FServerPort;
FTCPClient.Connect();
except
on E:Exception do
begin
AddToLog(llError,'Error connecting to host '+FServerHost+' : '+E.Message);
Exit;
end;
end;
if(FTCPClient.Connected) then
begin
ClientDisconnected:=false;
AddToLog(llWarning,'Re/Connection to Relay Server Successful');
resu:=CommandPing();
if(resu=0) then
begin
AddToLog(llWarning,'Ping failed');
Exit;
end;
AddToLog(llWarning,'Ping successful');
Result:=1;
end;
end;
{******************************************************************************}
function TReplicationRelayClient.SendCommand(ACommand,ASubcommand: Integer): Integer;
var
i: Integer;
lHeader: TReplicationHeader;
begin
Result:=0;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'SendCommand '+IntToStr(ACommand)+','+IntToStr(ASubcommand)+', payload size = '+IntToStr(FBufferStream.Size));
{$ENDIF}
lHeader.Command:=ACommand;
lHeader.Subcommand:=ASubcommand;
lHeader.PayloadSize:=FBufferStream.Size;
lHeader.Version:=REPRELAY_VERSION;
lHeader.ClientId:=FClientID;
lHeader.ClientToken:=FClientToken;
lHeader.Reserved1:=0;
lHeader.Reserved2:=0;
lHeader.Checksum:=lHeader.Command+lHeader.PayloadSize+lHeader.Subcommand+lHeader.Version+lHeader.ClientId+lHeader.ClientToken+lHeader.Reserved1+lHeader.Reserved2;
//AddToLog(llWarning,'Header Checksum = '+IntToHex(lHeader.Checksum,8));
if(FBufferStream.Size>0) then
begin
FBufferStream.Seek(0, soFromBeginning);
for i:=0 to FBufferStream.Size-1 do
lHeader.Checksum:=lHeader.Checksum+Ord(FBufferStream.ReadByte());
FBufferStream.Seek(0, soFromBeginning);
end;
try
FTCPClient.Socket.Write(RawToBytes(lHeader,Sizeof(lHeader)));
if(FBufferStream.Size>0) then
begin
FTCPClient.Socket.Write(FBufferStream,FBufferStream.Size);
end;
Result:=1;
except
on E:Exception do
begin
AddToLog(llError, 'TReplicationRelayClient.SendCommand (Disconnect?) => '+E.Message);
Sleep(1000); // Prevent cycling fast
Result:=0;
end;
end;
end;
{******************************************************************************}
function TReplicationRelayClient.GetReply(AStream: TMemoryStream):Integer;
var
lHeader: TReplicationReplyHeader;
i,lCheck: Integer;
lBuf: TIdBytes;
str: String;
tickcount: QWord;
begin
tickcount:=GetTickCount64;
try
FTCPClient.IOHandler.ReadBytes(lBuf,SizeOf(lHeader));
except
on E:Exception do
begin
AddToLog(llError, 'TReplicationRelayClient.GetReply (Disconnect?) => '+E.Message);
Sleep(1000); // Prevent cycling fast
Result:=$F0; // Disconnect
Exit;
end;
end;
BytesToRaw(lBuf,lHeader,SizeOf(lHeader));
{$IFDEF REPDEBUG}
AddToLog(llWarning,'Got reply '+IntToStr(lHeader.Result)+' : Payload size '+IntToStr(lHeader.PayloadSize)+' : Checksum '+IntToHex(lHeader.Checksum,4));
{$ENDIF}
{$IFDEF REPDEBUG}
AddToLog(llWarning,'Payload Size = '+IntToStr(lHeader.PayloadSize));
{$ENDIF}
AStream.Clear();
try
tickcount:=GetTickCount64;
FTCPClient.IOHandler.ReadStream(AStream,lHeader.PayloadSize);
if((GetTickCount64-tickcount)>1000) then AddToLog(llWarning,'Removed Halt, but ReadStram Delay = '+IntToStr(GetTickCount64-tickcount)+' ms');
except
on E:Exception do
begin
AddToLog(llError, 'TReplicationRelayClient.GetReply (Disconnect?) => '+E.Message);
Sleep(1000); // Prevent cycling fast
Result:=$F0; // Disconnect
Exit;
end;
end;
AStream.Seek(0,soFromBeginning);
lCheck:=lHeader.Result+lHeader.PayloadSize;
//AddToLog(llWarning,'Checksum header = '+IntToHex(lCheck,4));
for i:=0 to lHeader.PayloadSize-1 do
begin
lCheck:=lCheck+AStream.ReadByte;
end;
if(lCheck<>lHeader.Checksum) then
begin
AddToLog(llWarning,'Checksum mismatch in reply + '+IntToHex(lCheck,4)+' <> '+IntToHex(lHeader.Checksum,4));
Result:=$FE;
end
else
Result:=lHeader.Result;
end;
{******************************************************************************}
function TReplicationRelayClient.CommandPing(): Integer;
var
tmp: String;
resu,len,i: Integer;
lStr: String;
begin
Result:=0;
FBufferStream.Clear();
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPing');
{$ENDIF}
resu:=SendCommand(REPRELAY_PING,0);
if(resu=0) then
begin
Result:=0;
Exit;
end;
resu:=GetReply(FBufferStream);
if(resu=$F0) then
begin
Result:=0;
Exit;
end;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPing Result : '+IntToStr(resu));
AddToLog(llWarning,'Payload Size : '+IntToStr(FBufferStream.Size));
{$ENDIF}
FBufferStream.Seek(0, soFromBeginning);
lStr:='';
for i:=0 to FBufferStream.Size-1 do
lStr:=lStr+Char(FBufferStream.ReadByte());
{$IFDEF REPDEBUG}
AddToLog(llWarning,'Message : '+lStr);
{$ENDIF}
if(resu=0) then Result:=1;
end;
{******************************************************************************}
function TReplicationRelayClient.CommandAuth(AClientName: String; AAuthKey: QWord): Integer;
var
tmp: String;
resu,len,i: Integer;
lStr: String;
lPayload: TReplicationMessageAuth;
begin
Result:=0;
FBufferStream.Clear();
AddToLog(llWarning,'CommandAuth');
AddToLog(llWarning,'AuthKey = '+IntToHEx(AAuthKey,16));
lPayload.AuthKey:=AAuthKey;
lPayload.ClientID:=0;
lPayload.ClientToken:=0;
lPayload.ClientNameSize:=Length(AClientName)+1;
FBufferStream.Write(lPayload,8+4+4+4);
FBufferStream.Write(AClientName[1],Length(AClientName));
FBufferStream.WriteByte(0);
resu:=SendCommand(REPRELAY_AUTH,0);
if(resu=0) then
begin
Result:=0;
Exit;
end;
resu:=GetReply(FBufferStream);
if(resu=$F0) then
begin
Result:=0;
Exit;
end;
FBufferStream.Seek(0, soFromBeginning);
FBufferStream.Read(lPayload,8+4+4);
if(resu=0) then
begin
FClientID:=lPayload.ClientID;
FClientToken:=lPayload.ClientToken;
end;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandAuth Result : '+IntToStr(resu));
AddToLog(llWarning,'Payload Size : '+IntToStr(FBufferStream.Size));
{$ENDIF}
AddToLog(llWarning,'Client ID : '+IntToStr(lPayload.ClientID)+' Token : '+IntToHex(lPayload.ClientToken,8));
if(resu=0) then Result:=1;
end;
{******************************************************************************}
function TReplicationRelayClient.CommandReqFilter(AReqType: Byte; ASchemaName,ATableName: String; ADiscardType: Byte; AQueueLimit: Integer): Integer;
var
tmp: String;
resu,len,i: Integer;
lStr: String;
begin
Result:=0;
FBufferStream.Clear();
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandReqFilter');
{$ENDIF}
FBufferStream.WriteByte(AReqType);
FBufferStream.WriteByte(ADiscardType);
FBufferStream.WriteDWord(AQueueLimit);
len:=Length(ASchemaName)+1; // Extra Null
FBufferStream.WriteDWord(len);
FBufferStream.Write(ASchemaName[1],Length(ASchemaName));
FBufferStream.WriteByte(0);
len:=Length(ATableName)+1; // Extra Null
FBufferStream.WriteDWord(len);
FBufferStream.Write(ATableName[1],Length(ATableName));
FBufferStream.WriteByte(0);
resu:=SendCommand(REPRELAY_ADDFILTER,0);
if(resu=0) then
Begin
Result:=0;
Exit;
End;
resu:=GetReply(FBufferStream);
if(resu=$F0) then
begin
Result:=0;
Exit;
end;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandReqFilter Result : '+IntToStr(resu));
{$ENDIF}
if(resu=0) then Result:=1;
end;
{******************************************************************************}
function TReplicationRelayClient.CommandPoll(): TQueueItem;
var
resu,i: Integer;
lQueueItem: TQueueItem;
begin
Result:=nil;
FBufferStream.Clear();
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPoll');
{$ENDIF}
resu:=SendCommand(REPRELAY_POLL,0);
if(resu=0) then
Begin
ClientDisconnected:=true;
Exit;
End;
resu:=GetReply(FBufferStream);
//AddToLog(llWarning,'CommandPoll:GotReply');
if(resu=$F0) then
begin
AddToLog(llWarning,'Client Disconnected');
ClientDisconnected:=true;
Exit;
end;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPoll Result : '+IntToStr(resu));
{$ENDIF}
if(resu=0) then
begin
lQueueItem:=TQueueItem.Create;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'FBufferStream Length : '+IntToStr(FBufferStream.Size));
{$ENDIF}
FBufferStream.Seek(0, soFromBeginning);
lQueueItem.EventType:=FBufferStream.ReadByte();
lQueueItem.EventPosition:=FBufferStream.ReadQWord();
lQueueItem.QueueSize:=FBufferStream.ReadDWord();
lQueueItem.SchemaName:=PopStringFromStream(FBufferStream);
lQueueItem.TableName:=PopStringFromStream(FBufferStream);
i:=FBufferStream.ReadDWord();
SetLength(lQueueItem.Cols,i);
for i:=0 to Length(lQueueItem.Cols)-1 do
begin
lQueueItem.Cols[i].Name:=PopStringFromStream(FBufferStream);
lQueueItem.Cols[i].Before:=PopStringFromStream(FBufferStream);
lQueueItem.Cols[i].After:=PopStringFromStream(FBufferStream);
end;
Result:=lQueueItem;
end;
end;
{******************************************************************************}
function TReplicationRelayClient.CommandPollV2(): TQueueItem;
var
resu,i: Integer;
lQueueItem: TQueueItem;
begin
Result:=nil;
FBufferStream.Clear();
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPollV2');
{$ENDIF}
resu:=SendCommand(REPRELAY_POLLV2,0);
if(resu=0) then
Begin
ClientDisconnected:=true;
Exit;
End;
resu:=GetReply(FBufferStream);
if(resu=$F0) then
begin
AddToLog(llWarning,'Client Disconnected');
ClientDisconnected:=true;
Exit;
end;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'CommandPollV2 Result : '+IntToStr(resu));
//AddToLog(llWarning,'CommandPoll Result : '+IntToStr(resu));
{$ENDIF}
if(resu=0) then
begin
lQueueItem:=TQueueItem.Create;
{$IFDEF REPDEBUG}
AddToLog(llWarning,'FBufferStream Length : '+IntToStr(FBufferStream.Size));
{$ENDIF}
FBufferStream.Seek(0, soFromBeginning);
lQueueItem.EventType:=FBufferStream.ReadByte();
lQueueItem.EventPosition:=FBufferStream.ReadQWord();
lQueueItem.QueueSize:=FBufferStream.ReadDWord();
lQueueItem.InstanceID:=FBufferStream.ReadDWord();
lQueueItem.SchemaNameID:=FBufferStream.ReadDWord();
lQueueItem.SchemaName:=PopStringFromStream(FBufferStream);
lQueueItem.TableNameID:=FBufferStream.ReadDWord();
lQueueItem.TableName:=PopStringFromStream(FBufferStream);
i:=FBufferStream.ReadDWord();
SetLength(lQueueItem.Cols,i);
for i:=0 to Length(lQueueItem.Cols)-1 do
begin
lQueueItem.Cols[i].NameID:=FBufferStream.ReadDWord();
lQueueItem.Cols[i].Name:=PopStringFromStream(FBufferStream);
lQueueItem.Cols[i].Before:=PopStringFromStream(FBufferStream);
lQueueItem.Cols[i].After:=PopStringFromStream(FBufferStream);
end;
Result:=lQueueItem;
end;
end;
{******************************************************************************}
end.