tsocket.c 12.5 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
slguan 已提交
16
#include "os.h"
S
slguan 已提交
17
#include "tulog.h"
H
hzcheng 已提交
18
#include "tsocket.h"
19
#include "taoserror.h"
H
hzcheng 已提交
20

S
TD-1912  
Shengliang Guan 已提交
21
int32_t taosGetFqdn(char *fqdn) {
J
jtao1735 已提交
22 23
  char hostname[1024];
  hostname[1023] = '\0';
24 25 26 27
  if (gethostname(hostname, 1023) == -1) {
    uError("failed to get hostname, reason:%s", strerror(errno));
    return -1;
  }
J
jtao1735 已提交
28

S
TD-1912  
Shengliang Guan 已提交
29
  struct addrinfo  hints = {0};
30 31
  struct addrinfo *result = NULL;
  hints.ai_flags = AI_CANONNAME;
S
TD-1912  
Shengliang Guan 已提交
32
  int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
33
  if (!result) {
H
Haojun Liao 已提交
34
    uError("failed to get fqdn, code:%d, reason:%s", ret, gai_strerror(ret));
35
    return -1;
S
slguan 已提交
36
  }
J
jtao1735 已提交
37

38 39 40
  strcpy(fqdn, result->ai_canonname);
  freeaddrinfo(result);
  return 0;
H
hzcheng 已提交
41 42
}

J
jtao1735 已提交
43
uint32_t taosGetIpFromFqdn(const char *fqdn) {
44
  struct addrinfo hints = {0};
H
Haojun Liao 已提交
45 46 47
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;

48 49
  struct addrinfo *result = NULL;

50
  int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
51
  if (result) {
S
TD-1912  
Shengliang Guan 已提交
52 53 54 55
    struct sockaddr *   sa = result->ai_addr;
    struct sockaddr_in *si = (struct sockaddr_in *)sa;
    struct in_addr      ia = si->sin_addr;
    uint32_t            ip = ia.s_addr;
56 57 58
    freeaddrinfo(result);
    return ip;
  } else {
59
#ifdef EAI_SYSTEM
60 61 62 63
    if (ret == EAI_SYSTEM) {
      uError("failed to get the ip address, fqdn:%s, code:%d, reason:%s", fqdn, ret, strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
    } else {
64
      uError("failed to get the ip address, fqdn:%s, code:%d, reason:%s", fqdn, ret, gai_strerror(ret));
65
    }
66 67 68
#else
    uError("failed to get the ip address, fqdn:%s, code:%d, reason:%s", fqdn, ret, gai_strerror(ret));
#endif
69
    return 0xFFFFFFFF;
70
  }
J
jtao1735 已提交
71 72
}

S
TD-1912  
Shengliang Guan 已提交
73
// Function converting an IP address string to an uint32_t.
S
slguan 已提交
74
uint32_t ip2uint(const char *const ip_addr) {
H
hzcheng 已提交
75 76 77
  char ip_addr_cpy[20];
  char ip[5];

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
78
  tstrncpy(ip_addr_cpy, ip_addr, sizeof(ip_addr_cpy));
H
hzcheng 已提交
79 80 81 82 83

  char *s_start, *s_end;
  s_start = ip_addr_cpy;
  s_end = ip_addr_cpy;

S
TD-1912  
Shengliang Guan 已提交
84
  int32_t k;
H
hzcheng 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97

  for (k = 0; *s_start != '\0'; s_start = s_end) {
    for (s_end = s_start; *s_end != '.' && *s_end != '\0'; s_end++) {
    }
    if (*s_end == '.') {
      *s_end = '\0';
      s_end++;
    }
    ip[k++] = (char)atoi(s_start);
  }

  ip[k] = '\0';

S
TD-1912  
Shengliang Guan 已提交
98
  return *((uint32_t *)ip);
H
hzcheng 已提交
99 100
}

S
TD-1912  
Shengliang Guan 已提交
101 102 103
int32_t taosWriteMsg(SOCKET fd, void *buf, int32_t nbytes) {
  int32_t nleft, nwritten;
  char *  ptr = (char *)buf;
H
hzcheng 已提交
104 105 106 107

  nleft = nbytes;

  while (nleft > 0) {
S
TD-1912  
Shengliang Guan 已提交
108
    nwritten = (int32_t)taosWriteSocket(fd, (char *)ptr, (size_t)nleft);
H
hzcheng 已提交
109
    if (nwritten <= 0) {
S
TD-1924  
Shengliang Guan 已提交
110
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
H
hzcheng 已提交
111 112 113 114 115 116 117 118 119 120 121 122
        continue;
      else
        return -1;
    } else {
      nleft -= nwritten;
      ptr += nwritten;
    }
  }

  return (nbytes - nleft);
}

S
TD-1912  
Shengliang Guan 已提交
123 124 125
int32_t taosReadMsg(SOCKET fd, void *buf, int32_t nbytes) {
  int32_t nleft, nread;
  char *  ptr = (char *)buf;
H
hzcheng 已提交
126 127 128 129 130 131

  nleft = nbytes;

  if (fd < 0) return -1;

  while (nleft > 0) {
S
TD-1912  
Shengliang Guan 已提交
132
    nread = (int32_t)taosReadSocket(fd, ptr, (size_t)nleft);
H
hzcheng 已提交
133 134 135
    if (nread == 0) {
      break;
    } else if (nread < 0) {
S
TD-1924  
Shengliang Guan 已提交
136
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
H
hzcheng 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
        continue;
      } else {
        return -1;
      }
    } else {
      nleft -= nread;
      ptr += nread;
    }
  }

  return (nbytes - nleft);
}

S
TD-1912  
Shengliang Guan 已提交
150
int32_t taosNonblockwrite(SOCKET fd, char *ptr, int32_t nbytes) {
H
hzcheng 已提交
151 152
  taosSetNonblocking(fd, 1);

S
TD-1912  
Shengliang Guan 已提交
153 154
  int32_t nleft, nwritten, nready;
  fd_set  fset;
H
hzcheng 已提交
155 156 157 158 159 160 161 162
  struct timeval tv;

  nleft = nbytes;
  while (nleft > 0) {
    tv.tv_sec = 30;
    tv.tv_usec = 0;
    FD_ZERO(&fset);
    FD_SET(fd, &fset);
S
TD-1912  
Shengliang Guan 已提交
163
    if ((nready = select((int32_t)(fd + 1), NULL, &fset, NULL, &tv)) == 0) {
H
hzcheng 已提交
164
      errno = ETIMEDOUT;
S
slguan 已提交
165
      uError("fd %d timeout, no enough space to write", fd);
H
hzcheng 已提交
166 167 168 169 170
      break;

    } else if (nready < 0) {
      if (errno == EINTR) continue;

S
slguan 已提交
171
      uError("select error, %d (%s)", errno, strerror(errno));
H
hzcheng 已提交
172 173 174
      return -1;
    }

S
TD-1912  
Shengliang Guan 已提交
175
    nwritten = (int32_t)taosSend(fd, ptr, (size_t)nleft, MSG_NOSIGNAL);
H
hzcheng 已提交
176 177 178
    if (nwritten <= 0) {
      if (errno == EAGAIN || errno == EINTR) continue;

S
slguan 已提交
179
      uError("write error, %d (%s)", errno, strerror(errno));
H
hzcheng 已提交
180 181 182 183 184 185 186 187 188 189 190 191
      return -1;
    }

    nleft -= nwritten;
    ptr += nwritten;
  }

  taosSetNonblocking(fd, 0);

  return (nbytes - nleft);
}

S
TD-1912  
Shengliang Guan 已提交
192 193
int32_t taosReadn(SOCKET fd, char *ptr, int32_t nbytes) {
  int32_t nread, nready, nleft = nbytes;
H
hzcheng 已提交
194

S
TD-1912  
Shengliang Guan 已提交
195
  fd_set fset;
H
hzcheng 已提交
196 197 198 199 200 201 202
  struct timeval tv;

  while (nleft > 0) {
    tv.tv_sec = 30;
    tv.tv_usec = 0;
    FD_ZERO(&fset);
    FD_SET(fd, &fset);
S
TD-1912  
Shengliang Guan 已提交
203
    if ((nready = select((int32_t)(fd + 1), NULL, &fset, NULL, &tv)) == 0) {
H
hzcheng 已提交
204
      errno = ETIMEDOUT;
S
slguan 已提交
205
      uError("fd %d timeout\n", fd);
H
hzcheng 已提交
206 207 208
      break;
    } else if (nready < 0) {
      if (errno == EINTR) continue;
S
slguan 已提交
209
      uError("select error, %d (%s)", errno, strerror(errno));
H
hzcheng 已提交
210 211 212
      return -1;
    }

S
TD-1912  
Shengliang Guan 已提交
213
    if ((nread = (int32_t)taosReadSocket(fd, ptr, (size_t)nleft)) < 0) {
H
hzcheng 已提交
214
      if (errno == EINTR) continue;
S
slguan 已提交
215
      uError("read error, %d (%s)", errno, strerror(errno));
H
hzcheng 已提交
216 217 218
      return -1;

    } else if (nread == 0) {
S
slguan 已提交
219
      uError("fd %d EOF", fd);
H
hzcheng 已提交
220 221 222 223 224 225 226 227 228 229
      break;  // EOF
    }

    nleft -= nread;
    ptr += nread;
  }

  return (nbytes - nleft);
}

S
TD-1057  
Shengliang Guan 已提交
230
SOCKET taosOpenUdpSocket(uint32_t ip, uint16_t port) {
H
hzcheng 已提交
231
  struct sockaddr_in localAddr;
S
TD-1912  
Shengliang Guan 已提交
232 233
  SOCKET  sockFd;
  int32_t bufSize = 1024000;
H
hzcheng 已提交
234

235
  uDebug("open udp socket:0x%x:%hu", ip, port);
H
hzcheng 已提交
236 237 238

  memset((char *)&localAddr, 0, sizeof(localAddr));
  localAddr.sin_family = AF_INET;
J
jtao1735 已提交
239
  localAddr.sin_addr.s_addr = ip;
L
lihui 已提交
240
  localAddr.sin_port = (uint16_t)htons(port);
H
hzcheng 已提交
241

S
TD-1912  
Shengliang Guan 已提交
242
  if ((sockFd = (int32_t)socket(AF_INET, SOCK_DGRAM, 0)) <= 2) {
S
slguan 已提交
243
    uError("failed to open udp socket: %d (%s)", errno, strerror(errno));
S
TD-1090  
Shengliang Guan 已提交
244
    taosCloseSocketNoCheck(sockFd);
H
hzcheng 已提交
245 246 247 248
    return -1;
  }

  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_SNDBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
slguan 已提交
249
    uError("failed to set the send buffer size for UDP socket\n");
S
TD-1057  
Shengliang Guan 已提交
250
    taosCloseSocket(sockFd);
H
hzcheng 已提交
251 252 253 254
    return -1;
  }

  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_RCVBUF, (void *)&bufSize, sizeof(bufSize)) != 0) {
S
slguan 已提交
255
    uError("failed to set the receive buffer size for UDP socket\n");
S
TD-1057  
Shengliang Guan 已提交
256
    taosCloseSocket(sockFd);
H
hzcheng 已提交
257 258 259 260 261
    return -1;
  }

  /* bind socket to local address */
  if (bind(sockFd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
262
    uError("failed to bind udp socket: %d (%s), 0x%x:%hu", errno, strerror(errno), ip, port);
S
TD-1057  
Shengliang Guan 已提交
263
    taosCloseSocket(sockFd);
H
hzcheng 已提交
264 265 266 267 268 269
    return -1;
  }

  return sockFd;
}

S
TD-1057  
Shengliang Guan 已提交
270
SOCKET taosOpenTcpClientSocket(uint32_t destIp, uint16_t destPort, uint32_t clientIp) {
S
TD-1912  
Shengliang Guan 已提交
271 272
  SOCKET  sockFd = 0;
  int32_t ret;
H
hzcheng 已提交
273 274
  struct sockaddr_in serverAddr, clientAddr;

S
TD-1057  
Shengliang Guan 已提交
275
  sockFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
H
hzcheng 已提交
276

S
TD-1165  
Shengliang Guan 已提交
277
  if (sockFd <= 2) {
S
slguan 已提交
278
    uError("failed to open the socket: %d (%s)", errno, strerror(errno));
S
TD-1090  
Shengliang Guan 已提交
279
    taosCloseSocketNoCheck(sockFd);
H
hzcheng 已提交
280 281 282
    return -1;
  }

283
  /* set REUSEADDR option, so the portnumber can be re-used */
S
TD-1912  
Shengliang Guan 已提交
284
  int32_t reuse = 1;
285 286
  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
287
    taosCloseSocket(sockFd);
288
    return -1;
S
TD-1165  
Shengliang Guan 已提交
289
  }
290

S
TD-1057  
Shengliang Guan 已提交
291
  if (clientIp != 0) {
H
hzcheng 已提交
292 293
    memset((char *)&clientAddr, 0, sizeof(clientAddr));
    clientAddr.sin_family = AF_INET;
J
jtao1735 已提交
294
    clientAddr.sin_addr.s_addr = clientIp;
H
hzcheng 已提交
295 296 297 298
    clientAddr.sin_port = 0;

    /* bind socket to client address */
    if (bind(sockFd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) {
S
TD-1912  
Shengliang Guan 已提交
299 300
      uError("bind tcp client socket failed, client(0x%x:0), dest(0x%x:%d), reason:(%s)", clientIp, destIp, destPort,
             strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
301
      taosCloseSocket(sockFd);
H
hzcheng 已提交
302 303 304 305 306 307
      return -1;
    }
  }

  memset((char *)&serverAddr, 0, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
J
jtao1735 已提交
308
  serverAddr.sin_addr.s_addr = destIp;
H
hzcheng 已提交
309 310 311 312 313
  serverAddr.sin_port = (uint16_t)htons((uint16_t)destPort);

  ret = connect(sockFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));

  if (ret != 0) {
S
TD-1912  
Shengliang Guan 已提交
314
    // uError("failed to connect socket, ip:0x%x, port:%hu(%s)", destIp, destPort, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
315
    taosCloseSocket(sockFd);
H
hzcheng 已提交
316
    sockFd = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
317 318
  } else {
    taosKeepTcpAlive(sockFd);
H
hzcheng 已提交
319 320
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
321
  return sockFd;
H
hzcheng 已提交
322 323
}

S
TD-1912  
Shengliang Guan 已提交
324 325
int32_t taosKeepTcpAlive(SOCKET sockFd) {
  int32_t alive = 1;
H
hzcheng 已提交
326
  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_KEEPALIVE, (void *)&alive, sizeof(alive)) < 0) {
S
slguan 已提交
327
    uError("fd:%d setsockopt SO_KEEPALIVE failed: %d (%s)", sockFd, errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
328
    taosCloseSocket(sockFd);
H
hzcheng 已提交
329 330 331
    return -1;
  }

S
TD-1912  
Shengliang Guan 已提交
332
  int32_t probes = 3;
H
hzcheng 已提交
333
  if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPCNT, (void *)&probes, sizeof(probes)) < 0) {
S
slguan 已提交
334
    uError("fd:%d setsockopt SO_KEEPCNT failed: %d (%s)", sockFd, errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
335
    taosCloseSocket(sockFd);
H
hzcheng 已提交
336 337 338
    return -1;
  }

S
TD-1912  
Shengliang Guan 已提交
339
  int32_t alivetime = 10;
H
hzcheng 已提交
340
  if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPIDLE, (void *)&alivetime, sizeof(alivetime)) < 0) {
S
slguan 已提交
341
    uError("fd:%d setsockopt SO_KEEPIDLE failed: %d (%s)", sockFd, errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
342
    taosCloseSocket(sockFd);
H
hzcheng 已提交
343 344 345
    return -1;
  }

S
TD-1912  
Shengliang Guan 已提交
346
  int32_t interval = 3;
H
hzcheng 已提交
347
  if (taosSetSockOpt(sockFd, SOL_TCP, TCP_KEEPINTVL, (void *)&interval, sizeof(interval)) < 0) {
S
slguan 已提交
348
    uError("fd:%d setsockopt SO_KEEPINTVL failed: %d (%s)", sockFd, errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
349
    taosCloseSocket(sockFd);
H
hzcheng 已提交
350 351 352
    return -1;
  }

S
TD-1912  
Shengliang Guan 已提交
353
  int32_t nodelay = 1;
H
hzcheng 已提交
354
  if (taosSetSockOpt(sockFd, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay)) < 0) {
S
slguan 已提交
355
    uError("fd:%d setsockopt TCP_NODELAY failed %d (%s)", sockFd, errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
356
    taosCloseSocket(sockFd);
H
hzcheng 已提交
357 358 359
    return -1;
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
360 361
  struct linger linger = {0};
  linger.l_onoff = 1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
362
  linger.l_linger = 3;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
363 364
  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger)) < 0) {
    uError("setsockopt SO_LINGER failed: %d (%s)", errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
365
    taosCloseSocket(sockFd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
366 367 368
    return -1;
  }

H
hzcheng 已提交
369 370 371
  return 0;
}

S
TD-1057  
Shengliang Guan 已提交
372
SOCKET taosOpenTcpServerSocket(uint32_t ip, uint16_t port) {
H
hzcheng 已提交
373
  struct sockaddr_in serverAdd;
S
TD-1912  
Shengliang Guan 已提交
374 375
  SOCKET             sockFd;
  int32_t            reuse;
H
hzcheng 已提交
376

377
  uDebug("open tcp server socket:0x%x:%hu", ip, port);
H
hzcheng 已提交
378 379 380

  bzero((char *)&serverAdd, sizeof(serverAdd));
  serverAdd.sin_family = AF_INET;
J
jtao1735 已提交
381
  serverAdd.sin_addr.s_addr = ip;
L
lihui 已提交
382
  serverAdd.sin_port = (uint16_t)htons(port);
H
hzcheng 已提交
383

S
TD-1912  
Shengliang Guan 已提交
384
  if ((sockFd = (int32_t)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 2) {
S
slguan 已提交
385
    uError("failed to open TCP socket: %d (%s)", errno, strerror(errno));
S
TD-1090  
Shengliang Guan 已提交
386
    taosCloseSocketNoCheck(sockFd);
H
hzcheng 已提交
387 388 389 390 391 392
    return -1;
  }

  /* set REUSEADDR option, so the portnumber can be re-used */
  reuse = 1;
  if (taosSetSockOpt(sockFd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
S
slguan 已提交
393
    uError("setsockopt SO_REUSEADDR failed: %d (%s)", errno, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
394
    taosCloseSocket(sockFd);
H
hzcheng 已提交
395
    return -1;
396
  }
H
hzcheng 已提交
397 398 399

  /* bind socket to server address */
  if (bind(sockFd, (struct sockaddr *)&serverAdd, sizeof(serverAdd)) < 0) {
J
jtao1735 已提交
400
    uError("bind tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
401
    taosCloseSocket(sockFd);
H
hzcheng 已提交
402 403 404
    return -1;
  }

405 406
  if (taosKeepTcpAlive(sockFd) < 0) {
    uError("failed to set tcp server keep-alive option, 0x%x:%hu(%s)", ip, port, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
407
    taosCloseSocket(sockFd);
408 409
    return -1;
  }
H
hzcheng 已提交
410 411

  if (listen(sockFd, 10) < 0) {
J
jtao1735 已提交
412
    uError("listen tcp server socket failed, 0x%x:%hu(%s)", ip, port, strerror(errno));
S
TD-1057  
Shengliang Guan 已提交
413
    taosCloseSocket(sockFd);
H
hzcheng 已提交
414 415 416 417 418 419
    return -1;
  }

  return sockFd;
}

S
TD-1912  
Shengliang Guan 已提交
420
void tinet_ntoa(char *ipstr, uint32_t ip) {
H
hzcheng 已提交
421 422 423 424 425 426
  sprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
}

#define COPY_SIZE 32768
// sendfile shall be used

S
TD-1912  
Shengliang Guan 已提交
427
int32_t taosCopyFds(SOCKET sfd, SOCKET dfd, int64_t len) {
H
hzcheng 已提交
428
  int64_t leftLen;
S
TD-1912  
Shengliang Guan 已提交
429
  int32_t readLen, writeLen;
H
hzcheng 已提交
430 431 432 433 434 435
  char    temp[COPY_SIZE];

  leftLen = len;

  while (leftLen > 0) {
    if (leftLen < COPY_SIZE)
S
TD-1912  
Shengliang Guan 已提交
436
      readLen = (int32_t)leftLen;
H
hzcheng 已提交
437 438 439
    else
      readLen = COPY_SIZE;  // 4K

S
TD-1912  
Shengliang Guan 已提交
440
    int32_t retLen = taosReadMsg(sfd, temp, (int32_t)readLen);
H
hzcheng 已提交
441
    if (readLen != retLen) {
S
TD-1912  
Shengliang Guan 已提交
442 443
      uError("read error, readLen:%d retLen:%d len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", readLen, retLen, len,
             leftLen, strerror(errno));
H
hzcheng 已提交
444 445 446 447 448 449
      return -1;
    }

    writeLen = taosWriteMsg(dfd, temp, readLen);

    if (readLen != writeLen) {
S
TD-1912  
Shengliang Guan 已提交
450 451
      uError("copy error, readLen:%d writeLen:%d len:%" PRId64 " leftLen:%" PRId64 ", reason:%s", readLen, writeLen,
             len, leftLen, strerror(errno));
H
hzcheng 已提交
452 453 454 455 456 457 458 459
      return -1;
    }

    leftLen -= readLen;
  }

  return 0;
}