socket send and recv ignore erno EINTR

pull/48/head
yuqing 2014-08-10 21:54:24 +08:00
parent 56ae38fa16
commit fe04aada88
2 changed files with 54 additions and 20 deletions

View File

@ -7,6 +7,7 @@ Version 5.03 2014-08-10
* bug fixed: can't sync large files cause by v5.02 * bug fixed: can't sync large files cause by v5.02
* use newest files from libfastcommon * use newest files from libfastcommon
* change TRACKER_SYNC_STATUS_FILE_INTERVAL from 3600 to 300 * change TRACKER_SYNC_STATUS_FILE_INTERVAL from 3600 to 300
* socket send and recv ignore erno EINTR
Version 5.02 2014-07-20 Version 5.02 2014-07-20
* corect README spell mistake * corect README spell mistake

View File

@ -165,6 +165,10 @@ int tcprecvdata_ex(int sock, void *data, const int size, \
if (res < 0) if (res < 0)
{ {
if (errno == EINTR)
{
continue;
}
ret_code = errno != 0 ? errno : EINTR; ret_code = errno != 0 ? errno : EINTR;
break; break;
} }
@ -177,6 +181,10 @@ int tcprecvdata_ex(int sock, void *data, const int size, \
read_bytes = recv(sock, p, left_bytes, 0); read_bytes = recv(sock, p, left_bytes, 0);
if (read_bytes < 0) if (read_bytes < 0)
{ {
if (errno == EINTR)
{
continue;
}
ret_code = errno != 0 ? errno : EINTR; ret_code = errno != 0 ? errno : EINTR;
break; break;
} }
@ -244,6 +252,10 @@ int tcpsenddata(int sock, void* data, const int size, const int timeout)
if (result < 0) if (result < 0)
{ {
if (errno == EINTR)
{
continue;
}
return errno != 0 ? errno : EINTR; return errno != 0 ? errno : EINTR;
} }
else if (result == 0) else if (result == 0)
@ -254,6 +266,10 @@ int tcpsenddata(int sock, void* data, const int size, const int timeout)
write_bytes = send(sock, p, left_bytes, 0); write_bytes = send(sock, p, left_bytes, 0);
if (write_bytes < 0) if (write_bytes < 0)
{ {
if (errno == EINTR)
{
continue;
}
return errno != 0 ? errno : EINTR; return errno != 0 ? errno : EINTR;
} }
@ -309,7 +325,7 @@ int tcprecvdata_nb_ms(int sock, void *data, const int size, \
if (read_bytes < 0) if (read_bytes < 0)
{ {
if (!(errno == EAGAIN || errno == EWOULDBLOCK)) if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR))
{ {
ret_code = errno != 0 ? errno : EINTR; ret_code = errno != 0 ? errno : EINTR;
break; break;
@ -343,6 +359,10 @@ int tcprecvdata_nb_ms(int sock, void *data, const int size, \
if (res < 0) if (res < 0)
{ {
if (errno == EINTR)
{
continue;
}
ret_code = errno != 0 ? errno : EINTR; ret_code = errno != 0 ? errno : EINTR;
break; break;
} }
@ -389,7 +409,7 @@ int tcpsenddata_nb(int sock, void* data, const int size, const int timeout)
write_bytes = send(sock, p, left_bytes, 0); write_bytes = send(sock, p, left_bytes, 0);
if (write_bytes < 0) if (write_bytes < 0)
{ {
if (!(errno == EAGAIN || errno == EWOULDBLOCK)) if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR))
{ {
return errno != 0 ? errno : EINTR; return errno != 0 ? errno : EINTR;
} }
@ -422,6 +442,10 @@ int tcpsenddata_nb(int sock, void* data, const int size, const int timeout)
if (result < 0) if (result < 0)
{ {
if (errno == EINTR)
{
continue;
}
return errno != 0 ? errno : EINTR; return errno != 0 ? errno : EINTR;
} }
else if (result == 0) else if (result == 0)
@ -1106,12 +1130,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
#if defined(OS_FREEBSD) || defined(OS_LINUX) #if defined(OS_FREEBSD) || defined(OS_LINUX)
off_t offset; off_t offset;
#ifdef OS_LINUX
int64_t remain_bytes; int64_t remain_bytes;
#elif defined(DARWIN)
int64_t remain_bytes;
off_t len;
#endif
#endif #endif
#else #else
int64_t remain_bytes; int64_t remain_bytes;
@ -1175,13 +1194,16 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
if (send_bytes <= 0) if (send_bytes <= 0)
{ {
result = errno != 0 ? errno : EIO; result = errno != 0 ? errno : EIO;
if (result == EINTR)
{
continue;
}
break; break;
} }
remain_bytes -= send_bytes; remain_bytes -= send_bytes;
} }
*total_send_bytes = file_bytes - remain_bytes;
#else #else
#ifdef OS_FREEBSD #ifdef OS_FREEBSD
offset = file_offset; offset = file_offset;
@ -1190,27 +1212,38 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
remain_bytes = file_bytes; remain_bytes = file_bytes;
while (remain_bytes > 0) while (remain_bytes > 0)
{ {
off_t len;
len = remain_bytes; len = remain_bytes;
if (sendfile(fd, sock, offset, &len, NULL, 0) != 0) { if (sendfile(fd, sock, offset, &len, NULL, 0) != 0) {
result = errno != 0 ? errno : EIO; result = errno != 0 ? errno : EIO;
break; if (result != EINTR)
{
break;
}
} }
remain_bytes -= len; remain_bytes -= len;
} }
*total_send_bytes = file_bytes - remain_bytes;
#else #else
if (sendfile(fd, sock, offset, file_offset, NULL, NULL, 0) != 0) remain_bytes = file_bytes;
{ result = 0;
*total_send_bytes = 0; while (remain_bytes > 0)
result = errno != 0 ? errno : EIO; {
} off_t sbytes;
else sbytes = 0;
{ if (sendfile(fd, sock, offset, remain_bytes, NULL, &sbytes, 0) != 0)
*total_send_bytes = file_bytes; {
result = 0; result = errno != 0 ? errno : EIO;
} if (result != EINTR)
{
break;
}
}
remain_bytes -= fsbytes;
}
#endif #endif
#endif #endif
*total_send_bytes = file_bytes - remain_bytes;
#endif #endif
if (flags & O_NONBLOCK) //restore if (flags & O_NONBLOCK) //restore