Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IpTcpClient.h 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include "asio.hpp"
  3. #include <memory>
  4. using tcp = asio::ip::tcp;
  5. enum class TCP_REQUESTS {
  6. NA = 0,
  7. DOWNLOAD_DATA = 1,
  8. GET_STATUS = 2,
  9. GET_LOG_FILES
  10. };
  11. namespace REQUEST {
  12. enum class INTENT {
  13. NA = 0,
  14. DOWNLOAD_DATA = 1,
  15. GET_STATUS = 2,
  16. INVALID
  17. };
  18. INTENT assignInt(const unsigned int value);
  19. enum class CODE_RCV {
  20. SUCCESS = 0,
  21. FAILED,
  22. INVALID
  23. };
  24. inline CODE_RCV intToRcv(const unsigned int);
  25. enum class CODE_SND {
  26. CONTINUING,
  27. FINISHED,
  28. INVALID
  29. };
  30. template<typename E>
  31. constexpr auto toUnderlyingType(E e)
  32. {
  33. return static_cast<typename std::underlying_type<E>::type>(e);
  34. }
  35. }
  36. template<typename E>
  37. constexpr auto toUnderlyingType(E e)
  38. {
  39. return static_cast<typename std::underlying_type<E>::type>(e);
  40. }
  41. class IpTcpClient
  42. {
  43. private:
  44. std::string ip;
  45. unsigned int port;
  46. std::unique_ptr<tcp::socket> socket;
  47. asio::io_context context;
  48. tcp::resolver::results_type endpoints;
  49. std::stringstream inputStream;
  50. TCP_REQUESTS lastRequest;
  51. public:
  52. IpTcpClient(std::string targetIP, unsigned int targetPort);
  53. IpTcpClient(std::string targetIP, unsigned int targetPort, TCP_REQUESTS requestCode);
  54. //getter and setter
  55. std::stringstream& getInputStream() { return inputStream; }
  56. TCP_REQUESTS getLastRequestCode() const { return lastRequest; }
  57. //Client Routine
  58. void connect();
  59. //send request code to server
  60. void sendRequest(const TCP_REQUESTS& code);
  61. //Wait until server responds
  62. size_t waitRead();
  63. //Process the received data according to the requested data
  64. void processServerResponse();
  65. };