1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include "pdx/client.h"
#include <log/log.h>
#include <pdx/trace.h>
namespace android {
namespace pdx {
void Client::EnableAutoReconnect(int64_t reconnect_timeout_ms) {
if (channel_factory_) {
reconnect_timeout_ms_ = reconnect_timeout_ms;
auto_reconnect_enabled_ = true;
}
}
void Client::DisableAutoReconnect() { auto_reconnect_enabled_ = false; }
bool Client::IsConnected() const { return channel_.get() != nullptr; }
Status<void> Client::CheckReconnect() {
Status<void> ret;
bool was_disconnected = !IsConnected();
if (auto_reconnect_enabled_ && was_disconnected && channel_factory_) {
auto status = channel_factory_->Connect(reconnect_timeout_ms_);
if (!status) {
error_ = -status.error();
ret.SetError(status.error());
return ret;
}
channel_ = status.take();
}
if (!IsConnected()) {
ret.SetError(ESHUTDOWN);
} else {
// Call the subclass OnConnect handler. The subclass may choose to close the
// connection in the handler, in which case error_ will be non-zero.
if (was_disconnected)
OnConnect();
if (!IsConnected())
ret.SetError(-error_);
else
ret.SetValue();
}
return ret;
}
bool Client::NeedToDisconnectChannel(int error) const {
return error == ESHUTDOWN && auto_reconnect_enabled_;
}
void Client::CheckDisconnect(int error) {
if (NeedToDisconnectChannel(error))
Close(error);
}
Client::Client(std::unique_ptr<ClientChannel> channel)
: channel_{std::move(channel)} {}
Client::Client(std::unique_ptr<ClientChannelFactory> channel_factory,
int64_t timeout_ms)
: channel_factory_{std::move(channel_factory)} {
auto status = channel_factory_->Connect(timeout_ms);
if (!status) {
ALOGE("Client::Client: Failed to connect to service because: %s",
status.GetErrorMessage().c_str());
error_ = -status.error();
} else {
channel_ = status.take();
}
}
bool Client::IsInitialized() const {
return IsConnected() || (channel_factory_ && auto_reconnect_enabled_);
}
void Client::OnConnect() {}
int Client::error() const { return error_; }
Status<void> Client::SendImpulse(int opcode) {
PDX_TRACE_NAME("Client::SendImpulse");
auto status = CheckReconnect();
if (!status)
return status;
status = channel_->SendImpulse(opcode, nullptr, 0);
CheckDisconnect(status);
return status;
}
Status<void> Client::SendImpulse(int opcode, const void* buffer,
size_t length) {
PDX_TRACE_NAME("Client::SendImpulse");
auto status = CheckReconnect();
if (!status)
return status;
status = channel_->SendImpulse(opcode, buffer, length);
CheckDisconnect(status);
return status;
}
void Client::Close(int error) {
channel_.reset();
// Normalize error codes to negative integer space.
error_ = error <= 0 ? error : -error;
}
int Client::event_fd() const {
return IsConnected() ? channel_->event_fd() : -1;
}
LocalChannelHandle& Client::GetChannelHandle() {
return channel_->GetChannelHandle();
}
const LocalChannelHandle& Client::GetChannelHandle() const {
return channel_->GetChannelHandle();
}
///////////////////////////// Transaction implementation //////////////////////
Transaction::Transaction(Client& client) : client_{client} {}
Transaction::~Transaction() {
if (state_allocated_ && client_.GetChannel())
client_.GetChannel()->FreeTransactionState(state_);
}
bool Transaction::EnsureStateAllocated() {
if (!state_allocated_ && client_.GetChannel()) {
state_ = client_.GetChannel()->AllocateTransactionState();
state_allocated_ = true;
}
return state_allocated_;
}
void Transaction::SendTransaction(int opcode, Status<void>* ret,
const iovec* send_vector, size_t send_count,
const iovec* receive_vector,
size_t receive_count) {
*ret = client_.CheckReconnect();
if (!*ret)
return;
if (!EnsureStateAllocated()) {
ret->SetError(ESHUTDOWN);
return;
}
auto status = client_.GetChannel()->SendWithInt(
state_, opcode, send_vector, send_count, receive_vector, receive_count);
if (status) {
ret->SetValue();
} else {
ret->SetError(status.error());
}
CheckDisconnect(status);
}
void Transaction::SendTransaction(int opcode, Status<int>* ret,
const iovec* send_vector, size_t send_count,
const iovec* receive_vector,
size_t receive_count) {
auto status = client_.CheckReconnect();
if (!status) {
ret->SetError(status.error());
return;
}
if (!EnsureStateAllocated()) {
ret->SetError(ESHUTDOWN);
return;
}
*ret = client_.GetChannel()->SendWithInt(
state_, opcode, send_vector, send_count, receive_vector, receive_count);
CheckDisconnect(*ret);
}
void Transaction::SendTransaction(int opcode, Status<LocalHandle>* ret,
const iovec* send_vector, size_t send_count,
const iovec* receive_vector,
size_t receive_count) {
auto status = client_.CheckReconnect();
if (!status) {
ret->SetError(status.error());
return;
}
if (!EnsureStateAllocated()) {
ret->SetError(ESHUTDOWN);
return;
}
*ret = client_.GetChannel()->SendWithFileHandle(
state_, opcode, send_vector, send_count, receive_vector, receive_count);
CheckDisconnect(*ret);
}
void Transaction::SendTransaction(int opcode, Status<LocalChannelHandle>* ret,
const iovec* send_vector, size_t send_count,
const iovec* receive_vector,
size_t receive_count) {
auto status = client_.CheckReconnect();
if (!status) {
ret->SetError(status.error());
return;
}
if (!EnsureStateAllocated()) {
ret->SetError(ESHUTDOWN);
return;
}
*ret = client_.GetChannel()->SendWithChannelHandle(
state_, opcode, send_vector, send_count, receive_vector, receive_count);
CheckDisconnect(*ret);
}
Status<FileReference> Transaction::PushFileHandle(const LocalHandle& handle) {
if (client_.CheckReconnect() && EnsureStateAllocated())
return client_.GetChannel()->PushFileHandle(state_, handle);
return ErrorStatus{ESHUTDOWN};
}
Status<FileReference> Transaction::PushFileHandle(
const BorrowedHandle& handle) {
if (client_.CheckReconnect() && EnsureStateAllocated())
return client_.GetChannel()->PushFileHandle(state_, handle);
return ErrorStatus{ESHUTDOWN};
}
Status<FileReference> Transaction::PushFileHandle(const RemoteHandle& handle) {
return handle.Get();
}
Status<ChannelReference> Transaction::PushChannelHandle(
const LocalChannelHandle& handle) {
if (client_.CheckReconnect() && EnsureStateAllocated())
return client_.GetChannel()->PushChannelHandle(state_, handle);
return ErrorStatus{ESHUTDOWN};
}
Status<ChannelReference> Transaction::PushChannelHandle(
const BorrowedChannelHandle& handle) {
if (client_.CheckReconnect() && EnsureStateAllocated())
return client_.GetChannel()->PushChannelHandle(state_, handle);
return ErrorStatus{ESHUTDOWN};
}
Status<ChannelReference> Transaction::PushChannelHandle(
const RemoteChannelHandle& handle) {
return handle.value();
}
bool Transaction::GetFileHandle(FileReference ref, LocalHandle* handle) {
return client_.CheckReconnect() && EnsureStateAllocated() &&
client_.GetChannel()->GetFileHandle(state_, ref, handle);
}
bool Transaction::GetChannelHandle(ChannelReference ref,
LocalChannelHandle* handle) {
return client_.CheckReconnect() && EnsureStateAllocated() &&
client_.GetChannel()->GetChannelHandle(state_, ref, handle);
}
void Transaction::CheckDisconnect(int error) {
if (client_.NeedToDisconnectChannel(error)) {
if (state_allocated_) {
if (client_.GetChannel())
client_.GetChannel()->FreeTransactionState(state_);
state_ = nullptr;
state_allocated_ = false;
}
client_.Close(error);
}
}
} // namespace pdx
} // namespace android
|