proxygen
FileRegion.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 
18 #include <folly/Singleton.h>
22 #include <folly/futures/Future.h>
23 #include <folly/futures/Promise.h>
25 
26 #ifdef SPLICE_F_NONBLOCK
27 namespace wangle {
28 
29 class FileRegion {
30  public:
31  FileRegion(int fd, loff_t offset, size_t count)
32  : fd_(fd), offset_(offset), count_(count) {}
33 
34  folly::Future<folly::Unit> transferTo(
35  std::shared_ptr<folly::AsyncTransport> transport) {
36  auto socket = std::dynamic_pointer_cast<folly::AsyncSocket>(
37  transport);
38  CHECK(socket);
39  auto cb = new WriteCallback();
40  auto f = cb->promise_.getFuture();
41  auto req = new FileWriteRequest(socket.get(), cb, fd_, offset_, count_);
42  socket->writeRequest(req);
43  return f;
44  }
45 
46  private:
48  void writeSuccess() noexcept override {
49  promise_.setValue();
50  delete this;
51  }
52 
53  void writeErr(size_t /* bytesWritten */,
55  noexcept override {
56  promise_.setException(ex);
57  delete this;
58  }
59 
60  friend class FileRegion;
62  };
63 
64  const int fd_;
65  const loff_t offset_;
66  const size_t count_;
67 
68  class FileWriteRequest : public folly::AsyncSocket::WriteRequest,
69  public folly::NotificationQueue<size_t>::Consumer {
70  public:
71  FileWriteRequest(folly::AsyncSocket* socket, WriteCallback* callback,
72  int fd, loff_t offset, size_t count);
73 
74  void destroy() override;
75 
76  folly::AsyncSocket::WriteResult performWrite() override;
77 
78  void consume() override;
79 
80  bool isComplete() override;
81 
82  void messageAvailable(size_t&& count) noexcept override;
83 
84  void start() override;
85 
86  class FileReadHandler : public folly::EventHandler {
87  public:
88  FileReadHandler(FileWriteRequest* req, int pipe_in, size_t bytesToRead);
89 
90  ~FileReadHandler() override;
91 
92  void handlerReady(uint16_t events) noexcept override;
93 
94  private:
95  FileWriteRequest* req_;
96  int pipe_in_;
97  size_t bytesToRead_;
98  };
99 
100  private:
101  ~FileWriteRequest() override;
102 
103  void fail(const char* fn, const folly::AsyncSocketException& ex);
104 
105  const int readFd_;
106  loff_t offset_;
107  const size_t count_;
108  bool started_{false};
109  int pipe_out_{-1};
110 
111  size_t bytesInPipe_{0};
112  folly::EventBase* readBase_;
114  std::unique_ptr<FileReadHandler> readHandler_;
115  };
116 };
117 
118 } // wangle
119 #endif
auto f
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
static void destroy()
void fail()
Promise< Unit > promise_
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
auto start
int * count
off_t offset_