unidirectional pipelines
[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 template <class Rin, class Rout = Rin>
105 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
106  public:
107   static const HandlerDir dir = HandlerDir::IN;
108
109   typedef Rin rin;
110   typedef Rout rout;
111   typedef Nothing win;
112   typedef Nothing wout;
113   typedef InboundHandlerContext<Rout> Context;
114   virtual ~InboundHandler() {}
115
116   virtual void read(Context* ctx, Rin msg) = 0;
117   virtual void readEOF(Context* ctx) {
118     ctx->fireReadEOF();
119   }
120   virtual void readException(Context* ctx, exception_wrapper e) {
121     ctx->fireReadException(std::move(e));
122   }
123 };
124
125 template <class Win, class Wout = Win>
126 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
127  public:
128   static const HandlerDir dir = HandlerDir::OUT;
129
130   typedef Nothing rin;
131   typedef Nothing rout;
132   typedef Win win;
133   typedef Wout wout;
134   typedef OutboundHandlerContext<Wout> Context;
135   virtual ~OutboundHandler() {}
136
137   virtual Future<void> write(Context* ctx, Win msg) = 0;
138   virtual Future<void> close(Context* ctx) {
139     return ctx->fireClose();
140   }
141 };
142
143 template <class R, class W = R>
144 class HandlerAdapter : public Handler<R, R, W, W> {
145  public:
146   typedef typename Handler<R, R, W, W>::Context Context;
147
148   void read(Context* ctx, R msg) override {
149     ctx->fireRead(std::forward<R>(msg));
150   }
151
152   Future<void> write(Context* ctx, W msg) override {
153     return ctx->fireWrite(std::forward<W>(msg));
154   }
155 };
156
157 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
158 BytesToBytesHandler;
159
160 typedef InboundHandler<IOBufQueue&, std::unique_ptr<IOBuf>>
161 InboundBytesToBytesHandler;
162
163 typedef OutboundHandler<std::unique_ptr<IOBuf>>
164 OutboundBytesToBytesHandler;
165
166 }}