2080c2c224ddd00a4da33adbc753dfe7ace36fd0
[folly.git] / folly / wangle / channel / Handler.h
1 /*
2  * Copyright 2015 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>
20 #include <folly/wangle/channel/Pipeline.h>
21 #include <folly/io/IOBuf.h>
22 #include <folly/io/IOBufQueue.h>
23
24 namespace folly { namespace wangle {
25
26 template <class Context>
27 class HandlerBase {
28  public:
29   virtual ~HandlerBase() {}
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:
43   friend PipelineContext;
44   uint64_t attachCount_{0};
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;
57   typedef HandlerContext<Rout, Wout> Context;
58   virtual ~Handler() {}
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, exception_wrapper e) {
65     ctx->fireReadException(std::move(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 Future<void> write(Context* ctx, Win msg) = 0;
75   virtual Future<void> close(Context* ctx) {
76     return ctx->fireClose();
77   }
78
79   /*
80   // Other sorts of things we might want, all shamelessly stolen from Netty
81   // inbound
82   virtual void exceptionCaught(
83       HandlerContext* ctx,
84       exception_wrapper e) {}
85   virtual void channelRegistered(HandlerContext* ctx) {}
86   virtual void channelUnregistered(HandlerContext* ctx) {}
87   virtual void channelReadComplete(HandlerContext* ctx) {}
88   virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
89   virtual void channelWritabilityChanged(HandlerContext* ctx) {}
90
91   // outbound
92   virtual Future<void> bind(
93       HandlerContext* ctx,
94       SocketAddress localAddress) {}
95   virtual Future<void> connect(
96           HandlerContext* ctx,
97           SocketAddress remoteAddress, SocketAddress localAddress) {}
98   virtual Future<void> disconnect(HandlerContext* ctx) {}
99   virtual Future<void> deregister(HandlerContext* ctx) {}
100   virtual Future<void> read(HandlerContext* ctx) {}
101   virtual void flush(HandlerContext* ctx) {}
102   */
103 };
104
105 template <class Rin, class Rout = Rin>
106 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
107  public:
108   static const HandlerDir dir = HandlerDir::IN;
109
110   typedef Rin rin;
111   typedef Rout rout;
112   typedef Nothing win;
113   typedef Nothing wout;
114   typedef InboundHandlerContext<Rout> Context;
115   virtual ~InboundHandler() {}
116
117   virtual void read(Context* ctx, Rin msg) = 0;
118   virtual void readEOF(Context* ctx) {
119     ctx->fireReadEOF();
120   }
121   virtual void readException(Context* ctx, exception_wrapper e) {
122     ctx->fireReadException(std::move(e));
123   }
124   virtual void transportActive(Context* ctx) {
125     ctx->fireTransportActive();
126   }
127   virtual void transportInactive(Context* ctx) {
128     ctx->fireTransportInactive();
129   }
130 };
131
132 template <class Win, class Wout = Win>
133 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
134  public:
135   static const HandlerDir dir = HandlerDir::OUT;
136
137   typedef Nothing rin;
138   typedef Nothing rout;
139   typedef Win win;
140   typedef Wout wout;
141   typedef OutboundHandlerContext<Wout> Context;
142   virtual ~OutboundHandler() {}
143
144   virtual Future<void> write(Context* ctx, Win msg) = 0;
145   virtual Future<void> close(Context* ctx) {
146     return ctx->fireClose();
147   }
148 };
149
150 template <class R, class W = R>
151 class HandlerAdapter : public Handler<R, R, W, W> {
152  public:
153   typedef typename Handler<R, R, W, W>::Context Context;
154
155   void read(Context* ctx, R msg) override {
156     ctx->fireRead(std::forward<R>(msg));
157   }
158
159   Future<void> write(Context* ctx, W msg) override {
160     return ctx->fireWrite(std::forward<W>(msg));
161   }
162 };
163
164 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
165 BytesToBytesHandler;
166
167 typedef InboundHandler<IOBufQueue&, std::unique_ptr<IOBuf>>
168 InboundBytesToBytesHandler;
169
170 typedef OutboundHandler<std::unique_ptr<IOBuf>>
171 OutboundBytesToBytesHandler;
172
173 }}