|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #pragma once
-
- #include "asio.hpp"
- #include <memory>
-
- using tcp = asio::ip::tcp;
-
- enum class TCP_REQUESTS {
- NA = 0,
- DOWNLOAD_DATA = 1,
- GET_STATUS = 2,
- GET_LOG_FILES
- };
-
- namespace REQUEST {
- enum class INTENT {
- NA = 0,
- DOWNLOAD_DATA = 1,
- GET_STATUS = 2,
- INVALID
- };
-
- INTENT assignInt(const unsigned int value);
-
- enum class CODE_RCV {
- SUCCESS = 0,
- FAILED,
- INVALID
- };
-
- inline CODE_RCV intToRcv(const unsigned int);
-
- enum class CODE_SND {
- CONTINUING,
- FINISHED,
- INVALID
- };
-
- template<typename E>
- constexpr auto toUnderlyingType(E e)
- {
- return static_cast<typename std::underlying_type<E>::type>(e);
- }
- }
-
- template<typename E>
- constexpr auto toUnderlyingType(E e)
- {
- return static_cast<typename std::underlying_type<E>::type>(e);
- }
-
-
- class IpTcpClient
- {
- private:
- std::string ip;
- unsigned int port;
- std::unique_ptr<tcp::socket> socket;
- asio::io_context context;
- tcp::resolver::results_type endpoints;
-
- std::stringstream inputStream;
-
- TCP_REQUESTS lastRequest;
- public:
- IpTcpClient(std::string targetIP, unsigned int targetPort);
- IpTcpClient(std::string targetIP, unsigned int targetPort, TCP_REQUESTS requestCode);
-
- //getter and setter
- std::stringstream& getInputStream() { return inputStream; }
- TCP_REQUESTS getLastRequestCode() const { return lastRequest; }
-
-
-
- //Client Routine
- void connect();
- //send request code to server
- void sendRequest(const TCP_REQUESTS& code);
- //Wait until server responds
- size_t waitRead();
- //Process the received data according to the requested data
- void processServerResponse();
- };
-
|