moar CRTP to minimize copypasta for inbound/outbound handlers
[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   typedef Rin rin;
55   typedef Rout rout;
56   typedef Win win;
57   typedef Wout wout;
58   typedef HandlerContext<Rout, Wout> Context;
59   virtual ~Handler() {}
60
61   virtual void read(Context* ctx, Rin msg) = 0;
62   virtual void readEOF(Context* ctx) {
63     ctx->fireReadEOF();
64   }
65   virtual void readException(Context* ctx, exception_wrapper e) {
66     ctx->fireReadException(std::move(e));
67   }
68
69   virtual Future<void> write(Context* ctx, Win msg) = 0;
70   virtual Future<void> close(Context* ctx) {
71     return ctx->fireClose();
72   }
73
74   /*
75   // Other sorts of things we might want, all shamelessly stolen from Netty
76   // inbound
77   virtual void exceptionCaught(
78       HandlerContext* ctx,
79       exception_wrapper e) {}
80   virtual void channelRegistered(HandlerContext* ctx) {}
81   virtual void channelUnregistered(HandlerContext* ctx) {}
82   virtual void channelActive(HandlerContext* ctx) {}
83   virtual void channelInactive(HandlerContext* ctx) {}
84   virtual void channelReadComplete(HandlerContext* ctx) {}
85   virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
86   virtual void channelWritabilityChanged(HandlerContext* ctx) {}
87
88   // outbound
89   virtual Future<void> bind(
90       HandlerContext* ctx,
91       SocketAddress localAddress) {}
92   virtual Future<void> connect(
93           HandlerContext* ctx,
94           SocketAddress remoteAddress, SocketAddress localAddress) {}
95   virtual Future<void> disconnect(HandlerContext* ctx) {}
96   virtual Future<void> deregister(HandlerContext* ctx) {}
97   virtual Future<void> read(HandlerContext* ctx) {}
98   virtual void flush(HandlerContext* ctx) {}
99   */
100 };
101
102 template <class R, class W = R>
103 class HandlerAdapter : public Handler<R, R, W, W> {
104  public:
105   typedef typename Handler<R, R, W, W>::Context Context;
106
107   void read(Context* ctx, R msg) override {
108     ctx->fireRead(std::forward<R>(msg));
109   }
110
111   Future<void> write(Context* ctx, W msg) override {
112     return ctx->fireWrite(std::forward<W>(msg));
113   }
114 };
115
116 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
117 BytesToBytesHandler;
118
119 }}