tcp-client.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "tcp.h"
  3. class TCPClient;
  4. typedef void(*TCPClientDisconnectCallback)(TCPClient *);
  5. typedef void(*TCPClientDataCallback)(TCPClient *, const char *, unsigned int);
  6. typedef struct {
  7. TCPClientDisconnectCallback disconnect;
  8. TCPClientDataCallback data;
  9. } TCPClientCallbacks;
  10. class TCPClient {
  11. public:
  12. TCPClient();
  13. ~TCPClient();
  14. void SetHost(const char *host);
  15. void SetPort(const char *port);
  16. void SetCallbacks(TCPClientCallbacks *callbacks);
  17. const char * GetHost();
  18. const char * GetPort();
  19. bool IsConnected();
  20. bool HasError();
  21. const char * GetError();
  22. // struct sockaddr_storage * GetAddress();
  23. bool Start();
  24. void Stop();
  25. bool Process(unsigned int timeout);
  26. void Disconnect();
  27. bool Queue(const char *buf, unsigned int len);
  28. private:
  29. char host[64];
  30. char port[16];
  31. bool connected;
  32. TCPClientCallbacks callbacks;
  33. char error[256];
  34. struct pollfd fds;
  35. // struct sockaddr_storage addr;
  36. // socklen_t addr_len;
  37. char *outgoing;
  38. unsigned int outgoing_size;
  39. unsigned int outgoing_len;
  40. void DisconnectHelper(bool silent);
  41. bool Read();
  42. bool Write();
  43. };