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 Context>
29 virtual ~HandlerBase() = default;
31 virtual void attachPipeline(Context* ctx) {}
32 virtual void detachPipeline(Context* ctx) {}
34 Context* getContext() {
35 if (attachCount_ != 1) {
43 friend PipelineContext;
44 uint64_t attachCount_{0};
45 Context* ctx_{nullptr};
48 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
49 class Handler : public HandlerBase<HandlerContext<Rout, Wout>> {
51 static const HandlerDir dir = HandlerDir::BOTH;
57 typedef HandlerContext<Rout, Wout> Context;
58 virtual ~Handler() = default;
60 virtual void read(Context* ctx, Rin msg) = 0;
61 virtual void readEOF(Context* ctx) {
64 virtual void readException(Context* ctx, exception_wrapper e) {
65 ctx->fireReadException(std::move(e));
67 virtual void transportActive(Context* ctx) {
68 ctx->fireTransportActive();
70 virtual void transportInactive(Context* ctx) {
71 ctx->fireTransportInactive();
74 virtual Future<void> write(Context* ctx, Win msg) = 0;
75 virtual Future<void> close(Context* ctx) {
76 return ctx->fireClose();
80 // Other sorts of things we might want, all shamelessly stolen from Netty
82 virtual void exceptionCaught(
84 exception_wrapper e) {}
85 virtual void channelRegistered(HandlerContext* ctx) {}
86 virtual void channelUnregistered(HandlerContext* ctx) {}
87 virtual void channelReadComplete(HandlerContext* ctx) {}
88 virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
89 virtual void channelWritabilityChanged(HandlerContext* ctx) {}
92 virtual Future<void> bind(
94 SocketAddress localAddress) {}
95 virtual Future<void> connect(
97 SocketAddress remoteAddress, SocketAddress localAddress) {}
98 virtual Future<void> disconnect(HandlerContext* ctx) {}
99 virtual Future<void> deregister(HandlerContext* ctx) {}
100 virtual Future<void> read(HandlerContext* ctx) {}
101 virtual void flush(HandlerContext* ctx) {}
105 template <class Rin, class Rout = Rin>
106 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
108 static const HandlerDir dir = HandlerDir::IN;
114 typedef InboundHandlerContext<Rout> Context;
115 virtual ~InboundHandler() = default;
117 virtual void read(Context* ctx, Rin msg) = 0;
118 virtual void readEOF(Context* ctx) {
121 virtual void readException(Context* ctx, exception_wrapper e) {
122 ctx->fireReadException(std::move(e));
124 virtual void transportActive(Context* ctx) {
125 ctx->fireTransportActive();
127 virtual void transportInactive(Context* ctx) {
128 ctx->fireTransportInactive();
132 template <class Win, class Wout = Win>
133 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
135 static const HandlerDir dir = HandlerDir::OUT;
141 typedef OutboundHandlerContext<Wout> Context;
142 virtual ~OutboundHandler() = default;
144 virtual Future<void> write(Context* ctx, Win msg) = 0;
145 virtual Future<void> close(Context* ctx) {
146 return ctx->fireClose();
150 template <class R, class W = R>
151 class HandlerAdapter : public Handler<R, R, W, W> {
153 typedef typename Handler<R, R, W, W>::Context Context;
155 void read(Context* ctx, R msg) override {
156 ctx->fireRead(std::forward<R>(msg));
159 Future<void> write(Context* ctx, W msg) override {
160 return ctx->fireWrite(std::forward<W>(msg));
164 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
167 typedef InboundHandler<IOBufQueue&, std::unique_ptr<IOBuf>>
168 InboundBytesToBytesHandler;
170 typedef OutboundHandler<std::unique_ptr<IOBuf>>
171 OutboundBytesToBytesHandler;