ClientThread.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 22956: ClientThread.pas
  11. {
  12. { Rev 1.0 09/10/2003 3:10:54 PM Jeremy Darling
  13. { Project Checked into TC for the first time
  14. }
  15. unit ClientThread;
  16. interface
  17. uses
  18. IdTCPClient,
  19. ComCtrls,
  20. Classes,
  21. SysUtils,
  22. SyncObjs,
  23. Windows;
  24. type
  25. TClientThread=class(TThread)
  26. private
  27. FClient: TIdTCPClient;
  28. FState: Integer;
  29. FStatusText: String;
  30. FListItem: TListItem;
  31. FLastTick: Cardinal;
  32. FuiLock: TCriticalSection;
  33. FSleepTime: Integer;
  34. procedure SetState(const Value: Integer);
  35. procedure SetStatusText(const Value: String);
  36. procedure SetListItem(const Value: TListItem);
  37. procedure SetuiLock(const Value: TCriticalSection);
  38. procedure SetSleepTime(const Value: Integer);
  39. public
  40. procedure AssignClient(AClient: TIdTCPClient);
  41. procedure Execute; override;
  42. destructor Destroy; override;
  43. property SleepTime : Integer read FSleepTime write SetSleepTime;
  44. property Client : TIdTCPClient read FClient;
  45. property State : Integer read FState write SetState;
  46. property StatusText : String read FStatusText write SetStatusText;
  47. property ListItem : TListItem read FListItem write SetListItem;
  48. property uiLock : TCriticalSection read FuiLock write SetuiLock;
  49. end;
  50. implementation
  51. { TClientThread }
  52. procedure TClientThread.AssignClient(AClient: TIdTCPClient);
  53. begin
  54. if not Assigned(FClient) then
  55. FClient := TIdTCPClient.Create(nil);
  56. with FClient do
  57. begin
  58. Port := AClient.Port;
  59. Host := AClient.Host;
  60. IOHandler := AClient.IOHandler;
  61. OnConnected := AClient.OnConnected;
  62. OnDisconnected := AClient.OnDisconnected;
  63. OnStatus := AClient.OnStatus;
  64. OnWork := AClient.OnWork;
  65. OnWorkBegin := AClient.OnWorkBegin;
  66. OnWorkEnd := AClient.OnWorkEnd;
  67. end;
  68. end;
  69. destructor TClientThread.Destroy;
  70. begin
  71. uiLock.Enter;
  72. try
  73. ListItem.Free;
  74. finally
  75. uiLock.Leave;
  76. FClient.Free;
  77. inherited;
  78. end;
  79. end;
  80. procedure TClientThread.Execute;
  81. begin
  82. while not Terminated do
  83. begin
  84. if (not FClient.Connected) and
  85. (State = -2) then
  86. begin
  87. State := -1;
  88. FClient.Connect;
  89. FLastTick := GetTickCount;
  90. end
  91. else
  92. begin
  93. if State <> -1 then
  94. begin
  95. if GetTickCount - FLastTick > 1000 then
  96. begin
  97. State := State + 1;
  98. FLastTick := GetTickCount;
  99. if State > SleepTime then
  100. begin
  101. State := -3;
  102. FClient.Disconnect;
  103. end;
  104. end
  105. else
  106. Sleep(500);
  107. end;
  108. end;
  109. end
  110. end;
  111. procedure TClientThread.SetListItem(const Value: TListItem);
  112. begin
  113. FListItem := Value;
  114. end;
  115. procedure TClientThread.SetSleepTime(const Value: Integer);
  116. begin
  117. FSleepTime := Value;
  118. end;
  119. procedure TClientThread.SetState(const Value: Integer);
  120. begin
  121. FState := Value;
  122. case Value of
  123. -3 : StatusText := 'Disconnecting';
  124. -2 : StatusText := 'Creating';
  125. -1 : StatusText := 'Connecting';
  126. else
  127. StatusText := 'Sleeping [' + IntToStr(Value) + '/' + IntToStr(SleepTime) + '] while connected';
  128. end;
  129. end;
  130. procedure TClientThread.SetStatusText(const Value: String);
  131. begin
  132. uiLock.Enter;
  133. try
  134. FStatusText := Value;
  135. ListItem.SubItems[0] := value;
  136. finally
  137. uiLock.Leave;
  138. end;
  139. end;
  140. procedure TClientThread.SetuiLock(const Value: TCriticalSection);
  141. begin
  142. FuiLock := Value;
  143. end;
  144. end.