2 * Copyright 2015 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/futures/Future.h>
20 #include <folly/wangle/channel/Pipeline.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>
33 typedef HandlerContext<Rout, Wout> Context;
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(
60 exception_wrapper e) {}
61 virtual void channelRegistered(HandlerContext* ctx) {}
62 virtual void channelUnregistered(HandlerContext* ctx) {}
63 virtual void channelActive(HandlerContext* ctx) {}
64 virtual void channelInactive(HandlerContext* ctx) {}
65 virtual void channelReadComplete(HandlerContext* ctx) {}
66 virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
67 virtual void channelWritabilityChanged(HandlerContext* ctx) {}
70 virtual Future<void> bind(
72 SocketAddress localAddress) {}
73 virtual Future<void> connect(
75 SocketAddress remoteAddress, SocketAddress localAddress) {}
76 virtual Future<void> disconnect(HandlerContext* ctx) {}
77 virtual Future<void> deregister(HandlerContext* ctx) {}
78 virtual Future<void> read(HandlerContext* ctx) {}
79 virtual void flush(HandlerContext* ctx) {}
83 template <class R, class W = R>
84 class HandlerAdapter : public Handler<R, R, W, W> {
86 typedef typename Handler<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 HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
100 template <class HandlerT, bool Shared = true>
101 class HandlerPtr : public Handler<
102 typename HandlerT::rin,
103 typename HandlerT::rout,
104 typename HandlerT::win,
105 typename HandlerT::wout> {
107 typedef typename std::conditional<
109 std::shared_ptr<HandlerT>,
113 typedef typename HandlerT::Context Context;
115 explicit HandlerPtr(Ptr handler)
116 : handler_(std::move(handler)) {}
122 void setHandler(Ptr handler) {
123 if (handler == handler_) {
126 if (handler_ && ctx_) {
127 handler_->detachPipeline(ctx_);
129 handler_ = std::move(handler);
130 if (handler_ && ctx_) {
131 handler_->attachPipeline(ctx_);
132 if (ctx_->getTransport()) {
133 handler_->attachTransport(ctx_);
138 void attachPipeline(Context* ctx) override {
141 handler_->attachPipeline(ctx_);
145 void attachTransport(Context* ctx) override {
148 handler_->attachTransport(ctx_);
152 void detachPipeline(Context* ctx) override {
155 handler_->detachPipeline(ctx_);
159 void detachTransport(Context* ctx) override {
162 handler_->detachTransport(ctx_);
166 void read(Context* ctx, typename HandlerT::rin msg) override {
168 handler_->read(ctx, std::forward<typename HandlerT::rin>(msg));
171 void readEOF(Context* ctx) override {
173 handler_->readEOF(ctx);
176 void readException(Context* ctx, exception_wrapper e) override {
178 handler_->readException(ctx, std::move(e));
181 Future<void> write(Context* ctx, typename HandlerT::win msg) override {
183 return handler_->write(ctx, std::forward<typename HandlerT::win>(msg));
186 Future<void> close(Context* ctx) override {
188 return handler_->close(ctx);