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