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() {}
31 virtual void attachPipeline(Context* ctx) {}
32 virtual void attachTransport(Context* ctx) {}
34 virtual void detachPipeline(Context* ctx) {}
35 virtual void detachTransport(Context* ctx) {}
37 Context* getContext() {
38 if (attachCount_ != 1) {
46 friend PipelineContext;
47 uint64_t attachCount_{0};
48 Context* ctx_{nullptr};
51 template <class Rin, class Rout = Rin, class Win = Rout, class Wout = Rin>
52 class Handler : public HandlerBase<HandlerContext<Rout, Wout>> {
54 static const HandlerDir dir = HandlerDir::BOTH;
60 typedef HandlerContext<Rout, Wout> Context;
63 virtual void read(Context* ctx, Rin msg) = 0;
64 virtual void readEOF(Context* ctx) {
67 virtual void readException(Context* ctx, exception_wrapper e) {
68 ctx->fireReadException(std::move(e));
71 virtual Future<void> write(Context* ctx, Win msg) = 0;
72 virtual Future<void> close(Context* ctx) {
73 return ctx->fireClose();
77 // Other sorts of things we might want, all shamelessly stolen from Netty
79 virtual void exceptionCaught(
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) {}
91 virtual Future<void> bind(
93 SocketAddress localAddress) {}
94 virtual Future<void> connect(
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) {}
106 template <class Rin, class Rout = Rin>
107 class InboundHandler : public HandlerBase<InboundHandlerContext<Rout>> {
109 static const HandlerDir dir = HandlerDir::IN;
115 typedef InboundHandlerContext<Rout> Context;
116 virtual ~InboundHandler() {}
118 virtual void read(Context* ctx, Rin msg) = 0;
119 virtual void readEOF(Context* ctx) {
122 virtual void readException(Context* ctx, exception_wrapper e) {
123 ctx->fireReadException(std::move(e));
127 template <class Win, class Wout = Win>
128 class OutboundHandler : public HandlerBase<OutboundHandlerContext<Wout>> {
130 static const HandlerDir dir = HandlerDir::OUT;
136 typedef OutboundHandlerContext<Wout> Context;
137 virtual ~OutboundHandler() {}
139 virtual Future<void> write(Context* ctx, Win msg) = 0;
140 virtual Future<void> close(Context* ctx) {
141 return ctx->fireClose();
145 template <class R, class W = R>
146 class HandlerAdapter : public Handler<R, R, W, W> {
148 typedef typename Handler<R, R, W, W>::Context Context;
150 void read(Context* ctx, R msg) override {
151 ctx->fireRead(std::forward<R>(msg));
154 Future<void> write(Context* ctx, W msg) override {
155 return ctx->fireWrite(std::forward<W>(msg));
159 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>
162 typedef InboundHandler<IOBufQueue&, std::unique_ptr<IOBuf>>
163 InboundBytesToBytesHandler;
165 typedef OutboundHandler<std::unique_ptr<IOBuf>>
166 OutboundBytesToBytesHandler;