ClientService.pas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. unit ClientService;
  2. interface
  3. uses CfgPort, CorrespondClient, Forms, WinSock;
  4. type
  5. TScClient = class
  6. private
  7. FUser: Integer;
  8. FCfgPort: TScCfgPort;
  9. FCorrespondClient: TScCorrespondClient;
  10. function GetComputerIP: string;
  11. function GetIP: string;
  12. function GetPort: Integer;
  13. public
  14. constructor Create;
  15. destructor Destroy; override;
  16. function ConnectServer: Boolean;
  17. function CheckConnection(AAddr, ABytes: Integer; P: PByte): Boolean;
  18. procedure CloseConnection;
  19. property UserType: Integer read FUser write FUser;
  20. property Port: Integer read GetPort;
  21. property IP: string read GetIP;
  22. end;
  23. implementation
  24. uses SysUtils;
  25. { TScClient }
  26. function TScClient.CheckConnection(AAddr, ABytes: Integer; P: PByte): Boolean;
  27. begin
  28. Result := FCorrespondClient.ReadDogData(AAddr, ABytes, 9, P);
  29. end;
  30. procedure TScClient.CloseConnection;
  31. begin
  32. FCorrespondClient.QuestConnect(1);
  33. FCorrespondClient.CloseConnection;
  34. end;
  35. function TScClient.ConnectServer: Boolean;
  36. begin
  37. Result := FCorrespondClient.ConnectServer(FCfgPort.Port, FCfgPort.IP);
  38. if Result then
  39. begin
  40. FCorrespondClient.IP := GetComputerIP;
  41. FCorrespondClient.UserID := FUser;
  42. Result := FCorrespondClient.QuestConnect(0);
  43. end;
  44. { if Result then
  45. FCorrespondClient.ResumeThrds;}
  46. end;
  47. constructor TScClient.Create;
  48. begin
  49. FCfgPort := TScCfgPort.Create;
  50. FCorrespondClient := TScCorrespondClient.Create;
  51. FCfgPort.FileDir := ExtractFilePath(Application.ExeName);
  52. FCfgPort.ReadPort;
  53. end;
  54. destructor TScClient.Destroy;
  55. begin
  56. CloseConnection;
  57. FCfgPort.Free;
  58. FCorrespondClient.Free;
  59. inherited;
  60. end;
  61. function TScClient.GetComputerIP: string;
  62. var
  63. wsData: TWSAData;
  64. sName: string;
  65. host: PHostEnt;
  66. begin
  67. sName := '';
  68. Result := '';
  69. WSAStartup(2, wsData);
  70. host := gethostbyname(Pchar(sName));
  71. if host <> nil then
  72. Result := Result + Format('%d.%d.%d.%d', [Byte(host^.h_addr^[0]), Byte(host^.h_addr^[1]),
  73. Byte(host^.h_addr^[2]), Byte(host^.h_addr^[3])]);
  74. WSACleanup;
  75. end;
  76. function TScClient.GetIP: string;
  77. begin
  78. Result := FCfgPort.IP;
  79. end;
  80. function TScClient.GetPort: Integer;
  81. begin
  82. Result := FCfgPort.Port;
  83. end;
  84. end.