1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_UDP_SERVER_H 17 #define OHOS_SHARING_UDP_SERVER_H 18 19 #include <shared_mutex> 20 #include "base_server.h" 21 22 namespace OHOS { 23 namespace Sharing { 24 class UdpSocket; 25 class BaseNetworkSession; 26 27 class UdpServerEventListener : public BaseServerEventListener {}; 28 29 class UdpServerEventHandler : public BaseServerEventHandler {}; 30 31 class UdpServer final : public BaseServer { 32 public: 33 UdpServer(); 34 ~UdpServer() override; 35 36 void Stop() override; 37 bool Start(uint16_t port, const std::string &host, bool enableReuse = true, 38 uint32_t maxBackLog_ = MAX_BACKLOG_NUM) override; 39 40 SocketInfo::Ptr GetSocketInfo() override; 41 void CloseClientSocket(int32_t fd) override; 42 43 void OnServerReadable(int32_t fd) override; 44 45 private: 46 bool BindAndConnectClinetFd(int32_t fd, const struct sockaddr_in &addr); 47 std::shared_ptr<BaseNetworkSession> FindOrCreateSession(const struct sockaddr_in &addr); 48 49 private: 50 std::shared_mutex mutex_; 51 std::shared_ptr<UdpSocket> socket_ = nullptr; 52 std::map<int32_t, std::shared_ptr<BaseNetworkSession>> sessionMap_; 53 std::map<std::shared_ptr<struct sockaddr_in>, int32_t> addrToFdMap_; 54 }; 55 } // namespace Sharing 56 } // namespace OHOS 57 #endif