Skip to content

Commit 8c1e183

Browse files
committed
Revert "Revert "libvncclient: use poll() if available""
This reverts commit 7dd2750.
1 parent 5eb492d commit 8c1e183

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ check_include_file("dirent.h" LIBVNCSERVER_HAVE_DIRENT_H)
183183
check_include_file("endian.h" LIBVNCSERVER_HAVE_ENDIAN_H)
184184
check_include_file("fcntl.h" LIBVNCSERVER_HAVE_FCNTL_H)
185185
check_include_file("netinet/in.h" LIBVNCSERVER_HAVE_NETINET_IN_H)
186+
check_include_file("poll.h" LIBVNCSERVER_HAVE_POLL_H)
186187
check_include_file("sys/endian.h" LIBVNCSERVER_HAVE_SYS_ENDIAN_H)
187188
check_include_file("sys/socket.h" LIBVNCSERVER_HAVE_SYS_SOCKET_H)
188189
check_include_file("sys/stat.h" LIBVNCSERVER_HAVE_SYS_STAT_H)
@@ -218,6 +219,7 @@ check_function_exists(inet_ntoa LIBVNCSERVER_HAVE_INET_NTOA)
218219
check_function_exists(memmove LIBVNCSERVER_HAVE_MEMMOVE)
219220
check_function_exists(memset LIBVNCSERVER_HAVE_MEMSET)
220221
check_function_exists(mkfifo LIBVNCSERVER_HAVE_MKFIFO)
222+
check_function_exists(poll LIBVNCSERVER_HAVE_POLL)
221223
check_function_exists(select LIBVNCSERVER_HAVE_SELECT)
222224
check_function_exists(socket LIBVNCSERVER_HAVE_SOCKET)
223225
check_function_exists(strchr LIBVNCSERVER_HAVE_STRCHR)

include/rfb/rfbconfig.h.cmakein

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
/* Define to 1 if you have the `mkfifo' function. */
4343
#cmakedefine LIBVNCSERVER_HAVE_MKFIFO 1
4444

45+
/* Define to 1 if you have the `poll' function. */
46+
#cmakedefine LIBVNCSERVER_HAVE_POLL 1
47+
4548
/* Define to 1 if you have the `select' function. */
4649
#cmakedefine LIBVNCSERVER_HAVE_SELECT 1
4750

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

90+
/* Define to 1 if you have the <poll.h> header file. */
91+
#cmakedefine LIBVNCSERVER_HAVE_POLL_H 1
92+
8793
/* Define to 1 if you have the <sys/endian.h> header file. */
8894
#cmakedefine LIBVNCSERVER_HAVE_SYS_ENDIAN_H 1
8995

src/common/sockets.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ rfbBool sock_set_nonblocking(rfbSocket sock, rfbBool non_blocking, void (*log)(c
5353

5454
rfbBool sock_wait_for_connected(int socket, unsigned int timeout_seconds)
5555
{
56+
#ifdef LIBVNCSERVER_HAVE_POLL
57+
struct pollfd pfd;
58+
pfd.fd = socket;
59+
pfd.events = POLLIN | POLLPRI;
60+
61+
if (poll(&pfd, 1, timeout_seconds*1000)==1) {
62+
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP))
63+
return FALSE;
64+
#else
5665
fd_set writefds;
5766
fd_set exceptfds;
5867
struct timeval timeout;
@@ -68,7 +77,9 @@ rfbBool sock_wait_for_connected(int socket, unsigned int timeout_seconds)
6877
#ifdef WIN32
6978
if (FD_ISSET(socket, &exceptfds))
7079
return FALSE;
71-
#else
80+
#endif
81+
#endif
82+
#ifndef WIN32
7283
int so_error;
7384
socklen_t len = sizeof so_error;
7485
getsockopt(socket, SOL_SOCKET, SO_ERROR, &so_error, &len);

src/common/sockets.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
#include <netdb.h>
6565
#endif
6666

67+
#include "rfb/rfbconfig.h"
68+
#if LIBVNCSERVER_HAVE_POLL_H
69+
#include <poll.h>
70+
#endif
71+
6772
/*
6873
Common internal socket functions
6974
*/

src/libvncclient/sockets.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ ReadFromRFBServer(rfbClient* client, char *out, unsigned int n)
249249
rfbBool
250250
WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
251251
{
252+
#ifdef LIBVNCSERVER_HAVE_POLL
253+
struct pollfd pfd;
254+
#else
252255
fd_set fds;
256+
#endif
253257
int i = 0;
254258
int j;
255259
const char *obuf = buf;
@@ -296,13 +300,26 @@ WriteToRFBServer(rfbClient* client, const char *buf, unsigned int n)
296300
errno == ENOENT ||
297301
#endif
298302
errno == EAGAIN) {
303+
#ifdef LIBVNCSERVER_HAVE_POLL
304+
struct pollfd pfd;
305+
pfd.fd = client->sock;
306+
pfd.events = POLLOUT;
307+
308+
if (poll(&pfd, 1, -1) <= 0) {
309+
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP)) {
310+
rfbClientErr("poll\n");
311+
return FALSE;
312+
}
313+
}
314+
#else
299315
FD_ZERO(&fds);
300316
FD_SET(client->sock,&fds);
301317

302318
if (select(client->sock+1, NULL, &fds, NULL, NULL) <= 0) {
303319
rfbClientErr("select\n");
304320
return FALSE;
305321
}
322+
#endif
306323
j = 0;
307324
} else {
308325
rfbClientErr("write\n");
@@ -847,21 +864,33 @@ PrintInHex(char *buf, int len)
847864

848865
int WaitForMessage(rfbClient* client,unsigned int usecs)
849866
{
867+
#ifdef LIBVNCSERVER_HAVE_POLL
868+
struct pollfd pfd;
869+
#else
850870
fd_set fds;
851871
struct timeval timeout;
872+
#endif
852873
int num;
853874

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

879+
#ifdef LIBVNCSERVER_HAVE_POLL
880+
pfd.fd = client->sock;
881+
pfd.events = POLLIN | POLLPRI;
882+
num = poll(&pfd, 1, usecs/1000);
883+
if ((pfd.revents & POLLERR) || (pfd.revents & POLLHUP))
884+
return -1;
885+
#else
858886
timeout.tv_sec=(usecs/1000000);
859887
timeout.tv_usec=(usecs%1000000);
860888

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

864892
num=select(client->sock+1, &fds, NULL, NULL, &timeout);
893+
#endif
865894
if(num<0) {
866895
#ifdef WIN32
867896
errno=WSAGetLastError();

0 commit comments

Comments
 (0)