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 detail::HandlerContextBase<Context>;
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>> {
58 typedef HandlerContext<Rout, Wout> Context;
61 virtual void read(Context* ctx, Rin msg) = 0;
62 virtual void readEOF(Context* ctx) {
65 virtual void readException(Context* ctx, exception_wrapper e) {
66 ctx->fireReadException(std::move(e));
69 virtual Future<void> write(Context* ctx, Win msg) = 0;
70 virtual Future<void> close(Context* ctx) {
71 return ctx->fireClose();
75 // Other sorts of things we might want, all shamelessly stolen from Netty
77 virtual void exceptionCaught(
79 exception_wrapper e) {}
80 virtual void channelRegistered(HandlerContext* ctx) {}
81 virtual void channelUnregistered(HandlerContext* ctx) {}
82 virtual void channelActive(HandlerContext* ctx) {}
83 virtual void channelInactive(HandlerContext* ctx) {}
84 virtual void channelReadComplete(HandlerContext* ctx) {}
85 virtual void userEventTriggered(HandlerContext* ctx, void* evt) {}
86 virtual void channelWritabilityChanged(HandlerContext* ctx) {}
89 virtual Future<void> bind(
91 SocketAddress localAddress) {}
92 virtual Future<void> connect(
94 SocketAddress remoteAddress, SocketAddress localAddress) {}
95 virtual Future<void> disconnect(HandlerContext* ctx) {}
96 virtual Future<void> deregister(HandlerContext* ctx) {}
97 virtual Future<void> read(HandlerContext* ctx) {}
98 virtual void flush(HandlerContext* ctx) {}
102 template <class R, class W = R>
103 class HandlerAdapter : public Handler<R, R, W, W> {
105 typedef typename Handler<R, R, W, W>::Context Context;
107 void read(Context* ctx, R msg) override {
108 ctx->fireRead(std::forward<R>(msg));
111 Future<void> write(Context* ctx, W msg) override {
112 return ctx->fireWrite(std::forward<W>(msg));
116 typedef HandlerAdapter<IOBufQueue&, std::unique_ptr<IOBuf>>