2 * Copyright 2015 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.
19 namespace folly { namespace wangle {
21 class PipelineContext {
23 virtual ~PipelineContext() {}
25 virtual void attachPipeline() = 0;
26 virtual void detachPipeline() = 0;
28 virtual void attachTransport() = 0;
29 virtual void detachTransport() = 0;
31 template <class H, class HandlerContext>
32 void attachContext(H* handler, HandlerContext* ctx) {
33 if (++handler->attachCount_ == 1) {
36 handler->ctx_ = nullptr;
40 virtual void setNextIn(PipelineContext* ctx) = 0;
41 virtual void setNextOut(PipelineContext* ctx) = 0;
47 virtual ~InboundLink() {}
48 virtual void read(In msg) = 0;
49 virtual void readEOF() = 0;
50 virtual void readException(exception_wrapper e) = 0;
56 virtual ~OutboundLink() {}
57 virtual Future<void> write(Out msg) = 0;
58 virtual Future<void> close() = 0;
61 template <class P, class H, class Context>
62 class ContextImplBase : public PipelineContext {
67 return handler_.get();
70 void initialize(P* pipeline, std::shared_ptr<H> handler) {
72 handler_ = std::move(handler);
75 // PipelineContext overrides
76 void attachPipeline() override {
78 this->attachContext(handler_.get(), impl_);
79 handler_->attachPipeline(impl_);
84 void detachPipeline() override {
85 handler_->detachPipeline(impl_);
89 void attachTransport() override {
90 DestructorGuard dg(pipeline_);
91 handler_->attachTransport(impl_);
94 void detachTransport() override {
95 DestructorGuard dg(pipeline_);
96 handler_->detachTransport(impl_);
99 void setNextIn(PipelineContext* ctx) override {
100 auto nextIn = dynamic_cast<InboundLink<typename H::rout>*>(ctx);
104 throw std::invalid_argument("inbound type mismatch");
108 void setNextOut(PipelineContext* ctx) override {
109 auto nextOut = dynamic_cast<OutboundLink<typename H::wout>*>(ctx);
113 throw std::invalid_argument("outbound type mismatch");
120 std::shared_ptr<H> handler_;
121 InboundLink<typename H::rout>* nextIn_{nullptr};
122 OutboundLink<typename H::wout>* nextOut_{nullptr};
125 bool attached_{false};
126 using DestructorGuard = typename P::DestructorGuard;
129 template <class P, class H>
131 : public HandlerContext<typename H::rout,
133 public InboundLink<typename H::rin>,
134 public OutboundLink<typename H::win>,
135 public ContextImplBase<P, H, HandlerContext<typename H::rout,
138 typedef typename H::rin Rin;
139 typedef typename H::rout Rout;
140 typedef typename H::win Win;
141 typedef typename H::wout Wout;
142 static const HandlerDir dir = HandlerDir::BOTH;
144 explicit ContextImpl(P* pipeline, std::shared_ptr<H> handler) {
146 this->initialize(pipeline, std::move(handler));
149 // For StaticPipeline
156 // HandlerContext overrides
157 void fireRead(Rout msg) override {
158 DestructorGuard dg(this->pipeline_);
160 this->nextIn_->read(std::forward<Rout>(msg));
162 LOG(WARNING) << "read reached end of pipeline";
166 void fireReadEOF() override {
167 DestructorGuard dg(this->pipeline_);
169 this->nextIn_->readEOF();
171 LOG(WARNING) << "readEOF reached end of pipeline";
175 void fireReadException(exception_wrapper e) override {
176 DestructorGuard dg(this->pipeline_);
178 this->nextIn_->readException(std::move(e));
180 LOG(WARNING) << "readException reached end of pipeline";
184 Future<void> fireWrite(Wout msg) override {
185 DestructorGuard dg(this->pipeline_);
186 if (this->nextOut_) {
187 return this->nextOut_->write(std::forward<Wout>(msg));
189 LOG(WARNING) << "write reached end of pipeline";
194 Future<void> fireClose() override {
195 DestructorGuard dg(this->pipeline_);
196 if (this->nextOut_) {
197 return this->nextOut_->close();
199 LOG(WARNING) << "close reached end of pipeline";
204 std::shared_ptr<AsyncTransport> getTransport() override {
205 return this->pipeline_->getTransport();
208 void setWriteFlags(WriteFlags flags) override {
209 this->pipeline_->setWriteFlags(flags);
212 WriteFlags getWriteFlags() override {
213 return this->pipeline_->getWriteFlags();
216 void setReadBufferSettings(
217 uint64_t minAvailable,
218 uint64_t allocationSize) override {
219 this->pipeline_->setReadBufferSettings(minAvailable, allocationSize);
222 std::pair<uint64_t, uint64_t> getReadBufferSettings() override {
223 return this->pipeline_->getReadBufferSettings();
226 // InboundLink overrides
227 void read(Rin msg) override {
228 DestructorGuard dg(this->pipeline_);
229 this->handler_->read(this, std::forward<Rin>(msg));
232 void readEOF() override {
233 DestructorGuard dg(this->pipeline_);
234 this->handler_->readEOF(this);
237 void readException(exception_wrapper e) override {
238 DestructorGuard dg(this->pipeline_);
239 this->handler_->readException(this, std::move(e));
242 // OutboundLink overrides
243 Future<void> write(Win msg) override {
244 DestructorGuard dg(this->pipeline_);
245 return this->handler_->write(this, std::forward<Win>(msg));
248 Future<void> close() override {
249 DestructorGuard dg(this->pipeline_);
250 return this->handler_->close(this);
254 using DestructorGuard = typename P::DestructorGuard;
257 template <class P, class H>
258 class InboundContextImpl
259 : public InboundHandlerContext<typename H::rout>,
260 public InboundLink<typename H::rin>,
261 public ContextImplBase<P, H, InboundHandlerContext<typename H::rout>> {
263 typedef typename H::rin Rin;
264 typedef typename H::rout Rout;
265 typedef typename H::win Win;
266 typedef typename H::wout Wout;
267 static const HandlerDir dir = HandlerDir::IN;
269 explicit InboundContextImpl(P* pipeline, std::shared_ptr<H> handler) {
271 this->initialize(pipeline, std::move(handler));
274 // For StaticPipeline
275 InboundContextImpl() {
279 ~InboundContextImpl() {}
281 // InboundHandlerContext overrides
282 void fireRead(Rout msg) override {
283 DestructorGuard dg(this->pipeline_);
285 this->nextIn_->read(std::forward<Rout>(msg));
287 LOG(WARNING) << "read reached end of pipeline";
291 void fireReadEOF() override {
292 DestructorGuard dg(this->pipeline_);
294 this->nextIn_->readEOF();
296 LOG(WARNING) << "readEOF reached end of pipeline";
300 void fireReadException(exception_wrapper e) override {
301 DestructorGuard dg(this->pipeline_);
303 this->nextIn_->readException(std::move(e));
305 LOG(WARNING) << "readException reached end of pipeline";
309 std::shared_ptr<AsyncTransport> getTransport() override {
310 return this->pipeline_->getTransport();
313 // InboundLink overrides
314 void read(Rin msg) override {
315 DestructorGuard dg(this->pipeline_);
316 this->handler_->read(this, std::forward<Rin>(msg));
319 void readEOF() override {
320 DestructorGuard dg(this->pipeline_);
321 this->handler_->readEOF(this);
324 void readException(exception_wrapper e) override {
325 DestructorGuard dg(this->pipeline_);
326 this->handler_->readException(this, std::move(e));
330 using DestructorGuard = typename P::DestructorGuard;
333 template <class P, class H>
334 class OutboundContextImpl
335 : public OutboundHandlerContext<typename H::wout>,
336 public OutboundLink<typename H::win>,
337 public ContextImplBase<P, H, OutboundHandlerContext<typename H::wout>> {
339 typedef typename H::rin Rin;
340 typedef typename H::rout Rout;
341 typedef typename H::win Win;
342 typedef typename H::wout Wout;
343 static const HandlerDir dir = HandlerDir::OUT;
345 explicit OutboundContextImpl(P* pipeline, std::shared_ptr<H> handler) {
347 this->initialize(pipeline, std::move(handler));
350 // For StaticPipeline
351 OutboundContextImpl() {
355 ~OutboundContextImpl() {}
357 // OutboundHandlerContext overrides
358 Future<void> fireWrite(Wout msg) override {
359 DestructorGuard dg(this->pipeline_);
360 if (this->nextOut_) {
361 return this->nextOut_->write(std::forward<Wout>(msg));
363 LOG(WARNING) << "write reached end of pipeline";
368 Future<void> fireClose() override {
369 DestructorGuard dg(this->pipeline_);
370 if (this->nextOut_) {
371 return this->nextOut_->close();
373 LOG(WARNING) << "close reached end of pipeline";
378 std::shared_ptr<AsyncTransport> getTransport() override {
379 return this->pipeline_->getTransport();
382 // OutboundLink overrides
383 Future<void> write(Win msg) override {
384 DestructorGuard dg(this->pipeline_);
385 return this->handler_->write(this, std::forward<Win>(msg));
388 Future<void> close() override {
389 DestructorGuard dg(this->pipeline_);
390 return this->handler_->close(this);
394 using DestructorGuard = typename P::DestructorGuard;
397 template <class Handler, class Pipeline>
399 typedef typename std::conditional<
400 Handler::dir == HandlerDir::BOTH,
401 ContextImpl<Pipeline, Handler>,
402 typename std::conditional<
403 Handler::dir == HandlerDir::IN,
404 InboundContextImpl<Pipeline, Handler>,
405 OutboundContextImpl<Pipeline, Handler>