use inbound/outbound handlers in a few more places
[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 attachTransport(Context* ctx) {}
33
34   virtual void detachPipeline(Context* ctx) {}
35   virtual void detachTransport(Context* ctx) {}
36
37   Context* getContext() {
38     if (attachCount_ != 1) {
39       return nullptr;
40     }
41     CHECK(ctx_);
42     return ctx_;
43   }
44
45  private:
46   friend PipelineContext;
47   uint64_t attachCount_{0};
48   Context* ctx_{nullptr};
49 };
50
51 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
52 class Handler : public HandlerBase<HandlerContext<Rout, Wout>> {
53  public:
54   static const HandlerDir dir = HandlerDir::BOTH;
55
56   typedef Rin rin;
57   typedef Rout rout;
58   typedef Win win;
59   typedef Wout wout;
60   typedef HandlerContext<Rout, Wout> Context;
61   virtual ~Handler() {}
62
63   virtual void read(Context* ctx, Rin msg) = 0;
64   virtual void readEOF(Context* ctx) {
65     ctx->fireReadEOF();
66   }
67   virtual void readException(Context* ctx, exception_wrapper e) {
68     ctx->fireReadException(std::move(e));
69   }
70
71   virtual Future<void> write(Context* ctx, Win msg) = 0;
72   virtual Future<void> close(Context* ctx) {
73     return ctx->fireClose();
74   }
75
76   /*
77   // Other sorts of things we might want, all shamelessly stolen from Netty
78   // inbound
79   virtual void exceptionCaught(
80       HandlerContext* ctx,
81       exception_wrapper e) {}
82   virtual void channelRegistered(HandlerContext* ctx) {}
83   virtual void channelUnregistered(HandlerContext* ctx) {}
84   virtual void channelActive(HandlerContext* ctx) {}
85   virtual void channelInactive(HandlerContext* ctx) {}
86   virtual void channelReadComplete(HandlerContext* ctx) {}
87   virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
88   virtual void channelWritabilityChanged(HandlerContext* ctx) {}
89
90   // outbound
91   virtual Future<void> bind(
92       HandlerContext* ctx,
93       SocketAddress localAddress) {}
94   virtual Future<void> connect(
95           HandlerContext* ctx,
96           SocketAddress remoteAddress, SocketAddress localAddress) {}
97   virtual Future<void> disconnect(HandlerContext* ctx) {}
98   virtual Future<void> deregister(HandlerContext* ctx) {}
99   virtual Future<void> read(HandlerContext* ctx) {}
100   virtual void flush(HandlerContext* ctx) {}
101   */
102 };
103
104 struct Unit{};
105
106 template <class Rin, class Rout = Rin>
107 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
108  public:
109   static const HandlerDir dir = HandlerDir::IN;
110
111   typedef Rin rin;
112   typedef Rout rout;
113   typedef Unit win;
114   typedef Unit wout;
115   typedef InboundHandlerContext<Rout> Context;
116   virtual ~InboundHandler() {}
117
118   virtual void read(Context* ctx, Rin msg) = 0;
119   virtual void readEOF(Context* ctx) {
120     ctx->fireReadEOF();
121   }
122   virtual void readException(Context* ctx, exception_wrapper e) {
123     ctx->fireReadException(std::move(e));
124   }
125 };
126
127 template <class Win, class Wout = Win>
128 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
129  public:
130   static const HandlerDir dir = HandlerDir::OUT;
131
132   typedef Unit rin;
133   typedef Unit rout;
134   typedef Win win;
135   typedef Wout wout;
136   typedef OutboundHandlerContext<Wout> Context;
137   virtual ~OutboundHandler() {}
138
139   virtual Future<void> write(Context* ctx, Win msg) = 0;
140   virtual Future<void> close(Context* ctx) {
141     return ctx->fireClose();
142   }
143 };
144
145 template <class R, class W = R>
146 class HandlerAdapter : public Handler<R, R, W, W> {
147  public:
148   typedef typename Handler<R, R, W, W>::Context Context;
149
150   void read(Context* ctx, R msg) override {
151     ctx->fireRead(std::forward<R>(msg));
152   }
153
154   Future<void> write(Context* ctx, W msg) override {
155     return ctx->fireWrite(std::forward<W>(msg));
156   }
157 };
158
159 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
160 BytesToBytesHandler;
161
162 typedef InboundHandler<IOBufQueue&, std::unique_ptr<IOBuf>>
163 InboundBytesToBytesHandler;
164
165 typedef OutboundHandler<std::unique_ptr<IOBuf>>
166 OutboundBytesToBytesHandler;
167
168 }}