Skip to content

Commit

Permalink
- removed most #ifdef's in order to get clean code; rather use flavor…
Browse files Browse the repository at this point in the history
…- files to

  move conditional compilation logics to Makefile/configure level
  • Loading branch information
stealth committed Mar 8, 2012
1 parent 5551cb5 commit 6f2b2ee
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 126 deletions.
17 changes: 12 additions & 5 deletions Makefile
@@ -1,4 +1,8 @@
CXX=c++ -Wall -O2 -DGETFL_OPTIMIZATION
#
# This is the Makefile for the Linux flavor
#

CXX=c++ -Wall -O2
LD=c++

all: lhttpd frontend
Expand All @@ -9,12 +13,12 @@ clean:
distclean: clean
rm -f lhttpd

lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o -o lhttpd -lrt
lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o -o lhttpd -lrt


frontend: lonely.o socket.o frontend-main.o log.o multicore.o rproxy.o config.o misc.o
$(LD) lonely.o socket.o frontend-main.o misc.o log.o multicore.o rproxy.o config.o -o frontend -lrt
frontend: lonely.o socket.o frontend-main.o log.o multicore.o rproxy.o config.o misc.o flavor.o
$(LD) lonely.o socket.o frontend-main.o misc.o log.o multicore.o rproxy.o config.o flavor.o -o frontend -lrt

frontend-main.o: frontend-main.cc
$(CXX) -c frontend-main.cc
Expand All @@ -37,6 +41,9 @@ misc.o: misc.cc misc.h
main.o: main.cc
$(CXX) -c main.cc

flavor.o: flavor-linux.cc flavor.h
$(CXX) -c flavor-linux.cc -o flavor.o

socket.o: socket.cc socket.h
$(CXX) -c socket.cc

Expand Down
13 changes: 10 additions & 3 deletions Makefile.android
@@ -1,3 +1,7 @@
#
# This is the Makefile for the Android flavor
#

ROOT=/S/SOURCES
NDK=android-ndk-r7

Expand All @@ -8,7 +12,7 @@ INC=-I$(ROOT)/$(NDK)/sources/cxx-stl/gnu-libstdc++/include\
LIB=-Wl,$(ROOT)/$(NDK)/sources/cxx-stl/gnu-libstdc++/libs/armeabi/libgnustl_static.a


CXX=$(PREFIX)c++ $(INC) $(SYSROOT) -Wall -O2 -DGETFL_OPTIMIZATION -DANDROID
CXX=$(PREFIX)c++ $(INC) $(SYSROOT) -Wall -O2 -DANDROID
LD=$(PREFIX)c++ $(SYSROOT)

all: lhttpd
Expand All @@ -19,8 +23,8 @@ clean:
distclean: clean
rm -f lhttpd

lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o $(LIB) -static -o lhttpd
lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o $(LIB) -static -o lhttpd

multicore.o: multicore.cc multicore.h
$(CXX) -c multicore.cc
Expand All @@ -40,6 +44,9 @@ socket.o: socket.cc socket.h
lonely.o: lonely.cc lonely.h
$(CXX) -c lonely.cc

flavor.o: flavor-android.cc flavor.h
$(CXX) -c flavor-android.cc -o flavor.o

config.o: config.cc config.h
$(CXX) -c config.cc

52 changes: 52 additions & 0 deletions Makefile.bsd
@@ -0,0 +1,52 @@
#
# This is the Makefile for the BSD flavor
#

CXX=c++ -Wall -O2
LD=c++

all: lhttpd frontend

clean:
rm -f *.o

distclean: clean
rm -f lhttpd

lhttpd: lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o
$(LD) lonely.o socket.o main.o misc.o log.o multicore.o config.o flavor.o -o lhttpd -lrt


frontend: lonely.o socket.o frontend-main.o log.o multicore.o rproxy.o config.o misc.o flavor.o
$(LD) lonely.o socket.o frontend-main.o misc.o log.o multicore.o rproxy.o config.o flavor.o -o frontend -lrt

frontend-main.o: frontend-main.cc
$(CXX) -c frontend-main.cc

rproxy.o: rproxy.cc rproxy.h
$(CXX) -c rproxy.cc

config.o: config.cc config.h
$(CXX) -c config.cc

multicore.o: multicore.cc multicore.h
$(CXX) -c multicore.cc

log.o: log.cc log.h
$(CXX) -c log.cc

misc.o: misc.cc misc.h
$(CXX) -c misc.cc

main.o: main.cc
$(CXX) -c main.cc

flavor.o: flavor-bsd.cc flavor.h
$(CXX) -c flavor-bsd.cc -o flavor.o

socket.o: socket.cc socket.h
$(CXX) -c socket.cc

lonely.o: lonely.cc lonely.h
$(CXX) -c lonely.cc

74 changes: 74 additions & 0 deletions flavor-android.cc
@@ -0,0 +1,74 @@
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include "flavor.h"

#ifndef BLKGETSIZE64
#define BLKGETSIZE64 _IOR(0x12,114,size_t)
#endif

namespace flavor {

int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags)
{
int afd = 0;

if ((afd = accept(fd, saddr, slen)) < 0)
return -1;
if (flags == NONBLOCK) {
// no error check
fcntl(afd, F_SETFL, O_RDWR|O_NONBLOCK);
}
return afd;
}

bool servable_device(const struct stat &st)
{
return S_ISBLK(st.st_mode);
}


bool servable_file(const struct stat &st)
{
// no S_ISLNK() since stat() was used
return S_ISBLK(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode);
}


int device_size(const std::string &path, size_t &size, char &sendfile)
{
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY);
if (fd < 0)
return -1;

int r = 0, saved_errno = 0;
if (ioctl(fd, BLKGETSIZE64, &size) < 0) {
r = -1;
saved_errno = errno;
}
close(fd);
errno = saved_errno;
return r;
}


int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile)

This comment has been minimized.

Copy link
@anisse

anisse Mar 9, 2012

You could always abuse #include to pull common C code into a separate file. Anyway, it's not a big deal for just one function.

{
// Linux can, unlike BSD, use sendfile() on device files, so
// the last parameter is ignored
ssize_t r = 0;
if ((r = ::sendfile(peer, fd, offset, n)) < 0)
return -1;
left -= r;
copied += r;
return 0;
}

}

96 changes: 96 additions & 0 deletions flavor-bsd.cc
@@ -0,0 +1,96 @@
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include "flavor.h"


namespace flavor {

int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags)
{
int afd = 0;

if ((afd = accept(fd, saddr, slen)) < 0)
return -1;
if (flags == NONBLOCK) {
// no error check
fcntl(afd, F_SETFL, O_RDWR|O_NONBLOCK);
}
return afd;
}


bool servable_device(const struct stat &st)
{
return S_ISCHR(st.st_mode);
}


bool servable_file(const struct stat &st)
{
// no S_ISLNK() since stat() was used
return S_ISCHR(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode);
}


int device_size(const std::string &path, size_t &size, char &sendfile)
{
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY);
if (fd < 0)
return -1;

int r = 0, saved_errno = 0;
if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0) {
r = -1;
saved_errno = errno;
}
close(fd);
errno = saved_errno;

sendfile = 0;
return r;
}


int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile)
{
off_t sbytes = 0;
ssize_t r = 0;

if (can_sendfile) {
r = ::sendfile(fd, peer, *offset, n, NULL, &sbytes, 0);
if (sbytes > 0) {
*offset += sbytes;
left -= sbytes;
copied += sbytes;
}
// On FreeBSD, device files do not support sendfile()
} else {
char buf[n];
r = pread(fd, buf, n, *offset);
if (r > 0) {
// write(), not writen()
r = write(cur_peer, buf, r);
if (r > 0) {
*offset += r;
left -= r;
copied += r;
}
}
if (r <= 0)
r = -1;
else
r = 0;
}
return (int)r;
}


} // namespace flavor


66 changes: 66 additions & 0 deletions flavor-linux.cc
@@ -0,0 +1,66 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <string>
#include <fcntl.h>
#include <errno.h>
#include <sys/sendfile.h>

#include "flavor.h"


namespace flavor {

int accept(int fd, struct sockaddr *saddr, socklen_t *slen, int flags)
{
return accept4(fd, saddr, slen, flags == NONBLOCK ? SOCK_NONBLOCK : 0);
}


bool servable_device(const struct stat &st)
{
return S_ISBLK(st.st_mode);
}


bool servable_file(const struct stat &st)
{
// no S_ISLNK() since stat() was used
return S_ISBLK(st.st_mode) || S_ISREG(st.st_mode) || S_ISDIR(st.st_mode);
}


int device_size(const std::string &path, size_t &size, char &sendfile)
{
int fd = ::open(path.c_str(), O_RDONLY|O_NOCTTY);
if (fd < 0)
return -1;

int r = 0, saved_errno = 0;
if (ioctl(fd, BLKGETSIZE64, &size) < 0) {
r = -1;
saved_errno = errno;
}
close(fd);
errno = saved_errno;
return r;
}


int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile)
{
// Linux can, unlike BSD, use sendfile() on device files, so
// the last parameter is ignored
ssize_t r = 0;
if ((r = ::sendfile(peer, fd, offset, n)) < 0)
return -1;
left -= r;
copied += r;
return 0;
}


} // namespace flavor

30 changes: 30 additions & 0 deletions flavor.h
@@ -0,0 +1,30 @@
#ifndef __flavor__
#define __flavor__

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>

namespace flavor {

enum {
NONBLOCK = 1
};

int accept(int, struct sockaddr *, socklen_t *, int);

bool servable_device(const struct stat &);

bool servable_file(const struct stat &);

int device_size(const std::string &, size_t &, char &);

// calls sendfile() if can_sendfile indicates that. returns 0 on success, -1 on error.
// uses normal read/write if sendfile cannot be used. updates offset, left and copied accordingly
int sendfile(int peer, int fd, off_t *offset, size_t n, size_t &left, size_t &copied, bool can_sendfile);

}

#endif // __flavor_h__

0 comments on commit 6f2b2ee

Please sign in to comment.