1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for bug #200, deadloop when read/write 0 and ETIME. 2.0.16.

This commit is contained in:
winlin 2014-11-13 14:30:56 +08:00
parent 77cf885d8c
commit 0bc35e093c
6 changed files with 42 additions and 26 deletions

View file

@ -82,23 +82,27 @@ int SimpleSocketStream::read(void* buf, size_t size, ssize_t* nread)
{
int ret = ERROR_SUCCESS;
*nread = ::recv(fd, buf, size, 0);
ssize_t nb_read = ::recv(fd, buf, size, 0);
if (nread) {
*nread = nb_read;
}
// On success a non-negative integer indicating the number of bytes actually read is returned
// (a value of 0 means the network connection is closed or end of file is reached).
if (*nread <= 0) {
if (errno == ETIME) {
if (nb_read <= 0) {
if (nb_read < 0 && errno == ETIME) {
return ERROR_SOCKET_TIMEOUT;
}
if (*nread == 0) {
if (nb_read == 0) {
errno = ECONNRESET;
}
return ERROR_SOCKET_READ;
}
recv_bytes += *nread;
recv_bytes += nb_read;
return ret;
}
@ -165,21 +169,24 @@ int SimpleSocketStream::read_fully(void* buf, size_t size, ssize_t* nread)
int ret = ERROR_SUCCESS;
size_t left = size;
*nread = 0;
ssize_t nb_read = 0;
while (left > 0) {
char* this_buf = (char*)buf + *nread;
char* this_buf = (char*)buf + nb_read;
ssize_t this_nread;
if ((ret = this->read(this_buf, left, &this_nread)) != ERROR_SUCCESS) {
return ret;
}
*nread += this_nread;
nb_read += this_nread;
left -= this_nread;
}
recv_bytes += *nread;
if (nread) {
*nread = nb_read;
}
recv_bytes += nb_read;
return ret;
}
@ -188,17 +195,21 @@ int SimpleSocketStream::write(void* buf, size_t size, ssize_t* nwrite)
{
int ret = ERROR_SUCCESS;
*nwrite = ::send(fd, (void*)buf, size, 0);
ssize_t nb_write = ::send(fd, (void*)buf, size, 0);
if (*nwrite <= 0) {
if (errno == ETIME) {
if (nwrite) {
*nwrite = nb_write;
}
if (nb_write <= 0) {
if (nb_write < 0 && errno == ETIME) {
return ERROR_SOCKET_TIMEOUT;
}
return ERROR_SOCKET_WRITE;
}
send_bytes += *nwrite;
send_bytes += nb_write;
return ret;
}