1 /*
2  * Copyright (C) 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 OHOS_RPC_PROCESS_SKELETON_H
17 #define OHOS_RPC_PROCESS_SKELETON_H
18 
19 #include "ipc_skeleton.h"
20 #include "rpc_trans.h"
21 #include "rpc_session_handle.h"
22 #include "utils_list.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef struct {
29     pthread_mutex_t lock;
30     uint64_t stubIndex;
31     char *sessionName;
32     uint64_t seqNumber;
33     TransInterface *rpcTrans;
34     int32_t isServerCreated;
35 } RpcSkeleton;
36 
37 typedef struct {
38     UTILS_DL_LIST list;
39     uint64_t stubIndex;
40     OnRemoteRequest func;
41 } StubObject;
42 
43 typedef struct {
44     UTILS_DL_LIST stubObjects;
45     pthread_mutex_t mutex;
46 } StubObjectList;
47 
48 typedef struct {
49     UTILS_DL_LIST list;
50     pthread_t threadId;
51     uint32_t sessionId;
52     uint32_t packageSize;
53     char *buffer;
54 } ThreadProcessInfo;
55 
56 typedef struct {
57     UTILS_DL_LIST processInfo;
58     pthread_mutex_t mutex;
59 } ThreadProcessInfoList;
60 
61 typedef struct {
62     UTILS_DL_LIST list;
63     pthread_t threadId;
64     pthread_mutex_t mutex;
65     pthread_cond_t condition;
66 } SocketThreadLockInfo;
67 
68 typedef struct {
69     UTILS_DL_LIST socketLockInfo;
70     pthread_mutex_t mutex;
71 } SocketThreadLockInfoList;
72 
73 typedef struct {
74     UTILS_DL_LIST list;
75     pthread_t threadId;
76 } IdleDataThread;
77 
78 typedef struct {
79     UTILS_DL_LIST idleDataThread;
80     pthread_mutex_t mutex;
81 } IdleDataThreadsList;
82 
83 typedef struct {
84     UTILS_DL_LIST list;
85     uint32_t handle;
86     uint32_t sessionId;
87     char *buffer;
88     uint32_t len;
89 } HandleSessionList;
90 
91 typedef struct {
92     UTILS_DL_LIST list;
93     uint32_t handle;
94     uint64_t index;
95 } HandleToIndexList;
96 
97 typedef struct {
98     UTILS_DL_LIST list;
99     pthread_t threadId;
100     uint64_t seqNumber;
101     uint32_t flags;
102     size_t bufferSize;
103     size_t offsetsSize;
104     uintptr_t offsets;
105     uint32_t sessionId;
106     void *buffer;
107 } ThreadMessageInfo;
108 
109 int32_t RpcProcessSkeleton(void);
110 RpcSkeleton *GetCurrentRpcSkeleton(void);
111 int32_t AddStubByIndex(StubObject *stubObject);
112 StubObject *QueryStubByIndex(uint64_t stubIndex);
113 void AddDataThreadInWait(pthread_t threadId);
114 IdleDataThread *GetIdleDataThread(void);
115 void AddDataInfoToThread(ThreadProcessInfo *processInfo);
116 ThreadProcessInfo *PopDataInfoFromThread(pthread_t threadId);
117 int32_t AttachStubSession(HandleSessionList *handleSession);
118 void DetachStubSession(HandleSessionList *handleSession);
119 HandleSessionList *QueryStubSession(uint32_t handle);
120 int32_t AttachProxySession(HandleSessionList *handleSession);
121 void DetachProxySession(HandleSessionList *handleSession);
122 HandleSessionList *QueryProxySession(uint32_t handle);
123 HandleSessionList *QueryProxySessionBySessionId(uint32_t sessionId);
124 uint64_t ProcessGetSeqNumber(void);
125 int32_t AttachHandleToIndex(HandleToIndexList *handleToIndex);
126 void DetachHandleToIndex(HandleToIndexList *handleToIndex);
127 HandleToIndexList *QueryHandleToIndex(uint32_t handle);
128 int32_t AddSendThreadInWait(uint64_t seqNumber, ThreadMessageInfo *messageInfo, uint32_t userWaitTime);
129 void EraseThreadBySeqNumber(ThreadMessageInfo *messageInfo);
130 ThreadMessageInfo *QueryThreadBySeqNumber(uint64_t seqNumber);
131 void WakeUpThreadBySeqNumber(uint64_t seqNumber, uint32_t handle);
132 int32_t RpcOnRemoteRequestInner(uint32_t code, IpcIo *data, IpcIo *reply,
133     MessageOption option, IpcObjectStub *objectStub);
134 void UpdateProtoIfNeed(SvcIdentity *svc);
135 void WakeUpDataThread(pthread_t threadId);
136 uint64_t GetNewStubIndex(void);
137 SessionIdList *RpcGetSessionIdList(void);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 #endif // OHOS_RPC_PROCESS_SKELETON_H