iofetch.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 __IOFETCH_H
  15. #define __IOFETCH_H 1
  16. #include <config.h>
  17. #include <asio.hpp>
  18. #include <boost/shared_array.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <dns/buffer.h>
  21. #include <dns/message.h>
  22. #include <dns/messagerenderer.h>
  23. #include <asiolink/asiolink.h>
  24. #include <asiolink/internal/coroutine.h>
  25. // This file contains TCP/UDP-specific implementations of generic classes
  26. // defined in asiolink.h. It is *not* intended to be part of the public
  27. // API.
  28. namespace asiolink {
  29. //
  30. // Asynchronous UDP/TCP coroutine for upstream fetches
  31. //
  32. //class IOFetch : public coroutine, public UdpFetch, public TcpFetch {
  33. class IOFetch : public coroutine {
  34. public:
  35. // TODO Maybe this should be more generic than just for IOFetch?
  36. ///
  37. /// \brief Result of the query
  38. ///
  39. /// This is related only to contacting the remote server. If the answer
  40. ///indicates error, it is still counted as SUCCESS here, if it comes back.
  41. ///
  42. enum Result {
  43. SUCCESS,
  44. TIME_OUT,
  45. STOPPED
  46. };
  47. /// Abstract callback for the IOFetch.
  48. class Callback {
  49. public:
  50. virtual ~Callback() {}
  51. /// This will be called when the IOFetch is completed
  52. virtual void operator()(Result result) = 0;
  53. };
  54. ///
  55. /// \brief Constructor.
  56. ///
  57. /// It creates the query.
  58. /// @param callback will be called when we terminate. It is your task to
  59. /// delete it if allocated on heap.
  60. ///@param timeout in ms.
  61. ///
  62. IOFetch(asio::io_service& io_service,
  63. const isc::dns::Question& q,
  64. const IOAddress& addr, uint16_t port,
  65. isc::dns::OutputBufferPtr buffer,
  66. Callback* callback, int timeout = -1,
  67. int protocol = IPPROTO_UDP);
  68. void operator()(asio::error_code ec = asio::error_code(),
  69. size_t length = 0);
  70. /// Terminate the query.
  71. void stop(Result reason = STOPPED);
  72. private:
  73. enum { MAX_LENGTH = 4096 };
  74. ///
  75. /// \short Private data
  76. ///
  77. /// They are not private because of stability of the
  78. /// interface (this is private class anyway), but because this class
  79. /// will be copyed often (it is used as a coroutine and passed as callback
  80. /// to many async_*() functions) and we want keep the same data. Some of
  81. /// the data is not copyable too.
  82. ///
  83. //struct IOFetchProtocol;
  84. //boost::shared_ptr<IOFetchProtocol> data_;
  85. //struct UdpData;
  86. //struct TcpData;
  87. boost::shared_ptr<UdpFetch> data_;
  88. boost::shared_ptr<TcpFetch> tcp_data_;
  89. };
  90. class UdpFetch : public IOFetch {
  91. public:
  92. struct UdpData;
  93. explicit UdpFetch(asio::io_service& io_service,
  94. const isc::dns::Question& q,
  95. const IOAddress& addr,
  96. uint16_t port,
  97. isc::dns::OutputBufferPtr buffer,
  98. IOFetch::Callback *callback,
  99. int timeout);
  100. };
  101. class TcpFetch : public IOFetch {
  102. public:
  103. struct TcpData;
  104. explicit TcpFetch(io_service& io_service, const Question& q,
  105. const IOAddress& addr, uint16_t port,
  106. OutputBufferPtr buffer, Callback *callback, int timeout);
  107. };
  108. }
  109. #endif // __IOFETCH_H
  110. // Local Variables:
  111. // mode: c++
  112. // End: