1 /*
2  * Copyright (c) 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 IAM_SOFT_SERVER_SOCKET_H
17 #define IAM_SOFT_SERVER_SOCKET_H
18 
19 #include "soft_bus_base_socket.h"
20 
21 #define LOG_TAG "USER_AUTH_SA"
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 class ServerSocket : public BaseSocket {
26 public:
27     ServerSocket(const int32_t socketId);
28     ~ServerSocket() override = default;
29 
30     ResultCode SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
31         const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback) override;
32 
33     void OnBind(int32_t socketId, PeerSocketInfo info) override;
34     void OnShutdown(int32_t socketId, ShutdownReason reason) override;
35     void OnBytes(int32_t socketId, const void *data, uint32_t dataLen) override;
36     void OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount) override;
37 
38     std::string GetConnectionName() override;
39     std::string GetNetworkId() override;
40 
41 private:
42     void AddServerSocket(const int32_t socketId, const std::string &networkId);
43     void DeleteServerSocket(const int32_t socketId);
44 
45     void AddClientConnection(const int32_t socketId, const std::string &connectionName);
46     void DeleteClientConnection(const int32_t socketId);
47 
48     std::string GetNetworkIdBySocketId(int32_t socketId);
49     std::string GetClientConnectionName(const int32_t socketId);
50     int32_t GetSocketIdByClientConnectionName(const std::string &ConnectionName);
51 
52     std::recursive_mutex socketMutex_;
53     /* <socketId, networkId> */
54     std::map<int32_t, std::string> serverSocketBindMap_;
55 
56     std::recursive_mutex connectionMutex_;
57     /* <socketId, connectionName> */
58     std::map<int32_t, std::string> clientConnectionMap_;
59 };
60 } // namespace UserAuth
61 } // namespace UserIam
62 } // namespace OHOS
63 #endif // IAM_SOFT_SERVER_SOCKET_H
64