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