unix_domain_socket.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef UNIX_DOMAIN_SOCKET_H
  7. #define UNIX_DOMAIN_SOCKET_H
  8. #include <asiolink/io_service.h>
  9. #include <asiolink/io_socket.h>
  10. #include <boost/shared_ptr.hpp>
  11. #include <functional>
  12. #include <string>
  13. namespace isc {
  14. namespace asiolink {
  15. /// @brief Exception thrown upon socket error.
  16. class UnixDomainSocketError : public Exception {
  17. public:
  18. UnixDomainSocketError(const char* file, size_t line, const char* what) :
  19. isc::Exception(file, line, what) { };
  20. };
  21. class UnixDomainSocketImpl;
  22. /// @brief Represents unix domain socket implemented in terms
  23. /// of boost asio.
  24. class UnixDomainSocket : public IOSocket {
  25. public:
  26. /// @brief Callback type used in call to @ref UnixDomainSocket::asyncConnect.
  27. typedef std::function<void(const boost::system::error_code&)> ConnectHandler;
  28. /// @brief Callback type used in calls to @ref UnixDomainSocket::asyncSend
  29. /// and @ref UnixDomainSocket::asyncReceive.
  30. typedef std::function<void(const boost::system::error_code&, size_t)> Handler;
  31. /// @brief Constructor.
  32. ///
  33. /// @param io_service Reference to IOService to be used by this
  34. /// class.
  35. explicit UnixDomainSocket(IOService& io_service);
  36. /// @brief Returns native socket representation.
  37. virtual int getNative() const;
  38. /// @brief Always returns 0.
  39. virtual int getProtocol() const;
  40. /// @brief Connects the socket to the specified endpoint.
  41. ///
  42. /// @param path Path to the unix socket to which we should connect.
  43. ///
  44. /// @throw UnixDomainSocketError if error occurs.
  45. void connect(const std::string& path);
  46. /// @brief Asynchronously connects the socket to the specified endpoint.
  47. ///
  48. /// Always returns immediatelly.
  49. ///
  50. /// @param path Path to the unix socket to which we should connect.
  51. /// @param handler Callback to be invoked when connection is established or
  52. /// a connection error is signalled.
  53. void asyncConnect(const std::string& path, const ConnectHandler& handler);
  54. /// @brief Writes specified amount of data to a socket.
  55. ///
  56. /// @param data Pointer to data to be written.
  57. /// @param length Number of bytes to be written.
  58. ///
  59. /// @return Number of bytes written.
  60. /// @throw UnixDomainSocketError if error occurs.
  61. size_t write(const void* data, size_t length);
  62. /// @brief Asynchronously sends data over the socket.
  63. ///
  64. /// Always returns immediatelly.
  65. ///
  66. /// @param data Pointer to data to be sent.
  67. /// @param length Number of bytes to be sent.
  68. /// @param handler Callback to be invoked when data have been sent or
  69. /// sending error is signalled.
  70. void asyncSend(const void* data, const size_t length, const Handler& handler);
  71. /// @brief Receives data from a socket.
  72. ///
  73. /// @param [out] data Pointer to a location into which the read data should
  74. /// be stored.
  75. /// @param length Length of the buffer.
  76. ///
  77. /// @return Number of bytes read.
  78. /// @throw UnixDomainSocketError if error occurs.
  79. size_t receive(void* data, size_t length);
  80. /// @brief Asynchronously receives data over the socket.
  81. ///
  82. /// Always returns immediatelly.
  83. /// @param [out] data Pointer to a location into which the read data should
  84. /// be stored.
  85. /// @param length Length of the buffer.
  86. /// @param handler Callback to be invoked when data have been received or an
  87. /// error is signalled.
  88. void asyncReceive(void* data, const size_t length, const Handler& handler);
  89. /// @brief Disables read and write operations on the socket.
  90. ///
  91. /// @throw UnixDomainSocketError if an error occurs during shutdown.
  92. void shutdown();
  93. /// @brief Cancels scheduled asynchronous operations on the socket.
  94. ///
  95. /// @throw UnixDomainSocketError if an error occurs during cancel operation.
  96. void cancel();
  97. /// @brief Closes the socket.
  98. ///
  99. /// @throw UnixDomainSocketError if an error occurs during closure.
  100. void close();
  101. /// @brief Returns reference to the underlying ASIO socket.
  102. ///
  103. /// @return Reference to underlying ASIO socket.
  104. virtual boost::asio::local::stream_protocol::socket& getASIOSocket() const;
  105. private:
  106. /// @brief Pointer to the implementation of this class.
  107. boost::shared_ptr<UnixDomainSocketImpl> impl_;
  108. };
  109. } // end of namespace isc::asiolink
  110. } // end of namespace isc
  111. #endif // UNIX_DOMAIN_SOCKET_H