OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // Broker RPC Client implementation. |
| 6 |
| 7 #include "ceee/ie/broker/broker_rpc_client.h" |
| 8 |
| 9 #include "base/lock.h" |
| 10 #include "base/logging.h" |
| 11 #include "broker_rpc_lib.h" |
| 12 #include "ceee/common/com_utils.h" |
| 13 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 14 |
| 15 BrokerRpcClient::BrokerRpcClient() : context_(0), binding_handle_(NULL) { |
| 16 } |
| 17 |
| 18 BrokerRpcClient::~BrokerRpcClient() { |
| 19 Disconnect(); |
| 20 } |
| 21 |
| 22 int HandleRpcException(unsigned int rpc_exception_code) { |
| 23 switch (rpc_exception_code) { |
| 24 case STATUS_ACCESS_VIOLATION: |
| 25 case STATUS_DATATYPE_MISALIGNMENT: |
| 26 case STATUS_PRIVILEGED_INSTRUCTION: |
| 27 case STATUS_BREAKPOINT: |
| 28 case STATUS_STACK_OVERFLOW: |
| 29 case STATUS_IN_PAGE_ERROR: |
| 30 case STATUS_GUARD_PAGE_VIOLATION: |
| 31 return EXCEPTION_CONTINUE_SEARCH; |
| 32 default: |
| 33 break; |
| 34 } |
| 35 return EXCEPTION_EXECUTE_HANDLER; |
| 36 } |
| 37 |
| 38 void BrokerRpcClient::LockContext() { |
| 39 RpcTryExcept { |
| 40 context_ = BrokerRpcClient_Connect(binding_handle_); |
| 41 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 42 LOG(ERROR) << "RPC error in LockContext: " << RpcExceptionCode(); |
| 43 } RpcEndExcept |
| 44 } |
| 45 |
| 46 void BrokerRpcClient::ReleaseContext() { |
| 47 RpcTryExcept { |
| 48 BrokerRpcClient_Disconnect(binding_handle_, &context_); |
| 49 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 50 LOG(ERROR) << "RPC error in ReleaseContext: " << RpcExceptionCode(); |
| 51 } RpcEndExcept |
| 52 } |
| 53 |
| 54 bool BrokerRpcClient::Connect() { |
| 55 if (is_connected()) |
| 56 return true; |
| 57 |
| 58 std::wstring end_point = GetRpcEndPointAddress(); |
| 59 std::wstring protocol = kRpcProtocol; |
| 60 DCHECK(!protocol.empty()); |
| 61 DCHECK(!end_point.empty()); |
| 62 if (protocol.empty() || end_point.empty()) |
| 63 return false; |
| 64 |
| 65 // TODO(vitalybuka@google.com): There's no guarantee (aside from name |
| 66 // uniqueness) that it will connect to an endpoint created by the same user. |
| 67 // Hint: The missing invocation is RpcBindingSetAuthInfoEx. |
| 68 LOG(INFO) << "Connecting to RPC server. Endpoint: " << end_point; |
| 69 RPC_WSTR string_binding = NULL; |
| 70 // Create binding string with given protocol and end point. |
| 71 RPC_STATUS status = ::RpcStringBindingCompose( |
| 72 NULL, |
| 73 reinterpret_cast<RPC_WSTR>(&protocol[0]), |
| 74 NULL, |
| 75 reinterpret_cast<RPC_WSTR>(&end_point[0]), |
| 76 NULL, |
| 77 &string_binding); |
| 78 LOG_IF(ERROR, RPC_S_OK != status) << |
| 79 "Failed to compose binding string. RPC_STATUS=0x" << com::LogWe(status); |
| 80 |
| 81 if (RPC_S_OK == status) { |
| 82 // Create binding from just generated binding string. Binding handle should |
| 83 // used for PRC calls. |
| 84 status = ::RpcBindingFromStringBinding(string_binding, &binding_handle_); |
| 85 LOG_IF(ERROR, RPC_S_OK != status) << |
| 86 "Failed to bind. RPC_STATUS=0x" << com::LogWe(status); |
| 87 ::RpcStringFree(&string_binding); |
| 88 if (RPC_S_OK == status) { |
| 89 LOG(INFO) << "RPC client is connected. Endpoint: " << end_point; |
| 90 LockContext(); |
| 91 } |
| 92 } |
| 93 if (!is_connected()) |
| 94 Disconnect(); |
| 95 return is_connected(); |
| 96 } |
| 97 |
| 98 void BrokerRpcClient::Disconnect() { |
| 99 if (context_ != NULL) |
| 100 ReleaseContext(); |
| 101 if (binding_handle_ != NULL) { |
| 102 RPC_STATUS status = ::RpcBindingFree(&binding_handle_); |
| 103 LOG_IF(WARNING, RPC_S_OK != status) << |
| 104 "Failed to unbind. RPC_STATUS=0x" << com::LogWe(status); |
| 105 } |
| 106 } |
| 107 |
| 108 bool BrokerRpcClient::FireEvent(BSTR event_name, BSTR event_args) { |
| 109 RpcTryExcept { |
| 110 BrokerRpcClient_FireEvent(binding_handle_, event_name, event_args); |
| 111 return true; |
| 112 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 113 LOG(ERROR) << "RPC error in FireEvent: " << RpcExceptionCode(); |
| 114 } RpcEndExcept |
| 115 return false; |
| 116 } |
OLD | NEW |