PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indy/IdRemoteCMDClient.pas

https://code.google.com/
Pascal | 223 lines | 142 code | 21 blank | 60 comment | 6 complexity | e340d020551396c04bc788ad5454d020 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 2004.02.03 5:44:16 PM czhower
  18. Name changes
  19. Rev 1.6 2004.01.22 6:09:04 PM czhower
  20. IdCriticalSection
  21. Rev 1.5 1/21/2004 3:27:18 PM JPMugaas
  22. InitComponent
  23. Rev 1.4 4/4/2003 8:03:40 PM BGooijen
  24. fixed
  25. Rev 1.3 2/24/2003 09:32:56 PM JPMugaas
  26. Rev 1.2 1/31/2003 02:32:04 PM JPMugaas
  27. Should now compile.
  28. Rev 1.1 12/6/2002 05:30:32 PM JPMugaas
  29. Now decend from TIdTCPClientCustom instead of TIdTCPClient.
  30. Rev 1.0 11/13/2002 07:59:26 AM JPMugaas
  31. -2001.02.15 - J. Peter Mugaas
  32. Started this unit with code originally in TIdRexec
  33. }
  34. unit IdRemoteCMDClient;
  35. {
  36. Indy Rexec Client TIdRexec
  37. Copyright (C) 2001 Indy Pit Crew
  38. Author J. Peter Mugaas
  39. Based partly on code authored by Laurence LIew
  40. 2001-February-15
  41. }
  42. interface
  43. {$i IdCompilerDefines.inc}
  44. uses
  45. IdException, IdTCPClient;
  46. const
  47. IDRemoteUseStdErr = True;
  48. {for IdRSH, we set this to. IdRexec will override this}
  49. IDRemoteFixPort = True;
  50. type
  51. EIdCanNotBindRang = class(EIdException);
  52. TIdRemoteCMDClient = class(TIdTCPClientCustom)
  53. protected
  54. FUseReservedPorts: Boolean;
  55. FUseStdError : Boolean;
  56. FErrorMessage : String;
  57. FErrorReply : Boolean;
  58. //
  59. function InternalExec(AParam1, AParam2, ACommand : String) : String; virtual;
  60. procedure InitComponent; override;
  61. public
  62. destructor Destroy; override;
  63. Function Execute(ACommand: String): String; virtual;
  64. property ErrorReply : Boolean read FErrorReply;
  65. property ErrorMessage : String read FErrorMessage;
  66. published
  67. property UseStdError : Boolean read FUseStdError write FUseStdError default IDRemoteUseStdErr;
  68. end;
  69. implementation
  70. uses
  71. IdComponent, IdGlobal, IdIOHandlerStack, IdIOHandlerSocket,IdSimpleServer, IdTCPConnection, IdThread, SysUtils;
  72. type
  73. TIdStdErrThread = class(TIdThread)
  74. protected
  75. FStdErr : TIdSimpleServer;
  76. FOutput : String;
  77. public
  78. Constructor Create(AStdErr : TIdSimpleServer; ALock : TIdCriticalSection); reintroduce;
  79. Procedure Run; override;
  80. property Output : String read FOutput;
  81. end;
  82. { TIdRemoteCMDClient }
  83. procedure TIdRemoteCMDClient.InitComponent;
  84. begin
  85. inherited;
  86. FUseReservedPorts := IDRemoteFixPort;
  87. FUseStdError := IDRemoteUseStdErr;
  88. end;
  89. destructor TIdRemoteCMDClient.Destroy;
  90. begin
  91. inherited;
  92. end;
  93. function TIdRemoteCMDClient.Execute(ACommand: String): String;
  94. begin
  95. Result := ''; {Do not Localize}
  96. end;
  97. function TIdRemoteCMDClient.InternalExec(AParam1, AParam2, ACommand: String) : String;
  98. var
  99. stdErr : TIdSimpleServer;
  100. thr : TIdStdErrThread;
  101. procedure SendAuthentication(APort : TIdPort);
  102. begin
  103. // Send authentication and commands
  104. IOHandler.Write(IntToStr(APort)+#0); //stdErr Port Number - none for this session
  105. IOHandler.Write(AParam1 + #0);
  106. IOHandler.Write(AParam2 + #0);
  107. IOHandler.Write(ACommand + #0);
  108. end;
  109. begin
  110. Result := ''; {Do not Localize}
  111. if FUseReservedPorts then begin
  112. BoundPortMin := 512;
  113. BoundPortMax := 1023;
  114. end else begin
  115. BoundPortMin := 0;
  116. BoundPortMax := 0;
  117. end;
  118. if Socket = nil then begin
  119. IOHandler := TIdIOHandlerStack.Create(Self);
  120. end;
  121. {For RSH, we have to set the port the client to connect. I don't
  122. think it is required to this in Rexec.}
  123. Connect;
  124. try
  125. if FUseStdError then begin
  126. StdErr := TIdSimpleServer.Create(nil);
  127. try
  128. StdErr.BoundIP := Socket.Binding.IP;
  129. StdErr.BoundPortMin := BoundPortMin;
  130. StdErr.BoundPortMax := BoundPortMax;
  131. StdErr.BeginListen;
  132. thr := TIdStdErrThread.Create(StdErr, nil{, FLock});
  133. try
  134. SendAuthentication(StdErr.Binding.Port);
  135. Thr.Start;
  136. try
  137. FErrorReply := (IOHandler.ReadString(1) <> #0);
  138. {Receive answers}
  139. BeginWork(wmRead);
  140. try
  141. Result := IOHandler.AllData;
  142. finally
  143. EndWork(wmRead);
  144. FErrorMessage := thr.Output;
  145. end;
  146. finally
  147. StdErr.Abort;
  148. thr.Terminate;
  149. thr.WaitFor;
  150. end;
  151. finally
  152. FreeAndNil(thr);
  153. end;
  154. finally
  155. FreeAndNil(StdErr);
  156. end;
  157. end else
  158. begin
  159. SendAuthentication(0);
  160. FErrorReply := (IOHandler.ReadString(1) <> #0);
  161. {Receive answers}
  162. BeginWork(wmRead);
  163. try
  164. if FErrorReply then begin
  165. FErrorMessage := IOHandler.AllData;
  166. end else begin
  167. Result := IOHandler.AllData;
  168. end;
  169. finally
  170. EndWork(wmRead);
  171. end;
  172. end;
  173. finally
  174. Disconnect;
  175. end;
  176. end;
  177. { TIdStdErrThread }
  178. constructor TIdStdErrThread.Create(AStdErr: TIdSimpleServer;
  179. ALock: TIdCriticalSection);
  180. begin
  181. inherited Create(True);
  182. FStdErr := AStdErr;
  183. FreeOnTerminate := False;
  184. StopMode := smTerminate;
  185. FStdErr.BeginListen;
  186. end;
  187. procedure TIdStdErrThread.Run;
  188. begin
  189. FStdErr.Listen;
  190. FOutput := FStdErr.IOHandler.AllData;
  191. end;
  192. end.