sockets: Replace select() with poll()

select() has been deprecated for a very long time and is considered harmful
libvncclient-integration
Andri Yngvason 2022-06-05 13:54:10 +00:00
parent fe239b3e54
commit 34ad07b267
1 changed files with 66 additions and 75 deletions

View File

@ -28,6 +28,7 @@
#include <fcntl.h>
#include <assert.h>
#include <sys/param.h>
#include <poll.h>
#include "rfb/rfbclient.h"
#include "sockets.h"
#include "tls.h"
@ -94,7 +95,7 @@ rfbBool ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
rfbBool
WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
{
fd_set fds;
struct pollfd fds;
int i = 0;
int j;
const char *obuf = buf;
@ -129,16 +130,16 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
}
#endif /* LIBVNCSERVER_HAVE_SASL */
// TODO: Dispatch events while waiting
while (i < n) {
j = write(client->sock, obuf + i, (n - i));
if (j <= 0) {
if (j < 0) {
if (errno == EWOULDBLOCK ||
errno == EAGAIN) {
FD_ZERO(&fds);
FD_SET(client->sock,&fds);
if (errno == EWOULDBLOCK || errno == EAGAIN) {
fds.fd = client->sock;
fds.events = POLLIN;
if (select(client->sock+1, NULL, &fds, NULL, NULL) <= 0) {
if (poll(&fds, 1, -1) <= 0) {
rfbClientErr("select\n");
return FALSE;
}
@ -157,33 +158,23 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
return TRUE;
}
static rfbBool WaitForConnected(int socket, unsigned int secs)
{
fd_set writefds;
fd_set exceptfds;
struct timeval timeout;
struct pollfd fds = {
.fd = socket,
.events = POLLIN | POLLOUT | POLLERR | POLLHUP,
};
timeout.tv_sec=secs;
timeout.tv_usec=0;
if (poll(&fds, 1, secs * 1000) != 1)
return FALSE;
FD_ZERO(&writefds);
FD_SET(socket, &writefds);
FD_ZERO(&exceptfds);
FD_SET(socket, &exceptfds);
if (select(socket+1, NULL, &writefds, &exceptfds, &timeout)==1) {
int so_error;
socklen_t len = sizeof so_error;
int so_error = 0;
socklen_t len = sizeof(so_error);
getsockopt(socket, SOL_SOCKET, SO_ERROR, &so_error, &len);
if (so_error!=0)
return FALSE;
return TRUE;
}
return FALSE;
return so_error == 0 ? TRUE : FALSE;
}
rfbSocket
ConnectClientToTcpAddr(unsigned int host, int port)
{