forked from bvarga/delphizmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhelpers.pas
152 lines (126 loc) · 2.99 KB
/
zhelpers.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
unit zhelpers;
interface
uses
SysUtils
, Classes
, zmqapi
;
procedure s_dump( socket: TZMQSocket );
function s_random( len: Integer ): Utf8String;
procedure s_set_id( socket: TZMQSocket );
// for threadSafe logging to the console.
procedure zNote( str: String );
function zTimeStamp: Int64;
function zCalcTimeMs( tstart, tstop: Int64 ): Int64;
function zIncTimeMs( tNow, tIncrementMs: Int64 ): Int64;
function zIncTimeUs( tNow, tIncrementUs: Int64 ): Int64;
implementation
uses
Windows;
var
cs: TRTLCriticalSection;
fFrequency: Int64;
procedure zNote( str: String );
begin
EnterCriticalSection( cs );
Writeln( str );
LeaveCriticalSection( cs );
end;
procedure s_dump( socket: TZMQSocket );
function validChar( frm: TZMQFrame; indx: Integer ): Boolean;
var
pb: PByte;
begin
pb := PByte(Integer(frm.data) + indx);
result := ( pb^ >= 32 ) and ( pb^ < 127 );
end;
var
msg: TZMQMsg;
frame: TZMQFrame;
i: Cardinal;
str: Utf8String;
begin
zNote( '----------------------------------------' );
msg := TZMQMsg.create;
try
socket.recv( msg );
frame := msg.pop;
while frame <> nil do
try
i := 0;
while ( i < frame.size ) and validChar( frame, i ) do
inc( i );
if i = frame.size then
str := frame.asUtf8String
else begin
SetLength( str, frame.size * 2 );
BinToHex( frame.data, @str[1], frame.size );
end;
zNote( Format( '[%03d] %s', [ frame.size, str ] ) );
finally
frame.Free;
frame := msg.pop;
end;
finally
msg.Free;
end;
end;
function s_random( len: Integer ): Utf8String;
const
Chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
var
s: String;
i: integer;
begin
Randomize;
result := '';
for i := 1 to len do
result := result + Chars[Random(Length(Chars)) + 1];
end;
// Set simple random printable identity on socket
//
procedure s_set_id( socket: TZMQSocket );
begin
socket.Identity := s_random( 10 );
end;
// get timestamp
function zTimeStamp: Int64;
begin
QueryPerformanceCounter( result );
end;
function zCalcTimeMs( tstart, tstop: Int64 ): Int64;
begin
if fFrequency > 0 then
result := ( MSecsPerSec * (tstop - tstart ) ) div fFrequency
else
result := 0;
end;
function zCalcTimeUs( tstart, tstop: Int64 ): Int64;
begin
if fFrequency > 0 then
result := ( MSecsPerSec * MSecsPerSec * (tstop - tstart ) ) div fFrequency
else
result := 0;
end;
function zIncTimeMs( tNow, tIncrementMs: Int64 ): Int64;
begin
result := ( tIncrementMs * fFrequency ) div MSecsPerSec + tNow;
end;
function zIncTimeUs( tNow, tIncrementUs: Int64 ): Int64;
begin
result := ( tIncrementUs * fFrequency ) div ( MSecsPerSec * MSecsPerSec )+ tNow;
end;
initialization
{$ifdef UNIX}
InitCriticalSection( cs );
{$else}
InitializeCriticalSection( cs );
{$endif}
QueryPerformanceFrequency( fFrequency );
finalization
{$ifdef UNIX}
DoneCriticalSection( cs );
{$else}
DeleteCriticalSection( cs );
{$endif}
end.