1 : // Copyright 2012 Google Inc.
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 endoint/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 E : RpcStatus InvokeRpc(const Func& func, const T1& p1) {
55 E : RpcStatus status = { FALSE, FALSE };
56 E : RpcTryExcept {
57 E : status.result = func(p1);
58 E : } RpcExcept(1) {
59 i : status.exception_occurred = TRUE;
60 i : } RpcEndExcept;
61 E : return status;
62 E : }
63 :
64 : // Helper to invoke an RPC function taking two parameters.
65 : template<typename Func, typename T1, typename T2>
66 E : RpcStatus InvokeRpc(const Func& func, const T1& p1, const T2& p2) {
67 E : RpcStatus status = { FALSE, FALSE };
68 E : RpcTryExcept {
69 E : status.result = func(p1, p2);
70 E : } RpcExcept(1) {
71 i : status.exception_occurred = TRUE;
72 i : } RpcEndExcept;
73 E : return status;
74 E : }
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 E : const T1& p1, const T2& p2, const T3& p3) {
80 E : RpcStatus status = { FALSE, FALSE };
81 E : RpcTryExcept {
82 E : status.result = func(p1, p2, p3);
83 E : } RpcExcept(1) {
84 i : status.exception_occurred = TRUE;
85 i : } RpcEndExcept;
86 E : return status;
87 E : }
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 E : const T1& p1, const T2& p2, const T3& p3, const T4& p4) {
93 E : RpcStatus status = { FALSE, FALSE };
94 E : RpcTryExcept {
95 E : status.result = func(p1, p2, p3, p4);
96 E : } RpcExcept(1) {
97 E : status.exception_occurred = TRUE;
98 E : } RpcEndExcept;
99 E : return status;
100 E : }
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 : } // namespace trace::client
118 : } // namespace trace
119 :
120 : #endif // SYZYGY_TRACE_RPC_RPC_HELPERS_H_
|