Mir
mir_protobuf_rpc_channel.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alan Griffiths <alan@octopull.co.uk>
17  */
18 
19 #ifndef MIR_CLIENT_RPC_MIR_PROTOBUF_RPC_CHANNEL_H_
20 #define MIR_CLIENT_RPC_MIR_PROTOBUF_RPC_CHANNEL_H_
21 
22 #include "mir_basic_rpc_channel.h"
23 #include "stream_transport.h"
27 
28 #include "../lifecycle_control.h"
29 #include "../ping_handler.h"
30 
31 #include <thread>
32 #include <atomic>
33 #include <experimental/optional>
34 
35 namespace mir
36 {
37 
38 namespace client
39 {
40 class DisplayConfiguration;
41 class SurfaceMap;
42 class EventSink;
43 namespace rpc
44 {
45 
46 class RpcReport;
47 
49  public MirBasicRpcChannel,
52 {
53 public:
54  MirProtobufRpcChannel(std::unique_ptr<StreamTransport> transport,
55  std::shared_ptr<SurfaceMap> const& surface_map,
56  std::shared_ptr<DisplayConfiguration> const& disp_config,
57  std::shared_ptr<RpcReport> const& rpc_report,
58  std::shared_ptr<LifecycleControl> const& lifecycle_control,
59  std::shared_ptr<PingHandler> const& ping_handler,
60  std::shared_ptr<EventSink> const& event_sink);
61 
62  ~MirProtobufRpcChannel() = default;
63 
64  // StreamTransport::Observer
65  void on_data_available() override;
66  void on_disconnected() override;
67 
68  // Dispatchable
69  Fd watch_fd() const override;
70  bool dispatch(mir::dispatch::FdEvents events) override;
72 
86 
87  void call_method(
88  std::string const& method_name,
89  google::protobuf::MessageLite const* parameters,
90  google::protobuf::MessageLite* response,
91  google::protobuf::Closure* complete) override;
92 
93 private:
94  std::shared_ptr<RpcReport> const rpc_report;
95  detail::PendingCallCache pending_calls;
96 
97  static constexpr size_t size_of_header = 2;
98  detail::SendBuffer header_bytes;
99  detail::SendBuffer body_bytes;
100 
101  void receive_file_descriptors(google::protobuf::MessageLite* response);
102  template<class MessageType>
103  void receive_any_file_descriptors_for(MessageType* response);
104  void send_message(mir::protobuf::wire::Invocation const& body,
105  mir::protobuf::wire::Invocation const& invocation,
106  std::vector<mir::Fd>& fds);
107 
108  void read_message();
109  void process_event_sequence(std::string const& event);
110 
111  void notify_disconnected();
112 
113  std::shared_ptr<SurfaceMap> surface_map;
114  std::shared_ptr<DisplayConfiguration> display_configuration;
115  std::shared_ptr<LifecycleControl> lifecycle_control;
116  std::shared_ptr<PingHandler> const ping_handler;
117  std::shared_ptr<EventSink> event_sink;
118  std::atomic<bool> disconnected;
119  std::mutex read_mutex;
120  std::mutex write_mutex;
121 
122  bool prioritise_next_request{false};
123  std::experimental::optional<uint32_t> id_to_wait_for;
124 
125  /* We use the guarantee that the transport's destructor blocks until
126  * pending processing has finished to ensure that on_data_available()
127  * isn't called after the members it relies on are destroyed.
128  *
129  * This means that anything that owns a reference to the transport
130  * needs to be after anything that can be accessed from on_data_available().
131  *
132  * For simplicity's sake keep all of the dispatch infrastructure at the
133  * end to guarantee this.
134  */
135  std::shared_ptr<StreamTransport> const transport;
136  std::shared_ptr<mir::dispatch::ActionQueue> const delayed_processor;
138 };
139 
140 }
141 }
142 }
143 
144 #endif /* MIR_CLIENT_RPC_MIR_PROTOBUF_RPC_CHANNEL_H_ */
All things Mir.
Definition: atomic_callback.h:25
MirProtobufRpcChannel(std::unique_ptr< StreamTransport > transport, std::shared_ptr< SurfaceMap > const &surface_map, std::shared_ptr< DisplayConfiguration > const &disp_config, std::shared_ptr< RpcReport > const &rpc_report, std::shared_ptr< LifecycleControl > const &lifecycle_control, std::shared_ptr< PingHandler > const &ping_handler, std::shared_ptr< EventSink > const &event_sink)
Definition: mir_protobuf_rpc_channel.cpp:50
void on_disconnected() override
Called by the Transport when the connection to the server has been broken.
Definition: mir_protobuf_rpc_channel.cpp:422
Definition: fd.h:33
void process_next_request_first()
Switch the RpcChannel into out-of-order mode.
Definition: mir_protobuf_rpc_channel.cpp:442
Definition: mir_basic_rpc_channel.h:59
void call_method(std::string const &method_name, google::protobuf::MessageLite const *parameters, google::protobuf::MessageLite *response, google::protobuf::Closure *complete) override
Definition: mir_protobuf_rpc_channel.cpp:176
mir::dispatch::FdEvents relevant_events() const override
The set of file-descriptor events this Dispatchable handles.
Definition: mir_protobuf_rpc_channel.cpp:437
Definition: mir_basic_rpc_channel.h:100
bool dispatch(mir::dispatch::FdEvents events) override
Dispatch one pending event.
Definition: mir_protobuf_rpc_channel.cpp:432
An adaptor that combines multiple Dispatchables into a single Dispatchable.
Definition: multiplexing_dispatchable.h:52
std::vector< uint8_t > SendBuffer
Definition: mir_basic_rpc_channel.h:57
Fd watch_fd() const override
Get a poll()able file descriptor.
Definition: mir_protobuf_rpc_channel.cpp:427
Definition: dispatchable.h:38
Definition: mir_protobuf_rpc_channel.h:48
uint32_t FdEvents
Definition: dispatchable.h:36
void on_data_available() override
Called by the Transport when data is available for reading.
Definition: mir_protobuf_rpc_channel.cpp:342
Observer of IO status.
Definition: stream_transport.h:87

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Oct 8 16:20:16 UTC 2015