+/*
+ * Generated by tidlc 1.9.1.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <dlog.h>
+
+#include "BenchmarkProxy.h"
+
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "RPC_PORT_PROXY"
+
+#ifdef _E
+#undef _E
+#endif
+
+#ifdef _W
+#undef _W
+#endif
+
+#ifdef _I
+#undef _I
+#endif
+
+#ifdef _D
+#undef _D
+#endif
+
+#define _E(fmt, ...) dlog_print(DLOG_ERROR, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
+#define _W(fmt, ...) dlog_print(DLOG_WARN, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
+#define _I(fmt, ...) dlog_print(DLOG_INFO, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
+#define _D(fmt, ...) dlog_print(DLOG_DEBUG, LOG_TAG, "%s: %s(%d) > " fmt, basename(const_cast<char*>(__FILE__)), __FUNCTION__, __LINE__, ##__VA_ARGS__)
+
+#ifndef TIDL_VERSION
+#define TIDL_VERSION "1.9.1"
+#endif
+
+namespace rpc_port {
+namespace BenchmarkProxy {
+
+namespace proxy {
+
+
+std::atomic<int> Benchmark::CallbackBase::seq_num_ { 0 };
+
+Benchmark::CallbackBase::CallbackBase(int delegate_id, bool once)
+ : id_(delegate_id), once_(once) {
+ seq_id_ = seq_num_++;
+}
+
+int Benchmark::CallbackBase::GetId() const {
+ return id_;
+}
+
+int Benchmark::CallbackBase::GetSeqId() const {
+ return seq_id_;
+}
+
+bool Benchmark::CallbackBase::IsOnce() const {
+ return once_;
+}
+
+std::string Benchmark::CallbackBase::GetTag() const {
+ return std::to_string(id_) + "::" + std::to_string(seq_id_);
+}
+
+rpc_port_parcel_h operator << (rpc_port_parcel_h h, const Benchmark::CallbackBase& cb) {
+ rpc_port_parcel_write_int32(h, cb.id_);
+ rpc_port_parcel_write_int32(h, cb.seq_id_);
+ rpc_port_parcel_write_bool(h, cb.once_);
+
+ return h;
+}
+
+rpc_port_parcel_h operator >> (rpc_port_parcel_h h, Benchmark::CallbackBase& cb) {
+ rpc_port_parcel_read_int32(h, &cb.id_);
+ rpc_port_parcel_read_int32(h, &cb.seq_id_);
+ rpc_port_parcel_read_bool(h, &cb.once_);
+
+ return h;
+}
+
+Benchmark::Benchmark(IEventListener* listener, const std::string& target_appid)
+ : port_(nullptr), callback_port_(nullptr), proxy_(nullptr),
+ listener_(listener), target_appid_(target_appid) {
+ int r = rpc_port_proxy_create(&proxy_);
+
+ if (r != RPC_PORT_ERROR_NONE) {
+ _E("Failed to create proxy");
+ throw InvalidIOException();
+ }
+
+ rpc_port_proxy_add_connected_event_cb(proxy_, OnConnectedCB, this);
+ rpc_port_proxy_add_disconnected_event_cb(proxy_, OnDisconnectedCB, this);
+ rpc_port_proxy_add_rejected_event_cb(proxy_, OnRejectedCB, this);
+ rpc_port_proxy_add_received_event_cb(proxy_, OnReceivedCB, this);
+}
+
+Benchmark::~Benchmark() {
+ if (proxy_)
+ rpc_port_proxy_destroy(proxy_);
+}
+
+void Benchmark::Connect(bool sync) {
+ int ret;
+ if (sync)
+ ret = rpc_port_proxy_connect_sync(proxy_, target_appid_.c_str(), "Benchmark");
+ else
+ ret = rpc_port_proxy_connect(proxy_, target_appid_.c_str(), "Benchmark");
+ if (ret != RPC_PORT_ERROR_NONE) {
+ _E("Failed to connect Benchmark");
+ switch (ret) {
+ case RPC_PORT_ERROR_INVALID_PARAMETER:
+ throw InvalidIDException();
+
+ case RPC_PORT_ERROR_IO_ERROR:
+ throw InvalidIOException();
+
+ case RPC_PORT_ERROR_PERMISSION_DENIED:
+ throw PermissionDeniedException();
+ }
+ }
+}
+
+void Benchmark::Disconnect() {
+ int ret = rpc_port_disconnect(port_);
+ if (ret != RPC_PORT_ERROR_NONE) {
+ _E("Failed to disconnect Benchmark");
+ throw InvalidIDException();
+ }
+}
+
+void Benchmark::DisposeCallback(const std::string& tag) {
+ for (auto& i : delegate_list_) {
+ if (i->GetTag() == tag) {
+ delegate_list_.remove(i);
+ return;
+ }
+ }
+}
+
+void Benchmark::ProcessReceivedEvent(rpc_port_parcel_h parcel) {
+ int id = 0;
+ int seq_id = 0;
+ bool once = false;
+
+ rpc_port_parcel_read_int32(parcel, &id);
+ rpc_port_parcel_read_int32(parcel, &seq_id);
+ rpc_port_parcel_read_bool(parcel, &once);
+
+ for (auto& i : delegate_list_) {
+ if (i->GetId() == id && i->GetSeqId() == seq_id) {
+ i->OnReceivedEvent(parcel);
+ if (i->IsOnce())
+ delegate_list_.remove(i);
+ break;
+ }
+ }
+}
+
+void Benchmark::ConsumeCommand(rpc_port_parcel_h* parcel, rpc_port_h port) {
+ do {
+ rpc_port_parcel_h p;
+ int ret = rpc_port_parcel_create_from_port(&p, port);
+ int cmd;
+
+ if (ret != 0)
+ break;
+ rpc_port_parcel_read_int32(p, &cmd);
+ if (cmd == static_cast<int>(MethodId::__Result)) {
+ *parcel = p;
+ return;
+ }
+
+ rpc_port_parcel_destroy(p);
+ *parcel = nullptr;
+ } while (true);
+ *parcel = nullptr;
+}
+
+void Benchmark::OnConnectedCB(const char *ep, const char *port_name, rpc_port_h port, void *data) {
+ Benchmark* l = static_cast<Benchmark*>(data);
+ rpc_port_h cb_port;
+
+ l->port_ = port;
+ rpc_port_proxy_get_port(l->proxy_, RPC_PORT_PORT_CALLBACK, &cb_port);
+ l->callback_port_ = cb_port;
+ l->listener_->OnConnected();
+}
+
+void Benchmark::OnDisconnectedCB(const char *ep, const char *port_name, void *data) {
+ Benchmark* l = static_cast<Benchmark*>(data);
+ l->delegate_list_.clear();
+ l->listener_->OnDisconnected();
+}
+
+void Benchmark::OnRejectedCB(const char *ep, const char *port_name, void *data) {
+ Benchmark* l = static_cast<Benchmark*>(data);
+ l->listener_->OnRejected();
+}
+
+void Benchmark::OnReceivedCB(const char *ep, const char *port_name, void *data) {
+ Benchmark* l = static_cast<Benchmark*>(data);
+ int cmd;
+ rpc_port_parcel_h parcel_received;
+
+ if (rpc_port_parcel_create_from_port(&parcel_received, l->callback_port_) != 0) {
+ _E("Failed to create parcel from port");
+ return;
+ }
+
+ rpc_port_parcel_read_int32(parcel_received, &cmd);
+ if (cmd != static_cast<int>(MethodId::__Callback)) {
+ rpc_port_parcel_destroy(parcel_received);
+ return;
+ }
+
+ l->ProcessReceivedEvent(parcel_received);
+ rpc_port_parcel_destroy(parcel_received);
+}
+
+
+int Benchmark::Test(std::string data) {
+ if (port_ == nullptr) {
+ _E("Not connected");
+ throw NotConnectedSocketException();
+ }
+
+ rpc_port_parcel_h p;
+ rpc_port_parcel_create(&p);
+
+ rpc_port_parcel_header_h header_;
+ rpc_port_parcel_get_header(p, &header_);
+ rpc_port_parcel_header_set_tag(header_, TIDL_VERSION);
+ int seq_num_ = -1;
+ rpc_port_parcel_header_get_seq_num(header_, &seq_num_);
+ _W("[Version] \"%s\", [Sequence] %d", TIDL_VERSION, seq_num_);
+ rpc_port_parcel_write_int32(p, static_cast<int>(MethodId::Test));
+ rpc_port_parcel_write_string(p, data.c_str());
+
+ std::lock_guard<std::recursive_mutex> lock(mutex_);
+
+ // Send
+ int r = rpc_port_parcel_send(p, port_);
+ if (r != RPC_PORT_ERROR_NONE) {
+ _E("Failed to send parcel. result(%d)", r);
+ rpc_port_parcel_destroy(p);
+ throw InvalidIOException();
+ }
+
+ int ret;
+ bool done_ = false;
+ do {
+ rpc_port_parcel_h parcel_received = nullptr;
+ // Receive
+ ConsumeCommand(&parcel_received, port_);
+ if (parcel_received == nullptr) {
+ _E("Invalid protocol");
+ throw InvalidProtocolException();
+ }
+
+ rpc_port_parcel_get_header(parcel_received, &header_);
+ char* tag_ = nullptr;
+ rpc_port_parcel_header_get_tag(header_, &tag_);
+ std::unique_ptr<char, decltype(std::free)*> tag_auto_(tag_, std::free);
+ if (tag_ && tag_[0] != '\0') {
+ _W("[Version] %s", tag_);
+ int seq_num_received_ = -1;
+ rpc_port_parcel_header_get_seq_num(header_, &seq_num_received_);
+ if (seq_num_received_ != seq_num_) {
+ _E("Invalid protocol. %d", seq_num_received_);
+ rpc_port_parcel_destroy(parcel_received);
+ continue;
+ }
+ }
+ done_ = true;
+
+ rpc_port_parcel_read_int32(parcel_received, &ret);
+
+ rpc_port_parcel_destroy(parcel_received);
+ } while (!done_);
+
+ rpc_port_parcel_destroy(p);
+
+ return ret;
+
+}
+} // namespace proxy
+} // namespace BenchmarkProxy
+} // namespace rpc_port