udp_endpoint.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 __UDP_ENDPOINT_H
  15. #define __UDP_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/io_endpoint.h>
  20. namespace asiolink {
  21. /// \brief The \c UDPEndpoint class is a concrete derived class of
  22. /// \c IOEndpoint that represents an endpoint of a UDP packet.
  23. ///
  24. /// Other notes about \c TCPEndpoint applies to this class, too.
  25. class UDPEndpoint : public IOEndpoint {
  26. public:
  27. ///
  28. /// \name Constructors and Destructor.
  29. ///
  30. //@{
  31. /// \brief Constructor from a pair of address and port.
  32. ///
  33. /// \param address The IP address of the endpoint.
  34. /// \param port The UDP port number of the endpoint.
  35. UDPEndpoint(const IOAddress& address, const unsigned short port) :
  36. asio_endpoint_placeholder_(
  37. new asio::ip::udp::endpoint(asio::ip::address::from_string(address.toText()),
  38. port)),
  39. asio_endpoint_(*asio_endpoint_placeholder_)
  40. {}
  41. /// \brief Constructor from an ASIO UDP endpoint.
  42. ///
  43. /// This constructor is designed to be an efficient wrapper for the
  44. /// corresponding ASIO class, \c udp::endpoint.
  45. ///
  46. /// \param asio_endpoint The ASIO representation of the UDP endpoint.
  47. UDPEndpoint(const asio::ip::udp::endpoint& asio_endpoint) :
  48. asio_endpoint_placeholder_(NULL), asio_endpoint_(asio_endpoint)
  49. {}
  50. /// \brief The destructor.
  51. ~UDPEndpoint() { delete asio_endpoint_placeholder_; }
  52. //@}
  53. inline IOAddress getAddress() const {
  54. return (asio_endpoint_.address());
  55. }
  56. inline uint16_t getPort() const {
  57. return (asio_endpoint_.port());
  58. }
  59. inline short getProtocol() const {
  60. return (asio_endpoint_.protocol().protocol());
  61. }
  62. inline short getFamily() const {
  63. return (asio_endpoint_.protocol().family());
  64. }
  65. // This is not part of the exosed IOEndpoint API but allows
  66. // direct access to the ASIO implementation of the endpoint
  67. inline const asio::ip::udp::endpoint& getASIOEndpoint() const {
  68. return (asio_endpoint_);
  69. }
  70. private:
  71. const asio::ip::udp::endpoint* asio_endpoint_placeholder_;
  72. const asio::ip::udp::endpoint& asio_endpoint_;
  73. };
  74. } // namespace asiolink
  75. #endif // __UDP_ENDPOINT_H