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