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