1 /*
2  * Copyright (c) 2021-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 STREAM_SESSION_H
17 #define STREAM_SESSION_H
18 
19 #include <list>
20 #include <memory>
21 #include <map>
22 
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 
26 #include "nocopyable.h"
27 
28 #include "net_packet.h"
29 #include "proto.h"
30 
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 class StreamSession;
35 using SessionPtr = std::shared_ptr<StreamSession>;
36 class StreamSession : public std::enable_shared_from_this<StreamSession> {
37 public:
38     StreamSession(const std::string &programName, int32_t moduleType, int32_t fd, int32_t uid, int32_t pid);
39     DISALLOW_COPY_AND_MOVE(StreamSession);
40     virtual ~StreamSession() = default;
41 
42     bool SendMsg(const char *buf, size_t size) const;
43     bool SendMsg(NetPacket &pkt) const;
44     void Close();
45 
GetPid()46     int32_t GetPid() const
47     {
48         return pid_;
49     }
50 
GetSharedPtr()51     SessionPtr GetSharedPtr()
52     {
53         return shared_from_this();
54     }
55 
GetFd()56     int32_t GetFd() const
57     {
58         return fd_;
59     }
60 
SetTokenType(int32_t type)61     void SetTokenType(int32_t type)
62     {
63         tokenType_ = type;
64     }
65 
66 protected:
67     int32_t fd_ { -1 };
68     const int32_t pid_ { -1 };
69     int32_t tokenType_ { TokenType::TOKEN_INVALID };
70 };
71 } // namespace DeviceStatus
72 } // namespace Msdp
73 } // namespace OHOS
74 #endif // STREAM_SESSION_H