ServerServices.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. unit ServerServices;
  2. interface
  3. uses CorrespondService, ClientInfoService, SoftDogService, WinSock, SysUtils,
  4. Windows, Classes, Forms, TypeUnit;
  5. type
  6. TScServer = class;
  7. TCheckDogThrds = class(TThread)
  8. private
  9. FServer: TScServer;
  10. protected
  11. procedure CheckDog;
  12. procedure Execute; override;
  13. public
  14. constructor Create(Server: TScServer);
  15. end;
  16. TScServer = class
  17. private
  18. FCorrespondService: TScCorrespondService;
  19. FClientInfoService: TClientInfoDM;
  20. FSoftDogService: TScSoftDog;
  21. FCDThrds: TCheckDogThrds;
  22. function GetPort: string;
  23. procedure SetPort(const Value: string);
  24. function CheckDog: Boolean;
  25. public
  26. constructor Create;
  27. destructor Destroy; override;
  28. function ServerIP: string;
  29. function OpenServer: Boolean;
  30. procedure DeleteClient;
  31. procedure CloseServer;
  32. property Port: string read GetPort write SetPort;
  33. property ClientInfoService: TClientInfoDM read FClientInfoService;
  34. end;
  35. implementation
  36. { TScServer }
  37. function TScServer.CheckDog: Boolean;
  38. begin
  39. Result := FSoftDogService.ReadUserDog;
  40. if Result then
  41. with FSoftDogService do
  42. FCorrespondService.SetMaxLinks(V0MaxLinks, V1MaxLinks, V2MaxLinks)
  43. else
  44. MessageBox(0, PChar('没检测到加密狗!'), PChar('打开失败'), MB_OK or MB_ICONERROR);
  45. end;
  46. procedure TScServer.CloseServer;
  47. begin
  48. FCorrespondService.CloseService;
  49. end;
  50. constructor TScServer.Create;
  51. begin
  52. FCorrespondService := TScCorrespondService.Create;
  53. FClientInfoService := TClientInfoDM.Create(nil);
  54. FSoftDogService := TScSoftDog.Create;
  55. FCorrespondService.SoftDog := FSoftDogService;
  56. FCorrespondService.ClientInfoService := FClientInfoService;
  57. end;
  58. procedure TScServer.DeleteClient;
  59. begin
  60. FCorrespondService.DeleteClientThrds;
  61. end;
  62. destructor TScServer.Destroy;
  63. begin
  64. if Assigned(FCDThrds) then
  65. FCDThrds.Terminate;
  66. FreeAndNil(FCorrespondService);
  67. FreeAndNil(FClientInfoService);
  68. FreeAndNil(FSoftDogService);
  69. inherited;
  70. end;
  71. function TScServer.GetPort: string;
  72. begin
  73. Result := Format('%s%d', ['端口号:', FCorrespondService.Port]);
  74. end;
  75. function TScServer.OpenServer: Boolean;
  76. begin
  77. if CheckDog then
  78. begin
  79. Result := FCorrespondService.Open;
  80. FCDThrds := TCheckDogThrds.Create(Self);
  81. end;
  82. end;
  83. function TScServer.ServerIP: string;
  84. var
  85. wsData: TWSAData;
  86. sName: string;
  87. host: PHostEnt;
  88. begin
  89. sName := '';
  90. Result := 'IP:';
  91. WSAStartup(2, wsData);
  92. host := gethostbyname(Pchar(sName));
  93. if host <> nil then
  94. Result := Result + Format('%d.%d.%d.%d', [Byte(host^.h_addr^[0]), Byte(host^.h_addr^[1]),
  95. Byte(host^.h_addr^[2]), Byte(host^.h_addr^[3])]);
  96. WSACleanup;
  97. end;
  98. procedure TScServer.SetPort(const Value: string);
  99. begin
  100. if Value = '' then Exit;
  101. FCorrespondService.Port := StrToInt(Value);
  102. end;
  103. { TCheckDogThrds }
  104. procedure TCheckDogThrds.CheckDog;
  105. var
  106. blFlag: Boolean;
  107. ieTimes: Integer;
  108. begin
  109. ieTimes := 0;
  110. blFlag := False;
  111. while True do
  112. begin
  113. if not blFlag then ieTimes := 0;
  114. Sleep(3000);
  115. if Self.Terminated then Break;
  116. if not Assigned(FServer) then Break;
  117. if not Assigned(FServer.FSoftDogService) then Break;
  118. Sleep(3000);
  119. if Self.Terminated then Break;
  120. if not Assigned(FServer) then Break;
  121. if not Assigned(FServer.FSoftDogService) then Break;
  122. Sleep(4000);
  123. if Self.Terminated then Break;
  124. if not Assigned(FServer) then Break;
  125. if not Assigned(FServer.FSoftDogService) then Break;
  126. if Self.Terminated then Break;
  127. if not FServer.FSoftDogService.ReadUserDog then
  128. begin
  129. if ieTimes < 1 then
  130. begin
  131. Inc(ieTimes);
  132. blFlag := True;
  133. end
  134. else
  135. begin
  136. FServer.CloseServer;
  137. SendMessage(Application.MainForm.Handle, SM_Status, 0, 0);
  138. MessageBox(0, pchar('没检测到加密狗!'), pchar('提示'), MB_OK or MB_ICONINFORMATION);
  139. Break;
  140. end;
  141. end
  142. else blFlag := False;
  143. end;
  144. if Assigned(FServer) then FServer.CloseServer;
  145. end;
  146. constructor TCheckDogThrds.Create(Server: TScServer);
  147. begin
  148. FServer := Server;
  149. FreeOnTerminate := True;
  150. inherited Create(False);
  151. end;
  152. procedure TCheckDogThrds.Execute;
  153. begin
  154. inherited;
  155. CheckDog;
  156. end;
  157. end.