Jump to content

Non-blocking Socket Read


photo

Recommended Posts

Problem

 

For WIN32 Socket::read() blocks indefinitely in non-blocking mode if no data is available (caused by continuing read loop in case of recv return error WSAEWOULDBLOCK). Instead I would expect Socket::read() to return immediately in this case like LINUX/ANDROID version.

 

utils/Socket.cpp

size_t Socket::read(void *ptr,size_t size,size_t nmemb) const {
....
while(1) {
	while(to_read > 0 && (ret = recv(data->fd,(char*)ptr,(int)to_read,flags)) > 0) {
		ptr = (char*)ptr + ret;
		to_read -= ret;
		read += ret;
	}
	#ifdef _WIN32
		if(ret == SOCKET_ERROR) {
			int error = WSAGetLastError();
			if(error == WSAEWOULDBLOCK) continue;  // THIS causes blocking 
		}
	#endif
	break;
}
return read / size;
}

 

 

Proposal

 

Removal of endless-loop and windows-specific error check

while(1) {
...		}
	#ifdef _WIN32
		if(ret == SOCKET_ERROR) {
			int error = WSAGetLastError();
			if(error == WSAEWOULDBLOCK) continue;  // THIS causes blocking 
		}
	#endif
	break;
}

Link to comment
×
×
  • Create New...