sockets: Replace select() with poll()
select() has been deprecated for a very long time and is considered harmfulpull/12/head
parent
d4faccba28
commit
8970accb86
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue