• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SOCKET_UTILS_H
17 #define OHOS_SHARING_SOCKET_UTILS_H
18 
19 #include "base_socket.h"
20 #include "utils/data_buffer.h"
21 namespace OHOS {
22 namespace Sharing {
23 constexpr int32_t DEAFULT_READ_BUFFER_SIZE = 1500;
24 constexpr uint32_t SOCKET_DEFAULT_BUF_SIZE = 256 * 1024;
25 constexpr uint32_t READ_BUF_SIZE = 2048;
26 constexpr uint32_t MAX_READ_BUF_SIZE = 102400000;
27 class SocketUtils {
28 public:
29     static bool CreateUdpSession(unsigned port, int32_t &fd);
30     static bool CreateSocket(int32_t socketType, int32_t &fd);
31     static bool CreateTcpServer(const char *ip, unsigned port, int32_t &fd);
32     static bool CreateTcpClient(const char *ip, unsigned port, int32_t &fd, int32_t &ret);
33 
34     static bool CheckAsyncConnect(int32_t fd);
35     static bool ListenSocket(int32_t fd, uint32_t backlog = MAX_BACKLOG_NUM);
36     static bool BindSocket(int32_t fd, const std::string &host, uint16_t port);
37     static bool ConnectSocket(int32_t fd, bool isAsync, const std::string &ip, uint16_t port, int32_t &ret);
38 
39     static void SetKeepAlive(int32_t sockfd);
40     static bool SetReuseAddr(int32_t fd, bool isOn);
41     static bool SetReusePort(int32_t fd, bool isOn);
42     static bool SetCloExec(int32_t fd, bool isOn = true);
43     static bool SetNoDelay(int32_t fd, bool isOn = true);
44     static bool SetCloseWait(int32_t fd, int32_t second = 0);
45     static bool SetSendBuf(int32_t fd, int32_t size = SOCKET_DEFAULT_BUF_SIZE);
46     static bool SetRecvBuf(int32_t fd, int32_t size = SOCKET_DEFAULT_BUF_SIZE);
47     static bool SetNonBlocking(int32_t fd, bool isNonBlock = true, uint32_t write_timeout = 0);
48 
49     static int32_t ReadSocket(int32_t fd, DataBuffer::Ptr buf, int32_t &error);
50     static int32_t ReadSocket(int32_t fd, char *buf, uint32_t len, int32_t &error);
51 
52     static void CloseSocket(int32_t fd);
53     static void ShutDownSocket(int32_t fd);
54     static bool IsUdpPortAvailable(uint16_t port);
55 
56     static int32_t SendSocket(int32_t fd, const char *buf, int32_t len);
57     static int32_t AcceptSocket(int32_t fd, struct sockaddr_in *clientAddr, socklen_t *addrLen);
58     static int32_t RecvSocket(int32_t fd, char *buf, uint32_t len, int32_t flags, int32_t &error);
59     static int32_t Sendto(int32_t fd, const char *buf, size_t len, const char *ip, int32_t nPort);
60 
61     static uint16_t GetAvailableUdpPortPair();
62     static uint16_t GetAvailableUdpPortPair(uint16_t minPort, uint16_t maxPort);
63     static bool GetIpPortInfo(int32_t fd, std::string &strLocalAddr, std::string &strRemoteAddr, uint16_t &localPort,
64                               uint16_t &remotePort);
65 
66 public:
67     static uint16_t minPort_;
68     static uint16_t maxPort_;
69 };
70 } // namespace Sharing
71 } // namespace OHOS
72 #endif