proxygen
ServerDispatcher.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 
19 #include <wangle/channel/Handler.h>
20 #include <wangle/service/Service.h>
21 
22 namespace wangle {
23 
28 template <typename Req, typename Resp = Req>
29 class SerialServerDispatcher : public HandlerAdapter<Req, Resp> {
30  public:
31 
33 
35  : service_(service) {}
36 
37  void read(Context* ctx, Req in) override {
38  auto resp = (*service_)(std::move(in)).get();
39  ctx->fireWrite(std::move(resp));
40  }
41 
42  private:
43 
45 };
46 
51 template <typename Req, typename Resp = Req>
52 class PipelinedServerDispatcher : public HandlerAdapter<Req, Resp> {
53  public:
54 
56 
58  : service_(service) {}
59 
60  void read(Context*, Req in) override {
61  auto requestId = requestId_++;
62  (*service_)(std::move(in)).then([requestId,this](Resp& resp){
63  responses_[requestId] = resp;
64  sendResponses();
65  });
66  }
67 
68  void sendResponses() {
69  auto search = responses_.find(lastWrittenId_+1);
70  while (search != responses_.end()) {
71  Resp resp = std::move(search->second);
72  responses_.erase(search->first);
73  this->getContext()->fireWrite(std::move(resp));
74  lastWrittenId_++;
75  search = responses_.find(lastWrittenId_+1);
76  }
77  }
78 
79  private:
81  uint32_t requestId_{1};
82  std::unordered_map<uint32_t, Resp> responses_;
83  uint32_t lastWrittenId_{0};
84 };
85 
93 template <typename Req, typename Resp = Req>
94 class MultiplexServerDispatcher : public HandlerAdapter<Req, Resp> {
95  public:
96 
98 
100  : service_(service) {}
101 
102  void read(Context* ctx, Req in) override {
103  (*service_)(std::move(in)).thenValue([ctx](Resp resp) {
104  ctx->fireWrite(std::move(resp));
105  });
106  }
107 
108  private:
110 };
111 
112 } // namespace wangle
MultiplexServerDispatcher(Service< Req, Resp > *service)
std::unordered_map< uint32_t, Resp > responses_
virtual folly::Future< folly::Unit > fireWrite(Out msg)=0
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
PipelinedServerDispatcher(Service< Req, Resp > *service)
void read(Context *ctx, Req in) override
void read(Context *ctx, Req in) override
void read(Context *, Req in) override
HandlerContext< Req, Resp > * getContext()
Definition: Handler.h:34
HandlerAdapter< Req, Resp >::Context Context
HandlerAdapter< Req, Resp >::Context Context
SerialServerDispatcher(Service< Req, Resp > *service)
Service< Req, Resp > * service_
HandlerAdapter< Req, Resp >::Context Context
Service< Req, Resp > * service_