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
|
unit DebugConsoleServer;
{$mode objfpc}{$H+}
interface
uses
Classes,
SysUtils,
debugthread,
DebugInOutputProcessor,
lazCollections,
syncobjs,
pipes;
type
TFpDebugEventQueue = specialize TLazThreadedQueue<TFpDebugEvent>;
{ TFpDebugConsoleServer }
TFpDebugConsoleServer = class(TThread, IFpDebugListener)
private
FDebugThread: TFpDebugThread;
FConnectionIdentifier: integer;
FInOutputProcessor: TJSonInOutputProcessor;
FEventQueue: TFpDebugEventQueue;
protected
procedure Execute; override;
public
constructor create(ADebugThread: TFpDebugThread);
function GetOrigin: string;
procedure SendEvent(AnEvent: TFpDebugEvent);
destructor Destroy; override;
end;
implementation
{ TFpDebugConsoleServer }
procedure TFpDebugConsoleServer.Execute;
var
InputStream: TInputPipeStream;
DebugEvent: TFpDebugEvent;
CommandStr: string;
ACommand: TFpDebugThreadCommand;
b: char;
begin
InputStream:=TInputPipeStream.Create(StdInputHandle);
FInOutputProcessor := TJSonInOutputProcessor.create(FConnectionIdentifier, @FDebugThread.SendLogMessage);
FConnectionIdentifier := FDebugThread.AddListener(self);
try
while not terminated do
begin
if FEventQueue.PopItem(DebugEvent) = wrSignaled then
begin
writeln(FInOutputProcessor.EventToText(DebugEvent));
end;
while InputStream.NumBytesAvailable>0 do
begin
InputStream.Read(b,sizeof(b));
if b <> #10 then
CommandStr:=CommandStr+b
else
begin
ACommand := FInOutputProcessor.TextToCommand(CommandStr);
if assigned(ACommand) then
FDebugThread.QueueCommand(ACommand);
CommandStr:='';
end;
end;
end;
finally
FDebugThread.RemoveListener(self);
FInOutputProcessor.Free;
InputStream.Free;
end;
end;
constructor TFpDebugConsoleServer.create(ADebugThread: TFpDebugThread);
begin
FEventQueue:=TFpDebugEventQueue.Create(100, INFINITE, 100);
FDebugThread:=ADebugThread;
inherited Create(false);
end;
function TFpDebugConsoleServer.GetOrigin: string;
begin
result := 'console'
end;
procedure TFpDebugConsoleServer.SendEvent(AnEvent: TFpDebugEvent);
begin
FEventQueue.PushItem(AnEvent);
end;
destructor TFpDebugConsoleServer.Destroy;
begin
FEventQueue.Free;
inherited Destroy;
end;
end.
|