2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/wangle/futures/Future.h>
20 #include <folly/experimental/wangle/channel/ChannelPipeline.h>
21 #include <folly/io/IOBuf.h>
22 #include <folly/io/IOBufQueue.h>
24 namespace folly { namespace wangle {
26 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
27 class ChannelHandler {
33 typedef ChannelHandlerContext<Rout, Wout> Context;
34 virtual ~ChannelHandler() {}
36 virtual void read(Context* ctx, Rin msg) = 0;
37 virtual void readEOF(Context* ctx) {
40 virtual void readException(Context* ctx, exception_wrapper e) {
41 ctx->fireReadException(std::move(e));
44 virtual Future<void> write(Context* ctx, Win msg) = 0;
45 virtual Future<void> close(Context* ctx) {
46 return ctx->fireClose();
49 virtual void attachPipeline(Context* ctx) {}
50 virtual void attachTransport(Context* ctx) {}
52 virtual void detachPipeline(Context* ctx) {}
53 virtual void detachTransport(Context* ctx) {}
56 // Other sorts of things we might want, all shamelessly stolen from Netty
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) {}
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) {}
83 template <class R, class W = R>
84 class ChannelHandlerAdapter : public ChannelHandler<R, R, W, W> {
86 typedef typename ChannelHandler<R, R, W, W>::Context Context;
88 void read(Context* ctx, R msg) override {
89 ctx->fireRead(std::forward<R>(msg));
92 Future<void> write(Context* ctx, W msg) override {
93 return ctx->fireWrite(std::forward<W>(msg));
97 typedef ChannelHandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
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> {
107 typedef typename std::conditional<
109 std::shared_ptr<Handler>,
113 typedef typename Handler::Context Context;
115 explicit ChannelHandlerPtr(HandlerPtr handler)
116 : handler_(std::move(handler)) {}
118 void setHandler(HandlerPtr handler) {
119 if (handler == handler_) {
122 if (handler_ && ctx_) {
123 handler_->detachPipeline(ctx_);
125 handler_ = std::move(handler);
126 if (handler_ && ctx_) {
127 handler_->attachPipeline(ctx_);
128 if (ctx_->getTransport()) {
129 handler_->attachTransport(ctx_);
134 void attachPipeline(Context* ctx) override {
137 handler_->attachPipeline(ctx_);
141 void attachTransport(Context* ctx) override {
144 handler_->attachTransport(ctx_);
148 void detachPipeline(Context* ctx) override {
151 handler_->detachPipeline(ctx_);
155 void detachTransport(Context* ctx) override {
158 handler_->detachTransport(ctx_);
162 void read(Context* ctx, typename Handler::rin msg) override {
164 handler_->read(ctx, std::forward<typename Handler::rin>(msg));
167 void readEOF(Context* ctx) override {
169 handler_->readEOF(ctx);
172 void readException(Context* ctx, exception_wrapper e) override {
174 handler_->readException(ctx, std::move(e));
177 Future<void> write(Context* ctx, typename Handler::win msg) override {
179 return handler_->write(ctx, std::forward<typename Handler::win>(msg));
182 Future<void> close(Context* ctx) override {
184 return handler_->close(ctx);