From fd170582cf853f959b825619c4dfe66a08cf66b0 Mon Sep 17 00:00:00 2001 From: inoussa Date: Fri, 20 Jul 2007 00:25:29 +0000 Subject: [PATCH] + Indy TCP server listner ( FPC & Delphi ) git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@217 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- wst/trunk/indy_http_server.pas | 33 +- wst/trunk/indy_tcp_server.pas | 245 ++++++++++ .../delphi/http_server/http_server.dpr | 2 +- wst/trunk/samples/http_server/http_server.lpi | 418 ++++++++++++------ wst/trunk/samples/tcp_server/tcp_server.lpi | 43 +- wst/trunk/wst_indy9_utils.pas | 30 ++ 6 files changed, 581 insertions(+), 190 deletions(-) create mode 100644 wst/trunk/indy_tcp_server.pas create mode 100644 wst/trunk/wst_indy9_utils.pas diff --git a/wst/trunk/indy_http_server.pas b/wst/trunk/indy_http_server.pas index 0c2aa2583..3c1f9b9d5 100644 --- a/wst/trunk/indy_http_server.pas +++ b/wst/trunk/indy_http_server.pas @@ -94,35 +94,14 @@ implementation uses {$IFNDEF FPC} ActiveX, + {$IFDEF INDY_9} + wst_indy9_utils, + {$ENDIF} {$ENDIF} base_service_intf, server_service_intf, server_service_imputils, metadata_wsdl; - -{$IFNDEF FPC} -type - TwstIndy9Thread = class(TIdPeerThread) - protected - procedure AfterExecute; override; - procedure BeforeExecute; override; - end; - -{ TwstIndy9Thread } - -procedure TwstIndy9Thread.AfterExecute; -begin - CoUninitialize(); - inherited; -end; - -procedure TwstIndy9Thread.BeforeExecute; -begin - inherited; - CoInitialize(nil); -end; -{$ENDIF} - function ExtractNextPathElement(var AFullPath : string):string; var i : SizeInt; @@ -273,8 +252,8 @@ var b : TIdSocketHandle; begin inherited Create(); - FHTTPServerObject := TIdHTTPServer.Create({$IFNDEF INDY_10}nil{$ENDIF}); -{$IFNDEF FPC} + FHTTPServerObject := TIdHTTPServer.Create({$IFDEF INDY_9}nil{$ENDIF}); +{$IFDEF INDY_9} FHTTPServerObject.ThreadClass := TwstIndy9Thread; {$ENDIF} b := FHTTPServerObject.Bindings.Add(); @@ -310,7 +289,7 @@ end; class function TwstIndyHttpListener.GetDescription: string; begin - Result := 'Indy HTTP Listener'; + Result := 'WST Indy HTTP Listener'; end; initialization diff --git a/wst/trunk/indy_tcp_server.pas b/wst/trunk/indy_tcp_server.pas new file mode 100644 index 000000000..d82c5a869 --- /dev/null +++ b/wst/trunk/indy_tcp_server.pas @@ -0,0 +1,245 @@ +{ + This file is part of the Web Service Toolkit + Copyright (c) 2006 by Inoussa OUEDRAOGO + + This file is provide under modified LGPL licence + ( the files COPYING.modifiedLGPL and COPYING.LGPL). + + + 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. +} +unit indy_tcp_server; + +{$INCLUDE wst_global.inc} +//{$DEFINE WST_DBG} +interface + +uses + Classes, SysUtils, + IdTCPServer, +{$IFDEF INDY_10} + IdContext, IdCustomTCPServer, +{$ENDIF} +{$IFDEF INDY_9} + //IdTCPServer, +{$ENDIF} + IdSocketHandle, + server_listener; + +{$INCLUDE wst.inc} +{$INCLUDE wst_delphi.inc} + +type + +{$IFDEF INDY_9} + TIdBytes = array of Byte; +{$ENDIF} + + { TwstIndyTcpListener } + + TwstIndyTcpListener = class(TwstListener) + private + FTCPServerObject: TIdTCPServer; + FDefaultTime : PtrInt; + protected + procedure Handle_OnExecute( + {$IFDEF INDY_10} + AContext : TIdContext + {$ENDIF} + {$IFDEF INDY_9} + AContext : TIdPeerThread + {$ENDIF} + ); + public + constructor Create( + const AServerIpAddress : string = '127.0.0.1'; + const AListningPort : Integer = 1234; + const ADefaultClientPort : Integer = 25000 + ); + destructor Destroy(); override; + class function GetDescription() : string;override; + procedure Start();override; + procedure Stop();override; + end; + +implementation +uses +{$IFNDEF FPC} + ActiveX, + {$IFDEF INDY_9} + wst_indy9_utils, + {$ENDIF} +{$ENDIF} + IdGlobal, binary_streamer, server_service_intf, server_service_imputils; + +{ TwstIndyTcpListener } + +procedure TwstIndyTcpListener.Handle_OnExecute( +{$IFDEF INDY_10} + AContext : TIdContext +{$ENDIF} +{$IFDEF INDY_9} + AContext : TIdPeerThread +{$ENDIF} +); + + function ReadInputBuffer(AStream : TStream) : Integer; + var + strBuff : TIdBytes; + bufferLen : LongInt; + i, j, c : PtrInt; + begin + Result := 0; + bufferLen := 0; + try + SetLength(strBuff,SizeOf(bufferLen)); +{$IFDEF INDY_10} + AContext.Connection.IOHandler.ReadBytes(strBuff,SizeOf(bufferLen),False); +{$ENDIF} +{$IFDEF INDY_9} + AContext.Connection.ReadBuffer(strBuff[0],SizeOf(bufferLen)); +{$ENDIF} + Move(strBuff[0],bufferLen,SizeOf(bufferLen)); + bufferLen := Reverse_32(bufferLen); + AStream.Size := bufferLen; + if ( bufferLen > 0 ) then begin + c := 0; + i := 1024; + if ( i > bufferLen ) then + i := bufferLen; + SetLength(strBuff,i); + repeat + {$IFDEF INDY_10} + AContext.Connection.IOHandler.ReadBytes(strBuff,i,False); + {$ENDIF} + {$IFDEF INDY_9} + AContext.Connection.ReadBuffer(strBuff[0],i); + {$ENDIF} + AStream.Write(strBuff[0],i); + Inc(c,i); + if ( ( bufferLen - c ) > 1024 ) then + i := 1024 + else + i := bufferLen - c; + until ( i = 0 ); + end; + AStream.Position := 0; + Result := AStream.Size; + finally + SetLength(strBuff,0); + end; + end; + +var + locInStream, locOutStream : TMemoryStream; + wrtr : IDataStore; + rdr : IDataStoreReader; + buff, trgt,ctntyp, frmt : string; + rqst : IRequestBuffer; + i : PtrUInt; +begin +{$IFNDEF FPC} + //CoInitialize(nil); + //try +{$ENDIF} + locOutStream := nil; + locInStream := TMemoryStream.Create(); + try + locOutStream := TMemoryStream.Create(); + {$IFDEF INDY_10} + if ( Self.FDefaultTime <> 0 ) then + AContext.Connection.IOHandler.ReadTimeout := Self.FDefaultTime; + {$ENDIF} + if ( ReadInputBuffer(locInStream) >= SizeOf(LongInt) ) then begin + rdr := CreateBinaryReader(locInStream); + trgt := rdr.ReadStr(); + ctntyp := rdr.ReadStr(); + frmt := rdr.ReadStr(); + buff := rdr.ReadStr(); + +{$IFDEF WST_DBG} + WriteLn(buff); +{$ENDIF} + + rdr := nil; + locInStream.Size := 0; + locInStream.Write(buff[1],Length(buff)); + locInStream.Position := 0; + rqst := TRequestBuffer.Create(trgt,ctntyp,locInStream,locOutStream,frmt); + HandleServiceRequest(rqst); + i := locOutStream.Size; + SetLength(buff,i); + locOutStream.Position := 0; + locOutStream.Read(buff[1],i); + locOutStream.Size := 0; + wrtr := CreateBinaryWriter(locOutStream); + wrtr.WriteStr(buff); + locOutStream.Position := 0; + {$IFDEF INDY_10} + AContext.Connection.IOHandler.Write(locOutStream,locOutStream.Size,False); + {$ENDIF} + {$IFDEF INDY_9} + AContext.Connection.WriteStream(locOutStream,True,False,locOutStream.Size); + {$ENDIF} + end; + finally + FreeAndNil(locOutStream); + FreeAndNil(locInStream); + end; +{$IFNDEF FPC} + //finally + //CoUninitialize(); + //end; +{$ENDIF} +end; + +constructor TwstIndyTcpListener.Create( + const AServerIpAddress : string; + const AListningPort : Integer; + const ADefaultClientPort : Integer +); +var + b : TIdSocketHandle; +begin + inherited Create(); + FTCPServerObject := TIdTCPServer.Create({$IFNDEF INDY_10}nil{$ENDIF}); +{$IFDEF INDY_9} + FTCPServerObject.ThreadClass := TwstIndy9Thread; +{$ENDIF} + b := FTCPServerObject.Bindings.Add(); + b.IP := AServerIpAddress; + b.port := AListningPort; + + FTCPServerObject.DefaultPort := ADefaultClientPort; + FTCPServerObject.OnExecute := {$IFDEF FPC}@{$ENDIF}Handle_OnExecute; +end; + +destructor TwstIndyTcpListener.Destroy(); +begin + if ( FTCPServerObject <> nil ) then + Stop(); + FreeAndNil(FTCPServerObject); + inherited Destroy(); +end; + +class function TwstIndyTcpListener.GetDescription() : string; +begin + Result := 'WST Indy TCP Listener'; +end; + +procedure TwstIndyTcpListener.Start(); +begin + if not FTCPServerObject.Active then + FTCPServerObject.Active := True; +end; + +procedure TwstIndyTcpListener.Stop(); +begin + if FTCPServerObject.Active then + FTCPServerObject.Active := False; +end; + +end. + diff --git a/wst/trunk/samples/delphi/http_server/http_server.dpr b/wst/trunk/samples/delphi/http_server/http_server.dpr index fdcf04ffc..6758392b4 100644 --- a/wst/trunk/samples/delphi/http_server/http_server.dpr +++ b/wst/trunk/samples/delphi/http_server/http_server.dpr @@ -22,7 +22,7 @@ uses server_service_intf in '..\..\..\server_service_intf.pas'; var - AppObject : TwstIndyHttpListener; + AppObject : TwstListener; AppObject2 : TwstListener; begin Server_service_RegisterBinaryFormat(); Server_service_RegisterSoapFormat(); diff --git a/wst/trunk/samples/http_server/http_server.lpi b/wst/trunk/samples/http_server/http_server.lpi index 70e580307..a330ed17a 100644 --- a/wst/trunk/samples/http_server/http_server.lpi +++ b/wst/trunk/samples/http_server/http_server.lpi @@ -12,7 +12,7 @@ - + @@ -34,15 +34,15 @@ - + - - + + - + @@ -50,15 +50,15 @@ - + - - + + @@ -66,8 +66,8 @@ - - + + @@ -75,24 +75,24 @@ - + - - + + - + - - + + @@ -101,8 +101,8 @@ - - + + @@ -110,8 +110,8 @@ - - + + @@ -119,15 +119,15 @@ - + - - + + @@ -135,171 +135,173 @@ - + - + - + - + - + - + - + - + - + - + - + - + - - - + + + + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - - + + @@ -307,20 +309,20 @@ - + - + - + @@ -328,15 +330,15 @@ - + - - + + @@ -344,14 +346,14 @@ - + - + @@ -361,7 +363,7 @@ - + @@ -370,7 +372,7 @@ - + @@ -380,15 +382,15 @@ - + - - + + - + @@ -396,32 +398,32 @@ - + - + - + - + - + @@ -429,136 +431,280 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -570,7 +716,7 @@ - + diff --git a/wst/trunk/samples/tcp_server/tcp_server.lpi b/wst/trunk/samples/tcp_server/tcp_server.lpi index cfe3d227a..4cd7ce7e2 100644 --- a/wst/trunk/samples/tcp_server/tcp_server.lpi +++ b/wst/trunk/samples/tcp_server/tcp_server.lpi @@ -12,7 +12,7 @@ - + @@ -35,8 +35,8 @@ - - + + @@ -117,7 +117,7 @@ - + @@ -152,7 +152,7 @@ - + @@ -218,7 +218,7 @@ - + @@ -233,7 +233,7 @@ - + @@ -297,11 +297,9 @@ - - - + + - @@ -378,9 +376,9 @@ - + - + @@ -393,8 +391,8 @@ - - + + @@ -405,19 +403,11 @@ - + - - - - - - - - - - + + @@ -430,6 +420,7 @@ + diff --git a/wst/trunk/wst_indy9_utils.pas b/wst/trunk/wst_indy9_utils.pas new file mode 100644 index 000000000..e9b39ffe9 --- /dev/null +++ b/wst/trunk/wst_indy9_utils.pas @@ -0,0 +1,30 @@ +unit wst_indy9_utils; + +interface +uses SysUtils, IdTCPServer; + +type + + TwstIndy9Thread = class(TIdPeerThread) + protected + procedure AfterExecute; override; + procedure BeforeExecute; override; + end; + +implementation +uses ActiveX; +{ TwstIndy9Thread } + +procedure TwstIndy9Thread.AfterExecute; +begin + CoUninitialize(); + inherited; +end; + +procedure TwstIndy9Thread.BeforeExecute; +begin + inherited; + CoInitialize(nil); +end; + +end.