asio.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_COMMON_CONTINUATION_ASIO_H_
  19. #define LIB_COMMON_CONTINUATION_ASIO_H_
  20. #include "continuation.h"
  21. #include "common/util.h"
  22. #include "hdfspp/status.h"
  23. #include <asio/connect.hpp>
  24. #include <asio/read.hpp>
  25. #include <asio/write.hpp>
  26. #include <asio/ip/tcp.hpp>
  27. #include <memory>
  28. namespace hdfs {
  29. namespace asio_continuation {
  30. using namespace continuation;
  31. template <class Stream, class MutableBufferSequence>
  32. class ReadContinuation : public Continuation {
  33. public:
  34. ReadContinuation(std::shared_ptr<Stream>& stream, const MutableBufferSequence &buffer)
  35. : stream_(stream), buffer_(buffer) {}
  36. virtual void Run(const Next &next) override {
  37. auto handler =
  38. [next](const asio::error_code &ec, size_t) { next(ToStatus(ec)); };
  39. asio::async_read(*stream_, buffer_, handler);
  40. }
  41. private:
  42. // prevent construction from raw ptr
  43. ReadContinuation(Stream *stream, MutableBufferSequence &buffer);
  44. std::shared_ptr<Stream> stream_;
  45. MutableBufferSequence buffer_;
  46. };
  47. template <class Stream, class ConstBufferSequence>
  48. class WriteContinuation : public Continuation {
  49. public:
  50. WriteContinuation(std::shared_ptr<Stream>& stream, const ConstBufferSequence &buffer)
  51. : stream_(stream), buffer_(buffer) {}
  52. virtual void Run(const Next &next) override {
  53. auto handler =
  54. [next](const asio::error_code &ec, size_t) { next(ToStatus(ec)); };
  55. asio::async_write(*stream_, buffer_, handler);
  56. }
  57. private:
  58. // prevent construction from raw ptr
  59. WriteContinuation(Stream *stream, ConstBufferSequence &buffer);
  60. std::shared_ptr<Stream> stream_;
  61. ConstBufferSequence buffer_;
  62. };
  63. template <class Socket, class Iterator>
  64. class ConnectContinuation : public Continuation {
  65. public:
  66. ConnectContinuation(Socket *socket, Iterator begin, Iterator end,
  67. Iterator *connected_endpoint)
  68. : socket_(socket), begin_(begin), end_(end),
  69. connected_endpoint_(connected_endpoint) {}
  70. virtual void Run(const Next &next) override {
  71. auto handler = [this, next](const asio::error_code &ec, Iterator it) {
  72. if (connected_endpoint_) {
  73. *connected_endpoint_ = it;
  74. }
  75. next(ToStatus(ec));
  76. };
  77. asio::async_connect(*socket_, begin_, end_, handler);
  78. }
  79. private:
  80. Socket *socket_;
  81. Iterator begin_;
  82. Iterator end_;
  83. Iterator *connected_endpoint_;
  84. };
  85. template <class OutputIterator>
  86. class ResolveContinuation : public Continuation {
  87. public:
  88. ResolveContinuation(::asio::io_service *io_service, const std::string &server,
  89. const std::string &service, OutputIterator result)
  90. : resolver_(*io_service), query_(server, service), result_(result) {}
  91. virtual void Run(const Next &next) override {
  92. using resolver = ::asio::ip::tcp::resolver;
  93. auto handler =
  94. [this, next](const asio::error_code &ec, resolver::iterator it) {
  95. if (!ec) {
  96. std::copy(it, resolver::iterator(), result_);
  97. }
  98. next(ToStatus(ec));
  99. };
  100. resolver_.async_resolve(query_, handler);
  101. }
  102. private:
  103. ::asio::ip::tcp::resolver resolver_;
  104. ::asio::ip::tcp::resolver::query query_;
  105. OutputIterator result_;
  106. };
  107. template <class Stream, class ConstBufferSequence>
  108. static inline Continuation *Write(std::shared_ptr<Stream> stream,
  109. const ConstBufferSequence &buffer) {
  110. return new WriteContinuation<Stream, ConstBufferSequence>(stream, buffer);
  111. }
  112. template <class Stream, class MutableBufferSequence>
  113. static inline Continuation *Read(std::shared_ptr<Stream> stream,
  114. const MutableBufferSequence &buffer) {
  115. return new ReadContinuation<Stream, MutableBufferSequence>(stream, buffer);
  116. }
  117. template <class Socket, class Iterator>
  118. static inline Continuation *Connect(Socket *socket, Iterator begin,
  119. Iterator end) {
  120. return new ConnectContinuation<Socket, Iterator>(socket, begin, end, nullptr);
  121. }
  122. template <class OutputIterator>
  123. static inline Continuation *
  124. Resolve(::asio::io_service *io_service, const std::string &server,
  125. const std::string &service, OutputIterator result) {
  126. return new ResolveContinuation<OutputIterator>(io_service, server, service, result);
  127. }
  128. }
  129. }
  130. #endif