1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares utility functions used by the call trace client and its unit
16 : // tests.
17 :
18 : #include "syzygy/trace/rpc/rpc_helpers.h"
19 :
20 : #include <windows.h>
21 :
22 : #include "base/logging.h"
23 : #include "sawbuck/common/com_utils.h"
24 :
25 : namespace trace {
26 : namespace client {
27 :
28 : bool CreateRpcBinding(const base::StringPiece16& protocol,
29 : const base::StringPiece16& endpoint,
30 E : handle_t* out_handle) {
31 E : DCHECK(!protocol.empty());
32 E : DCHECK(!endpoint.empty());
33 E : DCHECK(out_handle != NULL);
34 :
35 E : std::wstring protocol_temp(protocol.begin(), protocol.end());
36 E : std::wstring endpoint_temp(endpoint.begin(), endpoint.end());
37 E : RPC_WSTR string_binding = NULL;
38 :
39 : RPC_STATUS status = ::RpcStringBindingCompose(
40 : NULL, // UUID.
41 : reinterpret_cast<RPC_WSTR>(&protocol_temp[0]),
42 : NULL, // Address.
43 : reinterpret_cast<RPC_WSTR>(&endpoint_temp[0]),
44 : NULL, // Options.
45 E : &string_binding);
46 E : if (status != RPC_S_OK) {
47 i : LOG(ERROR) << "Can't compose RPC binding: " << com::LogWe(status) << ".";
48 i : return false;
49 : }
50 :
51 E : handle_t binding = NULL;
52 E : status = ::RpcBindingFromStringBinding(string_binding, &binding);
53 :
54 E : ignore_result(::RpcStringFree(&string_binding));
55 :
56 E : if (status != RPC_S_OK) {
57 i : LOG(ERROR) << "Can't create RPC binding: " << com::LogWe(status) << ".";
58 i : return false;
59 : }
60 :
61 E : *out_handle = binding;
62 E : return true;
63 E : }
64 :
65 : std::wstring GetInstanceString(
66 E : const base::StringPiece16& root, const base::StringPiece16& instance_id) {
67 E : std::wstring result(root.begin(), root.end());
68 E : if (!instance_id.empty()) {
69 E : result += L'-';
70 E : result.append(instance_id.begin(), instance_id.end());
71 : }
72 :
73 E : return result;
74 E : }
75 :
76 E : ScopedRpcBinding::ScopedRpcBinding() : rpc_binding_(NULL) {
77 E : }
78 :
79 E : ScopedRpcBinding::~ScopedRpcBinding() {
80 E : Close();
81 E : }
82 :
83 : bool ScopedRpcBinding::Open(const base::StringPiece16& protocol,
84 E : const base::StringPiece16& endpoint) {
85 E : if (!CreateRpcBinding(protocol, endpoint, &rpc_binding_)) {
86 i : DCHECK(rpc_binding_ == NULL);
87 i : return false;
88 : }
89 :
90 E : return true;
91 E : }
92 :
93 E : bool ScopedRpcBinding::Close() {
94 E : if (rpc_binding_ == NULL)
95 E : return true;
96 :
97 E : RPC_STATUS status = ::RpcBindingFree(&rpc_binding_);
98 E : rpc_binding_ = NULL;
99 E : if (status != RPC_S_OK)
100 i : return false;
101 :
102 E : return true;
103 E : }
104 :
105 : } // namespace client
106 : } // namespace trace
|