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 #include "udp_socket.h"
17 #include "common/media_log.h"
18 #include "network/socket/socket_utils.h"
19
20 namespace OHOS {
21 namespace Sharing {
UdpSocket()22 UdpSocket::UdpSocket()
23 {
24 SHARING_LOGD("trace.");
25 }
26
~UdpSocket()27 UdpSocket::~UdpSocket()
28 {
29 SHARING_LOGD("trace.");
30 }
31
Bind(const uint16_t port, const std::string &host, bool enableReuse, uint32_t backlog)32 bool UdpSocket::Bind(const uint16_t port, const std::string &host, bool enableReuse, uint32_t backlog)
33 {
34 SHARING_LOGD("trace.");
35 int32_t fd = -1;
36 if (!SocketUtils::CreateSocket(SOCK_DGRAM, fd)) {
37 SHARING_LOGE("bind CreateSocket Failed!");
38 return false;
39 }
40 socketLocalFd_ = fd;
41
42 if (enableReuse) {
43 SocketUtils::SetReuseAddr(fd, enableReuse);
44 SocketUtils::SetReusePort(fd, enableReuse);
45 }
46
47 SocketUtils::SetNonBlocking(fd);
48 SocketUtils::SetSendBuf(fd);
49 SocketUtils::SetRecvBuf(fd);
50 SocketUtils::SetCloseWait(fd);
51 SocketUtils::SetCloExec(fd, true);
52
53 if (!SocketUtils::BindSocket(fd, host, port)) {
54 SocketUtils::ShutDownSocket(fd);
55 SHARING_LOGE("bind BindSocket Failed!");
56 return false;
57 }
58
59 localIp_ = host;
60 localPort_ = port;
61 socketType_ = SOCKET_TYPE_UDP;
62 socketState_ = SOCKET_STATE_INIT;
63 return true;
64 }
65
Connect(const std::string &peerIp, uint16_t peerPort, int32_t &retCode, bool isAsync, bool enableReuse, const std::string &localIp, uint16_t localPort)66 bool UdpSocket::Connect(const std::string &peerIp, uint16_t peerPort, int32_t &retCode, bool isAsync, bool enableReuse,
67 const std::string &localIp, uint16_t localPort)
68 {
69 SHARING_LOGD("trace.");
70 int32_t fd = -1;
71 if (!SocketUtils::CreateSocket(SOCK_DGRAM, fd)) {
72 return false;
73 }
74 socketLocalFd_ = fd;
75
76 if (enableReuse) {
77 SocketUtils::SetReuseAddr(fd, enableReuse);
78 SocketUtils::SetReusePort(fd, enableReuse);
79 }
80
81 SocketUtils::SetNonBlocking(fd, isAsync);
82 SocketUtils::SetSendBuf(fd);
83 SocketUtils::SetRecvBuf(fd);
84 SocketUtils::SetCloseWait(fd);
85 SocketUtils::SetCloExec(fd, true);
86
87 if (SocketUtils::ConnectSocket(fd, isAsync, peerIp, peerPort, retCode)) {
88 SHARING_LOGD("connect success .fd: %{public}d.", fd);
89 localIp_ = localIp;
90 peerIp_ = peerIp;
91 localPort_ = localPort;
92 peerPort_ = peerPort;
93 socketType_ = SOCKET_TYPE_UDP;
94 socketState_ = SOCKET_STATE_CONNECTED;
95 return true;
96 }
97 SHARING_LOGE("connect failed!");
98 return false;
99 }
100 } // namespace Sharing
101 } // namespace OHOS
102