1 /*
2  * Copyright (c) 2023 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 SOCKET_SESSION_H
17 #define SOCKET_SESSION_H
18 
19 #include "nocopyable.h"
20 
21 #include "i_epoll_event_source.h"
22 #include "i_socket_session.h"
23 
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27 class SocketSession final : public ISocketSession, public IEpollEventSource {
28 public:
29     SocketSession(const std::string &programName, int32_t moduleType,
30                   int32_t tokenType, int32_t fd, int32_t uid, int32_t pid);
31     DISALLOW_COPY_AND_MOVE(SocketSession);
32     ~SocketSession();
33 
34     bool SendMsg(NetPacket &pkt) const override;
35 
36     int32_t GetUid() const override;
37     int32_t GetPid() const override;
38     std::string ToString() const override;
39     std::string GetProgramName() const override;
40     void SetProgramName(const std::string &programName) override;
41 
42     int32_t GetFd() const override;
43     void Dispatch(const struct epoll_event &ev) override;
44 
45 private:
46     bool SendMsg(const char *buf, size_t size) const;
47 
48 private:
49     int32_t fd_ { -1 };
50     int32_t uid_ { -1 };
51     int32_t pid_ { -1 };
52     int32_t tokenType_ { TokenType::TOKEN_INVALID };
53     std::string programName_;
54 };
55 
GetUid()56 inline int32_t SocketSession::GetUid() const
57 {
58     return uid_;
59 }
60 
GetPid()61 inline int32_t SocketSession::GetPid() const
62 {
63     return pid_;
64 }
65 
GetFd()66 inline int32_t SocketSession::GetFd() const
67 {
68     return fd_;
69 }
70 
GetProgramName()71 inline std::string SocketSession::GetProgramName() const
72 {
73     return programName_;
74 }
75 
SetProgramName(const std::string & programName)76 inline void SocketSession::SetProgramName(const std::string &programName)
77 {
78     programName_ = programName;
79 }
80 } // namespace Msdp
81 } // namespace OHOS
82 } // namespace DeviceStatus
83 #endif // SOCKET_SESSION_H