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 Server implementation. |
| 6 |
| 7 #include "ceee/ie/broker/broker_rpc_server.h" |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/win_util.h" |
| 11 #include "broker_rpc_lib.h" |
| 12 #include "ceee/common/com_utils.h" |
| 13 #include "ceee/ie/broker/broker_module_util.h" |
| 14 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 15 #include "ceee/ie/broker/chrome_postman.h" |
| 16 |
| 17 BrokerRpcServer::BrokerRpcServer() |
| 18 : is_started_(false), |
| 19 current_thread_(::GetCurrentThreadId()) { |
| 20 } |
| 21 |
| 22 BrokerRpcServer::~BrokerRpcServer() { |
| 23 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 24 Stop(); |
| 25 } |
| 26 |
| 27 bool BrokerRpcServer::Start() { |
| 28 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 29 |
| 30 if (is_started()) |
| 31 return true; |
| 32 |
| 33 std::wstring end_point = GetRpcEndPointAddress(); |
| 34 std::wstring protocol = kRpcProtocol; |
| 35 DCHECK(!protocol.empty()); |
| 36 DCHECK(!end_point.empty()); |
| 37 if (protocol.empty() || end_point.empty()) |
| 38 return false; |
| 39 |
| 40 LOG(INFO) << "RPC server is starting. Endpoint: " << end_point; |
| 41 // Tell RPC runtime to use local interprocess communication for given |
| 42 // end point. |
| 43 RPC_STATUS status = ::RpcServerUseProtseqEp( |
| 44 reinterpret_cast<RPC_WSTR>(&protocol[0]), |
| 45 RPC_C_PROTSEQ_MAX_REQS_DEFAULT, |
| 46 reinterpret_cast<RPC_WSTR>(&end_point[0]), |
| 47 NULL); |
| 48 LOG_IF(ERROR, RPC_S_OK != status && RPC_S_DUPLICATE_ENDPOINT != status) << |
| 49 "Failed to set protocol for RPC end point. RPC_STATUS=0x" << |
| 50 com::LogWe(status); |
| 51 if (RPC_S_OK == status || RPC_S_DUPLICATE_ENDPOINT == status) { |
| 52 // Register RPC interface with the RPC runtime. |
| 53 status = ::RpcServerRegisterIfEx(BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, |
| 54 NULL, NULL, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL); |
| 55 LOG_IF(ERROR, RPC_S_OK != status) << |
| 56 "Failed to register RPC interface. RPC_STATUS=0x" << com::LogWe(status); |
| 57 if (RPC_S_OK == status) { |
| 58 // Start listen for RPC calls. |
| 59 status = ::RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE); |
| 60 LOG_IF(ERROR, RPC_S_OK != status) << |
| 61 "Failed to start listening. RPC_STATUS=0x" << com::LogWe(status); |
| 62 if (RPC_S_OK == status) { |
| 63 LOG(INFO) << "RPC server is started. Endpoint: " << end_point; |
| 64 is_started_ = true; |
| 65 } |
| 66 } |
| 67 } |
| 68 if (!is_started()) |
| 69 Stop(); |
| 70 |
| 71 return is_started(); |
| 72 } |
| 73 |
| 74 bool BrokerRpcServer::Stop() { |
| 75 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 76 is_started_ = false; |
| 77 // Stop server listening for RPC. |
| 78 RPC_STATUS status = ::RpcMgmtStopServerListening(NULL); |
| 79 LOG_IF(WARNING, RPC_S_OK != status) << |
| 80 "Failed to stop listening. RPC_STATUS=0x" << com::LogWe(status); |
| 81 // Wait while server stops listening threads. |
| 82 status = ::RpcMgmtWaitServerListen(); |
| 83 LOG_IF(WARNING, RPC_S_OK != status) << |
| 84 "Failed to wait server listen. RPC_STATUS=0x" << com::LogWe(status); |
| 85 // Unregister RPC interface. |
| 86 status = ::RpcServerUnregisterIf( |
| 87 BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, NULL, FALSE); |
| 88 LOG_IF(WARNING, RPC_S_OK != status) << |
| 89 "Failed to unregister interface. RPC_STATUS=0x" << com::LogWe(status); |
| 90 return RPC_S_OK == status; |
| 91 } |
| 92 |
| 93 bool BrokerRpcServer::is_started() const { |
| 94 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 95 return is_started_; |
| 96 } |
| 97 |
| 98 BrokerContextHandle BrokerRpcServer_Connect(handle_t binding_handle) { |
| 99 // TODO(vitalybuka@google.com): Add client identity check. |
| 100 static long current_context = 0; |
| 101 ceee_module_util::LockModule(); |
| 102 return reinterpret_cast<void*>(InterlockedIncrement(¤t_context)); |
| 103 } |
| 104 |
| 105 void BrokerRpcServer_Disconnect( |
| 106 handle_t binding_handle, |
| 107 BrokerContextHandle* context) { |
| 108 DCHECK(context != NULL); |
| 109 if (context) |
| 110 *context = NULL; |
| 111 ceee_module_util::UnlockModule(); |
| 112 } |
| 113 |
| 114 // Called when client process terminated without releasing context handle. |
| 115 void __RPC_USER BrokerContextHandle_rundown(BrokerContextHandle context) { |
| 116 DCHECK(context != NULL); |
| 117 ceee_module_util::UnlockModule(); |
| 118 } |
| 119 |
| 120 void BrokerRpcServer_FireEvent( |
| 121 handle_t binding_handle, |
| 122 BSTR event_name, |
| 123 BSTR event_args) { |
| 124 DCHECK(ChromePostman::GetInstance()); |
| 125 if (ChromePostman::GetInstance()) |
| 126 ChromePostman::GetInstance()->FireEvent(event_name, event_args); |
| 127 } |
OLD | NEW |