Skip to content

Commit

Permalink
Revert "Revert "libvncclient: use poll() if available""
Browse files Browse the repository at this point in the history
This reverts commit 7dd2750.
  • Loading branch information
tobydox committed Feb 17, 2023
1 parent 5eb492d commit 8c1e183
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ check_include_file("dirent.h" LIBVNCSERVER_HAVE_DIRENT_H)
check_include_file("endian.h" LIBVNCSERVER_HAVE_ENDIAN_H)
check_include_file("fcntl.h" LIBVNCSERVER_HAVE_FCNTL_H)
check_include_file("netinet/in.h" LIBVNCSERVER_HAVE_NETINET_IN_H)
check_include_file("poll.h" LIBVNCSERVER_HAVE_POLL_H)
check_include_file("sys/endian.h" LIBVNCSERVER_HAVE_SYS_ENDIAN_H)
check_include_file("sys/socket.h" LIBVNCSERVER_HAVE_SYS_SOCKET_H)
check_include_file("sys/stat.h" LIBVNCSERVER_HAVE_SYS_STAT_H)
Expand Down Expand Up @@ -218,6 +219,7 @@ check_function_exists(inet_ntoa LIBVNCSERVER_HAVE_INET_NTOA)
check_function_exists(memmove LIBVNCSERVER_HAVE_MEMMOVE)
check_function_exists(memset LIBVNCSERVER_HAVE_MEMSET)
check_function_exists(mkfifo LIBVNCSERVER_HAVE_MKFIFO)
check_function_exists(poll LIBVNCSERVER_HAVE_POLL)
check_function_exists(select LIBVNCSERVER_HAVE_SELECT)
check_function_exists(socket LIBVNCSERVER_HAVE_SOCKET)
check_function_exists(strchr LIBVNCSERVER_HAVE_STRCHR)
Expand Down
6 changes: 6 additions & 0 deletions include/rfb/rfbconfig.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
/* Define to 1 if you have the `mkfifo' function. */
#cmakedefine LIBVNCSERVER_HAVE_MKFIFO 1

/* Define to 1 if you have the `poll' function. */
#cmakedefine LIBVNCSERVER_HAVE_POLL 1

/* Define to 1 if you have the `select' function. */
#cmakedefine LIBVNCSERVER_HAVE_SELECT 1

Expand Down Expand Up @@ -84,6 +87,9 @@
/* Define to 1 if you have the <netinet/in.h> header file. */
#cmakedefine LIBVNCSERVER_HAVE_NETINET_IN_H 1

/* Define to 1 if you have the <poll.h> header file. */
#cmakedefine LIBVNCSERVER_HAVE_POLL_H 1

/* Define to 1 if you have the <sys/endian.h> header file. */
#cmakedefine LIBVNCSERVER_HAVE_SYS_ENDIAN_H 1

Expand Down
13 changes: 12 additions & 1 deletion src/common/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ rfbBool sock_set_nonblocking(rfbSocket sock, rfbBool non_blocking, void (*log)(c

rfbBool sock_wait_for_connected(int socket, unsigned int timeout_seconds)
{
#ifdef LIBVNCSERVER_HAVE_POLL
struct pollfd pfd;
pfd.fd = socket;
pfd.events = POLLIN | POLLPRI;

if (poll(&pfd, 1, timeout_seconds*1000)==1) {
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP))
return FALSE;
#else
fd_set writefds;
fd_set exceptfds;
struct timeval timeout;
Expand All @@ -68,7 +77,9 @@ rfbBool sock_wait_for_connected(int socket, unsigned int timeout_seconds)
#ifdef WIN32
if (FD_ISSET(socket, &exceptfds))
return FALSE;
#else
#endif
#endif
#ifndef WIN32
int so_error;
socklen_t len = sizeof so_error;
getsockopt(socket, SOL_SOCKET, SO_ERROR, &so_error, &len);
Expand Down
5 changes: 5 additions & 0 deletions src/common/sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
#include <netdb.h>
#endif

#include "rfb/rfbconfig.h"
#if LIBVNCSERVER_HAVE_POLL_H
#include <poll.h>
#endif

/*
Common internal socket functions
*/
Expand Down
29 changes: 29 additions & 0 deletions src/libvncclient/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
rfbBool
WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
{
#ifdef LIBVNCSERVER_HAVE_POLL
struct pollfd pfd;
#else
fd_set fds;
#endif
int i = 0;
int j;
const char *obuf = buf;
Expand Down Expand Up @@ -296,13 +300,26 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
errno == ENOENT ||
#endif
errno == EAGAIN) {
#ifdef LIBVNCSERVER_HAVE_POLL
struct pollfd pfd;
pfd.fd = client->sock;
pfd.events = POLLOUT;

if (poll(&pfd, 1, -1) <= 0) {
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP)) {
rfbClientErr("poll\n");
return FALSE;
}
}
#else
FD_ZERO(&fds);
FD_SET(client->sock,&fds);

if (select(client->sock+1, NULL, &fds, NULL, NULL) <= 0) {
rfbClientErr("select\n");
return FALSE;
}
#endif
j = 0;
} else {
rfbClientErr("write\n");
Expand Down Expand Up @@ -847,21 +864,33 @@ PrintInHex(char *buf, int len)

int WaitForMessage(rfbClient* client,unsigned int usecs)
{
#ifdef LIBVNCSERVER_HAVE_POLL
struct pollfd pfd;
#else
fd_set fds;
struct timeval timeout;
#endif
int num;

if (client->serverPort==-1)
/* playing back vncrec file */
return 1;

#ifdef LIBVNCSERVER_HAVE_POLL
pfd.fd = client->sock;
pfd.events = POLLIN | POLLPRI;
num = poll(&pfd, 1, usecs/1000);
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP))
return -1;
#else
timeout.tv_sec=(usecs/1000000);
timeout.tv_usec=(usecs%1000000);

FD_ZERO(&fds);
FD_SET(client->sock,&fds);

num=select(client->sock+1, &fds, NULL, NULL, &timeout);
#endif
if(num<0) {
#ifdef WIN32
errno=WSAGetLastError();
Expand Down

0 comments on commit 8c1e183

Please sign in to comment.