uDTcpClientCoderImpl.pas 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (*
  2. unit owner: d10.ymofen
  3. *)
  4. unit uDTcpClientCoderImpl;
  5. interface
  6. uses
  7. uICoderSocket, DTcpClient;
  8. type
  9. TDTcpClientCoderImpl = class(TInterfacedObject, ICoderSocket)
  10. private
  11. FReconnect: Boolean;
  12. FTcpClient: TDTcpClient;
  13. protected
  14. function sendBuf(buf:Pointer; len:Cardinal):Cardinal; stdcall;
  15. function recvBuf(buf:Pointer; len:Cardinal):Cardinal; stdcall;
  16. procedure closeSocket; stdcall;
  17. public
  18. constructor Create(ATcpClient: TDTcpClient; pvReconnect: Boolean = true);
  19. destructor Destroy; override;
  20. end;
  21. implementation
  22. constructor TDTcpClientCoderImpl.Create(ATcpClient: TDTcpClient; pvReconnect:
  23. Boolean = true);
  24. begin
  25. inherited Create;
  26. FTcpClient := ATcpClient;
  27. FReconnect := pvReconnect;
  28. end;
  29. destructor TDTcpClientCoderImpl.Destroy;
  30. begin
  31. inherited Destroy;
  32. end;
  33. { TDTcpClientCoderImpl }
  34. procedure TDTcpClientCoderImpl.closeSocket;
  35. begin
  36. FTcpClient.Disconnect;
  37. end;
  38. function TDTcpClientCoderImpl.recvBuf(buf: Pointer; len: Cardinal): Cardinal;
  39. begin
  40. if FReconnect then
  41. begin
  42. if not FTcpClient.Active then FTcpClient.connect;
  43. try
  44. FTcpClient.recv(buf, len);
  45. Result := len;
  46. except
  47. FTcpClient.Disconnect;
  48. raise;
  49. end;
  50. end else
  51. begin
  52. FTcpClient.recv(buf, len);
  53. Result := len;
  54. end;
  55. end;
  56. function TDTcpClientCoderImpl.sendBuf(buf: Pointer; len: Cardinal): Cardinal;
  57. begin
  58. if FReconnect then
  59. begin
  60. if not FTcpClient.Active then FTcpClient.connect;
  61. try
  62. Result := FTcpClient.sendBuffer(buf, len);
  63. except
  64. FTcpClient.Disconnect;
  65. raise;
  66. end;
  67. end else
  68. begin
  69. Result := FTcpClient.sendBuffer(buf, len);
  70. end;
  71. end;
  72. end.