Copyright 2014->2015
[folly.git] / folly / wangle / channel / ChannelHandler.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/ChannelPipeline.h>
21 #include <folly/io/IOBuf.h>
22 #include <folly/io/IOBufQueue.h>
23
24 namespace folly { namespace wangle {
25
26 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
27 class ChannelHandler {
28  public:
29   typedef Rin rin;
30   typedef Rout rout;
31   typedef Win win;
32   typedef Wout wout;
33   typedef ChannelHandlerContext<Rout, Wout> Context;
34   virtual ~ChannelHandler() {}
35
36   virtual void read(Context* ctx, Rin msg) = 0;
37   virtual void readEOF(Context* ctx) {
38     ctx->fireReadEOF();
39   }
40   virtual void readException(Context* ctx, exception_wrapper e) {
41     ctx->fireReadException(std::move(e));
42   }
43
44   virtual Future<void> write(Context* ctx, Win msg) = 0;
45   virtual Future<void> close(Context* ctx) {
46     return ctx->fireClose();
47   }
48
49   virtual void attachPipeline(Context* ctx) {}
50   virtual void attachTransport(Context* ctx) {}
51
52   virtual void detachPipeline(Context* ctx) {}
53   virtual void detachTransport(Context* ctx) {}
54
55   /*
56   // Other sorts of things we might want, all shamelessly stolen from Netty
57   // inbound
58   virtual void exceptionCaught(
59       ChannelHandlerContext* ctx,
60       exception_wrapper e) {}
61   virtual void channelRegistered(ChannelHandlerContext* ctx) {}
62   virtual void channelUnregistered(ChannelHandlerContext* ctx) {}
63   virtual void channelActive(ChannelHandlerContext* ctx) {}
64   virtual void channelInactive(ChannelHandlerContext* ctx) {}
65   virtual void channelReadComplete(ChannelHandlerContext* ctx) {}
66   virtual void userEventTriggered(ChannelHandlerContext* ctx, void* evt) {}
67   virtual void channelWritabilityChanged(ChannelHandlerContext* ctx) {}
68
69   // outbound
70   virtual Future<void> bind(
71       ChannelHandlerContext* ctx,
72       SocketAddress localAddress) {}
73   virtual Future<void> connect(
74           ChannelHandlerContext* ctx,
75           SocketAddress remoteAddress, SocketAddress localAddress) {}
76   virtual Future<void> disconnect(ChannelHandlerContext* ctx) {}
77   virtual Future<void> deregister(ChannelHandlerContext* ctx) {}
78   virtual Future<void> read(ChannelHandlerContext* ctx) {}
79   virtual void flush(ChannelHandlerContext* ctx) {}
80   */
81 };
82
83 template <class R, class W = R>
84 class ChannelHandlerAdapter : public ChannelHandler<R, R, W, W> {
85  public:
86   typedef typename ChannelHandler<R, R, W, W>::Context Context;
87
88   void read(Context* ctx, R msg) override {
89     ctx->fireRead(std::forward<R>(msg));
90   }
91
92   Future<void> write(Context* ctx, W msg) override {
93     return ctx->fireWrite(std::forward<W>(msg));
94   }
95 };
96
97 typedef ChannelHandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
98 BytesToBytesHandler;
99
100 template <class Handler, bool Shared = true>
101 class ChannelHandlerPtr : public ChannelHandler<
102                                    typename Handler::rin,
103                                    typename Handler::rout,
104                                    typename Handler::win,
105                                    typename Handler::wout> {
106  public:
107   typedef typename std::conditional<
108     Shared,
109     std::shared_ptr<Handler>,
110     Handler*>::type
111   HandlerPtr;
112
113   typedef typename Handler::Context Context;
114
115   explicit ChannelHandlerPtr(HandlerPtr handler)
116     : handler_(std::move(handler)) {}
117
118   void setHandler(HandlerPtr handler) {
119     if (handler == handler_) {
120       return;
121     }
122     if (handler_ && ctx_) {
123       handler_->detachPipeline(ctx_);
124     }
125     handler_ = std::move(handler);
126     if (handler_ && ctx_) {
127       handler_->attachPipeline(ctx_);
128       if (ctx_->getTransport()) {
129         handler_->attachTransport(ctx_);
130       }
131     }
132   }
133
134   void attachPipeline(Context* ctx) override {
135     ctx_ = ctx;
136     if (handler_) {
137       handler_->attachPipeline(ctx_);
138     }
139   }
140
141   void attachTransport(Context* ctx) override {
142     ctx_ = ctx;
143     if (handler_) {
144       handler_->attachTransport(ctx_);
145     }
146   }
147
148   void detachPipeline(Context* ctx) override {
149     ctx_ = ctx;
150     if (handler_) {
151       handler_->detachPipeline(ctx_);
152     }
153   }
154
155   void detachTransport(Context* ctx) override {
156     ctx_ = ctx;
157     if (handler_) {
158       handler_->detachTransport(ctx_);
159     }
160   }
161
162   void read(Context* ctx, typename Handler::rin msg) override {
163     DCHECK(handler_);
164     handler_->read(ctx, std::forward<typename Handler::rin>(msg));
165   }
166
167   void readEOF(Context* ctx) override {
168     DCHECK(handler_);
169     handler_->readEOF(ctx);
170   }
171
172   void readException(Context* ctx, exception_wrapper e) override {
173     DCHECK(handler_);
174     handler_->readException(ctx, std::move(e));
175   }
176
177   Future<void> write(Context* ctx, typename Handler::win msg) override {
178     DCHECK(handler_);
179     return handler_->write(ctx, std::forward<typename Handler::win>(msg));
180   }
181
182   Future<void> close(Context* ctx) override {
183     DCHECK(handler_);
184     return handler_->close(ctx);
185   }
186
187  private:
188   Context* ctx_;
189   HandlerPtr handler_;
190 };
191
192 }}