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_SERVER_H
17#define STREAM_SERVER_H
18
19#include <functional>
20#include <list>
21#include <map>
22#include <mutex>
23
24#include "nocopyable.h"
25
26#include "circle_stream_buffer.h"
27#include "i_stream_server.h"
28#include "stream_socket.h"
29
30namespace OHOS {
31namespace Msdp {
32namespace DeviceStatus {
33enum EpollEventType {
34    EPOLL_EVENT_BEGIN = 0,
35    EPOLL_EVENT_INPUT = EPOLL_EVENT_BEGIN,
36    EPOLL_EVENT_SOCKET,
37    EPOLL_EVENT_ETASK,
38    EPOLL_EVENT_TIMER,
39    EPOLL_EVENT_DEVICE_MGR,
40    EPOLL_EVENT_END
41};
42
43using MsgServerFunCallback = std::function<void(SessionPtr, NetPacket&)>;
44class StreamServer : public StreamSocket, public IStreamServer {
45public:
46    StreamServer() = default;
47    DISALLOW_COPY_AND_MOVE(StreamServer);
48    virtual ~StreamServer();
49    void UdsStop();
50    bool SendMsg(int32_t fd, NetPacket &pkt);
51    void Multicast(const std::vector<int32_t> &fdList, NetPacket &pkt);
52    int32_t GetClientFd(int32_t pid) const;
53    int32_t GetClientPid(int32_t fd) const;
54    void AddSessionDeletedCallback(int32_t pid, std::function<void(SessionPtr)> callback);
55    int32_t AddSocketPairInfo(const std::string &programName, int32_t moduleType, int32_t uid, int32_t pid,
56        int32_t &serverFd, int32_t &toReturnClientFd, int32_t &tokenType) override;
57
58    SessionPtr GetSession(int32_t fd) const;
59    SessionPtr GetSessionByPid(int32_t pid) const override;
60
61private:
62    int32_t SetSockOpt(int32_t &serverFd, int32_t &toReturnClientFd, int32_t &tokenType);
63    int32_t CloseFd(int32_t &serverFd, int32_t &toReturnClientFd);
64
65protected:
66    virtual void OnConnected(SessionPtr s) = 0;
67    virtual void OnDisconnected(SessionPtr s) = 0;
68    virtual int32_t AddEpoll(EpollEventType type, int32_t fd) = 0;
69
70    void SetRecvFun(MsgServerFunCallback fun);
71    void ReleaseSession(int32_t fd, epoll_event &ev);
72    void OnPacket(int32_t fd, NetPacket &pkt);
73    void OnEpollRecv(int32_t fd, epoll_event &ev);
74    void OnEpollEvent(epoll_event &ev);
75    bool AddSession(SessionPtr ses);
76    void DelSession(int32_t fd);
77    void DumpSession(const std::string &title);
78    void NotifySessionDeleted(SessionPtr ses);
79
80protected:
81    MsgServerFunCallback recvFun_ { nullptr };
82    std::map<int32_t, SessionPtr> sessionss_;
83    std::map<int32_t, int32_t> idxPids_;
84    std::map<int32_t, CircleStreamBuffer> circleBufs_;
85    std::map<int32_t, std::function<void(SessionPtr)>> callbacks_;
86};
87} // namespace DeviceStatus
88} // namespace Msdp
89} // namespace OHOS
90#endif // STREAM_SERVER_H