| 
 | 1 | +unit Debug;  | 
 | 2 | + | 
 | 3 | +interface  | 
 | 4 | + | 
 | 5 | +uses Windows,kol,Share;  | 
 | 6 | + | 
 | 7 | +{$include def.inc}  | 
 | 8 | +{$include share.inc}  | 
 | 9 | + | 
 | 10 | +var  | 
 | 11 | +   _Init:boolean;  | 
 | 12 | + | 
 | 13 | +type  | 
 | 14 | +{$ifndef _DEBUG_}  | 
 | 15 | +  TDebug = TObject;  | 
 | 16 | +{$else}  | 
 | 17 | +  TDebug = class  | 
 | 18 | +    private  | 
 | 19 | +    public  | 
 | 20 | +     //ID:Cardinal;  | 
 | 21 | +     constructor Create;  | 
 | 22 | +  end;  | 
 | 23 | + | 
 | 24 | + | 
 | 25 | +procedure CallEvent(PT:byte; Event:THI_Event;const Data:TData);  | 
 | 26 | +{$endif}  | 
 | 27 | + | 
 | 28 | + | 
 | 29 | +implementation  | 
 | 30 | + | 
 | 31 | +uses WinSock;  | 
 | 32 | + | 
 | 33 | +{$ifdef _DEBUG_}  | 
 | 34 | +const  | 
 | 35 | +  EB_NONE     = 0;  | 
 | 36 | +  EB_TRACE    = 1;  | 
 | 37 | +  EB_RUN      = 2;  | 
 | 38 | + | 
 | 39 | +  EB_SELECT   = 3;  | 
 | 40 | +  EB_UNSELECT = 4;  | 
 | 41 | + | 
 | 42 | +type  | 
 | 43 | + TEventBuf = record  | 
 | 44 | +    cmd:cardinal;  | 
 | 45 | +    Point:cardinal;  | 
 | 46 | +    PointType:cardinal;  | 
 | 47 | +    DataType:cardinal;  | 
 | 48 | +    data:array[0..127]of Char;  | 
 | 49 | + end;  | 
 | 50 | +   | 
 | 51 | +var ms:THandle;  | 
 | 52 | + | 
 | 53 | +constructor TDebug.Create;  | 
 | 54 | +{$ifndef _DEBUG_MAIL_}  | 
 | 55 | +var SockAddr:TSockAddr;  | 
 | 56 | +{$endif}  | 
 | 57 | +begin  | 
 | 58 | +   inherited;  | 
 | 59 | +   if ms = 0 then  | 
 | 60 | +    begin  | 
 | 61 | +     {$ifdef _DEBUG_MAIL_}  | 
 | 62 | +     ms := CreateMailslot(PChar('\\.\mailslot\hiDebug_'),512,MAILSLOT_WAIT_FOREVER,nil);  | 
 | 63 | +     SetMailslotInfo(ms,10);  | 
 | 64 | +     if ms = INVALID_HANDLE_VALUE then  | 
 | 65 | +      _debug('mail slot failed!');  | 
 | 66 | +     {$else}  | 
 | 67 | +     UPD_Init;  | 
 | 68 | +     ms := socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);  | 
 | 69 | +     FillChar(SockAddr,SizeOf(SockAddr),0);  | 
 | 70 | +     SockAddr.sin_family := AF_INET;  | 
 | 71 | +     SockAddr.sin_port := htons(_dbServer);  | 
 | 72 | +     {$endif}  | 
 | 73 | +    end;  | 
 | 74 | +end;  | 
 | 75 | + | 
 | 76 | +procedure Write(Buf:TEventBuf; size:integer);  | 
 | 77 | +var {$ifdef _DEBUG_MAIL_}  | 
 | 78 | +    hf:cardinal;  | 
 | 79 | +    nBytesRead:cardinal;  | 
 | 80 | +    {$else}  | 
 | 81 | +    SockAddr:TSockAddr;  | 
 | 82 | +    {$endif}  | 
 | 83 | +begin  | 
 | 84 | +  {$ifdef _DEBUG_MAIL_}  | 
 | 85 | +  hf := CreateFile(PChar('\\.\mailslot\hiDebug'),GENERIC_WRITE,  | 
 | 86 | +      FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);  | 
 | 87 | +  WriteFile(hf,Buf,size,nBytesRead,nil);  | 
 | 88 | +  CloseHandle(hf);  | 
 | 89 | +  {$else}  | 
 | 90 | +  SockAddr.sin_family := AF_INET;  | 
 | 91 | +  SockAddr.sin_port := htons(_dbClient);  | 
 | 92 | +  SockAddr.sin_addr.S_addr := inet_addr('127.0.0.1');  | 
 | 93 | +  sendto(ms,Buf,size,0,SockAddr,sizeof(SockAddr));  | 
 | 94 | +  {$endif}  | 
 | 95 | +end;  | 
 | 96 | + | 
 | 97 | +var rBuf:TEventBuf;  | 
 | 98 | + | 
 | 99 | +function Read(var buf:TEventBuf):boolean;  | 
 | 100 | +var  | 
 | 101 | +  {$ifdef _DEBUG_MAIL_}  | 
 | 102 | +  nBytesRead:cardinal;  | 
 | 103 | +  {$else}  | 
 | 104 | +  FDSet:TFDSet;  | 
 | 105 | +  tm:TTimeVal;  | 
 | 106 | +  {$endif}  | 
 | 107 | +  size:integer;  | 
 | 108 | +begin  | 
 | 109 | +   if buf.point <> 0 then begin sleep(1); Result := true; exit; end;  | 
 | 110 | +     | 
 | 111 | +   {$ifdef _DEBUG_MAIL_}  | 
 | 112 | +   Result := ReadFile(ms, buf, size, nBytesRead, nil );  | 
 | 113 | +   {$else}  | 
 | 114 | +   Result := false;  | 
 | 115 | +   FD_ZERO(FDSet);  | 
 | 116 | +   FD_SET(ms, FDSet);  | 
 | 117 | +   tm.tv_sec := 0;  | 
 | 118 | +   tm.tv_usec := 500;  | 
 | 119 | +   if select(0,@FDSet,nil,nil,@tm) > 0 then begin  | 
 | 120 | +     ioctlsocket(ms, FIONREAD, size);  | 
 | 121 | +     if Size = 0 then  | 
 | 122 | +     else begin  | 
 | 123 | +       Recv(ms,buf,8,0);  | 
 | 124 | +       Result := true;  | 
 | 125 | +     end;  | 
 | 126 | +   end;  | 
 | 127 | +   {$endif}  | 
 | 128 | +end;  | 
 | 129 | + | 
 | 130 | +procedure CallEvent(PT:byte; Event:THI_Event;const Data:TData);  | 
 | 131 | +var  | 
 | 132 | +    Buf:TEventBuf;  | 
 | 133 | +    len:integer;  | 
 | 134 | +    s:string;  | 
 | 135 | +begin  | 
 | 136 | +  if not _Init then exit;  | 
 | 137 | + | 
 | 138 | +  Buf.cmd := EB_SELECT;  | 
 | 139 | +  Buf.Point := Event.dbgPoint;  | 
 | 140 | +  Buf.PointType := PT;  | 
 | 141 | +  len := 4*sizeof(cardinal);  | 
 | 142 | +  buf.DataType := Data.data_type;   | 
 | 143 | +  case Data.data_type of  | 
 | 144 | +    data_int :   | 
 | 145 | +     begin  | 
 | 146 | +       integer(pointer(@buf.data[0])^) := Data.idata;  | 
 | 147 | +       inc(len, sizeof(integer));  | 
 | 148 | +     end;  | 
 | 149 | +    data_real:   | 
 | 150 | +     begin  | 
 | 151 | +       real(pointer(@buf.data[0])^) := Data.rdata;  | 
 | 152 | +       inc(len, sizeof(real));  | 
 | 153 | +     end;   | 
 | 154 | +    data_str:   | 
 | 155 | +     begin  | 
 | 156 | +       StrLCopy(@buf.data[0], PChar(Data.sdata), 127);  | 
 | 157 | +       inc(len, min(128, length(Data.sdata)));  | 
 | 158 | +     end;        | 
 | 159 | +    data_stream:  | 
 | 160 | +     begin  | 
 | 161 | +       s := 'Size: ' + Int2Str(PStream(Data.idata).Size) + ', Position: ' + int2str(PStream(Data.idata).Position);   | 
 | 162 | +       StrCopy(@buf.data[0], PChar(s));  | 
 | 163 | +     end;  | 
 | 164 | +    data_bitmap:  | 
 | 165 | +     begin  | 
 | 166 | +       s := 'Size: ' + Int2Str(PBitmap(Data.idata).Width) + 'x' + Int2Str(PBitmap(Data.idata).Height);   | 
 | 167 | +       StrCopy(@buf.data[0], PChar(s));  | 
 | 168 | +     end;  | 
 | 169 | +    data_types:  | 
 | 170 | +     begin  | 
 | 171 | +       s := GetTypeString(PType(data.idata));   | 
 | 172 | +       StrCopy(@buf.data[0], PChar(s));  | 
 | 173 | +     end;   | 
 | 174 | +    else ;  | 
 | 175 | +  end;  | 
 | 176 | +  Write(Buf,len);  | 
 | 177 | +  repeat  | 
 | 178 | +   if Read(rBuf)and ((rBuf.Point = Event.dbgPoint)or(rBuf.Point = $FFFFFFFF)or(rBuf.cmd = EB_RUN)) then  | 
 | 179 | +     case rBuf.cmd of  | 
 | 180 | +      EB_TRACE: break;  | 
 | 181 | +      EB_RUN: _Init := false;  | 
 | 182 | +     end;  | 
 | 183 | +  until not _Init;  | 
 | 184 | +  rBuf.Point := 0;  | 
 | 185 | +    | 
 | 186 | +  Buf.cmd := EB_UNSELECT;  | 
 | 187 | +  Buf.Point := Event.dbgPoint;  | 
 | 188 | +  Write(Buf,8);  | 
 | 189 | +end;  | 
 | 190 | +{$endif}  | 
 | 191 | +end.  | 
0 commit comments