2 * Generated by tidlc 1.9.1.
10 #include "BenchmarkStub.h"
17 #define LOG_TAG "RPC_PORT_STUB"
35 #define _E(fmt, ...) dlog_print(DLOG_ERROR, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
36 #define _W(fmt, ...) dlog_print(DLOG_WARN, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
37 #define _I(fmt, ...) dlog_print(DLOG_INFO, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
38 #define _D(fmt, ...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
41 #define TIDL_VERSION "1.9.1"
45 namespace BenchmarkStub {
49 Benchmark::ServiceBase::ServiceBase(std::string sender, std::string instance)
50 : sender_(std::move(sender)), instance_(std::move(instance)),
51 active_object_(new ActiveObject()) {}
53 void Benchmark::ServiceBase::SetPort(rpc_port_h port) {
57 void Benchmark::ServiceBase::Disconnect() {
58 int ret = rpc_port_disconnect(port_);
59 if (ret == RPC_PORT_ERROR_NONE) {
60 _E("Failed to disconnect the port(%d)", ret);
68 std::atomic<int> Benchmark::CallbackBase::seq_num_ { 0 };
70 Benchmark::CallbackBase::CallbackBase(int delegate_id, bool once)
71 : id_(delegate_id), once_(once) {
75 int Benchmark::CallbackBase::GetId() const {
79 int Benchmark::CallbackBase::GetSeqId() const {
83 bool Benchmark::CallbackBase::IsOnce() const {
87 std::string Benchmark::CallbackBase::GetTag() const {
88 return std::to_string(id_) + "::" + std::to_string(seq_id_);
91 rpc_port_parcel_h operator << (rpc_port_parcel_h h, const Benchmark::CallbackBase& cb) {
92 rpc_port_parcel_write_int32(h, cb.id_);
93 rpc_port_parcel_write_int32(h, cb.seq_id_);
94 rpc_port_parcel_write_bool(h, cb.once_);
99 rpc_port_parcel_h operator >> (rpc_port_parcel_h h, Benchmark::CallbackBase& cb) {
100 rpc_port_parcel_read_int32(h, &cb.id_);
101 rpc_port_parcel_read_int32(h, &cb.seq_id_);
102 rpc_port_parcel_read_bool(h, &cb.once_);
107 Benchmark::Benchmark() {
108 int r = rpc_port_stub_create(&stub_, "Benchmark");
109 if (r != RPC_PORT_ERROR_NONE) {
110 _E("Failed to create stub handle");
111 throw InvalidIOException();
113 rpc_port_stub_add_connected_event_cb(stub_, OnConnectedCB, this);
114 rpc_port_stub_add_disconnected_event_cb(stub_, OnDisconnectedCB, this);
115 rpc_port_stub_add_received_event_cb(stub_, OnReceivedCB, this);
118 Benchmark::~Benchmark() {
119 for (auto& i : services_) {
124 rpc_port_stub_destroy(stub_);
128 void Benchmark::Listen(std::shared_ptr<Benchmark::ServiceBase::Factory> service_factory) {
129 service_factory_ = std::move(service_factory);
130 int r = rpc_port_stub_listen(stub_);
131 if (r != RPC_PORT_ERROR_NONE) {
132 _E("Failed to listen stub");
134 case RPC_PORT_ERROR_INVALID_PARAMETER:
135 case RPC_PORT_ERROR_IO_ERROR:
136 throw InvalidIOException();
141 void Benchmark::OnConnectedCB(const char* sender, const char* instance, void* data) {
142 Benchmark* stub = static_cast<Benchmark*>(data);
143 auto s = stub->service_factory_->CreateService(sender, instance);
146 int ret = rpc_port_stub_get_port(stub->stub_, RPC_PORT_PORT_CALLBACK, instance, &port);
147 if (ret != RPC_PORT_ERROR_NONE) {
148 _E("Failed to get the port(%d)", ret);
154 stub->services_.emplace_back(std::move(s));
157 void Benchmark::OnDisconnectedCB(const char* sender, const char* instance, void *data) {
158 Benchmark* stub = static_cast<Benchmark*>(data);
160 for (auto& i : stub->services_) {
161 if (i->GetInstance() == instance) {
163 stub->services_.remove(i);
169 int Benchmark::OnReceivedCB(const char* sender, const char* instance, rpc_port_h port, void *data)
171 auto* cxt = static_cast<Benchmark*>(data);
173 rpc_port_parcel_h result;
174 rpc_port_parcel_h header;
178 std::shared_ptr<ServiceBase> b;
179 rpc_port_h callback_port;
181 for (auto& i : cxt->services_) {
182 if (i->GetInstance() == instance) {
188 if (b.get() == nullptr) {
189 _E("Failed to find Benchmark context(%s)", instance);
193 ret = rpc_port_stub_get_port(cxt->stub_, RPC_PORT_PORT_CALLBACK, instance,
196 _E("Failed to get callback port");
199 ret = rpc_port_parcel_create_from_port(&p, port);
201 _E("Failed to create parcel from port");
205 rpc_port_parcel_get_header(p, &header);
206 rpc_port_parcel_header_get_seq_num(header, &seq_num);
207 _W("[Sequence] %d", seq_num);
209 rpc_port_parcel_create(&result);
210 rpc_port_parcel_get_header(result, &header);
211 rpc_port_parcel_header_set_tag(header, TIDL_VERSION);
212 rpc_port_parcel_header_set_seq_num(header, seq_num);
214 rpc_port_parcel_read_int32(p, &cmd);
217 case static_cast<int>(MethodId::Test): {
218 char* param1_raw = nullptr;
219 rpc_port_parcel_read_string(p, ¶m1_raw);
220 std::string param1(param1_raw);
222 auto retVal = b->Test(param1);
223 rpc_port_parcel_write_int32(result, static_cast<int>(MethodId::__Result));
224 rpc_port_parcel_write_int32(result, retVal);
225 ret = rpc_port_parcel_send(result, port);
226 _I("Parcel send result(%d)", ret);
231 _E("Unknown command(%d)", cmd);
232 rpc_port_parcel_destroy(p);
233 rpc_port_parcel_destroy(result);
237 rpc_port_parcel_destroy(p);
238 rpc_port_parcel_destroy(result);
244 } // namespace BenchmarkStub
245 } // namespace rpc_port