proxygen
FizzBase-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
10 
11 namespace fizz {
12 
13 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
16  pendingEvents_.push_back(std::move(w));
17  processPendingEvents();
18 }
19 
20 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
22  pendingEvents_.push_back(std::move(w));
23  processPendingEvents();
24 }
25 
26 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
28  EarlyAppWrite w) {
29  pendingEvents_.push_back(std::move(w));
30  processPendingEvents();
31 }
32 
33 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
35  pendingEvents_.push_back(AppClose());
36  processPendingEvents();
37 }
38 
39 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
41  waitForData_ = true;
42 }
43 
44 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
46  waitForData_ = false;
47  processPendingEvents();
48 }
49 
50 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
52  const folly::AsyncSocketException& ex) {
53  // We use a separate flag here rather than just moving the state to Error
54  // since there may be a currently processing action.
55  inErrorState_ = true;
56  while (!pendingEvents_.empty()) {
57  auto event = std::move(pendingEvents_.front());
58  pendingEvents_.pop_front();
60  event,
61  [&ex](AppWrite& write) {
62  if (write.callback) {
63  write.callback->writeErr(0, ex);
64  }
65  },
66  [&ex](EarlyAppWrite& write) {
67  if (write.callback) {
68  write.callback->writeErr(0, ex);
69  }
70  },
71  [](auto&) {});
72  }
73 }
74 
75 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
77  return inErrorState_ || state_.state() == decltype(state_.state())::Error;
78 }
79 
80 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
82  const {
83  return actionGuard_.hasValue();
84 }
85 
86 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
88  typename StateMachine::CompletedActions actions) {
89  // This extra DestructorGuard is needed due to the gap between clearing
90  // actionGuard_ and potentially processing another action.
92 
93  for (auto& action : actions) {
94  boost::apply_visitor(visitor_, action);
95  }
96 
97  actionGuard_.clear();
98  processPendingEvents();
99 }
100 
101 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
103  typename StateMachine::ProcessingActions actions) {
104  if (actionGuard_) {
105  throw std::runtime_error("actions already processing");
106  }
107 
108  actionGuard_ = folly::DelayedDestruction::DestructorGuard(owner_);
109 
110  static_cast<Derived*>(this)->startActions(std::move(actions));
111 }
112 
113 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
116  if (inProcessPendingEvents_) {
117  return;
118  }
119 
121  inProcessPendingEvents_ = true;
122  SCOPE_EXIT {
123  inProcessPendingEvents_ = false;
124  };
125 
126  while (!actionGuard_ && !inErrorState()) {
128  actionGuard_ = folly::DelayedDestruction::DestructorGuard(owner_);
129  if (!waitForData_) {
130  actions = machine_.processSocketData(state_, transportReadBuf_);
131  } else if (!pendingEvents_.empty()) {
132  auto event = std::move(pendingEvents_.front());
133  pendingEvents_.pop_front();
135  event,
136  [&actions, this](WriteNewSessionTicket& write) {
137  actions =
138  machine_.processWriteNewSessionTicket(state_, std::move(write));
139  },
140  [&actions, this](AppWrite& write) {
141  actions = machine_.processAppWrite(state_, std::move(write));
142  },
143  [&actions, this](EarlyAppWrite& write) {
144  actions = machine_.processEarlyAppWrite(state_, std::move(write));
145  },
146  [&actions, this](AppClose&) {
147  actions = machine_.processAppClose(state_);
148  });
149  } else {
150  actionGuard_.clear();
151  return;
152  }
153 
154  static_cast<Derived*>(this)->startActions(std::move(*actions));
155  }
156 }
157 
158 template <typename Derived, typename ActionMoveVisitor, typename StateMachine>
161  const Buf& context,
162  uint16_t length) const {
163  return Exporter::getEkm(
164  *state_.cipher(),
165  (*state_.exporterMasterSecret())->coalesce(),
166  label,
167  context ? context->clone() : nullptr,
168  length);
169 }
170 } // namespace fizz
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
context
Definition: CMakeCache.txt:563
void processActions(typename StateMachine::CompletedActions actions)
Definition: FizzBase-inl.h:87
void moveToErrorState(const folly::AsyncSocketException &ex)
Definition: FizzBase-inl.h:51
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
void earlyAppWrite(EarlyAppWrite appWrite)
Definition: FizzBase-inl.h:27
Buf getEkm(folly::StringPiece label, const Buf &context, uint16_t length) const
Definition: FizzBase-inl.h:159
void processPendingEvents()
Definition: FizzBase-inl.h:115
bool inErrorState() const
Definition: FizzBase-inl.h:76
Definition: Actions.h:16
void waitForData()
Definition: FizzBase-inl.h:40
Actions actions(Args &&...act)
Definition: Actions.h:86
void newTransportData()
Definition: FizzBase-inl.h:45
void addProcessingActions(typename StateMachine::ProcessingActions actions)
Definition: FizzBase-inl.h:102
static Buf getEkm(CipherSuite cipher, folly::ByteRange exporterMaster, folly::StringPiece label, Buf context, uint16_t length)
Definition: Exporter.cpp:13
decltype(auto) apply_visitor(Visitor &&visitor, const DiscriminatedPtr< Args... > &variant)
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
decltype(auto) variant_match(Variant &&variant, Cases &&...cases)
Definition: Overload.h:74
void appWrite(AppWrite appWrite)
Definition: FizzBase-inl.h:21
virtual void writeErr(size_t bytesWritten, const AsyncSocketException &ex) noexcept=0
void writeNewSessionTicket(WriteNewSessionTicket writeNewSessionTicket)
Definition: FizzBase-inl.h:14
bool actionProcessing() const
Definition: FizzBase-inl.h:81
StringPiece label
folly::AsyncTransportWrapper::WriteCallback * callback
Definition: Params.h:53
action
Definition: upload.py:393