1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef SOCKET_SESSION_H
17c29fa5a6Sopenharmony_ci#define SOCKET_SESSION_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include "nocopyable.h"
20c29fa5a6Sopenharmony_ci
21c29fa5a6Sopenharmony_ci#include "i_epoll_event_source.h"
22c29fa5a6Sopenharmony_ci#include "i_socket_session.h"
23c29fa5a6Sopenharmony_ci
24c29fa5a6Sopenharmony_cinamespace OHOS {
25c29fa5a6Sopenharmony_cinamespace Msdp {
26c29fa5a6Sopenharmony_cinamespace DeviceStatus {
27c29fa5a6Sopenharmony_ciclass SocketSession final : public ISocketSession, public IEpollEventSource {
28c29fa5a6Sopenharmony_cipublic:
29c29fa5a6Sopenharmony_ci    SocketSession(const std::string &programName, int32_t moduleType,
30c29fa5a6Sopenharmony_ci                  int32_t tokenType, int32_t fd, int32_t uid, int32_t pid);
31c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(SocketSession);
32c29fa5a6Sopenharmony_ci    ~SocketSession();
33c29fa5a6Sopenharmony_ci
34c29fa5a6Sopenharmony_ci    bool SendMsg(NetPacket &pkt) const override;
35c29fa5a6Sopenharmony_ci
36c29fa5a6Sopenharmony_ci    int32_t GetUid() const override;
37c29fa5a6Sopenharmony_ci    int32_t GetPid() const override;
38c29fa5a6Sopenharmony_ci    std::string ToString() const override;
39c29fa5a6Sopenharmony_ci    std::string GetProgramName() const override;
40c29fa5a6Sopenharmony_ci    void SetProgramName(const std::string &programName) override;
41c29fa5a6Sopenharmony_ci
42c29fa5a6Sopenharmony_ci    int32_t GetFd() const override;
43c29fa5a6Sopenharmony_ci    void Dispatch(const struct epoll_event &ev) override;
44c29fa5a6Sopenharmony_ci
45c29fa5a6Sopenharmony_ciprivate:
46c29fa5a6Sopenharmony_ci    bool SendMsg(const char *buf, size_t size) const;
47c29fa5a6Sopenharmony_ci
48c29fa5a6Sopenharmony_ciprivate:
49c29fa5a6Sopenharmony_ci    int32_t fd_ { -1 };
50c29fa5a6Sopenharmony_ci    int32_t uid_ { -1 };
51c29fa5a6Sopenharmony_ci    int32_t pid_ { -1 };
52c29fa5a6Sopenharmony_ci    int32_t tokenType_ { TokenType::TOKEN_INVALID };
53c29fa5a6Sopenharmony_ci    std::string programName_;
54c29fa5a6Sopenharmony_ci};
55c29fa5a6Sopenharmony_ci
56c29fa5a6Sopenharmony_ciinline int32_t SocketSession::GetUid() const
57c29fa5a6Sopenharmony_ci{
58c29fa5a6Sopenharmony_ci    return uid_;
59c29fa5a6Sopenharmony_ci}
60c29fa5a6Sopenharmony_ci
61c29fa5a6Sopenharmony_ciinline int32_t SocketSession::GetPid() const
62c29fa5a6Sopenharmony_ci{
63c29fa5a6Sopenharmony_ci    return pid_;
64c29fa5a6Sopenharmony_ci}
65c29fa5a6Sopenharmony_ci
66c29fa5a6Sopenharmony_ciinline int32_t SocketSession::GetFd() const
67c29fa5a6Sopenharmony_ci{
68c29fa5a6Sopenharmony_ci    return fd_;
69c29fa5a6Sopenharmony_ci}
70c29fa5a6Sopenharmony_ci
71c29fa5a6Sopenharmony_ciinline std::string SocketSession::GetProgramName() const
72c29fa5a6Sopenharmony_ci{
73c29fa5a6Sopenharmony_ci    return programName_;
74c29fa5a6Sopenharmony_ci}
75c29fa5a6Sopenharmony_ci
76c29fa5a6Sopenharmony_ciinline void SocketSession::SetProgramName(const std::string &programName)
77c29fa5a6Sopenharmony_ci{
78c29fa5a6Sopenharmony_ci    programName_ = programName;
79c29fa5a6Sopenharmony_ci}
80c29fa5a6Sopenharmony_ci} // namespace Msdp
81c29fa5a6Sopenharmony_ci} // namespace OHOS
82c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
83c29fa5a6Sopenharmony_ci#endif // SOCKET_SESSION_H