Differences

This shows you the differences between two versions of the page.

Link to this comparison view

public:howto:debugsocket [2008/02/04 22:56]
geby created
public:howto:debugsocket [2008/02/04 23:16] (current)
geby
Line 12: Line 12:
  
 Recommended very good, freeware and opensource multiplatform packet capturing tool is [[http://www.wireshark.org/|WireShark]]. Recommended very good, freeware and opensource multiplatform packet capturing tool is [[http://www.wireshark.org/|WireShark]].
 +
 +===== Socket events =====
 +In some cases you not need to know what happens on the wires. You need to know what happen on the socket interface inside computer instead.
 +
 +Each sockat based class in Synapse have special event for this purpose. It is called //OnStatus//. Each Synapse protocol imeplemtation have property //Sock//((Some protocols using more sockets, like FTP have //Sock// and //DSock//)). It is socket class used for handling socket communication, and this class have //OnStatus// event.
 +Hook this event and log events to the file. Sample:
 +<code delphi>
 +class procedure TSynaDebug.HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
 +var
 +  s: string;
 +begin
 +  case Reason of
 +    HR_ResolvingBegin:
 +      s := 'HR_ResolvingBegin';
 +    HR_ResolvingEnd:
 +      s := 'HR_ResolvingEnd';
 +    HR_SocketCreate:
 +      s := 'HR_SocketCreate';
 +    HR_SocketClose:
 +      s := 'HR_SocketClose';
 +    HR_Bind:
 +      s := 'HR_Bind';
 +    HR_Connect:
 +      s := 'HR_Connect';
 +    HR_CanRead:
 +      s := 'HR_CanRead';
 +    HR_CanWrite:
 +      s := 'HR_CanWrite';
 +    HR_Listen:
 +      s := 'HR_Listen';
 +    HR_Accept:
 +      s := 'HR_Accept';
 +    HR_ReadCount:
 +      s := 'HR_ReadCount';
 +    HR_WriteCount:
 +      s := 'HR_WriteCount';
 +    HR_Wait:
 +      s := 'HR_Wait';
 +    HR_Error:
 +      s := 'HR_Error';
 +  else
 +    s := '-unknown-';
 +  end;
 +  s := inttohex(integer(Sender), 8) + s + ': ' + value + CRLF;
 +  AppendToLog(s);
 +end;
 +</code>
 +
 +===== Data monitoring =====
 +In some cases you wish to monitor what datas are written and readed to socket by application. Socket class have special event //OnMonitor// for this. Hook this event (similar to OnStatus) and log informations. Sample:
 +<code delphi>
 +class procedure TSynaDebug.HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
 +var
 +  s, d: string;
 +begin
 +  setlength(s, len);
 +  move(Buffer^, pointer(s)^, len);
 +  if writing then
 +    d := '-> '
 +  else
 +    d := '<- ';
 +  s :=inttohex(integer(Sender), 8) + d + s + CRLF;
 +  AppendToLog(s);
 +end;
 +</code>
 +
 +===== synadbg unit =====
 +For helping with synapse debug tasks I prepare simple unit for debugging on application level. Just include it to your project uses, and then you can made debug file (on same place as your EXE file, but with extension '.slog') by settings hooks to socket what you wish to debug.
 +
 +Example showing setting of debug hooks on THTTPSend class:
 +<code delphi>
 +procedure TForm1.Button4Click(Sender: TObject);
 +var
 +  d: THTTPSend;
 +begin
 +  d := THTTPSend.Create;
 +  try
 +    d.Sock.OnStatus := TSynaDebug.HookStatus;
 +    d.Sock.OnMonitor := TSynaDebug.HookMonitor;
 +//...continue with work...
 +</code>
 +
 +You want this unit? [[http://synapse.ararat.cz/files/contrib/synadbg.zip|Download it here!]]
public/howto/debugsocket.1202162217.txt.gz · Last modified: 2008/02/04 22:56 by geby
Driven by DokuWiki Recent changes RSS feed