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 "tcp_socket.h"
17 #include "common/media_log.h"
18 #include "network/socket/socket_utils.h"
19
20 namespace OHOS {
21 namespace Sharing {
TcpSocket()22 TcpSocket::TcpSocket()
23 {
24 SHARING_LOGD("trace.");
25 }
26
~TcpSocket()27 TcpSocket::~TcpSocket()
28 {
29 SHARING_LOGD("trace.");
30 }
31
Bind(const uint16_t port,const std::string & host,bool enableReuse,uint32_t backlog)32 bool TcpSocket::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_STREAM, 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::SetCloseWait(fd);
48 SocketUtils::SetCloExec(fd, true);
49
50 if (!SocketUtils::BindSocket(fd, host, port)) {
51 SHARING_LOGE("bind BindSocket Failed!");
52 SocketUtils::ShutDownSocket(fd);
53 return false;
54 }
55
56 if (!SocketUtils::ListenSocket(fd, backlog)) {
57 SHARING_LOGE("bind ListenSocket Failed!");
58 SocketUtils::ShutDownSocket(fd);
59 return false;
60 }
61
62 localIp_ = host;
63 localPort_ = port;
64 socketType_ = SOCKET_TYPE_TCP;
65 SetSocketState(SOCKET_STATE_LISTENING);
66 return true;
67 }
68
Connect(const std::string & peerIp,uint16_t peerPort,int32_t & retCode,bool isAsync,bool enableReuse,const std::string & localIp,uint16_t localPort)69 bool TcpSocket::Connect(const std::string &peerIp, uint16_t peerPort, int32_t &retCode, bool isAsync, bool enableReuse,
70 const std::string &localIp, uint16_t localPort)
71 {
72 SHARING_LOGD("trace.");
73 int32_t fd = -1;
74 if (!SocketUtils::CreateSocket(SOCK_STREAM, fd)) {
75 SHARING_LOGE("create socket failed!");
76 return false;
77 }
78
79 socketLocalFd_ = fd;
80 if (enableReuse) {
81 SocketUtils::SetReuseAddr(fd, enableReuse);
82 SocketUtils::SetReusePort(fd, enableReuse);
83 }
84 SocketUtils::SetNoDelay(fd, true);
85 SocketUtils::SetSendBuf(fd);
86 SocketUtils::SetRecvBuf(fd);
87 SocketUtils::SetCloseWait(fd);
88 SocketUtils::SetCloExec(fd, true);
89 SocketUtils::SetKeepAlive(fd);
90 SocketUtils::SetNonBlocking(fd, isAsync);
91
92 int32_t value = 0xD0;
93 setsockopt(fd, IPPROTO_IP, IP_TOS, &value, sizeof(value));
94
95 if (!SocketUtils::ConnectSocket(fd, isAsync, peerIp, peerPort, retCode)) {
96 SHARING_LOGE("connect socket failed, ip: %{public}s, port: %{public}d.", peerIp.c_str(), peerPort);
97 return false;
98 } else {
99 SHARING_LOGI("connect socket success, ip: %{public}s, port: %{public}d.", peerIp.c_str(), peerPort);
100 }
101
102 localIp_ = localIp;
103 localPort_ = localPort;
104 peerIp_ = peerIp;
105 peerPort_ = peerPort;
106 socketType_ = SOCKET_TYPE_TCP;
107 socketState_ = SOCKET_STATE_CONNECTED;
108 return true;
109 }
110 } // namespace Sharing
111 } // namespace OHOS