osSocket.h 4.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TDENGINE_OS_SOCKET_H
#define TDENGINE_OS_SOCKET_H

#ifdef __cplusplus
extern "C" {
#endif

S
TD-4088  
Shengliang Guan 已提交
23 24 25 26 27 28 29 30
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  #define taosSend(sockfd, buf, len, flags) send((SOCKET)sockfd, buf, len, flags)
  #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto((SOCKET)sockfd, buf, len, flags, dest_addr, addrlen)
  #define taosWriteSocket(fd, buf, len) send((SOCKET)fd, buf, len, 0)
  #define taosReadSocket(fd, buf, len) recv((SOCKET)fd, buf, len, 0)
  #define taosCloseSocketNoCheck(fd) closesocket((SOCKET)fd)
  #define taosCloseSocket(fd) closesocket((SOCKET)fd)
#else
S
slguan 已提交
31 32 33 34
  #define taosSend(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
  #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, buf, len, flags, dest_addr, addrlen)
  #define taosReadSocket(fd, buf, len) read(fd, buf, len)
  #define taosWriteSocket(fd, buf, len) write(fd, buf, len)
S
TD-1090  
Shengliang Guan 已提交
35
  #define taosCloseSocketNoCheck(x) close(x)
S
slguan 已提交
36 37 38 39 40 41 42
  #define taosCloseSocket(x) \
    {                        \
      if (FD_VALID(x)) {     \
        close(x);            \
        x = FD_INITIALIZER;  \
      }                      \
    }
S
TD-1057  
Shengliang Guan 已提交
43 44
#endif

S
TD-4088  
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  #define TAOS_EPOLL_WAIT_TIME 100
  typedef SOCKET eventfd_t; 
  #define eventfd(a, b) -1
  typedef SOCKET EpollFd; 
  #define EpollClose(pollFd) epoll_close(pollFd) 
  #ifndef EPOLLWAKEUP
    #define EPOLLWAKEUP (1u << 29)
  #endif
#elif defined(_TD_DARWIN_64)
  #define TAOS_EPOLL_WAIT_TIME 500
  typedef int32_t SOCKET;
  typedef SOCKET EpollFd;
  #define EpollClose(pollFd) epoll_close(pollFd)
#else
dengyihao's avatar
dengyihao 已提交
60
  #define TAOS_EPOLL_WAIT_TIME 500 
61 62 63
  typedef int32_t SOCKET;
  typedef SOCKET EpollFd;
  #define EpollClose(pollFd) taosCloseSocket(pollFd)
S
TD-1057  
Shengliang Guan 已提交
64
#endif  
S
Shengliang Guan 已提交
65 66

#ifdef TAOS_RANDOM_NETWORK_FAIL
S
Shengliang Guan 已提交
67
  #ifdef TAOS_RANDOM_NETWORK_FAIL_TEST
S
TD-1912  
Shengliang Guan 已提交
68 69 70 71
    int64_t taosSendRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags);
    int64_t taosSendToRandomFail(int32_t sockfd, const void *buf, size_t len, int32_t flags, const struct sockaddr *dest_addr, socklen_t addrlen);
    int64_t taosReadSocketRandomFail(int32_t fd, void *buf, size_t count);
    int64_t taosWriteSocketRandomFail(int32_t fd, const void *buf, size_t count);
S
Shengliang Guan 已提交
72 73 74 75 76 77 78 79 80
    #undef taosSend
    #undef taosSendto
    #undef taosReadSocket
    #undef taosWriteSocket
    #define taosSend(sockfd, buf, len, flags) taosSendRandomFail(sockfd, buf, len, flags)
    #define taosSendto(sockfd, buf, len, flags, dest_addr, addrlen) taosSendToRandomFail(sockfd, buf, len, flags, dest_addr, addrlen)
    #define taosReadSocket(fd, buf, len) taosReadSocketRandomFail(fd, buf, len)
    #define taosWriteSocket(fd, buf, len) taosWriteSocketRandomFail(fd, buf, len)
  #endif  
S
Shengliang Guan 已提交
81 82
#endif

83
int32_t taosSetNonblocking(SOCKET sock, int32_t on);
S
TD-2524  
Shengliang Guan 已提交
84
void    taosIgnSIGPIPE();
S
TD-1912  
Shengliang Guan 已提交
85
void    taosBlockSIGPIPE();
S
TD-1207  
Shengliang Guan 已提交
86
void    taosSetMaskSIGPIPE();
87
int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen);
Y
yihaoDeng 已提交
88 89
int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t* optlen);

S
TD-1912  
Shengliang Guan 已提交
90
uint32_t    taosInetAddr(char *ipAddr);
S
TD-1057  
Shengliang Guan 已提交
91
const char *taosInetNtoa(struct in_addr ipInt);
92

S
TD-4088  
Shengliang Guan 已提交
93 94 95 96 97 98 99 100 101
#if (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) 
  #define htobe64 htonll
  #if defined(_TD_GO_DLL_)
    uint64_t htonll(uint64_t val);
  #endif
#elif defined(_TD_DARWIN_64)
  #define htobe64 htonll
#endif

S
Shengliang Guan 已提交
102 103 104 105 106
#ifdef __cplusplus
}
#endif

#endif