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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
Interface for debug server.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
{$h+}
unit debugserverintf;
Interface
Uses
msgintf,baseunix,classes,sockets,sysutils;
Const
MsgTypes : Array[-1..3] of string =
('Disconnect','Information','Warning','Error','Identify');
Type
Thandle = Longint; // Abstraction for easier porting.
TClient = Class(TObject)
Handle : THandle;
Peer : ShortString;
Data : Pointer;
end;
TDebugEvent = Record
Client : TClient;
LogCode : Integer;
TimeStamp : TDateTime;
Event : String;
end;
Var
FClients : TList;
Accepting : Boolean;
Quit : Boolean;
DebugLogCallback : Procedure (Const Event : TDebugEvent);
DebugObjLogCallBack : Procedure (Const Event : TDebugEvent) of Object;
CloseConnectionCallBack : Procedure (Client : TClient);
CloseObjConnectionCallBack : Procedure (Client : TClient) of Object;
Procedure OpenDebugServer;
Procedure CloseDebugServer;
Function ClientFromHandle (AHandle : THandle) : TClient;
Procedure ReadMessage(Handle : THandle);
Procedure ReadMessageEvent(Handle : THandle; Var Event : TDebugEvent);
Function CheckNewConnection : TClient;
procedure CloseConnection(Client : TClient);
Procedure CloseClientHandle(Handle : THandle);
ResourceString
SClientLog = 'Client log %d';
SEvent = 'Event';
SMessage = 'Message';
SStopAccepting = 'Stop accepting new connections';
SStartAccepting = 'Start accepting new connections';
SErrSocketFailed = 'Creation of socket failed: %s';
SErrBindFailed = 'Binding of socket failed: %s';
SErrListenFailed = 'Listening on port #%d failed: %s';
SErrAcceptFailed = 'Could not accept a client connection: %d';
SClosingConnection = 'Closing connection.';
SErrFailedToSetSignalHandler = 'Failed to set signal handler.';
SPeerAt = 'Peer at %d';
Implementation
Function ClientFromHandle (AHandle : THandle) : TClient;
Var
I : Longint;
begin
Result:=Nil;
I:=0;
With FClients do
While (I<Count) and (Result=Nil) do
Begin
If TClient(Items[i]).Handle=AHandle then
Result:=TClient(Items[i]);
Inc(I);
end;
end;
{ ---------------------------------------------------------------------
Communications handling: Unix Socket setup
---------------------------------------------------------------------}
Var
FSocket : Integer;
Procedure SetupUnixSocket;
var
Flags,AddrLen : Integer;
FUnixAddr : TUnixSockAddr;
FFileName : String;
Quit : Boolean;
begin
FFileName:=DebugSocket;
FSocket:=FPSocket(AF_UNIX,SOCK_STREAM,0);
If FSocket<0 Then
Raise Exception.Create(SErrSocketFailed);
Flags:=fpFCntl(FSOCket,F_GETFL);
Flags:=Flags or O_NONBLOCK;
fpFCntl(FSocket,F_SETFL,Flags);
Str2UnixSockAddr(FFilename,FUnixAddr,AddrLen);
If FPBind(FSocket,@FUnixAddr,AddrLen)<>0 then
Raise Exception.CreateFmt(SErrBindFailed,[FFileName]);
If (fpListen(FSocket,5)<>0) then
Raise Exception.CreateFmt(SErrListenFailed,[FSocket]);
FClients:=TList.Create;
Accepting:=True;
end;
Procedure DestroyUnixSocket;
Var
C : TClient;
begin
If Assigned(FClients) then
begin
With FClients do
While Count>0 do
begin
C:=TClient(Items[Count-1]);
FileClose(C.Handle);
C.Free;
Delete(Count-1);
end;
FileClose(FSocket);
DeleteFile(DebugSocket);
end;
end;
{ ---------------------------------------------------------------------
Communications handling: Inet Socket setup
---------------------------------------------------------------------}
Procedure SetupInetSocket(Aport : Word);
var
Flags,AddrLen : Integer;
FInetAddr : TInetSockAddr;
FFileName : String;
Quit : Boolean;
begin
FSocket:=FPSocket(AF_INET,SOCK_STREAM,0);
If FSocket<0 Then
Raise Exception.Create(SErrSocketFailed);
Flags:=fpFCntl(FSocket,F_GETFL);
Flags:=Flags or O_NONBLOCK;
fpFCntl(FSocket,F_SETFL,Flags);
FInetAddr.Family := AF_INET;
Writeln('Using port : ',APort);
FInetAddr.Port := Swap(APort);
FInetAddr.Addr := 0;
If FPBind(FSocket,@FInetAddr,SizeOf(FInetAddr))<>0 then
Raise Exception.CreateFmt(SErrBindFailed,[FFileName]);
If fpListen(FSocket,5)<>0 then
Raise Exception.CreateFmt(SErrListenFailed,[FSocket]);
end;
Procedure DestroyInetSocket;
Var
C : TClient;
begin
If Assigned(FClients) then
begin
With FClients do
While Count>0 do
begin
C:=TClient(Items[Count-1]);
FileClose(C.Handle);
C.Free;
Delete(Count-1);
end;
FileClose(FSocket);
end;
end;
{ ---------------------------------------------------------------------
Communications handling: Public interface
---------------------------------------------------------------------}
Procedure OpenDebugServer;
begin
Case DebugConnection of
dcUnix : SetupUnixSocket;
dcInet : SetupInetSocket(DebugPort);
end;
FClients:=TList.Create;
Accepting:=True;
end;
Procedure CloseDebugServer;
begin
Accepting:=False;
Case DebugConnection of
dcUnix : DestroyUnixSocket;
dcInet : DestroyInetSocket;
end;
FClients.Free;
FClients:=Nil;
end;
{ ---------------------------------------------------------------------
Communications handling: Connection handling
---------------------------------------------------------------------}
Function GetNewConnection : THandle;
Var
ClientAddr: TUnixSockAddr;
L : Integer;
begin
If Accepting then
begin
L:=SizeOf(ClientAddr);
Result:=fpAccept(FSocket,@ClientAddr,@L);
If (Result<0) Then
if (Errno<>ESYSEAgain) then
Raise Exception.CreateFmt(SErrAcceptFailed,[FSocket])
else
Result:=-1
{$ifdef debug}
else
Writeln('New connection detected at ',Result)
{$endif debug}
end
else
Result:=-1;
end;
Function CheckNewConnection : TClient;
Var
NC : THandle;
begin
NC:=GetNewConnection;
If (NC=-1) then
Result:=Nil
else
begin
Result:=TClient.Create;
Result.Handle:=NC;
{$ifdef debug}
Writeln('Added new client', nc, ' at : ',FClients.Add(Result));
{$else}
FClients.Add(Result);
{$endif debug}
end;
end;
Procedure CloseClientHandle(Handle : THandle);
begin
fpShutDown(Handle,2);
FileClose(Handle);
end;
Procedure CloseConnection(Client : TClient);
Var
I : longint;
C : TClient;
begin
If Assigned(Client) then
begin
If Assigned(CloseConnectionCallBack) then
CloseConnectionCallBack(Client);
If Assigned(CloseObjConnectionCallBack) then
CloseObjConnectionCallBack(Client);
CloseClientHandle(Client.Handle);
FClients.Remove(Client);
Client.Free;
end;
end;
{ ---------------------------------------------------------------------
Message handling
---------------------------------------------------------------------}
Function MsgToEvent(AHandle: THandle; ALogCode : Integer; ATimeStamp : TDateTime; AEvent : String) : TDebugEvent;
begin
With Result do
begin
Client:=ClientFromHandle(AHandle);
If (Client<>Nil) then
begin
If (ALogCode=lctIdentify) then
Client.Peer:=AEvent;
end;
LogCode:=ALogCode;
TimeStamp:=ATimeStamp;
Event:=AEvent;
end;
end;
Procedure LogEvent(Event : TDebugEvent);
begin
if Assigned(DebugLogCallback) then
DebugLogCallBack(Event);
If Assigned(DebugObjLogCallBack) then
DebugObjLogCallBack(Event);
end;
Procedure ReadMessageEvent(Handle : THandle; Var Event : TDebugEvent);
Var
FDebugMessage : TDebugMessage;
msgSize : Integer;
begin
Try
With FDebugMessage do
begin
// Select reports read ready when closed, so check for this.
If (FileRead(Handle,msgType,SizeOf(Integer))=0) or (MsgType=-1) then
begin
event:=MsgToEvent(Handle,lctStop,Now,SClosingConnection);
If Assigned(Event.Client) then
CloseConnection(Event.Client)
else
CloseClientHandle(Handle);
end
else
begin
FileRead(Handle,msgTimeStamp,sizeof(TDateTime));
FileRead(Handle,MsgSize,SizeOf(Integer));
SetLength(Msg,MsgSize);
FileRead(Handle,Msg[1],MsgSize);
Event:=MsgToEvent(Handle,msgType,msgTimeStamp,Msg);
end
end;
except
On E : Exception do
Event:=MsgToEvent(Handle,lctError,Now,E.Message);
end;
end;
Procedure ReadMessage(Handle : THandle);
Var
Event : TDebugEvent;
begin
ReadMessageEvent(Handle,Event);
LogEvent(Event);
end;
end.
|