tcp_endpoint.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2011 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 __TCP_ENDPOINT_H
  15. #define __TCP_ENDPOINT_H 1
  16. #ifndef ASIO_HPP
  17. #error "asio.hpp must be included before including this, see asiolink.h as to why"
  18. #endif
  19. #include <asiolink/ioendpoint.h>
  20. namespace asiolink {
  21. /// \brief The \c TCPEndpoint class is a concrete derived class of
  22. /// \c IOEndpoint that represents an endpoint of a TCP connection.
  23. ///
  24. /// In the current implementation, an object of this class is always
  25. /// instantiated within the wrapper routines. Applications are expected to
  26. /// get access to the object via the abstract base class, \c IOEndpoint.
  27. /// This design may be changed when we generalize the wrapper interface.
  28. ///
  29. /// Note: this implementation is optimized for the case where this object
  30. /// is created from an ASIO endpoint object in a receiving code path
  31. /// by avoiding to make a copy of the base endpoint. For TCP it may not be
  32. /// a big deal, but when we receive UDP packets at a high rate, the copy
  33. /// overhead might be significant.
  34. class TCPEndpoint : public IOEndpoint {
  35. public:
  36. ///
  37. /// \name Constructors and Destructor
  38. ///
  39. //@{
  40. /// \brief Constructor from a pair of address and port.
  41. ///
  42. /// \param address The IP address of the endpoint.
  43. /// \param port The TCP port number of the endpoint.
  44. TCPEndpoint(const IOAddress& address, const unsigned short port) :
  45. asio_endpoint_placeholder_(
  46. new asio::ip::tcp::endpoint(
  47. asio::ip::address::from_string(address.toText()), port)),
  48. asio_endpoint_(*asio_endpoint_placeholder_)
  49. {}
  50. /// \brief Constructor from an ASIO TCP endpoint.
  51. ///
  52. /// This constructor is designed to be an efficient wrapper for the
  53. /// corresponding ASIO class, \c tcp::endpoint.
  54. ///
  55. /// \param asio_endpoint The ASIO representation of the TCP endpoint.
  56. TCPEndpoint(const asio::ip::tcp::endpoint& asio_endpoint) :
  57. asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
  58. {}
  59. /// \brief The destructor.
  60. ~TCPEndpoint() { delete asio_endpoint_placeholder_; }
  61. //@}
  62. IOAddress getAddress() const {
  63. return (asio_endpoint_.address());
  64. }
  65. uint16_t getPort() const {
  66. return (asio_endpoint_.port());
  67. }
  68. short getProtocol() const {
  69. return (asio_endpoint_.protocol().protocol());
  70. }
  71. short getFamily() const {
  72. return (asio_endpoint_.protocol().family());
  73. }
  74. // This is not part of the exosed IOEndpoint API but allows
  75. // direct access to the ASIO implementation of the endpoint
  76. const asio::ip::tcp::endpoint& getASIOEndpoint() const {
  77. return (asio_endpoint_);
  78. }
  79. private:
  80. const asio::ip::tcp::endpoint* asio_endpoint_placeholder_;
  81. const asio::ip::tcp::endpoint& asio_endpoint_;
  82. };
  83. } // namespace asiolink
  84. #endif // __TCP_ENDPOINT_H