sendfile tricks

This commit is contained in:
Isaac Connor 2015-02-24 09:20:55 -05:00
parent 1ffb497757
commit 0bea381510
3 changed files with 101 additions and 1 deletions

63
ac_check_sendfile.m4 Normal file
View File

@ -0,0 +1,63 @@
AC_DEFUN([AC_CHECK_SENDFILE],[
AC_MSG_CHECKING([whether sendfile() is supported and what prototype it has])
saved_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Werror-implicit-function-declaration"
ac_sendfile_supported=no
AC_TRY_LINK([#include <sys/sendfile.h>
#include <stdio.h>],
[sendfile(1, 1, NULL, 0);],
[
AC_DEFINE(HAVE_SENDFILE4_SUPPORT, 1,
[Define this if Linux/Solaris sendfile() is supported])
AC_MSG_RESULT([Linux sendfile()])
ac_sendfile_supported=yes
], [])
if test x$ac_sendfile_supported = xno; then
dnl Checking wether we need libsendfile
dnl Presumably on Solaris
AC_CHECK_LIB(sendfile, sendfile,
[
AC_DEFINE(HAVE_SENDFILE4_SUPPORT, 1,
[Define this if Linux/Solaris sendfile() is supported])
SENDFILE_LIBS="-lsendfile"
AC_SUBST(SENDFILE_LIBS)
AC_MSG_RESULT([Solaris sendfile()])
ac_sendfile_supported=yes
], [])
fi
if test x$ac_sendfile_supported = xno; then
dnl Checking wether we have FreeBSD-like sendfile() support.
AC_TRY_LINK([#include <sys/socket.h>
#include <stdio.h>],
[sendfile(1, 1, 0, 0, NULL, NULL, 0);],
[
AC_DEFINE(HAVE_SENDFILE7_SUPPORT, 1,
[Define this if FreeBSD sendfile() is supported])
AC_MSG_RESULT([FreeBSD sendfile()])
ac_sendfile_supported=yes
], [])
fi
if test x$ac_sendfile_supported = xno; then
dnl Checking wether we have MacOS-like sendfile() support.
AC_TRY_LINK([#include <sys/socket.h>
#include <stdio.h>
#include <sys/uio.h>],
[sendfile(1, 1, 0, NULL, NULL, 0);],
[
AC_DEFINE(HAVE_SENDFILE6_SUPPORT, 1,
[Define this if MacOS sendfile() is supported])
AC_MSG_RESULT([MacOS sendfile()])
ac_sendfile_supported=yes
], [])
fi
CFLAGS="$saved_CFLAGS"
if test x$ac_sendfile_supported = xno; then
AC_MSG_RESULT([no sendfile() support, using read/send])
fi
])

View File

@ -36,6 +36,12 @@
#include "zm_event.h" #include "zm_event.h"
#include "zm_monitor.h" #include "zm_monitor.h"
// sendfile tricks
extern "C"
{
#include "zm_sendfile.h"
}
#include "zmf.h" #include "zmf.h"
#if HAVE_SYS_SENDFILE_H #if HAVE_SYS_SENDFILE_H
@ -1309,7 +1315,7 @@ bool EventStream::sendFrame( int delta_us )
if(send_raw) { if(send_raw) {
#if HAVE_SENDFILE #if HAVE_SENDFILE
fprintf( stdout, "Content-Length: %d\r\n\r\n", (int)filestat.st_size ); fprintf( stdout, "Content-Length: %d\r\n\r\n", (int)filestat.st_size );
if(sendfile(fileno(stdout), fileno(fdj), 0, (int)filestat.st_size) != (int)filestat.st_size) { if(zm_sendfile(fileno(stdout), fileno(fdj), 0, (int)filestat.st_size) != (int)filestat.st_size) {
/* sendfile() failed, use standard way instead */ /* sendfile() failed, use standard way instead */
img_buffer_size = fread( img_buffer, 1, sizeof(temp_img_buffer), fdj ); img_buffer_size = fread( img_buffer, 1, sizeof(temp_img_buffer), fdj );
if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) { if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {

31
src/zm_sendfile.h Normal file
View File

@ -0,0 +1,31 @@
#ifdef HAVE_SENDFILE4_SUPPORT
#include <sys/sendfile.h>
int zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
int err;
err = sendfile(out_fd, in_fd, offset, size);
if (err < 0)
return -errno;
return err;
}
#elif HAVE_SENDFILE7_SUPPORT
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
int zm_sendfile(int out_fd, int in_fd, off_t *offset, off_t size) {
int err;
err = sendfile(in_fd, out_fd, *offset, size, NULL, &size, 0);
if (err && errno != EAGAIN)
return -errno;
if (size) {
*offset += size;
return size;
}
return -EAGAIN;
}
#else
#error "Your platform does not support sendfile. Sorry."
#endif