synadbg.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.001.000 |
  3. |==============================================================================|
  4. | Content: Socket debug tools |
  5. |==============================================================================|
  6. | Copyright (c)2008, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2008. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(Socket debug tools)
  45. Routines for help with debugging of events on the Sockets.
  46. }
  47. unit synadbg;
  48. interface
  49. uses
  50. blcksock, synsock, synautil, classes, sysutils;
  51. type
  52. TSynaDebug = class(TObject)
  53. class procedure HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
  54. class procedure HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
  55. end;
  56. procedure AppendToLog(const value: Ansistring);
  57. var
  58. LogFile: string;
  59. implementation
  60. procedure AppendToLog(const value: Ansistring);
  61. var
  62. st: TFileStream;
  63. s: string;
  64. h, m, ss, ms: word;
  65. dt: Tdatetime;
  66. begin
  67. if fileexists(LogFile) then
  68. st := TFileStream.Create(LogFile, fmOpenReadWrite or fmShareDenyWrite)
  69. else
  70. st := TFileStream.Create(LogFile, fmCreate or fmShareDenyWrite);
  71. try
  72. st.Position := st.Size;
  73. dt := now;
  74. decodetime(dt, h, m, ss, ms);
  75. s := formatdatetime('yyyymmdd-hhnnss', dt) + format('.%.3d', [ms]) + ' ' + value;
  76. WriteStrToStream(st, s);
  77. finally
  78. st.free;
  79. end;
  80. end;
  81. class procedure TSynaDebug.HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
  82. var
  83. s: string;
  84. begin
  85. case Reason of
  86. HR_ResolvingBegin:
  87. s := 'HR_ResolvingBegin';
  88. HR_ResolvingEnd:
  89. s := 'HR_ResolvingEnd';
  90. HR_SocketCreate:
  91. s := 'HR_SocketCreate';
  92. HR_SocketClose:
  93. s := 'HR_SocketClose';
  94. HR_Bind:
  95. s := 'HR_Bind';
  96. HR_Connect:
  97. s := 'HR_Connect';
  98. HR_CanRead:
  99. s := 'HR_CanRead';
  100. HR_CanWrite:
  101. s := 'HR_CanWrite';
  102. HR_Listen:
  103. s := 'HR_Listen';
  104. HR_Accept:
  105. s := 'HR_Accept';
  106. HR_ReadCount:
  107. s := 'HR_ReadCount';
  108. HR_WriteCount:
  109. s := 'HR_WriteCount';
  110. HR_Wait:
  111. s := 'HR_Wait';
  112. HR_Error:
  113. s := 'HR_Error';
  114. else
  115. s := '-unknown-';
  116. end;
  117. s := inttohex(integer(Sender), 8) + s + ': ' + value + CRLF;
  118. AppendToLog(s);
  119. end;
  120. class procedure TSynaDebug.HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
  121. var
  122. s, d: Ansistring;
  123. begin
  124. setlength(s, len);
  125. move(Buffer^, pointer(s)^, len);
  126. if writing then
  127. d := '-> '
  128. else
  129. d := '<- ';
  130. s :=inttohex(integer(Sender), 8) + d + s + CRLF;
  131. AppendToLog(s);
  132. end;
  133. initialization
  134. begin
  135. Logfile := changefileext(paramstr(0), '.slog');
  136. end;
  137. end.