io_endpoint.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __IO_ENDPOINT_H
  15. #define __IO_ENDPOINT_H 1
  16. // IMPORTANT NOTE: only very few ASIO headers files can be included in
  17. // this file. In particular, asio.hpp should never be included here.
  18. // See the description of the namespace below.
  19. #include <unistd.h> // for some network system calls
  20. #include <functional>
  21. #include <string>
  22. #include <exceptions/exceptions.h>
  23. #include <asiolink/io_address.h>
  24. namespace isc {
  25. namespace asiolink {
  26. /// \brief The \c IOEndpoint class is an abstract base class to represent
  27. /// a communication endpoint.
  28. ///
  29. /// This class is a wrapper for the ASIO endpoint classes such as
  30. /// \c ip::tcp::endpoint and \c ip::udp::endpoint.
  31. ///
  32. /// Derived class implementations are completely hidden within the
  33. /// implementation. User applications only get access to concrete
  34. /// \c IOEndpoint objects via the abstract interfaces.
  35. class IOEndpoint {
  36. ///
  37. /// \name Constructors and Destructor
  38. ///
  39. /// Note: The copy constructor and the assignment operator are
  40. /// intentionally defined as private, making this class non-copyable.
  41. //@{
  42. private:
  43. IOEndpoint(const IOEndpoint& source);
  44. IOEndpoint& operator=(const IOEndpoint& source);
  45. protected:
  46. /// \brief The default constructor.
  47. ///
  48. /// This is intentionally defined as \c protected as this base class
  49. /// should never be instantiated (except as part of a derived class).
  50. IOEndpoint() {}
  51. public:
  52. /// The destructor.
  53. virtual ~IOEndpoint() {}
  54. //@}
  55. /// \brief Returns the address of the endpoint.
  56. ///
  57. /// This method returns an IOAddress object corresponding to \c this
  58. /// endpoint.
  59. ///
  60. /// Note that the return value is a real object, not a reference or
  61. /// a pointer.
  62. ///
  63. /// This is aligned with the interface of the ASIO counterpart:
  64. /// the \c address() method of \c ip::xxx::endpoint classes returns
  65. /// an \c ip::address object.
  66. ///
  67. /// This also means handling the address of an endpoint using this method
  68. /// can be expensive. If the address information is necessary in a
  69. /// performance sensitive context and there's a more efficient interface
  70. /// for that purpose, it's probably better to avoid using this method.
  71. ///
  72. /// This method never throws an exception.
  73. ///
  74. /// \return A copy of \c IOAddress object corresponding to the endpoint.
  75. virtual IOAddress getAddress() const = 0;
  76. /// \brief Returns the port of the endpoint.
  77. virtual uint16_t getPort() const = 0;
  78. /// \brief Returns the protocol number of the endpoint (TCP, UDP...)
  79. virtual short getProtocol() const = 0;
  80. /// \brief Returns the address family of the endpoint.
  81. virtual short getFamily() const = 0;
  82. bool operator==(const IOEndpoint& other) const;
  83. bool operator!=(const IOEndpoint& other) const;
  84. /// \brief A polymorphic factory of endpoint from address and port.
  85. ///
  86. /// This method creates a new instance of (a derived class of)
  87. /// \c IOEndpoint object that identifies the pair of given address
  88. /// and port.
  89. /// The appropriate derived class is chosen based on the specified
  90. /// transport protocol. If the \c protocol doesn't specify a protocol
  91. /// supported in this implementation, an exception of class \c IOError
  92. /// will be thrown.
  93. ///
  94. /// Memory for the created object will be dynamically allocated. It's
  95. /// the caller's responsibility to \c delete it later.
  96. /// If resource allocation for the new object fails, a corresponding
  97. /// standard exception will be thrown.
  98. ///
  99. /// \param protocol The transport protocol used for the endpoint.
  100. /// Currently, only \c IPPROTO_UDP and \c IPPROTO_TCP can be specified.
  101. /// \param address The (IP) address of the endpoint.
  102. /// \param port The transport port number of the endpoint
  103. /// \return A pointer to a newly created \c IOEndpoint object.
  104. static const IOEndpoint* create(const int protocol,
  105. const IOAddress& address,
  106. const unsigned short port);
  107. };
  108. } // namespace asiolink
  109. } // namespace isc
  110. #endif // __IO_ENDPOINT_H