IcmpClientImpl.h 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef NETWORK_ICMPCLIENTIMPL_H
  2. #define NETWORK_ICMPCLIENTIMPL_H
  3. #include "Ipv4Header.h"
  4. #include "IcmpHeader.h"
  5. #include "asio.hpp"
  6. #include <condition_variable>
  7. #include <functional>
  8. #include <mutex>
  9. #include <string>
  10. #include <thread>
  11. #include <vector>
  12. namespace Network {
  13. class IcmpClientImpl
  14. {
  15. public:
  16. IcmpClientImpl();
  17. ~IcmpClientImpl();
  18. public:
  19. bool Ping(const std::string& targetAddress);
  20. private:
  21. void SendEchoRequest(const asio::ip::icmp::endpoint& targetEndpoint, const std::string& data);
  22. void StartReceive();
  23. void HandleReceive(const asio::error_code& error, std::size_t length);
  24. unsigned short GetIdentifier();
  25. private:
  26. std::thread m_thread;
  27. std::mutex m_mutex;
  28. std::condition_variable m_condition;
  29. asio::io_context m_ioContext;
  30. asio::ip::icmp::socket m_socket;
  31. asio::streambuf m_replyBuffer;
  32. bool m_done;
  33. };
  34. } // namespace Network
  35. #endif // NETWORK_ICMPCLIENTIMPL_H