1 /*
2  * Copyright (c) 2021-2024 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 SESSION_POOL_H
17 #define SESSION_POOL_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <map>
24 
25 #include "network/base_session.h"
26 #include "network/kernel_talker.h"
27 
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 class SessionPool final : protected NoCopyable {
32 public:
SessionPool(std::shared_ptr<KernelTalker> &talker)33     explicit SessionPool(std::shared_ptr<KernelTalker> &talker) : talker_(talker) {}
34     ~SessionPool() = default;
35     void OccupySession(int32_t sessionId, uint8_t linkType);
36     bool FindSession(int32_t sessionId);
37     void HoldSession(std::shared_ptr<BaseSession> session, const std::string backStage);
38     uint8_t ReleaseSession(const int32_t fd);
39     void ReleaseSession(const std::string &cid, const uint8_t linkType);
40     void ReleaseAllSession();
41     bool DeviceConnectCountOnly(std::shared_ptr<BaseSession> session);
42     bool DeviceDisconnectCountOnly(const std::string &cid, const uint8_t linkType, bool needErase);
43 
44 private:
45     std::recursive_mutex sessionPoolLock_;
46     std::list<std::shared_ptr<BaseSession>> usrSpaceSessionPool_;
47     std::shared_ptr<KernelTalker> &talker_;
48     std::map<int32_t, uint8_t> occupySession_;
49     std::unordered_map<std::string, int32_t> deviceConnectCount_;
50 
51     void AddSessionToPool(std::shared_ptr<BaseSession> session);
52 };
53 } // namespace DistributedFile
54 } // namespace Storage
55 } // namespace OHOS
56 #endif // SESSION_POOL_H