fcc8260e创建于 2025年10月29日历史提交
/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef UDS_SESSION_H
#define UDS_SESSION_H

#include <list>

#include "net_packet.h"

namespace OHOS {
namespace MMI {
class UDSSession;
using SessionPtr = std::shared_ptr<UDSSession>;
class UDSSession : public std::enable_shared_from_this<UDSSession> {
public:
    UDSSession(const std::string &programName, const int32_t moduleType, const int32_t fd, const int32_t uid,
               const int32_t pid);
    DISALLOW_COPY_AND_MOVE(UDSSession);
    virtual ~UDSSession() = default;

    bool SendMsg(const char *buf, size_t size);
    bool SendMsg(NetPacket &pkt);
    void Close();
    struct EventTime {
        int32_t id { 0 };
        int64_t eventTime { 0 };
        int32_t timerId { -1 };
    };

    int32_t GetUid() const
    {
        return uid_;
    }

    int32_t GetPid() const
    {
        return pid_;
    }

    int32_t GetModuleType() const
    {
        return moduleType_;
    }

    SessionPtr GetSharedPtr()
    {
        return shared_from_this();
    }

    int32_t GetFd() const
    {
        return fd_;
    }

    const std::string& GetDescript() const
    {
        return descript_;
    }

    const std::string GetProgramName();

    void SetAnrStatus(int32_t type, bool status)
    {
        isAnrProcess_[type] = status;
    }

    bool CheckAnrStatus(int32_t type)
    {
        return isAnrProcess_[type];
    }

    void SetTokenType(int32_t type)
    {
        tokenType_ = type;
    }

    void SetTokenId(uint32_t tokenId)
    {
        tokenId_ = tokenId;
    }

    void SetIsRealProcessName(bool isRealProcessName)
    {
        isRealProcessName_ = isRealProcessName;
    }

    int32_t GetTokenType() const
    {
        return tokenType_;
    }

    bool IsSocketValid()
    {
        return !invalidSocket_;
    }
    void UpdateDescript();
    void SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId);
    std::vector<int32_t> GetTimerIds(int32_t type);
    std::list<int32_t> DelEvents(int32_t type, int32_t id);
    int64_t GetEarliestEventTime(int32_t type = 0) const;
    bool IsEventQueueEmpty(int32_t type = 0);
    void ReportSocketBufferFull();
    std::vector<EventTime> GetEventsByType(int32_t type) const;

protected:
    std::map<int32_t, std::vector<EventTime>> events_;
    std::map<int32_t, bool> isAnrProcess_;
    std::string descript_;
    std::string programName_;
    const int32_t moduleType_ { -1 };
    int32_t fd_ { -1 };
    const int32_t uid_ { -1 };
    const int32_t pid_ { -1 };
    int32_t tokenType_ { TokenType::TOKEN_INVALID };
    mutable bool invalidSocket_ { false };
    int64_t lastReportTime_ = 0;
    int32_t lastReportedPid_ = 0;
    uint32_t tokenId_ = 0;
    bool isRealProcessName_ = true;
};
} // namespace MMI
} // namespace OHOS
#endif // UDS_SESSION_H