io_endpoint.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 asiolink {
  25. /// \brief The \c IOEndpoint class is an abstract base class to represent
  26. /// a communication endpoint.
  27. ///
  28. /// This class is a wrapper for the ASIO endpoint classes such as
  29. /// \c ip::tcp::endpoint and \c ip::udp::endpoint.
  30. ///
  31. /// Derived class implementations are completely hidden within the
  32. /// implementation. User applications only get access to concrete
  33. /// \c IOEndpoint objects via the abstract interfaces.
  34. class IOEndpoint {
  35. ///
  36. /// \name Constructors and Destructor
  37. ///
  38. /// Note: The copy constructor and the assignment operator are
  39. /// intentionally defined as private, making this class non-copyable.
  40. //@{
  41. private:
  42. IOEndpoint(const IOEndpoint& source);
  43. IOEndpoint& operator=(const IOEndpoint& source);
  44. protected:
  45. /// \brief The default constructor.
  46. ///
  47. /// This is intentionally defined as \c protected as this base class
  48. /// should never be instantiated (except as part of a derived class).
  49. IOEndpoint() {}
  50. public:
  51. /// The destructor.
  52. virtual ~IOEndpoint() {}
  53. //@}
  54. /// \brief Returns the address of the endpoint.
  55. ///
  56. /// This method returns an IOAddress object corresponding to \c this
  57. /// endpoint.
  58. ///
  59. /// Note that the return value is a real object, not a reference or
  60. /// a pointer.
  61. ///
  62. /// This is aligned with the interface of the ASIO counterpart:
  63. /// the \c address() method of \c ip::xxx::endpoint classes returns
  64. /// an \c ip::address object.
  65. ///
  66. /// This also means handling the address of an endpoint using this method
  67. /// can be expensive. If the address information is necessary in a
  68. /// performance sensitive context and there's a more efficient interface
  69. /// for that purpose, it's probably better to avoid using this method.
  70. ///
  71. /// This method never throws an exception.
  72. ///
  73. /// \return A copy of \c IOAddress object corresponding to the endpoint.
  74. virtual IOAddress getAddress() const = 0;
  75. /// \brief Returns the port of the endpoint.
  76. virtual uint16_t getPort() const = 0;
  77. /// \brief Returns the protocol number of the endpoint (TCP, UDP...)
  78. virtual short getProtocol() const = 0;
  79. /// \brief Returns the address family of the endpoint.
  80. virtual short getFamily() const = 0;
  81. bool operator==(const IOEndpoint& other) const;
  82. bool operator!=(const IOEndpoint& other) const;
  83. /// \brief A polymorphic factory of endpoint from address and port.
  84. ///
  85. /// This method creates a new instance of (a derived class of)
  86. /// \c IOEndpoint object that identifies the pair of given address
  87. /// and port.
  88. /// The appropriate derived class is chosen based on the specified
  89. /// transport protocol. If the \c protocol doesn't specify a protocol
  90. /// supported in this implementation, an exception of class \c IOError
  91. /// will be thrown.
  92. ///
  93. /// Memory for the created object will be dynamically allocated. It's
  94. /// the caller's responsibility to \c delete it later.
  95. /// If resource allocation for the new object fails, a corresponding
  96. /// standard exception will be thrown.
  97. ///
  98. /// \param protocol The transport protocol used for the endpoint.
  99. /// Currently, only \c IPPROTO_UDP and \c IPPROTO_TCP can be specified.
  100. /// \param address The (IP) address of the endpoint.
  101. /// \param port The transport port number of the endpoint
  102. /// \return A pointer to a newly created \c IOEndpoint object.
  103. static const IOEndpoint* create(const int protocol,
  104. const IOAddress& address,
  105. const unsigned short port);
  106. };
  107. } // asiolink
  108. #endif // __IO_ENDPOINT_H