udp_server.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_SERVER_H
  15. #define __UDP_SERVER_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/dns_server.h>
  20. #include <asiolink/simple_callback.h>
  21. #include <asiolink/dns_lookup.h>
  22. #include <asiolink/dns_answer.h>
  23. #include <coroutine.h>
  24. namespace asiolink {
  25. //
  26. // Asynchronous UDP server coroutine
  27. //
  28. ///
  29. /// \brief This class implements the coroutine to handle UDP
  30. /// DNS query event. As such, it is both a \c DNSServer and
  31. /// a \c coroutine
  32. ///
  33. class UDPServer : public virtual DNSServer, public virtual coroutine {
  34. public:
  35. /// \brief Constructor
  36. /// \param io_service the asio::io_service to work with
  37. /// \param addr the IP address to listen for queries on
  38. /// \param port the port to listen for queries on
  39. /// \param checkin the callbackprovider for non-DNS events
  40. /// \param lookup the callbackprovider for DNS lookup events
  41. /// \param answer the callbackprovider for DNS answer events
  42. explicit UDPServer(asio::io_service& io_service,
  43. const asio::ip::address& addr, const uint16_t port,
  44. SimpleCallback* checkin = NULL,
  45. DNSLookup* lookup = NULL,
  46. DNSAnswer* answer = NULL);
  47. /// \brief The function operator
  48. void operator()(asio::error_code ec = asio::error_code(),
  49. size_t length = 0);
  50. /// \brief Calls the lookup callback
  51. void asyncLookup();
  52. /// \brief Resume operation
  53. ///
  54. /// \param done Set this to true if the lookup action is done and
  55. /// we have an answer
  56. void resume(const bool done);
  57. /// \brief Check if we have an answer
  58. ///
  59. /// \return true if we have an answer
  60. bool hasAnswer();
  61. /// \brief Returns the coroutine state value
  62. ///
  63. /// \return the coroutine state value
  64. int value() { return (get_value()); }
  65. /// \brief Clones the object
  66. ///
  67. /// \return a newly allocated copy of this object
  68. DNSServer* clone() {
  69. UDPServer* s = new UDPServer(*this);
  70. return (s);
  71. }
  72. private:
  73. enum { MAX_LENGTH = 4096 };
  74. /**
  75. * \brief Internal state and data.
  76. *
  77. * We use the pimple design pattern, but not because we need to hide
  78. * internal data. This class and whole header is for private use anyway.
  79. * It turned out that UDPServer is copied a lot, because it is a coroutine.
  80. * This way the overhead of copying is lower, we copy only one shared
  81. * pointer instead of about 10 of them.
  82. */
  83. class Data;
  84. boost::shared_ptr<Data> data_;
  85. };
  86. } // namespace asiolink
  87. #endif // __UDP_SERVER_H