You've already forked lazarus-ccr
Refactor TBaseTransport and TBaseTCPTransport so http and tcp transports no longer manage filtering which is now done by TBaseTransport and TBaseTCPTransport
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1675 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -31,29 +31,22 @@ Type
|
||||
|
||||
{$M+}
|
||||
{ TTCPTransport }
|
||||
TTCPTransport = class(TBaseTransport,ITransport)
|
||||
TTCPTransport = class(TBaseTCPTransport,ITransport)
|
||||
Private
|
||||
FFormat : string;
|
||||
FConnection : TInetSocket;
|
||||
FContentType : string;
|
||||
FTarget: string;
|
||||
FAddress : string;
|
||||
FPort : string;
|
||||
procedure ReadResponse(ADest: TStream);
|
||||
procedure SendRequest(ARequest: TStream);
|
||||
private
|
||||
procedure Connect();
|
||||
protected
|
||||
procedure DoSend(const AData; const ALength : Int64); override;
|
||||
function DoReceive(var AData; const ALength : Int64) : Int64; override;
|
||||
public
|
||||
constructor Create();override;
|
||||
destructor Destroy();override;
|
||||
destructor Destroy();override;
|
||||
function GetTransportName() : string; override;
|
||||
procedure SendAndReceive(ARequest,AResponse:TStream); override;
|
||||
Published
|
||||
property Target : string Read FTarget Write FTarget;
|
||||
property ContentType : string Read FContentType Write FContentType;
|
||||
property Address : string Read FAddress Write FAddress;
|
||||
property Port : string Read FPort Write FPort;
|
||||
property Format : string read FFormat write FFormat;
|
||||
End;
|
||||
|
||||
procedure FPC_RegisterTCP_Transport();
|
||||
@ -61,7 +54,7 @@ procedure FPC_RegisterTCP_Transport();
|
||||
implementation
|
||||
|
||||
uses
|
||||
binary_streamer, Math, wst_types;
|
||||
wst_consts, binary_streamer, Math, wst_types;
|
||||
|
||||
{ TTCPTransport }
|
||||
|
||||
@ -72,9 +65,47 @@ begin
|
||||
FConnection:=TInetSocket.Create(FAddress,StrToInt(Port));
|
||||
end;
|
||||
|
||||
constructor TTCPTransport.Create();
|
||||
procedure TTCPTransport.DoSend(const AData; const ALength : Int64);
|
||||
var
|
||||
c, len : integer;
|
||||
P : PByte;
|
||||
begin
|
||||
inherited Create();
|
||||
Connect();
|
||||
P := PByte(@AData);
|
||||
len := ALength;
|
||||
Repeat
|
||||
C:=FConnection.Write(P^,len);
|
||||
if (C<0) then
|
||||
Raise ETCPException.CreateFmt(SERR_ErrorSendindDataToSocket,[FConnection.LastError]);
|
||||
If (C>0) then
|
||||
begin
|
||||
inc(P,C);
|
||||
Dec(len,C);
|
||||
end;
|
||||
Until (len=0);
|
||||
end;
|
||||
|
||||
function TTCPTransport.DoReceive(var AData; const ALength : Int64) : Int64;
|
||||
Var
|
||||
P : PByte;
|
||||
C : integer;
|
||||
len : Int64;
|
||||
begin
|
||||
if (ALength=0) then
|
||||
exit;
|
||||
len := ALength;
|
||||
P:=PByte(@AData);
|
||||
repeat
|
||||
C:=FConnection.Read(P^,len);
|
||||
If (C<=0) then
|
||||
Raise ETCPException.CreateFmt(SERR_ErrorReadindDataToSocket,[FConnection.LastError]);
|
||||
If (C>0) then
|
||||
begin
|
||||
Inc(P,C);
|
||||
Dec(len,C);
|
||||
end
|
||||
Until (len=0);
|
||||
Result := ALength;
|
||||
end;
|
||||
|
||||
destructor TTCPTransport.Destroy();
|
||||
@ -88,139 +119,6 @@ begin
|
||||
Result := sTRANSPORT_NAME;
|
||||
end;
|
||||
|
||||
procedure TTCPTransport.SendRequest(ARequest : TStream);
|
||||
|
||||
Procedure SendBuffer(P : PByte; ACount : Integer);
|
||||
|
||||
Var
|
||||
c : integer;
|
||||
|
||||
begin
|
||||
Repeat
|
||||
C:=FConnection.Write(P^,ACount);
|
||||
if (C<0) then
|
||||
Raise ETCPException.CreateFmt('Error %d sending data to socket',[FConnection.LastError]);
|
||||
If (C>0) then
|
||||
begin
|
||||
inc(P,C);
|
||||
Dec(ACount,C);
|
||||
end;
|
||||
Until (ACount=0);
|
||||
end;
|
||||
|
||||
|
||||
Var
|
||||
M : TMemoryStream;
|
||||
binBuff : TByteDynArray;
|
||||
wrtr : IDataStore;
|
||||
|
||||
begin
|
||||
SetLength(binBuff,ARequest.Size);
|
||||
ARequest.Position := 0;
|
||||
ARequest.ReadBuffer(binBuff[0],Length(binBuff));
|
||||
M := TMemoryStream.Create();
|
||||
Try
|
||||
wrtr:=CreateBinaryWriter(M);
|
||||
wrtr.WriteInt32S(0);
|
||||
wrtr.WriteAnsiStr(Target);
|
||||
wrtr.WriteAnsiStr(ContentType);
|
||||
wrtr.WriteAnsiStr(Self.Format);
|
||||
wrtr.WriteBinary(binBuff);
|
||||
M.Position := 0;
|
||||
wrtr.WriteInt32S(M.Size-4);
|
||||
M.Position := 0;
|
||||
SendBuffer(TMemoryStream(M).Memory,M.Size);
|
||||
Finally
|
||||
M.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTCPTransport.ReadResponse(ADest : TStream);
|
||||
|
||||
Procedure ReadBuffer(Var Buf; ACount : Integer);
|
||||
|
||||
Var
|
||||
P : PByte;
|
||||
C : integer;
|
||||
|
||||
begin
|
||||
if (ACount=0) then exit;
|
||||
P:=PByte(@Buf);
|
||||
repeat
|
||||
C:=FConnection.Read(P^,ACount);
|
||||
If (C<=0) then
|
||||
Raise ETCPException.CreateFmt('Error %d reading data from socket',[FConnection.LastError]);
|
||||
If (C>0) then
|
||||
begin
|
||||
Inc(P,C);
|
||||
Dec(ACount,C);
|
||||
end
|
||||
Until (ACount=0);
|
||||
end;
|
||||
|
||||
var
|
||||
bufferLen : LongInt;
|
||||
i, j, c : PtrInt;
|
||||
locBinBuff : TByteDynArray;
|
||||
begin
|
||||
bufferLen := 0;
|
||||
ReadBuffer(BufferLen,SizeOf(BufferLen));
|
||||
bufferLen := Reverse_32(bufferLen);
|
||||
ADest.Size := bufferLen;
|
||||
ADest.Position:=0;
|
||||
if (bufferLen>0) then
|
||||
begin
|
||||
c := 0;
|
||||
i := Min(1024,Bufferlen);
|
||||
SetLength(locBinBuff,i);
|
||||
repeat
|
||||
ReadBuffer(locBinBuff[0],i);
|
||||
ADest.Write(locBinBuff[0],i);
|
||||
Inc(c,i);
|
||||
i:=Min(1024,(bufferLen-c));
|
||||
until (i=0);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTCPTransport.SendAndReceive(ARequest, AResponse: TStream);
|
||||
|
||||
Var
|
||||
buffStream : TMemoryStream;
|
||||
binBuff : TByteDynArray;
|
||||
M : TStream;
|
||||
begin
|
||||
// Connect
|
||||
Connect();
|
||||
// Filter
|
||||
if HasFilter() then
|
||||
M:=TMemoryStream.Create()
|
||||
else
|
||||
M:=ARequest;
|
||||
try
|
||||
if HasFilter() then
|
||||
FilterInput(ARequest,M);
|
||||
// Actually send buffer
|
||||
SendRequest(M);
|
||||
Finally
|
||||
if (M<>ARequest) then
|
||||
FreeAndNil(M);
|
||||
end;
|
||||
// Prepare to read response
|
||||
if HasFilter() then
|
||||
M:=TmemoryStream.Create
|
||||
else
|
||||
M:=AResponse;
|
||||
try
|
||||
// Actually read response
|
||||
ReadResponse(M);
|
||||
if HasFilter() then
|
||||
FilterOutput(M,AResponse);
|
||||
Finally
|
||||
if (M<>AResponse) then
|
||||
FreeAndNil(M);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure FPC_RegisterTCP_Transport();
|
||||
begin
|
||||
GetTransportRegistry().Register(sTRANSPORT_NAME,TSimpleItemFactory.Create(TTCPTransport) as IItemFactory);
|
||||
|
Reference in New Issue
Block a user