proxygen
SocketPeeker.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 <array>
21 
22 namespace wangle {
23 
26  public:
27  using UniquePtr =
28  std::unique_ptr<SocketPeeker, folly::DelayedDestruction::Destructor>;
29 
30  class Callback {
31  public:
32  virtual ~Callback() = default;
33  virtual void peekSuccess(std::vector<uint8_t> data) noexcept = 0;
34  virtual void peekError(const folly::AsyncSocketException& ex) noexcept = 0;
35  };
36 
37  SocketPeeker(folly::AsyncSocket& socket, Callback* callback, size_t numBytes)
38  : socket_(socket), callback_(callback), peekBytes_(numBytes) {}
39 
40  ~SocketPeeker() override {
41  if (socket_.getReadCallback() == this) {
42  socket_.setReadCB(nullptr);
43  }
44  }
45 
46  void start() {
47  if (peekBytes_.size() == 0) {
48  // No peeking necessary.
49  auto callback = callback_;
50  callback_ = nullptr;
51  callback->peekSuccess(std::move(peekBytes_));
52  } else {
53  socket_.setReadCB(this);
54  }
55  }
56 
57  void getReadBuffer(void** bufReturn, size_t* lenReturn) override {
58  CHECK_LT(read_, peekBytes_.size());
59  *bufReturn = reinterpret_cast<void*>(peekBytes_.data() + read_);
60  *lenReturn = peekBytes_.size() - read_;
61  }
62 
63  void readEOF() noexcept override {
65 
66  auto type =
67  folly::AsyncSocketException::AsyncSocketExceptionType::END_OF_FILE;
68  readErr(folly::AsyncSocketException(type, "Unexpected EOF"));
69  }
70 
71  void readErr(const folly::AsyncSocketException& ex) noexcept override {
73 
74  socket_.setReadCB(nullptr);
75  if (callback_) {
76  auto callback = callback_;
77  callback_ = nullptr;
78  callback->peekError(ex);
79  }
80  }
81 
82  void readDataAvailable(size_t len) noexcept override {
84 
85  read_ += len;
86  CHECK_LE(read_, peekBytes_.size());
87 
88  if (read_ == peekBytes_.size()) {
91  socket_.setReadCB(nullptr);
92  auto callback = callback_;
93  callback_ = nullptr;
94  callback->peekSuccess(std::move(peekBytes_));
95  }
96  }
97 
98  bool isBufferMovable() noexcept override {
99  // Returning false so that we can supply the exact length of the
100  // number of bytes we want to read.
101  return false;
102  }
103 
104  private:
107  size_t read_{0};
108  std::vector<uint8_t> peekBytes_;
109 };
110 }
bool isBufferMovable() noexceptoverride
Definition: SocketPeeker.h:98
PskType type
std::vector< uint8_t > peekBytes_
Definition: SocketPeeker.h:108
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
requires E e noexcept(noexcept(s.error(std::move(e))))
void readEOF() noexceptoverride
Definition: SocketPeeker.h:63
void readDataAvailable(size_t len) noexceptoverride
Definition: SocketPeeker.h:82
folly::AsyncSocket & socket_
Definition: SocketPeeker.h:105
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
~SocketPeeker() override
Definition: SocketPeeker.h:40
virtual void peekError(const folly::AsyncSocketException &ex) noexcept=0
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
virtual void peekSuccess(std::vector< uint8_t > data) noexcept=0
void setReadCB(ReadCallback *callback) override
virtual void setPreReceivedData(std::unique_ptr< IOBuf > data)
Definition: AsyncSocket.h:770
void readErr(const folly::AsyncSocketException &ex) noexceptoverride
Definition: SocketPeeker.h:71
SocketPeeker(folly::AsyncSocket &socket, Callback *callback, size_t numBytes)
Definition: SocketPeeker.h:37
ReadCallback * getReadCallback() const override
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
void getReadBuffer(void **bufReturn, size_t *lenReturn) override
Definition: SocketPeeker.h:57
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28