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 #include "ceee/ie/broker/broker_rpc_client.h" |
| 6 |
| 7 #include "base/lock.h" |
| 8 #include "base/logging.h" |
| 9 #include "broker_rpc_lib.h" |
| 10 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 11 |
| 12 BrokerRpcClient::BrokerRpcClient() : context_(0), binding_handle_(NULL) { |
| 13 } |
| 14 |
| 15 BrokerRpcClient::~BrokerRpcClient() { |
| 16 Disconnect(); |
| 17 } |
| 18 |
| 19 int HandleRpcException(unsigned int rpc_exception_code) { |
| 20 switch (rpc_exception_code) { |
| 21 case STATUS_ACCESS_VIOLATION: |
| 22 case STATUS_DATATYPE_MISALIGNMENT: |
| 23 case STATUS_PRIVILEGED_INSTRUCTION: |
| 24 case STATUS_BREAKPOINT: |
| 25 case STATUS_STACK_OVERFLOW: |
| 26 case STATUS_IN_PAGE_ERROR: |
| 27 case STATUS_GUARD_PAGE_VIOLATION: |
| 28 return EXCEPTION_CONTINUE_SEARCH; |
| 29 default: |
| 30 break; |
| 31 } |
| 32 return EXCEPTION_EXECUTE_HANDLER; |
| 33 } |
| 34 |
| 35 void BrokerRpcClient::LockContext() { |
| 36 RpcTryExcept { |
| 37 context_ = BrokerRpcClient_Connect(binding_handle_); |
| 38 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 39 LOG(ERROR) << "RPC error: " << RpcExceptionCode(); |
| 40 } RpcEndExcept |
| 41 } |
| 42 |
| 43 void BrokerRpcClient::ReleaseContext() { |
| 44 RpcTryExcept { |
| 45 BrokerRpcClient_Disconnect(binding_handle_, &context_); |
| 46 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 47 LOG(ERROR) << "RPC error: " << RpcExceptionCode(); |
| 48 } RpcEndExcept |
| 49 } |
| 50 |
| 51 bool BrokerRpcClient::Connect() { |
| 52 if (is_connected()) |
| 53 return true; |
| 54 |
| 55 std::wstring end_point = GetRpcEndPointAddress(); |
| 56 std::wstring protocol = kRpcProtocol; |
| 57 DCHECK(!protocol.empty()); |
| 58 DCHECK(!end_point.empty()); |
| 59 if (protocol.empty() || end_point.empty()) |
| 60 return false; |
| 61 |
| 62 LOG(INFO) << "Connecting to RPC server. Endpoint: " << end_point; |
| 63 RPC_WSTR string_binding = NULL; |
| 64 RPC_STATUS status = RpcStringBindingCompose( |
| 65 NULL, |
| 66 reinterpret_cast<RPC_WSTR>(&protocol[0]), |
| 67 NULL, |
| 68 reinterpret_cast<RPC_WSTR>(&end_point[0]), |
| 69 NULL, |
| 70 &string_binding); |
| 71 |
| 72 DCHECK(RPC_S_OK == status); |
| 73 if (RPC_S_OK == status) { |
| 74 status = RpcBindingFromStringBinding(string_binding, &binding_handle_); |
| 75 RpcStringFree(&string_binding); |
| 76 DCHECK(RPC_S_OK == status); |
| 77 if (RPC_S_OK == status) { |
| 78 status = RpcEpResolveBinding(binding_handle_, |
| 79 BrokerRpcClient_Broker_v1_1_c_ifspec); |
| 80 DCHECK(RPC_S_OK == status); |
| 81 if (RPC_S_OK == status) { |
| 82 LOG(INFO) << "RPC client is connected. Endpoint: " << end_point; |
| 83 LockContext(); |
| 84 } |
| 85 } |
| 86 } |
| 87 if (!is_connected()) |
| 88 Disconnect(); |
| 89 return is_connected(); |
| 90 } |
| 91 |
| 92 void BrokerRpcClient::Disconnect() { |
| 93 if (context_ != NULL) { |
| 94 ReleaseContext(); |
| 95 } |
| 96 if (binding_handle_ != NULL) { |
| 97 RPC_STATUS status = RpcBindingFree(&binding_handle_); |
| 98 DCHECK(RPC_S_OK == status); |
| 99 } |
| 100 } |
| 101 |
| 102 bool BrokerRpcClient::FireEvent(BSTR event_name, BSTR event_args) { |
| 103 RpcTryExcept { |
| 104 BrokerRpcClient_FireEvent(binding_handle_, event_name, event_args); |
| 105 return true; |
| 106 } RpcExcept(HandleRpcException(RpcExceptionCode())) { |
| 107 LOG(ERROR) << "RPC error: " << RpcExceptionCode(); |
| 108 } RpcEndExcept |
| 109 return false; |
| 110 } |
OLD | NEW |