1 /* 2 * Copyright (c) 2021 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 16 #ifndef SOFTBUS_TCP_SOCKET_H 17 #define SOFTBUS_TCP_SOCKET_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 #include <sys/types.h> 22 #include <sys/uio.h> 23 24 #include "softbus_adapter_errcode.h" 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 #ifndef TEMP_FAILURE_RETRY 32 #define TEMP_FAILURE_RETRY(expression) \ 33 ( \ 34 __extension__ \ 35 ( \ 36 { \ 37 long int __result; \ 38 do __result = (long int) (expression); \ 39 while (__result == SOFTBUS_ADAPTER_SOCKET_EINTR); \ 40 __result; \ 41 } \ 42 ) \ 43 ) 44 #endif 45 46 enum { 47 SOFTBUS_SOCKET_OUT, // writable 48 SOFTBUS_SOCKET_IN, // readable 49 SOFTBUS_SOCKET_EXCEPTION, // exception 50 }; 51 52 int32_t OpenTcpServerSocket(const char *ip, int32_t port); 53 int32_t OpenTcpClientSocket(const char *peerIp, const char *myIp, int32_t port, bool isNonBlock); 54 int32_t GetTcpSockPort(int32_t fd); 55 ssize_t SendTcpData(int32_t fd, const char *buf, size_t len, int32_t timeout); 56 ssize_t RecvTcpData(int32_t fd, char *buf, size_t len, int32_t timeout); 57 void CloseTcpFd(int32_t fd); 58 void TcpShutDown(int32_t fd); 59 int32_t ConnSetTcpKeepAlive(int32_t fd, int32_t seconds); 60 int32_t SetIpTos(int fd, uint32_t tos); 61 int32_t ConnToggleNonBlockMode(int32_t fd, bool isNonBlock); 62 int32_t ConnGetSocketErr(int32_t fd); 63 64 #ifdef __cplusplus 65 #if __cplusplus 66 } 67 #endif /* __cplusplus */ 68 #endif /* __cplusplus */ 69 70 #endif // SOFTBUS_TCP_SOCKET_H 71