-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsqfunctions.pas
859 lines (768 loc) · 24.1 KB
/
nsqfunctions.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
unit NSQFunctions;
{$mode ObjFPC}{$H+}
interface
uses
lazutf8, Interfaces, Classes, SysUtils,
IdTCPClient, IdHTTP,
fpjson, DateUtils, NSQTypes;
function NSQConnect(InClient: TIdTCPClient;
InIdentify: TNSQIdentify;
InHost: string;
InPort: Integer;
InTopic: string;
InChannel: string): boolean;
function NSQGetFrameType(typeId: Integer): TNSQFrameType;
function NSQGetHostName: string;
function FavForJSON(InValue: Integer): string; overload;
function FavForJSON(InValue: string): string; overload;
function FavForJSONWithNull(InValue: string): string;
function FavForJSON(InValue: Boolean): string; overload;
function NSQGetBigEndianInt(InInteger: integer): integer;
function NSQIdentifyToJson(var InIdentify: TNSQIdentify): string;
function NSQCreateMessage(InMemoryStream: TMemoryStream; InString: string): integer;
function NSQWriteMessage(InMemoryStream: TMemoryStream; InTCPClient: TIdTCPClient): integer;
function NSQSWriteSubscribeMessage(InTCPClient: TIdTCPClient; InTopic, InChannel: string): integer;
function NSQWriteMagicMessage(InTCPClient: TIdTCPClient): integer;
function NSQWriteIdentifyMessage(InTCPClient: TIdTCPClient; var InIdentify: TNSQIdentify): integer;
function NSQWriteNOPMessage(InTCPClient: TIdTCPClient): integer;
function NSQWritePUBMessage(InTCPClient: TIdTCPClient; InTopicName, InMessage: string): integer;
function NSQWriteMPUBMessage(InTCPClient: TIdTCPClient; InTopicName: string; InMessages: array of string): integer;
function NSQWriteDPUBMessage(InTCPClient: TIdTCPClient; InTopicName: string; InDeferTime: Int32; InMessage: string): integer;
function NSQWriteRDYMessage(InTCPClient: TIdTCPClient; InCount: Integer): integer;
function NSQWriteFINMessage(InTCPClient: TIdTCPClient; InMessageId: string): integer;
function NSQWriteREQMessage(InTCPClient: TIdTCPClient; InMessageId: string; InTimeout: Integer): integer;
function NSQWriteTOUCHMessage(InTCPClient: TIdTCPClient; InMessageId: string): integer;
function NSQWriteCLSMessage(InTCPClient: TIdTCPClient): integer;
function NSQReadMessage(MyInStream: TMemoryStream; IdTCPClient1: TIdTCPClient;
var OutNanoSecondTimestamp: int64; var OutAttempts: integer;
var OutMessageID: string; var OutBody: string): Integer;
procedure NSQProtoStream(InStream: TMemoryStream);
procedure NSQReadStream(InStream: TMemoryStream; var OutType: int32; var OutString: string);
function NSQGetTopicData(InNSQLookupUrl: string; InTopicName: string): string;
function FavEpochToDateTime(InEpoch: Int64): TDateTime;
function FavDateTimeToEpoch(InDateTime: TDateTime; InWithMilliSec: Boolean): Int64;
implementation
function NSQGetTopicData(InNSQLookupUrl: string; InTopicName: string): string;
var MyHttp: TIdHTTP;
MyResponse: string;
begin
MyHttp := nil;
MyResponse := '';
try
MyHttp := TIdHTTP.Create;
MyHttp.Request.ContentType := 'text/json';
MyHttp.Request.CharSet := 'utf-8';
MyHttp.Request.ContentEncoding := 'utf-8';
MyHttp.Request.Accept := 'text/json';
// MyHttp.Request.AcceptEncoding := 'utf-8, gzip';
if NSQ_DEBUG then begin
NSQWrite(InNSQLookupUrl + '/lookup?topic=%s', [InTopicName]);
end;
MyResponse := MyHttp.Get(Format(InNSQLookupUrl + '/lookup?topic=%s', [InTopicName]));
if MyHttp <> nil then FreeAndNil(MyHttp);
except
on E: Exception do begin
if NSQ_DEBUG then begin
NSQWrite('NSQGetTopicData: %s', [E.Message]);
end;
if MyHttp <> nil then FreeAndNil(MyHttp);
raise
end;
end;
Result := MyResponse;
end;
function FavEpochToDateTime(InEpoch: Int64): TDateTime;
var MyResult: TDateTime;
MyMilliSec: Integer;
begin
MyResult := 0;
try
if InEpoch <> 0 then begin
MyResult := UnixToDateTime(InEpoch);
// if time is toob big that means InEpoch contains milliseconds
// so devide with 1000 to get seconds and
// calculate millisecond
if MyResult > 200000 then begin
MyMilliSec := InEpoch mod 1000;
InEpoch := InEpoch div 1000;
MyResult := UnixToDateTime(InEpoch) + (MyMilliSec * OneMillisecond);
end
else begin
if MyResult < -200000 then begin
MyMilliSec := InEpoch mod 1000;
InEpoch := InEpoch div 1000;
MyResult := UnixToDateTime(InEpoch) - (MyMilliSec * OneMillisecond);
end
end;
end;
except
MyResult := 0;
end;
Result := MyResult;
end;
function FavDateTimeToEpoch(InDateTime: TDateTime; InWithMilliSec: Boolean
): Int64;
var MyResult: Int64;
begin
MyResult := DateTimeToUnix(InDateTime);
if InWithMilliSec then begin
if MyResult >= 0 then begin
MyResult := (MyResult * 1000) + StrToInt(FormatDateTime('zzz', InDateTime));
end
else begin
MyResult := (MyResult * 1000) - StrToInt(FormatDateTime('zzz', InDateTime));
end;
end;
Result := MyResult;
end;
procedure NSQProtoStream(InStream: TMemoryStream);
var F: integer;
MyByte: Byte;
begin
InStream.Position := 0;
for F := 0 to InStream.Size-1 do begin
InStream.read(MyByte, 1);
Write(Format('%2.2X', [MyByte]));
end;
InStream.Position := 0;
NSQWrite('', []);
InStream.Position := 0;
for F := 0 to InStream.Size-1 do begin
if F > 3 then begin
InStream.read(MyByte, 1);
Write(Format('%s', [Char(MyByte)]));
end;
end;
NSQWrite('', []);
InStream.Position := 0;
end;
procedure NSQReadStream(InStream: TMemoryStream; var OutType: Int32; var OutString: string);
var F: integer;
MyByte: Byte;
MyByteArray: array [0..3] of byte;
begin
OutType := -1;
OutString := '';
FillByte(MyByteArray, SizeOf(MyByteArray), 0);
InStream.Position := 0;
for F := 0 to InStream.Size-1 do begin
InStream.read(MyByte, 1);
if F > 3 then begin
OutString := OutString + Char(MyByte);
end
else begin
MyByteArray[F] := MyByte;
if F = 3 then begin
Move(MyByteArray, OutType, SizeOf(MyByteArray));
end;
end;
end;
InStream.Position := 0;
end;
function FavForJSON(InValue: Integer): string;
begin
Result := Format('%d', [InValue]);
end;
function FavForJSON(InValue: string): string;
begin
Result := '"' + StringToJSONString(InValue) + '"'
end;
function FavForJSONWithNull(InValue: string): string;
var MyResult: string;
begin
// this function returns null or string encloed in "string"
MyResult := FavForJSON(InValue);
if MyResult = '' then MyResult := 'null'
else MyResult := '"' + MyResult + '"';
Result := MyResult;
end;
function FavForJSON(InValue: Boolean): string;
begin
if InValue then Result := 'true'
else Result := 'false';
end;
function NSQGetBigEndianInt(InInteger: integer): integer;
begin
Result := NtoBE(InInteger)
end;
function NSQConnect(InClient: TIdTCPClient; InIdentify: TNSQIdentify;
InHost: string; InPort: Integer; InTopic: string; InChannel: string): boolean;
var MyResult: Boolean;
begin
MyResult := False;
try
if InClient = nil then begin
raise Exception.Create('TCP client object is nil');
end;
if InHost = '' then begin
raise Exception.Create('Host is empty')
end;
InClient.Host := InHost;
InClient.Port := InPort;
try
InClient.Connect;
while InClient.Connected = false do begin
sleep(100);
end;
if InClient.Connected then begin;
if NSQ_DEBUG then begin
NSQWrite('Port: %s', [InClient.BoundPort.ToString]);
end;
end;
except
on E: Exception do begin
NSQWrite('%s', [E.Message]);
raise E;
end;
end;
NSQWriteMagicMessage(InClient);
NSQWriteIdentifyMessage(InClient, InIdentify);
NSQSWriteSubscribeMessage(InClient, InTopic, InChannel);
NSQWriteRDYMessage(InClient, 10);
MyResult := True;
except
on E: exception do begin
raise
end;
end;
Result := MyResult;
end;
function NSQGetFrameType(typeId: Integer): TNSQFrameType;
begin
case (typeId) of
0: result := TNSQFrameType.NSQ_FRAMETYPERESPONSE;
1: result := TNSQFrameType.NSQ_FRAMETYPEERROR;
2: result := TNSQFrameType.NSQ_FRAMETYPEMESSAGE;
else result := TNSQFrameType.NSQ_FRAMETYPEUNKNOWN;
end;
end;
function NSQGetHostName: string;
begin
Result := 'Dinko'
end;
function NSQIdentifyToJson(var InIdentify: TNSQIdentify): string;
begin
Result := Format(
'{' +
'"client_id": %s,' +
'"deflate": %s,' +
'"deflate_level": %s,' +
'"feature_negotiation": %s,' +
'"heartbeat_interval": %s,' +
'"hostname": %s,' +
'"long_id": %s,' +
'"msg_timeout": %s,' +
'"output_buffer_size": %s,' +
'"output_buffer_timeout": %s,' +
'"sample_rate": %s,' +
'"short_id": %s,' +
'"snappy": %s,' +
'"tls_v1": %s,' +
'"user_agent": %s' +
'}',
[
FavForJSON(InIdentify.client_id),
FavForJSON(InIdentify.deflate),
FavForJSON(InIdentify.deflate_level),
FavForJSON(InIdentify.feature_negotiation),
FavForJSON(InIdentify.heartbeat_interval),
FavForJSON(InIdentify.hostname),
FavForJSON(InIdentify.long_id),
FavForJSON(InIdentify.msg_timeout),
FavForJSON(InIdentify.output_buffer_size),
FavForJSON(InIdentify.output_buffer_timeout),
FavForJSON(InIdentify.sample_rate),
FavForJSON(InIdentify.short_id),
FavForJSON(InIdentify.snappy),
FavForJSON(InIdentify.tls_v1),
FavForJSON(InIdentify.user_agent)
]
);
{"client_id":"LAPTOP-FV2TOBTH","deflate":false,"deflate_level":6,"feature_negotiation":true,
"heartbeat_interval":30000,"hostname":"LAPTOP-FV2TOBTH","long_id":"LAPTOP-FV2TOBTH","msg_timeout":0,
"output_buffer_size":16384,"output_buffer_timeout":250,"sample_rate":0,"short_id":"LAPTOP-FV2TOBTH",
"snappy":false,"tls_v1":false,"user_agent":"go-nsq/1.0.7"}
end;
function NSQCreateMessage(InMemoryStream: TMemoryStream; InString: string
): integer;
var MyLen: Int32;
MyResult: Integer;
begin
MyResult := 0;
try
InMemoryStream.Clear;
MyLen := Length(InString);
MyLen := NtoBE(MyLen); // natural to big endian - big endian is network way
MyResult := MyResult + InMemoryStream.Write(MyLen, SizeOf(MyLen));
MyResult := MyResult + InMemoryStream.Write(@InString[1], Length(InString));
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteMessage(InMemoryStream: TMemoryStream;
InTCPClient: TIdTCPClient): integer;
var MyResult: Integer;
begin
MyResult := 0;
try
InMemoryStream.Position := 0;
InTCPClient.IOHandler.Write(InMemoryStream, InMemoryStream.Size);
MyResult := InMemoryStream.Size;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQSWriteSubscribeMessage(InTCPClient: TIdTCPClient; InTopic,
InChannel: string): integer;
var MyString: string;
MyResult: Integer;
begin
MyResult := 0;
// SUB <topic_name> <channel_name>\n
try
MyString := Format('SUB %s %s%s', [InTopic, InChannel, #10]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
if NSQ_DEBUG then begin
NSQWrite('SUB: "%s"', [MyString])
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteMagicMessage(InTCPClient: TIdTCPClient): Integer;
var MyResult: Integer;
begin
MyResult := 0;
// V2 (4-byte ASCII [space][space][V][2]) a push based streaming protocol
// for consuming (and request/response for publishing)
try
InTCPClient.IOHandler.Write(NSQ_MAGIC_V2);
if NSQ_DEBUG then begin
NSQWrite('MAGIC: %s', [NSQ_MAGIC_V2])
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise;
end;
end;
Result := MyResult;
end;
function NSQWriteIdentifyMessage(InTCPClient: TIdTCPClient;
var InIdentify: TNSQIdentify): Integer;
var MyString: string;
MySize: Int32;
MyResult: Integer;
begin
MyResult := 0;
// IDENTIFY\n
// [ 4-byte size in bytes ][ N-byte JSON data ]
try
MyString := NSQIdentifyToJson(InIdentify);
InTCPClient.IOHandler.Write('IDENTIFY');
InTCPClient.IOHandler.Write(NSQ_NL);
MySize := Length(MyString);
if NSQ_DEBUG then begin
NSQWrite('IDENTIFY My Size: %d', [MySize]);
end;
InTCPClient.IOHandler.Write(MySize); // Indy automatically swap bytes to newtwork bigendian format
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
if NSQ_DEBUG then begin
NSQWrite('IDENTIFY: %s', [MyString]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise;
end;
end;
Result := MyResult;
end;
function NSQWriteNOPMessage(InTCPClient: TIdTCPClient): integer;
var MyString: string;
MyResult: Integer;
begin
MyResult := 0;
// NOP\n
try
MyString := 'NOP';
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
InTCPClient.IOHandler.Write(NSQ_NL);
if NSQ_DEBUG then begin
NSQWrite('NOP: %s', [MyString]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWritePUBMessage(InTCPClient: TIdTCPClient; InTopicName,
InMessage: string): integer;
var MyString: string;
MySize: Int32;
MyResult: Integer;
begin
MyResult := 0;
// PUB <topic_name>\n
try
MyString := Format('PUB %s%s', [InTopicName, string(#10)]);
if NSQ_DEBUG then begin
NSQWrite('PUB: %s', [MyString]);
end;
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
MySize := Length(InMessage);
if NSQ_DEBUG then begin
NSQWrite('PUB: MySize: %d', [MySize]);
end;
InTCPClient.IOHandler.Write(MySize);
InTCPClient.IOHandler.Write(@InMessage[1], Length(InMessage));
if NSQ_DEBUG then begin
NSQWrite('PUB: %s; "%s"', [InTopicName, InMessage]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteMPUBMessage(InTCPClient: TIdTCPClient; InTopicName: string;
InMessages: array of string): integer;
var MyString: string;
MySize: Int32;
F: Integer;
MyBodySize: Int32;
MyMessageCount: Int32;
MyResult: Integer;
MyMessage: string;
begin
// MPUB <topic_name>\n
// [ 4-byte body size ]
// [ 4-byte num messages ]
// [ 4-byte message #1 size ][ N-byte binary data ]
// ... (repeated <num_messages> times)
// <topic_name> - a valid string (optionally having #ephemeral suffix)
MyResult := 0;
try
MyMessageCount := Length(InMessages);
MyBodySize := 0;
for F := 0 to Length(InMessages)-1 do begin
MyBodySize := MyBodySize + Length(InMessages[F]);
end;
MyString := Format('MPUB %s%s', [InTopicName, string(#10)]);
if NSQ_DEBUG then begin
NSQWrite('MPUB: %s', [MyString]);
end;
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
if NSQ_DEBUG then begin
NSQWrite('MPUB: MyBodySize: %d', [MyBodySize]);
end;
InTCPClient.IOHandler.Write(MyBodySize);
if NSQ_DEBUG then begin
NSQWrite('MPUB: MyMessageCount: %d', [MyMessageCount]);
end;
InTCPClient.IOHandler.Write(MyMessageCount);
for F := 0 to Length(InMessages)-1 do begin
MyMessage := '';
MyMessage := InMessages[F];
MySize := Length(InMessages[F]);
if NSQ_DEBUG then begin
NSQWrite('MPUB: MySize: %d, %s', [MySize, MyMessage]);
end;
InTCPClient.IOHandler.Write(MySize);
InTCPClient.IOHandler.Write(@MyMessage[1], MySize);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteDPUBMessage(InTCPClient: TIdTCPClient; InTopicName: string; InDeferTime: Int32;
InMessage: string): integer;
var MyString: string;
MySize: Int32;
MyResult: Integer;
begin
// DPUB <topic_name> <defer_time>\n
// [ 4-byte size in bytes ][ N-byte binary data ]
//<topic_name> - a valid string (optionally having #ephemeral suffix)
//<defer_time> - a string representation of integer D which defines the time for how long to defer where 0 <= D < max-requeue-timeout
MyResult := 0;
try
MyString := Format('DPUB %s %d%s', [InTopicName, InDeferTime, string(#10)]);
if NSQ_DEBUG then begin
NSQWrite('DPUB: %s', [MyString]);
end;
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
MySize := Length(InMessage);
if NSQ_DEBUG then begin
NSQWrite('DPUB: MySize: %d', [MySize]);
end;
InTCPClient.IOHandler.Write(MySize);
InTCPClient.IOHandler.Write(@InMessage[1], Length(InMessage));
if NSQ_DEBUG then begin
NSQWrite('DPUB: %s; %s', [InTopicName, InMessage]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteRDYMessage(InTCPClient: TIdTCPClient; InCount: Integer
): integer;
var MyString: string;
MyResult: Integer;
begin
//RDY <count>\n
//Update RDY state (indicate you are ready to receive N messages)
MyResult := 0;
try
MyString := Format('RDY %d%s', [InCount, string(#10)]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
if NSQ_DEBUG then begin
NSQWrite('RDY: %s', [MyString]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteFINMessage(InTCPClient: TIdTCPClient; InMessageId: string
): integer;
var MyString: string;
MyResult: Integer;
begin
//FIN <message_id>\n
//Finish a message (indicate successful processing)
MyResult := 0;
try
MyString := Format('FIN %s%s', [InMessageID, string(#10)]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
if NSQ_DEBUG then begin
NSQWrite('FIN: %s', [MyString])
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
function NSQWriteREQMessage(InTCPClient: TIdTCPClient; InMessageId: string;
InTimeout: Integer): Integer;
var MyString: string;
MyResult: Integer;
begin
//REQ <message_id><timeout>\n
//Re-queue a message (indicate failure to process)
MyResult := 0;
try
MyString := Format('REQ %16.16s %d%s', [InMessageID, InTimeout, string(#10)]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
if NSQ_DEBUG then begin
NSQWrite('REQ: %s', [MyString]);
end;
Result := MyResult;
end;
function NSQWriteTOUCHMessage(InTCPClient: TIdTCPClient; InMessageId: string
): integer;
var MyString: string;
MyResult: Integer;
begin
//TOUCH <message_id><timeout>\n
// Reset the timeout for an in-flight message
MyResult := 0;
try
MyString := Format('TOUCH %16.16s%s', [InMessageID, string(#10)]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
if NSQ_DEBUG then begin
NSQWrite('TOUCH: %s', [MyString]);
end;
Result := MyResult;
end;
function NSQWriteCLSMessage(InTCPClient: TIdTCPClient): integer;
var MyString: string;
MyResult: Integer;
begin
//CLS\n
// Cleanly close your connection (no more messages are sent)
MyResult := 0;
try
MyString := Format('CLS%s', [string(#10)]);
InTCPClient.IOHandler.Write(@MyString[1], Length(MyString));
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
if NSQ_DEBUG then begin
NSQWrite('CLS: %s', [MyString]);
end;
Result := MyResult;
end;
function NSQReadMessage(MyInStream: TMemoryStream; IdTCPClient1: TIdTCPClient;
var OutNanoSecondTimestamp: int64; var OutAttempts: integer;
var OutMessageID: string; var OutBody: string): Integer;
var F: integer;
MyTimestamp: Int64; // message timestamp from server in nanoseconds
MyAttempts: Int16; // how many OutAttempts to deliver message to client
MyMessageID: array[0..15] of char; // message id in hex format
MyFrameType: Int32; // frame type - usually 2 as indicator that this is normal message with OutBody
MyResult: Integer;
begin
// DecodeMessage deserializes data (as []byte) and creates a new Message
// message format:
// [x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x][x]...
// | (int64) || || (hex string encoded in ASCII) || (binary)
// | 8-byte || || 16-byte || N-byte
// ------------------------------------------------------------------------------------------...
// nanosecond timestamp ^^ message ID message OutBody
// (uint16)
// 2-byte
// OutAttempts
// 00 00 00 23 00 00 00 02 15 56 EA 20 C3 34 07 58
//00 01 30 61 35 36 38 35 34 32 37 38 63 63 32 30
//30 30 45 64 69 74 31
MyResult := 0;
OutNanoSecondTimestamp := 0;
OutAttempts := 0;
OutMessageID := '';
OutBody := '';
try
//OutNanoSecondTimestamp 8byte
MyInStream.Position := 0;
if NSQ_DEBUG then begin
NSQWrite('READ: SizeOfMessage: %d', [MyInStream.Size]);
end;
MyInStream.ReadBuffer(MyFrameType, 4);
MyFrameType := BEtoN(MyFrameType);
if NSQ_DEBUG then begin
NSQWrite('READ: MyFrameType: %d', [MyFrameType]);
end;
MyFrameType := BEtoN(MyFrameType);
MyInStream.ReadBuffer(MyTimestamp, 8);
MyTimeStamp := BEtoN(MyTimestamp);
if NSQ_DEBUG then begin
NSQWrite('READ: MyTimeStamp: %d', [MyTimestamp]);
NSQWrite('READ: MyTimeStamp: %s', [FormatDateTime('yyyy-mm-dd nn:hh:ss.zzz', FavEpochToDateTime(MyTimestamp div 1000000))]);
end;
OutNanoSecondTimestamp := MyTimestamp;
//OutAttempts 2byte
MyInStream.ReadBuffer(MyAttempts, 2);
MyAttempts := BEtoN(MyAttempts);
if NSQ_DEBUG then begin
NSQWrite('READ: Attempts: %d', [MyAttempts]);
end;
OutAttempts := MyAttempts;
//OutMessageID 16 byte
FillChar(MyMessageID, 16, 0);
MyInStream.ReadBuffer(MyMessageID, 16);
OutMessageID := '';
for F := 0 to 15 do
begin
OutMessageID := OutMessageID + MyMessageID[F];
end;
if NSQ_DEBUG then begin
NSQWrite('READ: MessageID: %s', [string(MyMessageID)]);
end;
SetLength(OutBody,MyInStream.Size - 8 - 2 - 16 - 4);
for F := 1 to Length(OutBody) do begin
OutBody[F] := ' ';
end;
MyInStream.Read(OutBody[1], MyInStream.Size - 8 - 2 - 16 - 4);
if NSQ_DEBUG then begin
NSQWrite('READ: MsgBody: %s', [OutBody]);
end;
except
on E: Exception do begin
MyResult := -1;
if NSQ_DEBUG then begin
NSQWrite('%s', [E.Message]);
end;
raise
end;
end;
Result := MyResult;
end;
end.