WriteChainAsyncTransportWrapper.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/io/IOBuf.h>
  18. #include <folly/io/async/AsyncTransport.h>
  19. #include <folly/io/async/DecoratedAsyncTransportWrapper.h>
  20. namespace folly {
  21. /**
  22. * Helper class that redirects write() and writev() calls to writeChain().
  23. */
  24. template <class T>
  25. class WriteChainAsyncTransportWrapper
  26. : public DecoratedAsyncTransportWrapper<T> {
  27. public:
  28. using DecoratedAsyncTransportWrapper<T>::DecoratedAsyncTransportWrapper;
  29. void write(
  30. folly::AsyncTransportWrapper::WriteCallback* callback,
  31. const void* buf,
  32. size_t bytes,
  33. folly::WriteFlags flags = folly::WriteFlags::NONE) override {
  34. auto ioBuf = folly::IOBuf::wrapBuffer(buf, bytes);
  35. writeChain(callback, std::move(ioBuf), flags);
  36. }
  37. void writev(
  38. folly::AsyncTransportWrapper::WriteCallback* callback,
  39. const iovec* vec,
  40. size_t count,
  41. folly::WriteFlags flags = folly::WriteFlags::NONE) override {
  42. auto writeBuffer = folly::IOBuf::wrapIov(vec, count);
  43. writeChain(callback, std::move(writeBuffer), flags);
  44. }
  45. /**
  46. * It only makes sense to use this class if you override writeChain, so force
  47. * derived classes to do that.
  48. */
  49. void writeChain(
  50. folly::AsyncTransportWrapper::WriteCallback* callback,
  51. std::unique_ptr<folly::IOBuf>&& buf,
  52. folly::WriteFlags flags = folly::WriteFlags::NONE) override = 0;
  53. };
  54. } // namespace folly