2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/io/IOBuf.h>
19 #include <folly/io/async/AsyncTransport.h>
20 #include <folly/io/async/DecoratedAsyncTransportWrapper.h>
25 * Helper class that redirects write() and writev() calls to writeChain().
28 class WriteChainAsyncTransportWrapper :
29 public DecoratedAsyncTransportWrapper<T> {
31 using DecoratedAsyncTransportWrapper<T>::DecoratedAsyncTransportWrapper;
34 folly::AsyncTransportWrapper::WriteCallback* callback,
37 folly::WriteFlags flags = folly::WriteFlags::NONE) override {
38 auto ioBuf = folly::IOBuf::wrapBuffer(buf, bytes);
39 writeChain(callback, std::move(ioBuf), flags);
43 folly::AsyncTransportWrapper::WriteCallback* callback,
46 folly::WriteFlags flags = folly::WriteFlags::NONE) override {
47 std::unique_ptr<folly::IOBuf> writeBuffer;
49 for (size_t i = 0; i < count; ++i) {
50 size_t len = vec[i].iov_len;
51 void* data = vec[i].iov_base;
52 auto buf = folly::IOBuf::wrapBuffer(data, len);
54 writeBuffer = std::move(buf);
56 writeBuffer->prependChain(std::move(buf));
60 writeChain(callback, std::move(writeBuffer), flags);
65 * It only makes sense to use this class if you override writeChain, so force
66 * derived classes to do that.
68 virtual void writeChain(
69 folly::AsyncTransportWrapper::WriteCallback* callback,
70 std::unique_ptr<folly::IOBuf>&& buf,
71 folly::WriteFlags flags = folly::WriteFlags::NONE) override = 0;