diff --git a/common/sockopt.c b/common/sockopt.c index 2a8dc42..3f4aece 100644 --- a/common/sockopt.c +++ b/common/sockopt.c @@ -267,6 +267,12 @@ int tcpsenddata(int sock, void* data, const int size, const int timeout) int tcprecvdata_nb_ex(int sock, void *data, const int size, \ const int timeout, int *count) +{ + return tcprecvdata_nb_ms(sock, data, size, timeout * 1000, count); +} + +int tcprecvdata_nb_ms(int sock, void *data, const int size, \ + const int timeout_ms, int *count) { int left_bytes; int read_bytes; @@ -304,7 +310,6 @@ int tcprecvdata_nb_ex(int sock, void *data, const int size, \ if (read_bytes < 0) { - if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { ret_code = errno != 0 ? errno : EINTR; @@ -324,12 +329,12 @@ int tcprecvdata_nb_ex(int sock, void *data, const int size, \ } else { - t.tv_usec = 0; - t.tv_sec = timeout; + t.tv_usec = timeout_ms * 1000; + t.tv_sec = timeout_ms / 1000; res = select(sock+1, &read_set, NULL, NULL, &t); } #else - res = poll(&pollfds, 1, 1000 * timeout); + res = poll(&pollfds, 1, timeout_ms); if (pollfds.revents & POLLHUP) { ret_code = ENOTCONN; @@ -1104,6 +1109,9 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \ off_t offset; #ifdef OS_LINUX int64_t remain_bytes; + #elif defined(DARWIN) + int64_t remain_bytes; + off_t len; #endif #endif #else @@ -1178,7 +1186,21 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \ #else #ifdef OS_FREEBSD offset = file_offset; - if (sendfile(fd, sock, offset, file_bytes, NULL, NULL, 0) != 0) +#if defined(DARWIN) + result = 0; + remain_bytes = file_bytes; + while (remain_bytes > 0) + { + len = remain_bytes; + if (sendfile(fd, sock, offset, &len, NULL, 0) != 0) { + result = errno != 0 ? errno : EIO; + break; + } + remain_bytes -= len; + } + *total_send_bytes = file_bytes - remain_bytes; +#else + if (sendfile(fd, sock, offset, file_offset, NULL, NULL, 0) != 0) { *total_send_bytes = 0; result = errno != 0 ? errno : EIO; @@ -1189,6 +1211,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \ result = 0; } #endif +#endif #endif if (flags & O_NONBLOCK) //restore diff --git a/common/sockopt.h b/common/sockopt.h index 1c8773c..6019a66 100644 --- a/common/sockopt.h +++ b/common/sockopt.h @@ -61,13 +61,25 @@ int tcprecvdata_ex(int sock, void *data, const int size, \ * sock: the socket * data: the buffer * size: buffer size (max bytes can receive) - * timeout: read timeout + * timeout: read timeout in seconds * count: store the bytes recveived * return: error no, 0 success, != 0 fail */ int tcprecvdata_nb_ex(int sock, void *data, const int size, \ const int timeout, int *count); +/** recv data (non-block mode) in ms + * parameters: + * sock: the socket + * data: the buffer + * size: buffer size (max bytes can receive) + * timeout: read timeout in milliseconds + * count: store the bytes recveived + * return: error no, 0 success, != 0 fail +*/ +int tcprecvdata_nb_ms(int sock, void *data, const int size, \ + const int timeout_ms, int *count); + /** send data (block mode) * parameters: * sock: the socket diff --git a/make.sh b/make.sh index 6b6f11a..a8d30d6 100755 --- a/make.sh +++ b/make.sh @@ -76,8 +76,11 @@ LIBS='' uname=$(uname) if [ "$uname" = "Linux" ]; then CFLAGS="$CFLAGS -DOS_LINUX -DIOEVENT_USE_EPOLL" -elif [ "$uname" = "FreeBSD" ]; then +elif [ "$uname" = "FreeBSD" ] || [ "$uname" = "Darwin" ]; then CFLAGS="$CFLAGS -DOS_FREEBSD -DIOEVENT_USE_KQUEUE" + if [ "$uname" = "Darwin" ]; then + CFLAGS="$CFLAGS -DDARWIN" + fi elif [ "$uname" = "SunOS" ]; then CFLAGS="$CFLAGS -DOS_SUNOS -D_THREAD_SAFE -DIOEVENT_USE_PORT" LIBS="$LIBS -lsocket -lnsl -lresolv" @@ -121,7 +124,7 @@ elif [ "$uname" = "FreeBSD" ]; then fi fi -if [ $have_pthread -eq 0 ]; then +if [ $have_pthread -eq 0 ] && [ "$uname" != "Darwin" ]; then /sbin/ldconfig -p | fgrep libpthread.so > /dev/null if [ $? -eq 0 ]; then LIBS="$LIBS -lpthread"