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 : // Helper functions to wrap RPC invocations.
16 :
17 : #ifndef SYZYGY_TRACE_RPC_RPC_HELPERS_H_
18 : #define SYZYGY_TRACE_RPC_RPC_HELPERS_H_
19 :
20 : #include <rpc.h>
21 : #include <wtypes.h>
22 :
23 : #include "base/string_piece.h"
24 :
25 : // TODO(rogerm): Is there directly usable stuff in base/callback.h that
26 : // might make this simpler/cleaner?
27 :
28 : namespace trace {
29 : namespace client {
30 :
31 : // Create an RPC binding.
32 : //
33 : // @param protocol The RPC protocol to bind.
34 : // @param endpoint The endpoint/address to bind.
35 : // @param out_handle A handle to the rpc binding will be returned here.
36 : //
37 : // @return true on success.
38 : bool CreateRpcBinding(const base::StringPiece16& protocol,
39 : const base::StringPiece16& endpoint,
40 : handle_t* out_handle);
41 :
42 : // Structure returned by RPC calls
43 : struct RpcStatus {
44 : boolean exception_occurred;
45 : boolean result;
46 :
47 E : bool succeeded() const {
48 E : return exception_occurred == FALSE && result == TRUE;
49 E : }
50 : };
51 :
52 : // Helper to invoke an RPC function taking one parameter.
53 : template<typename Func, typename T1>
54 i : RpcStatus InvokeRpc(const Func& func, const T1& p1) {
55 i : RpcStatus status = { FALSE, FALSE };
56 i : RpcTryExcept {
57 i : status.result = func(p1);
58 i : } RpcExcept(1) {
59 i : status.exception_occurred = TRUE;
60 i : } RpcEndExcept;
61 i : return status;
62 i : }
63 :
64 : // Helper to invoke an RPC function taking two parameters.
65 : template<typename Func, typename T1, typename T2>
66 i : RpcStatus InvokeRpc(const Func& func, const T1& p1, const T2& p2) {
67 i : RpcStatus status = { FALSE, FALSE };
68 i : RpcTryExcept {
69 i : status.result = func(p1, p2);
70 i : } RpcExcept(1) {
71 i : status.exception_occurred = TRUE;
72 i : } RpcEndExcept;
73 i : return status;
74 i : }
75 :
76 : // Helper to invoke an RPC function taking three parameters.
77 : template<typename Func, typename T1, typename T2, typename T3>
78 : RpcStatus InvokeRpc(const Func& func,
79 i : const T1& p1, const T2& p2, const T3& p3) {
80 i : RpcStatus status = { FALSE, FALSE };
81 i : RpcTryExcept {
82 i : status.result = func(p1, p2, p3);
83 i : } RpcExcept(1) {
84 i : status.exception_occurred = TRUE;
85 i : } RpcEndExcept;
86 i : return status;
87 i : }
88 :
89 : // Helper to invoke an RPC function taking four parameters.
90 : template<typename Func, typename T1, typename T2, typename T3, typename T4>
91 : RpcStatus InvokeRpc(const Func& func,
92 i : const T1& p1, const T2& p2, const T3& p3, const T4& p4) {
93 i : RpcStatus status = { FALSE, FALSE };
94 i : RpcTryExcept {
95 i : status.result = func(p1, p2, p3, p4);
96 i : } RpcExcept(1) {
97 i : status.exception_occurred = TRUE;
98 i : } RpcEndExcept;
99 i : return status;
100 i : }
101 :
102 : // Helper to invoke an RPC function taking five parameters.
103 : template<typename Func,
104 : typename T1, typename T2, typename T3, typename T4, typename T5>
105 : RpcStatus InvokeRpc(const Func& func,
106 : const T1& p1, const T2& p2, const T3& p3, const T4& p4,
107 : const T5& p5) {
108 : RpcStatus status = { FALSE, FALSE };
109 : RpcTryExcept {
110 : status.result = func(p1, p2, p3, p4, p5);
111 : } RpcExcept(1) {
112 : status.exception_occurred = TRUE;
113 : } RpcEndExcept;
114 : return status;
115 : }
116 :
117 : // A helper function to get an @p instance_id specialized version of the
118 : // given @p root string.
119 : std::wstring GetInstanceString(const base::StringPiece16& root,
120 : const base::StringPiece16& instance_id);
121 :
122 : // A helper class to manage an RPC binding handle.
123 : class ScopedRpcBinding {
124 : public:
125 : ScopedRpcBinding();
126 : ~ScopedRpcBinding();
127 :
128 : // @returns the underlying RPC handle.
129 E : handle_t Get() const { return rpc_binding_; }
130 :
131 : // Opens an RPC connection to @p endpoint using @p protocol.
132 : bool Open(const base::StringPiece16& protocol,
133 : const base::StringPiece16& endpoint);
134 :
135 : // Closes this RPC connection.
136 : bool Close();
137 :
138 : protected:
139 : // The OS level binding to the RPC layer.
140 : handle_t rpc_binding_;
141 :
142 : private:
143 : DISALLOW_COPY_AND_ASSIGN(ScopedRpcBinding);
144 : };
145 :
146 : } // namespace client
147 : } // namespace trace
148 :
149 : #endif // SYZYGY_TRACE_RPC_RPC_HELPERS_H_
|