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 input
39 {
40 class InputDevices;
41 }
42 namespace client
43 {
44 class DisplayConfiguration;
45 class SurfaceMap;
46 class EventSink;
47 class AsyncBufferFactory;
48 namespace rpc
49 {
50 
51 class RpcReport;
52 
54  public MirBasicRpcChannel,
57 {
58 public:
59  MirProtobufRpcChannel(std::unique_ptr<StreamTransport> transport,
60  std::shared_ptr<SurfaceMap> const& surface_map,
61  std::shared_ptr<AsyncBufferFactory> const& buffer_factory,
62  std::shared_ptr<DisplayConfiguration> const& disp_config,
63  std::shared_ptr<input::InputDevices> const& input_devices,
64  std::shared_ptr<RpcReport> const& rpc_report,
65  std::shared_ptr<LifecycleControl> const& lifecycle_control,
66  std::shared_ptr<PingHandler> const& ping_handler,
67  std::shared_ptr<EventSink> const& event_sink);
68 
69  ~MirProtobufRpcChannel() = default;
70 
71  // StreamTransport::Observer
72  void on_data_available() override;
73  void on_disconnected() override;
74 
75  // Dispatchable
76  Fd watch_fd() const override;
77  bool dispatch(mir::dispatch::FdEvents events) override;
78  mir::dispatch::FdEvents relevant_events() const override;
79 
92  void process_next_request_first();
93 
94  void call_method(
95  std::string const& method_name,
96  google::protobuf::MessageLite const* parameters,
97  google::protobuf::MessageLite* response,
98  google::protobuf::Closure* complete) override;
99 
100 private:
101  std::shared_ptr<RpcReport> const rpc_report;
102  detail::PendingCallCache pending_calls;
103 
104  static constexpr size_t size_of_header = 2;
105  detail::SendBuffer header_bytes;
106  detail::SendBuffer body_bytes;
107 
108  void receive_file_descriptors(google::protobuf::MessageLite* response);
109  template<class MessageType>
110  void receive_any_file_descriptors_for(MessageType* response);
111  void send_message(mir::protobuf::wire::Invocation const& body,
112  mir::protobuf::wire::Invocation const& invocation,
113  std::vector<mir::Fd>& fds);
114 
115  void read_message();
116  void process_event_sequence(std::string const& event);
117 
118  void notify_disconnected();
119 
120  std::weak_ptr<SurfaceMap> surface_map;
121  std::shared_ptr<AsyncBufferFactory> const buffer_factory;
122  std::shared_ptr<DisplayConfiguration> display_configuration;
123  std::shared_ptr<input::InputDevices> input_devices;
124  std::shared_ptr<LifecycleControl> lifecycle_control;
125  std::shared_ptr<PingHandler> const ping_handler;
126  std::shared_ptr<EventSink> event_sink;
127  std::atomic<bool> disconnected;
128  std::mutex read_mutex;
129  std::mutex write_mutex;
130 
131  bool prioritise_next_request{false};
132  std::experimental::optional<uint32_t> id_to_wait_for;
133 
134  /* We use the guarantee that the transport's destructor blocks until
135  * pending processing has finished to ensure that on_data_available()
136  * isn't called after the members it relies on are destroyed.
137  *
138  * This means that anything that owns a reference to the transport
139  * needs to be after anything that can be accessed from on_data_available().
140  *
141  * For simplicity's sake keep all of the dispatch infrastructure at the
142  * end to guarantee this.
143  */
144  std::shared_ptr<StreamTransport> const transport;
145  std::shared_ptr<mir::dispatch::ActionQueue> const delayed_processor;
147 };
148 
149 }
150 }
151 }
152 
153 #endif /* MIR_CLIENT_RPC_MIR_PROTOBUF_RPC_CHANNEL_H_ */
All things Mir.
Definition: atomic_callback.h:25
Definition: fd.h:33
Definition: mir_basic_rpc_channel.h:59
Definition: mir_basic_rpc_channel.h:102
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
Definition: dispatchable.h:38
Definition: mir_protobuf_rpc_channel.h:53
uint32_t FdEvents
Definition: dispatchable.h:36
Observer of IO status.
Definition: stream_transport.h:87

Copyright © 2012-2015 Canonical Ltd.
Generated on Wed Mar 30 00:29:56 UTC 2016