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"
25 
26 #include <google/protobuf/service.h>
27 #include <google/protobuf/descriptor.h>
28 
29 #include <thread>
30 #include <atomic>
31 
32 namespace mir
33 {
34 namespace protobuf
35 {
36 namespace wire
37 {
38 class Invocation;
39 class Result;
40 }
41 }
42 
43 namespace client
44 {
45 class DisplayConfiguration;
46 class SurfaceMap;
47 class LifecycleControl;
48 class EventSink;
49 namespace rpc
50 {
51 
52 class RpcReport;
53 
55  public MirBasicRpcChannel,
58 {
59 public:
60  MirProtobufRpcChannel(std::unique_ptr<StreamTransport> transport,
61  std::shared_ptr<SurfaceMap> const& surface_map,
62  std::shared_ptr<DisplayConfiguration> const& disp_config,
63  std::shared_ptr<RpcReport> const& rpc_report,
64  std::shared_ptr<LifecycleControl> const& lifecycle_control,
65  std::shared_ptr<EventSink> const& event_sink);
66 
67  ~MirProtobufRpcChannel() = default;
68 
69  // StreamTransport::Observer
70  void on_data_available() override;
71  void on_disconnected() override;
72 
73  // Dispatchable
74  Fd watch_fd() const override;
75  bool dispatch(mir::dispatch::FdEvents events) override;
77 private:
78  virtual void CallMethod(const google::protobuf::MethodDescriptor* method, google::protobuf::RpcController*,
79  const google::protobuf::Message* parameters, google::protobuf::Message* response,
80  google::protobuf::Closure* complete) override;
81 
82  std::shared_ptr<RpcReport> const rpc_report;
83  detail::PendingCallCache pending_calls;
84 
85  static constexpr size_t size_of_header = 2;
86  detail::SendBuffer header_bytes;
87  detail::SendBuffer body_bytes;
88 
89  void receive_file_descriptors(google::protobuf::Message* response, google::protobuf::Closure* complete);
90  template<class MessageType>
91  void receive_any_file_descriptors_for(MessageType* response);
92  void send_message(mir::protobuf::wire::Invocation const& body,
93  mir::protobuf::wire::Invocation const& invocation,
94  std::vector<mir::Fd>& fds);
95 
96  void read_message();
97  void process_event_sequence(std::string const& event);
98 
99  void notify_disconnected();
100 
101  std::shared_ptr<SurfaceMap> surface_map;
102  std::shared_ptr<DisplayConfiguration> display_configuration;
103  std::shared_ptr<LifecycleControl> lifecycle_control;
104  std::shared_ptr<EventSink> event_sink;
105  std::atomic<bool> disconnected;
106  std::mutex read_mutex;
107  std::mutex write_mutex;
108 
109  /* We use the guarantee that the transport's destructor blocks until
110  * pending processing has finished to ensure that on_data_available()
111  * isn't called after the members it relies on are destroyed.
112  *
113  * This means the transport field must appear after any field used
114  * by on_data_available. For simplicity, put it last.
115  */
116  std::unique_ptr<StreamTransport> transport;
117 };
118 
119 }
120 }
121 }
122 
123 #endif /* MIR_CLIENT_RPC_MIR_PROTOBUF_RPC_CHANNEL_H_ */
All things Mir.
Definition: buffer_stream.h:37
void on_disconnected() override
Called by the Transport when the connection to the server has been broken.
Definition: mir_protobuf_rpc_channel.cpp:354
Definition: fd.h:33
Definition: mir_basic_rpc_channel.h:52
mir::dispatch::FdEvents relevant_events() const override
The set of file-descriptor events this Dispatchable handles.
Definition: mir_protobuf_rpc_channel.cpp:369
Definition: mir_basic_rpc_channel.h:91
bool dispatch(mir::dispatch::FdEvents events) override
Dispatch one pending event.
Definition: mir_protobuf_rpc_channel.cpp:364
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< EventSink > const &event_sink)
Definition: mir_protobuf_rpc_channel.cpp:50
std::vector< uint8_t > SendBuffer
Definition: mir_basic_rpc_channel.h:50
Fd watch_fd() const override
Get a poll()able file descriptor.
Definition: mir_protobuf_rpc_channel.cpp:359
Definition: dispatchable.h:38
Definition: mir_protobuf_rpc_channel.h:54
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:299
Observer of IO status.
Definition: stream_transport.h:87

Copyright © 2012,2013 Canonical Ltd.
Generated on Tue Mar 24 16:15:19 UTC 2015