From aaeba28fb81d90150cf6dda2acbd53ae3b8e4c34 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Fri, 4 Dec 2020 10:10:48 +0000 Subject: [PATCH] fix poll --- socket_wrapper.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/socket_wrapper.c b/socket_wrapper.c index 5153bcf..7996ef6 100644 --- a/socket_wrapper.c +++ b/socket_wrapper.c @@ -24,7 +24,7 @@ int getBufferSizeFrom(int sock){ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout) { - ssize_t ret; + ssize_t ret = 0; int poll_ret; struct pollfd fd_single; fd_single.fd = fd; @@ -32,19 +32,15 @@ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout) poll_ret = (poll(&fd_single,1,timeout * 1000)); if (poll_ret < 0) return -1; else if(poll_ret == 0) return -2; - switch (fd_single.revents){ - case POLLHUP: //We'll treat hangups state like timeouts state. + if (fd_single.revents & POLLHUP) //We'll treat hangups state like timeouts state. return -2; - case POLLERR: - case POLLNVAL: + if ((fd_single.revents & POLLERR) || (fd_single.revents & POLLNVAL)) return -1; - case POLL_IN: + if (fd_single.revents & POLL_IN) ret = recv(fd,buf,n,0); assert(ret != 0); return ret; - default: - assert(0 && "unreachable"); - } + assert(0 && "unreachable"); }