proxygen
Handler.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 
17 #pragma once
18 
19 #include <folly/futures/Future.h>
21 #include <folly/io/IOBuf.h>
22 #include <folly/io/IOBufQueue.h>
23 
24 namespace wangle {
25 
26 template <class Context>
27 class HandlerBase {
28  public:
29  virtual ~HandlerBase() = default;
30 
31  virtual void attachPipeline(Context* /*ctx*/) {}
32  virtual void detachPipeline(Context* /*ctx*/) {}
33 
34  Context* getContext() {
35  if (attachCount_ != 1) {
36  return nullptr;
37  }
38  CHECK(ctx_);
39  return ctx_;
40  }
41 
42  private:
45  Context* ctx_{nullptr};
46 };
47 
48 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
49 class Handler : public HandlerBase<HandlerContext<Rout, Wout>> {
50  public:
51  static const HandlerDir dir = HandlerDir::BOTH;
52 
53  typedef Rin rin;
54  typedef Rout rout;
55  typedef Win win;
56  typedef Wout wout;
58  ~Handler() override = default;
59 
60  virtual void read(Context* ctx, Rin msg) = 0;
61  virtual void readEOF(Context* ctx) {
62  ctx->fireReadEOF();
63  }
64  virtual void readException(Context* ctx, folly::exception_wrapper e) {
66  }
67  virtual void transportActive(Context* ctx) {
68  ctx->fireTransportActive();
69  }
70  virtual void transportInactive(Context* ctx) {
71  ctx->fireTransportInactive();
72  }
73 
74  virtual folly::Future<folly::Unit> write(Context* ctx, Win msg) = 0;
77  return ctx->fireWriteException(std::move(e));
78  }
79  virtual folly::Future<folly::Unit> close(Context* ctx) {
80  return ctx->fireClose();
81  }
82 
83  /*
84  // Other sorts of things we might want, all shamelessly stolen from Netty
85  // inbound
86  virtual void exceptionCaught(
87  HandlerContext* ctx,
88  folly::exception_wrapper e) {}
89  virtual void channelRegistered(HandlerContext* ctx) {}
90  virtual void channelUnregistered(HandlerContext* ctx) {}
91  virtual void channelReadComplete(HandlerContext* ctx) {}
92  virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
93  virtual void channelWritabilityChanged(HandlerContext* ctx) {}
94 
95  // outbound
96  virtual folly::Future<folly::Unit> bind(
97  HandlerContext* ctx,
98  SocketAddress localAddress) {}
99  virtual folly::Future<folly::Unit> connect(
100  HandlerContext* ctx,
101  SocketAddress remoteAddress, SocketAddress localAddress) {}
102  virtual folly::Future<folly::Unit> disconnect(HandlerContext* ctx) {}
103  virtual folly::Future<folly::Unit> deregister(HandlerContext* ctx) {}
104  virtual folly::Future<folly::Unit> read(HandlerContext* ctx) {}
105  virtual void flush(HandlerContext* ctx) {}
106  */
107 };
108 
109 template <class Rin, class Rout = Rin>
110 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
111  public:
112  static const HandlerDir dir = HandlerDir::IN;
113 
114  typedef Rin rin;
115  typedef Rout rout;
116  typedef folly::Unit win;
117  typedef folly::Unit wout;
119  ~InboundHandler() override = default;
120 
121  virtual void read(Context* ctx, Rin msg) = 0;
122  virtual void readEOF(Context* ctx) {
123  ctx->fireReadEOF();
124  }
125  virtual void readException(Context* ctx, folly::exception_wrapper e) {
126  ctx->fireReadException(std::move(e));
127  }
128  virtual void transportActive(Context* ctx) {
129  ctx->fireTransportActive();
130  }
131  virtual void transportInactive(Context* ctx) {
132  ctx->fireTransportInactive();
133  }
134 };
135 
136 template <class Win, class Wout = Win>
137 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
138  public:
139  static const HandlerDir dir = HandlerDir::OUT;
140 
141  typedef folly::Unit rin;
142  typedef folly::Unit rout;
143  typedef Win win;
144  typedef Wout wout;
146  ~OutboundHandler() override = default;
147 
148  virtual folly::Future<folly::Unit> write(Context* ctx, Win msg) = 0;
150  Context* ctx, folly::exception_wrapper e) {
151  return ctx->fireWriteException(std::move(e));
152  }
153  virtual folly::Future<folly::Unit> close(Context* ctx) {
154  return ctx->fireClose();
155  }
156 };
157 
158 template <class R, class W = R>
159 class HandlerAdapter : public Handler<R, R, W, W> {
160  public:
162 
163  void read(Context* ctx, R msg) override {
164  ctx->fireRead(std::forward<R>(msg));
165  }
166 
167  folly::Future<folly::Unit> write(Context* ctx, W msg) override {
168  return ctx->fireWrite(std::forward<W>(msg));
169  }
170 };
171 
174 
177 
180 
181 } // namespace wangle
virtual void fireTransportInactive()=0
virtual void fireTransportActive()=0
virtual folly::Future< folly::Unit > fireWriteException(folly::exception_wrapper e)=0
virtual void transportInactive(Context *ctx)
Definition: Handler.h:131
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
virtual folly::Future< folly::Unit > writeException(Context *ctx, folly::exception_wrapper e)
Definition: Handler.h:75
virtual folly::Future< folly::Unit > close(Context *ctx)
Definition: Handler.h:153
virtual void fireTransportActive()=0
InboundHandlerContext< Rout > Context
Definition: Handler.h:118
virtual void fireReadEOF()=0
virtual folly::Future< folly::Unit > fireWrite(Out msg)=0
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Unit wout
Definition: Handler.h:117
virtual void transportInactive(Context *ctx)
Definition: Handler.h:70
folly::Unit rout
Definition: Handler.h:142
HandlerAdapter< folly::IOBufQueue &, std::unique_ptr< folly::IOBuf > > BytesToBytesHandler
Definition: Handler.h:173
uint64_t attachCount_
Definition: Handler.h:44
OutboundHandlerContext< Wout > Context
Definition: Handler.h:145
folly::Unit win
Definition: Handler.h:116
folly::Future< folly::Unit > write(Context *ctx, W msg) override
Definition: Handler.h:167
virtual void readEOF(Context *ctx)
Definition: Handler.h:122
Context * getContext()
Definition: Handler.h:34
Context * ctx_
Definition: Handler.h:45
virtual folly::Future< folly::Unit > fireClose()=0
virtual void transportActive(Context *ctx)
Definition: Handler.h:128
virtual void attachPipeline(Context *)
Definition: Handler.h:31
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
virtual void fireTransportInactive()=0
virtual void detachPipeline(Context *)
Definition: Handler.h:32
void read(Context *ctx, R msg) override
Definition: Handler.h:163
virtual void fireReadEOF()=0
virtual ~HandlerBase()=default
InboundHandler< folly::IOBufQueue &, std::unique_ptr< folly::IOBuf > > InboundBytesToBytesHandler
Definition: Handler.h:176
virtual folly::Future< folly::Unit > close(Context *ctx)
Definition: Handler.h:79
virtual void fireRead(In msg)=0
virtual folly::Future< folly::Unit > writeException(Context *ctx, folly::exception_wrapper e)
Definition: Handler.h:149
virtual folly::Future< folly::Unit > fireClose()=0
virtual void fireReadException(folly::exception_wrapper e)=0
Handler< R, R, W, W >::Context Context
Definition: Handler.h:161
virtual void fireReadException(folly::exception_wrapper e)=0
virtual void readException(Context *ctx, folly::exception_wrapper e)
Definition: Handler.h:64
virtual folly::Future< folly::Unit > fireWriteException(folly::exception_wrapper e)=0
OutboundHandler< std::unique_ptr< folly::IOBuf > > OutboundBytesToBytesHandler
Definition: Handler.h:179
virtual void readException(Context *ctx, folly::exception_wrapper e)
Definition: Handler.h:125
virtual void transportActive(Context *ctx)
Definition: Handler.h:67
HandlerContext< Rout, Wout > Context
Definition: Handler.h:57
friend PipelineContext
Definition: Handler.h:43
virtual void readEOF(Context *ctx)
Definition: Handler.h:61