support OS Darwin

pull/48/head
yuqing 2014-08-09 11:25:47 +08:00
parent 3b6c9dc5e8
commit bb4a0730c7
3 changed files with 46 additions and 8 deletions

View File

@ -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, \ int tcprecvdata_nb_ex(int sock, void *data, const int size, \
const int timeout, int *count) 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 left_bytes;
int read_bytes; int read_bytes;
@ -304,7 +310,6 @@ int tcprecvdata_nb_ex(int sock, void *data, const int size, \
if (read_bytes < 0) if (read_bytes < 0)
{ {
if (!(errno == EAGAIN || errno == EWOULDBLOCK)) if (!(errno == EAGAIN || errno == EWOULDBLOCK))
{ {
ret_code = errno != 0 ? errno : EINTR; ret_code = errno != 0 ? errno : EINTR;
@ -324,12 +329,12 @@ int tcprecvdata_nb_ex(int sock, void *data, const int size, \
} }
else else
{ {
t.tv_usec = 0; t.tv_usec = timeout_ms * 1000;
t.tv_sec = timeout; t.tv_sec = timeout_ms / 1000;
res = select(sock+1, &read_set, NULL, NULL, &t); res = select(sock+1, &read_set, NULL, NULL, &t);
} }
#else #else
res = poll(&pollfds, 1, 1000 * timeout); res = poll(&pollfds, 1, timeout_ms);
if (pollfds.revents & POLLHUP) if (pollfds.revents & POLLHUP)
{ {
ret_code = ENOTCONN; ret_code = ENOTCONN;
@ -1104,6 +1109,9 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
off_t offset; off_t offset;
#ifdef OS_LINUX #ifdef OS_LINUX
int64_t remain_bytes; int64_t remain_bytes;
#elif defined(DARWIN)
int64_t remain_bytes;
off_t len;
#endif #endif
#endif #endif
#else #else
@ -1178,7 +1186,21 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
#else #else
#ifdef OS_FREEBSD #ifdef OS_FREEBSD
offset = file_offset; 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; *total_send_bytes = 0;
result = errno != 0 ? errno : EIO; result = errno != 0 ? errno : EIO;
@ -1189,6 +1211,7 @@ int tcpsendfile_ex(int sock, const char *filename, const int64_t file_offset, \
result = 0; result = 0;
} }
#endif #endif
#endif
#endif #endif
if (flags & O_NONBLOCK) //restore if (flags & O_NONBLOCK) //restore

View File

@ -61,13 +61,25 @@ int tcprecvdata_ex(int sock, void *data, const int size, \
* sock: the socket * sock: the socket
* data: the buffer * data: the buffer
* size: buffer size (max bytes can receive) * size: buffer size (max bytes can receive)
* timeout: read timeout * timeout: read timeout in seconds
* count: store the bytes recveived * count: store the bytes recveived
* return: error no, 0 success, != 0 fail * return: error no, 0 success, != 0 fail
*/ */
int tcprecvdata_nb_ex(int sock, void *data, const int size, \ int tcprecvdata_nb_ex(int sock, void *data, const int size, \
const int timeout, int *count); 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) /** send data (block mode)
* parameters: * parameters:
* sock: the socket * sock: the socket

View File

@ -76,8 +76,11 @@ LIBS=''
uname=$(uname) uname=$(uname)
if [ "$uname" = "Linux" ]; then if [ "$uname" = "Linux" ]; then
CFLAGS="$CFLAGS -DOS_LINUX -DIOEVENT_USE_EPOLL" CFLAGS="$CFLAGS -DOS_LINUX -DIOEVENT_USE_EPOLL"
elif [ "$uname" = "FreeBSD" ]; then elif [ "$uname" = "FreeBSD" ] || [ "$uname" = "Darwin" ]; then
CFLAGS="$CFLAGS -DOS_FREEBSD -DIOEVENT_USE_KQUEUE" CFLAGS="$CFLAGS -DOS_FREEBSD -DIOEVENT_USE_KQUEUE"
if [ "$uname" = "Darwin" ]; then
CFLAGS="$CFLAGS -DDARWIN"
fi
elif [ "$uname" = "SunOS" ]; then elif [ "$uname" = "SunOS" ]; then
CFLAGS="$CFLAGS -DOS_SUNOS -D_THREAD_SAFE -DIOEVENT_USE_PORT" CFLAGS="$CFLAGS -DOS_SUNOS -D_THREAD_SAFE -DIOEVENT_USE_PORT"
LIBS="$LIBS -lsocket -lnsl -lresolv" LIBS="$LIBS -lsocket -lnsl -lresolv"
@ -121,7 +124,7 @@ elif [ "$uname" = "FreeBSD" ]; then
fi fi
fi fi
if [ $have_pthread -eq 0 ]; then if [ $have_pthread -eq 0 ] && [ "$uname" != "Darwin" ]; then
/sbin/ldconfig -p | fgrep libpthread.so > /dev/null /sbin/ldconfig -p | fgrep libpthread.so > /dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
LIBS="$LIBS -lpthread" LIBS="$LIBS -lpthread"