1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 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 16 #ifndef CRPC_CLIENT_H 17 #define CRPC_CLIENT_H 18 19 #include <pthread.h> 20 #include "context.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 typedef struct RpcClient RpcClient; 27 28 /* 29 * RPC CLIENT 30 * RPC client sends a request and wait for a response from the server, 31 * and process the callback function initiated by the server. 32 * so we start a thread to get the server's reply message, judge message type, 33 * and to deal reply or callback. 34 * the client may process like this: 35 * 1. thread: poll read ---> server reply msg ---> notify RemoteCall 36 * client: request functions --->RemoteCall ---> Wait Reply ---> Get Return 37 * 2. thread: poll read ---> server callback msg ---> OnTransact 38 * client: OnTransact ---> deal event callback functions 39 */ 40 41 struct RpcClient { 42 Context *context; 43 int threadRunFlag; 44 pthread_t threadId; 45 int waitReply; 46 pthread_mutex_t mutex; 47 pthread_cond_t condW; 48 int callLockFlag; 49 pthread_mutex_t lockMutex; 50 pthread_cond_t lockCond; 51 }; 52 53 /** 54 * @Description Create a Rpc Client object 55 * 56 * @param path - Unix domain communication file 57 * @return RpcClient* - return client pointer or NULL 58 */ 59 RpcClient *CreateRpcClient(const char *path); 60 61 /** 62 * @Description Release RpcClient object 63 * 64 * @param client - RpcClient object's pointer 65 */ 66 void ReleaseRpcClient(RpcClient *client); 67 68 /** 69 * @Description Lock the RPC Client 70 * 71 * @param client - RpcClient object's pointer 72 */ 73 void LockRpcClient(RpcClient *client); 74 75 /** 76 * @Description Unlock the RPC Client 77 * 78 * @param client - RpcClient object's pointer 79 */ 80 void UnlockRpcClient(RpcClient *client); 81 82 /** 83 * @Description Send request message to server and wait response message 84 * 85 * @param client - RpcClient object's pointer 86 * @return int 0 success, other is failed 87 */ 88 int RemoteCall(RpcClient *client); 89 90 /** 91 * @Description End dealing response message 92 * 93 * @param client - RpcClient object's pointer 94 */ 95 void ReadClientEnd(RpcClient *client); 96 97 /** 98 * @Description Deal callback messages. This function must be implemented when the RPC client is implemented 99 * 100 * @param context - pointer to the rpc context 101 * @return int - 0 success; -1 deal message failed 102 */ 103 int OnTransact(Context *context); 104 105 #ifdef __cplusplus 106 } 107 #endif 108 #endif 109