proxygen
HandlerContext.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #pragma once
18 
20 #include <folly/futures/Future.h>
21 #include <folly/ExceptionWrapper.h>
22 
23 namespace wangle {
24 
25 class PipelineBase;
26 
27 template <class In, class Out>
29  public:
30  virtual ~HandlerContext() = default;
31 
32  virtual void fireRead(In msg) = 0;
33  virtual void fireReadEOF() = 0;
34  virtual void fireReadException(folly::exception_wrapper e) = 0;
35  virtual void fireTransportActive() = 0;
36  virtual void fireTransportInactive() = 0;
37 
38  virtual folly::Future<folly::Unit> fireWrite(Out msg) = 0;
42 
43  virtual PipelineBase* getPipeline() = 0;
44  virtual std::shared_ptr<PipelineBase> getPipelineShared() = 0;
45  std::shared_ptr<folly::AsyncTransport> getTransport() {
46  return getPipeline()->getTransport();
47  }
48 
49  virtual void setWriteFlags(folly::WriteFlags flags) = 0;
50  virtual folly::WriteFlags getWriteFlags() = 0;
51 
52  virtual void setReadBufferSettings(
53  uint64_t minAvailable,
54  uint64_t allocationSize) = 0;
55  virtual std::pair<uint64_t, uint64_t> getReadBufferSettings() = 0;
56 
57  /* TODO
58  template <class H>
59  virtual void addHandlerBefore(H&&) {}
60  template <class H>
61  virtual void addHandlerAfter(H&&) {}
62  template <class H>
63  virtual void replaceHandler(H&&) {}
64  virtual void removeHandler() {}
65  */
66 };
67 
68 template <class In>
70  public:
71  virtual ~InboundHandlerContext() = default;
72 
73  virtual void fireRead(In msg) = 0;
74  virtual void fireReadEOF() = 0;
75  virtual void fireReadException(folly::exception_wrapper e) = 0;
76  virtual void fireTransportActive() = 0;
77  virtual void fireTransportInactive() = 0;
78 
79  virtual PipelineBase* getPipeline() = 0;
80  virtual std::shared_ptr<PipelineBase> getPipelineShared() = 0;
81  std::shared_ptr<folly::AsyncTransport> getTransport() {
82  return getPipeline()->getTransport();
83  }
84 
85  // TODO Need get/set writeFlags, readBufferSettings? Probably not.
86  // Do we even really need them stored in the pipeline at all?
87  // Could just always delegate to the socket impl
88 };
89 
90 template <class Out>
92  public:
93  virtual ~OutboundHandlerContext() = default;
94 
95  virtual folly::Future<folly::Unit> fireWrite(Out msg) = 0;
99 
100  virtual PipelineBase* getPipeline() = 0;
101  virtual std::shared_ptr<PipelineBase> getPipelineShared() = 0;
102  std::shared_ptr<folly::AsyncTransport> getTransport() {
103  return getPipeline()->getTransport();
104  }
105 };
106 
107 // #include <windows.h> has blessed us with #define IN & OUT, typically mapped
108 // to nothing, so letting the preprocessor delete each of these symbols, leading
109 // to interesting compiler errors around HandlerDir.
110 #ifdef IN
111 # undef IN
112 #endif
113 #ifdef OUT
114 # undef OUT
115 #endif
116 
117 enum class HandlerDir {
118  IN,
119  OUT,
120  BOTH
121 };
122 
123 } // namespace wangle
124 
std::shared_ptr< folly::AsyncTransport > getTransport()
virtual folly::WriteFlags getWriteFlags()=0
virtual void fireTransportInactive()=0
flags
Definition: http_parser.h:127
virtual void fireTransportActive()=0
virtual folly::Future< folly::Unit > fireWriteException(folly::exception_wrapper e)=0
std::shared_ptr< folly::AsyncTransport > getTransport()
virtual void fireReadEOF()=0
virtual folly::Future< folly::Unit > fireWrite(Out msg)=0
virtual void setReadBufferSettings(uint64_t minAvailable, uint64_t allocationSize)=0
virtual folly::Future< folly::Unit > fireClose()=0
std::shared_ptr< folly::AsyncTransport > getTransport()
Definition: Pipeline.h:65
virtual void fireRead(In msg)=0
virtual ~HandlerContext()=default
virtual void setWriteFlags(folly::WriteFlags flags)=0
virtual void fireReadException(folly::exception_wrapper e)=0
virtual std::pair< uint64_t, uint64_t > getReadBufferSettings()=0
std::shared_ptr< folly::AsyncTransport > getTransport()
virtual std::shared_ptr< PipelineBase > getPipelineShared()=0
virtual PipelineBase * getPipeline()=0