zhelpers.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. unit zhelpers;
  2. interface
  3. uses
  4. SysUtils
  5. , Classes
  6. , zmqapi
  7. ;
  8. procedure s_dump( socket: TZMQSocket );
  9. function s_random( len: Integer ): Utf8String;
  10. procedure s_set_id( socket: TZMQSocket );
  11. // for threadSafe logging to the console.
  12. procedure zNote( str: String );
  13. function zTimeStamp: Int64;
  14. function zCalcTimeMs( tstart, tstop: Int64 ): Int64;
  15. function zIncTimeMs( tNow, tIncrementMs: Int64 ): Int64;
  16. function zIncTimeUs( tNow, tIncrementUs: Int64 ): Int64;
  17. implementation
  18. uses
  19. Windows;
  20. var
  21. cs: TRTLCriticalSection;
  22. fFrequency: Int64;
  23. procedure zNote( str: String );
  24. begin
  25. EnterCriticalSection( cs );
  26. Writeln( str );
  27. LeaveCriticalSection( cs );
  28. end;
  29. procedure s_dump( socket: TZMQSocket );
  30. function validChar( frm: TZMQFrame; indx: Integer ): Boolean;
  31. var
  32. pb: PByte;
  33. begin
  34. pb := PByte(Integer(frm.data) + indx);
  35. result := ( pb^ >= 32 ) and ( pb^ < 127 );
  36. end;
  37. var
  38. msg: TZMQMsg;
  39. frame: TZMQFrame;
  40. i: Cardinal;
  41. str: Utf8String;
  42. begin
  43. zNote( '----------------------------------------' );
  44. msg := TZMQMsg.create;
  45. try
  46. socket.recv( msg );
  47. frame := msg.pop;
  48. while frame <> nil do
  49. try
  50. i := 0;
  51. while ( i < frame.size ) and validChar( frame, i ) do
  52. inc( i );
  53. if i = frame.size then
  54. str := frame.asUtf8String
  55. else begin
  56. SetLength( str, frame.size * 2 );
  57. BinToHex( frame.data, @str[1], frame.size );
  58. end;
  59. zNote( Format( '[%03d] %s', [ frame.size, str ] ) );
  60. finally
  61. frame.Free;
  62. frame := msg.pop;
  63. end;
  64. finally
  65. msg.Free;
  66. end;
  67. end;
  68. function s_random( len: Integer ): Utf8String;
  69. const
  70. Chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
  71. var
  72. s: String;
  73. i: integer;
  74. begin
  75. Randomize;
  76. result := '';
  77. for i := 1 to len do
  78. result := result + Chars[Random(Length(Chars)) + 1];
  79. end;
  80. // Set simple random printable identity on socket
  81. //
  82. procedure s_set_id( socket: TZMQSocket );
  83. begin
  84. socket.Identity := s_random( 10 );
  85. end;
  86. // get timestamp
  87. function zTimeStamp: Int64;
  88. begin
  89. QueryPerformanceCounter( result );
  90. end;
  91. function zCalcTimeMs( tstart, tstop: Int64 ): Int64;
  92. begin
  93. if fFrequency > 0 then
  94. result := ( MSecsPerSec * (tstop - tstart ) ) div fFrequency
  95. else
  96. result := 0;
  97. end;
  98. function zCalcTimeUs( tstart, tstop: Int64 ): Int64;
  99. begin
  100. if fFrequency > 0 then
  101. result := ( MSecsPerSec * MSecsPerSec * (tstop - tstart ) ) div fFrequency
  102. else
  103. result := 0;
  104. end;
  105. function zIncTimeMs( tNow, tIncrementMs: Int64 ): Int64;
  106. begin
  107. result := ( tIncrementMs * fFrequency ) div MSecsPerSec + tNow;
  108. end;
  109. function zIncTimeUs( tNow, tIncrementUs: Int64 ): Int64;
  110. begin
  111. result := ( tIncrementUs * fFrequency ) div ( MSecsPerSec * MSecsPerSec )+ tNow;
  112. end;
  113. initialization
  114. {$ifdef UNIX}
  115. InitCriticalSection( cs );
  116. {$else}
  117. InitializeCriticalSection( cs );
  118. {$endif}
  119. QueryPerformanceFrequency( fFrequency );
  120. finalization
  121. {$ifdef UNIX}
  122. DoneCriticalSection( cs );
  123. {$else}
  124. DeleteCriticalSection( cs );
  125. {$endif}
  126. end.