CorrespondClient.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. unit CorrespondClient;
  2. interface
  3. uses IdTCPClient, EnctyptClientData, Windows, SysUtils, Classes;
  4. type
  5. TScCorrespondClient = class;
  6. TThrds = class(TThread)
  7. private
  8. FCorrpdClient: TScCorrespondClient;
  9. protected
  10. procedure HandlerMessage; virtual;
  11. procedure Execute; override;
  12. public
  13. constructor Create(CorrpdClient: TScCorrespondClient);
  14. end;
  15. TRecevieThrds = class(TThrds)
  16. protected
  17. procedure HandlerMessage; override;
  18. end;
  19. TSendThrds = class(TThrds)
  20. protected
  21. procedure HandlerMessage; override;
  22. end;
  23. TScCorrespondClient = class
  24. private
  25. FIP: string;
  26. FUser: Integer;
  27. FIdTcpClient: TIdTCPClient;
  28. FEncryptData: TScEnryptClient;
  29. FReceiveThrds: TRecevieThrds;
  30. FSendThrds: TSendThrds;
  31. function SendMessages(const AIP: string; AUser, AAction: Integer): Boolean;
  32. procedure ReceiveMessages(var AText: string);
  33. function CheckMessage(const MsgText: string): Boolean;
  34. procedure DoOnDisconnect(Sender: TObject);
  35. public
  36. constructor Create;
  37. destructor Destroy; override;
  38. function ConnectServer(APort: Integer; const AIP: string): Boolean;
  39. procedure CloseConnection;
  40. procedure ResumeThrds;
  41. function ReadDogData(AAddr, ABytes, AAction: Integer; Data: PByte): Boolean;
  42. function QuestConnect(AAction: Integer): Boolean;
  43. property IP: string read FIP write FIP;
  44. property UserID: Integer read FUser write FUser;
  45. end;
  46. implementation
  47. uses Forms, IdTCPConnection;
  48. { TScCorrespondClient }
  49. function TScCorrespondClient.CheckMessage(const MsgText: string): Boolean;
  50. begin
  51. if SameText(MsgText, 'SUCCESS') then
  52. Result := True
  53. else
  54. begin
  55. Result := False;
  56. if SameText(MsgText, 'ISFULL') then
  57. MessageBox(0, PChar('服务器已满,连接失败!'), PChar('提示'), MB_OK or MB_ICONERROR);
  58. end;
  59. end;
  60. procedure TScCorrespondClient.CloseConnection;
  61. begin
  62. FIdTcpClient.Disconnect;
  63. end;
  64. function TScCorrespondClient.ConnectServer(APort: Integer; const AIP: string): Boolean;
  65. begin
  66. Result := True;
  67. FIdTcpClient.Host := AIP;
  68. FIdTcpClient.Port := APort;
  69. FIdTcpClient.Disconnect;
  70. try
  71. FIdTcpClient.Connect(1000);
  72. except
  73. Result := False;
  74. CloseConnection;
  75. { MessageBox(0, PChar('可能服务器没打开 或者 网线没插好 或者 您输入的端口号或IP不正确!'),
  76. PChar('连接服务器失败'), MB_OK or MB_ICONINFORMATION); }
  77. end;
  78. end;
  79. constructor TScCorrespondClient.Create;
  80. begin
  81. FIdTcpClient := TIdTCPClient.Create(nil);
  82. FEncryptData := TScEnryptClient.Create;
  83. //FIdTcpClient.OnDisconnected := DoOnDisconnect;
  84. end;
  85. destructor TScCorrespondClient.Destroy;
  86. begin
  87. { if FSendThrds <> nil then
  88. FSendThrds.Terminate;
  89. if FReceiveThrds <> nil then
  90. FReceiveThrds.Terminate; }
  91. FreeAndNil(FIdTcpClient);
  92. FEncryptData.Free;
  93. inherited;
  94. end;
  95. procedure TScCorrespondClient.DoOnDisconnect(Sender: TObject);
  96. begin
  97. end;
  98. function TScCorrespondClient.QuestConnect(AAction: Integer): Boolean;
  99. var
  100. sText: string;
  101. begin
  102. Result := SendMessages(FIP, FUser, AAction);
  103. if not Result then Exit;
  104. if AAction <> 1 then
  105. begin
  106. ReceiveMessages(sText);
  107. Result := CheckMessage(sText);
  108. end;
  109. end;
  110. function TScCorrespondClient.ReadDogData(AAddr, ABytes, AAction: Integer;
  111. Data: PByte): Boolean;
  112. var
  113. b: Byte;
  114. I, iValue: Integer;
  115. sText: string;
  116. begin
  117. Result := SendMessages(IntToStr(AAddr), ABytes, AAction);
  118. if not Result then Exit;
  119. if AAction <> 1 then
  120. begin
  121. ReceiveMessages(sText);
  122. if SameText(sText, '') then Result := False
  123. else
  124. begin
  125. if (AAddr = 0) and (ABytes = 0) then
  126. begin
  127. b := $FF;
  128. iValue := StrToInt(sText);
  129. for I := 1 to SizeOf(iValue) do
  130. begin
  131. Data^ := iValue and b;
  132. iValue := iValue shr 8;
  133. Inc(Data);
  134. end;
  135. end
  136. else
  137. begin
  138. // SetLength(arrData, ABytes);
  139. // Move(sText[1], arrData[0], ABytes);
  140. for I := 1 to ABytes do
  141. begin
  142. Data^ := Ord(sText[I]);
  143. Inc(Data);
  144. end;
  145. end;
  146. end;
  147. end;
  148. end;
  149. procedure TScCorrespondClient.ReceiveMessages(var AText: string);
  150. begin
  151. if FIdTcpClient.Connected then
  152. begin
  153. try
  154. AText := FIdTcpClient.ReadLn(#13#10, 1000, 100);
  155. AText := FEncryptData.DeEncryptData(AText);
  156. except
  157. AText := '';
  158. end;
  159. end;
  160. end;
  161. procedure TScCorrespondClient.ResumeThrds;
  162. begin
  163. FReceiveThrds := TRecevieThrds.Create(Self);
  164. FSendThrds := TSendThrds.Create(Self);
  165. end;
  166. function TScCorrespondClient.SendMessages(const AIP: string; AUser, AAction: Integer): Boolean;
  167. var
  168. sText: string;
  169. begin
  170. Result := True;
  171. if Assigned(FIdTcpClient) and FIdTcpClient.Connected then
  172. begin
  173. sText := FEncryptData.EncryptData(AIP, AUser, AAction);
  174. try
  175. FIdTcpClient.WriteLn(sText);
  176. except
  177. FIdTcpClient.Disconnect;
  178. Result := False;
  179. end;
  180. end
  181. else Result := False;
  182. end;
  183. { TThrds }
  184. constructor TThrds.Create(CorrpdClient: TScCorrespondClient);
  185. begin
  186. FCorrpdClient := CorrpdClient;
  187. FreeOnTerminate := True;
  188. inherited Create(False);
  189. end;
  190. procedure TThrds.Execute;
  191. begin
  192. inherited;
  193. HandlerMessage;
  194. end;
  195. procedure TThrds.HandlerMessage;
  196. begin
  197. end;
  198. { TRecevieThrds }
  199. procedure TRecevieThrds.HandlerMessage;
  200. var
  201. sText: string;
  202. begin
  203. while True do
  204. begin
  205. try
  206. if Self.Terminated then Break;
  207. sText := FCorrpdClient.FIdTcpClient.ReadLn('', 1000, 100);
  208. if Self.Terminated then Break;
  209. sText := FCorrpdClient.FEncryptData.DeEncryptData(sText);
  210. if not FCorrpdClient.CheckMessage(sText) then
  211. begin
  212. FCorrpdClient.CloseConnection;
  213. Break;
  214. end;
  215. except
  216. Break;
  217. end;
  218. end;
  219. end;
  220. { TSendThrds }
  221. procedure TSendThrds.HandlerMessage;
  222. begin
  223. while True do
  224. begin
  225. Sleep(10000);
  226. if Self.Terminated then Break;
  227. if not Assigned(FCorrpdClient) then Break;
  228. // if not FCorrpdClient.SendMessages(9) then Break;
  229. end;
  230. end;
  231. end.