Skip to content

Commit

Permalink
partial ok
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Jun 15, 2015
1 parent cb8a2b9 commit 3ea91fe
Show file tree
Hide file tree
Showing 18 changed files with 609 additions and 303 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
$(shell ./build_config 1>&2)
include config.mk
Nothing: default
default clean:
@for d in $(SUBDIRS);\
do \
echo "making $@ in $$d"; \
(cd $$d && make $@ -j2) || exit 1; \
done
cd handy && make $@ || exit 1
195 changes: 161 additions & 34 deletions build_config
Original file line number Diff line number Diff line change
@@ -1,38 +1,165 @@
#!/bin/sh
LIBNAME=handy
OPT="-O0 -g -std=c++11 -Wall -I. -I../$LIBNAME $OPT"
#OPT ?= -O2 -DNDEBUG # (A) Production use (optimized mode)
#OPT ?= -g # (B) Debug mode, w/ full line-level debugging symbols
# OPT ?= -O2 -g2 -DNDEBUG # (C) Profiling mode: opt, but w/debugging symbols
#
# Detects OS we're compiling on and outputs a file specified by the first
# argument, which in turn gets read while processing Makefile.
#
# The output will set the following variables:
# CC C Compiler path
# CXX C++ Compiler path
# PLATFORM_LDFLAGS Linker flags
# PLATFORM_LIBS Libraries flags
# PLATFORM_SHARED_EXT Extension for shared libraries
# PLATFORM_SHARED_LDFLAGS Flags for building shared library
# This flag is embedded just before the name
# of the shared library without intervening spaces
# PLATFORM_SHARED_CFLAGS Flags for compiling objects for shared library
# PLATFORM_CCFLAGS C compiler flags
# PLATFORM_CXXFLAGS C++ compiler flags. Will contain:
# PLATFORM_SHARED_VERSIONED Set to 'true' if platform supports versioned
# shared libraries, empty otherwise.
#

OUTPUT=config.mk
SUBDIRS="handy examples"
#git clone https://github.com/yedf/handy-ut ut
#git clone https://github.com/yedf/gtest.git ut/gtest
[ -e app ] && SUBDIRS="$SUBDIRS app"
pkg-config --cflags protobuf > /dev/null 2>&1 && SUBDIRS="$SUBDIRS protobuf"

if [ -e ut/gtest ]; then
echo "making gtest"
cd ut/gtest/make; make; cd ../../..
SUBDIRS="$SUBDIRS ut"
# Delete existing output, if it exists
rm -f $OUTPUT
touch $OUTPUT

if test -z "$CC"; then
CC=cc
fi

if test -z "$CXX"; then
CXX=g++
fi

echo '#include <openssl/ssl.h>' > /tmp/handy.test.cc
g++ -c /tmp/handy.test.cc -o /tmp/handy.test.o && SUBDIRS="$SUBDIRS ssl"
SSL=$?
[ $SSL = 0 ] && ! [ -e ssl ] && git clone https://github.com/yedf/handy-ssl.git ssl
! [ $SSL = 0 ] && echo "openssl dev not installed, ignore ssl support"

CFLAGS="$CFLAGS $OPT"
CXXFLAGS="$CXXFLAGS $OPT"
LDFLAGS="$LDFLAGS -pthread -L../$LIBNAME -l$LIBNAME"
echo '' > $OUTPUT
echo "CC=cc" >> $OUTPUT
echo "CXX=g++" >> $OUTPUT
echo "CFLAGS=$CFLAGS" >> $OUTPUT
echo "CXXFLAGS=$CXXFLAGS" >> $OUTPUT
echo "LDFLAGS=$LDFLAGS" >> $OUTPUT
echo "SUBDIRS=$SUBDIRS" >> $OUTPUT
echo "LIBNAME=$LIBNAME" >> $OUTPUT
echo "LIBFILE=../$LIBNAME/lib${LIBNAME}.a" >> $OUTPUT
echo "$OUTPUT generated"
if test -z "$TMPDIR"; then
TMPDIR=/tmp
fi

# Detect OS
if test -z "$TARGET_OS"; then
TARGET_OS=`uname -s`
fi

COMMON_FLAGS=
CROSS_COMPILE=
PLATFORM_CCFLAGS=
PLATFORM_CXXFLAGS=
PLATFORM_LDFLAGS=
PLATFORM_LIBS=
PLATFORM_SHARED_EXT="so"
PLATFORM_SHARED_LDFLAGS="-shared -Wl,-soname -Wl,"
PLATFORM_SHARED_CFLAGS="-fPIC"
PLATFORM_SHARED_VERSIONED=true

case "$TARGET_OS" in
CYGWIN_*)
PLATFORM=OS_LINUX
COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
PLATFORM_LDFLAGS="-lpthread"
PORT_FILE=port/port_posix.cc
;;
Darwin)
PLATFORM=OS_MACOSX
COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX -Dthread_local=__thread"
PLATFORM_SHARED_EXT=dylib
[ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
PORT_FILE=port/port_posix.cc
;;
Linux)
PLATFORM=OS_LINUX
COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
PLATFORM_LDFLAGS="-pthread"
PORT_FILE=port/port_posix.cc
;;
SunOS)
PLATFORM=OS_SOLARIS
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
PLATFORM_LIBS="-lpthread -lrt"
PORT_FILE=port/port_posix.cc
;;
FreeBSD)
PLATFORM=OS_FREEBSD
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
PLATFORM_LIBS="-lpthread"
PORT_FILE=port/port_posix.cc
;;
NetBSD)
PLATFORM=OS_NETBSD
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
PLATFORM_LIBS="-lpthread -lgcc_s"
PORT_FILE=port/port_posix.cc
;;
OpenBSD)
PLATFORM=OS_OPENBSD
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
PLATFORM_LDFLAGS="-pthread"
PORT_FILE=port/port_posix.cc
;;
DragonFly)
PLATFORM=OS_DRAGONFLYBSD
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
PLATFORM_LIBS="-lpthread"
PORT_FILE=port/port_posix.cc
;;
OS_ANDROID_CROSSCOMPILE)
PLATFORM=OS_ANDROID
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
PLATFORM_LDFLAGS="" # All pthread features are in the Android C library
PORT_FILE=port/port_posix.cc
CROSS_COMPILE=true
;;
HP-UX)
PLATFORM=OS_HPUX
COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
PLATFORM_LDFLAGS="-pthread"
PORT_FILE=port/port_posix.cc
# man ld: +h internal_name
PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
;;
IOS)
PLATFORM=IOS
COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
[ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
PORT_FILE=port/port_posix.cc
PLATFORM_SHARED_EXT=
PLATFORM_SHARED_LDFLAGS=
PLATFORM_SHARED_CFLAGS=
PLATFORM_SHARED_VERSIONED=
;;
*)
echo "Unknown platform!" >&2
exit 1
esac

$CXX -x c++ - -o $TMPDIR/handy_build_config.out <<EOF
int main(){
unsigned short a = 1;
return *(unsigned char*)&a == 1;
}
EOF
$TMPDIR/handy_build_config.out
PLATFORM_IS_LITTLE_ENDIAN=$?

$CXX -x c++ - -o $TMPDIR/handy_build_config.out >/dev/null 2>&1 <<EOF
#include <sys/epoll.h>
int main(){}
EOF
[ x$? == 0 ] && COMMON_FLAGS="$COMMON_FLAGS -DUSE_EPOLL"

COMMON_FLAGS="$COMMON_FLAGS -DPLATFORM_IS_LITTLE_ENDIAN=$PLATFORM_IS_LITTLE_ENDIAN -std=c++11"
PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"

echo "CC=$CC" >> $OUTPUT
echo "CXX=$CXX" >> $OUTPUT
echo "PLATFORM=$PLATFORM" >> $OUTPUT
echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT
4 changes: 2 additions & 2 deletions handy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $(LIBRARY): $(OBJECTS)
$(AR) -rs $@ $(OBJECTS)

.cc.o:
$(CXX) $(CXXFLAGS) -c $< -o $@
$(CXX) $(PLATFORM_CXXFLAGS) -c $< -o $@

.c.o:
$(CC) $(CFLAGS) -c $< -o $@
$(CC) $(PLATFORM_CFLAGS) -c $< -o $@
1 change: 1 addition & 0 deletions handy/conf.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "conf.h"
#include <algorithm>
#include <memory>
#include <stdlib.h>

using namespace std;

Expand Down
22 changes: 14 additions & 8 deletions handy/conn.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "conn.h"
#include "logging.h"
#include <sys/epoll.h>
#include <poll.h>
#include <fcntl.h>
#include "poller.h"


using namespace std;
Expand All @@ -19,7 +19,7 @@ void TcpConn::attach(EventBase* base, int fd, Ip4Addr local, Ip4Addr peer)
state_ = State::Handshaking;
local_ = local;
peer_ = peer;
channel_ = new Channel(base, fd, EPOLLOUT|EPOLLIN);
channel_ = new Channel(base, fd, kWriteEvent|kReadEvent);
trace("tcp constructed %s - %s fd: %d",
local_.toString().c_str(),
peer_.toString().c_str(),
Expand All @@ -33,8 +33,10 @@ int TcpConn::connect(EventBase* base, const string& host, short port, int timeou
fatalif(state_ != State::Invalid, "you should use a new TcpConn to connect. state: %d", state_);
isClient_ = true;
Ip4Addr addr(host, port);
int fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
int fd = socket(AF_INET, SOCK_STREAM, 0);
net::setNonBlock(fd);
int t = util::addFdFlag(fd, FD_CLOEXEC);
fatalif(t, "addFdFlag FD_CLOEXEC failed");
int r = ::connect(fd, (sockaddr*)&addr.getAddr(), sizeof (sockaddr_in));
if (r != 0 && errno != EINPROGRESS) {
error("connect to %s error %d %s", addr.toString().c_str(), errno, strerror(errno));
Expand Down Expand Up @@ -108,7 +110,7 @@ void TcpConn::handleRead(const TcpConnPtr& con) {
int rd = 0;
if (channel_->fd() >= 0) {
rd = readImp(channel_->fd(), input_.end(), input_.space());
trace("channel %ld fd %d readed %d bytes", channel_->id(), channel_->fd(), rd);
trace("channel %lld fd %d readed %d bytes", channel_->id(), channel_->fd(), rd);
}
if (rd == -1 && errno == EINTR) {
continue;
Expand Down Expand Up @@ -174,7 +176,7 @@ ssize_t TcpConn::isend(const char* buf, size_t len) {
size_t sended = 0;
while (len > sended) {
ssize_t wd = writeImp(channel_->fd(), buf + sended, len - sended);
trace("channel %ld fd %d write %ld bytes", channel_->id(), channel_->fd(), wd);
trace("channel %lld fd %d write %ld bytes", channel_->id(), channel_->fd(), wd);
if (wd > 0) {
sended += wd;
continue;
Expand Down Expand Up @@ -257,23 +259,25 @@ base_(bases->allocBase()),
bases_(bases),
addr_(host, port), createcb_([]{ return TcpConnPtr(new TcpConn); })
{
int fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0);
int fd = socket(AF_INET, SOCK_STREAM, 0);
int r = net::setReuseAddr(fd);
fatalif(r, "set socket reuse option failed");
r = util::addFdFlag(fd, FD_CLOEXEC);
fatalif(r, "addFdFlag FD_CLOEXEC failed");
r = ::bind(fd,(struct sockaddr *)&addr_.getAddr(),sizeof(struct sockaddr));
fatalif(r, "bind to %s failed %d %s", addr_.toString().c_str(), errno, strerror(errno));
r = listen(fd, 20);
fatalif(r, "listen failed %d %s", errno, strerror(errno));
info("fd %d listening at %s", fd, addr_.toString().c_str());
listen_channel_ = new Channel(base_, fd, EPOLLIN);
listen_channel_ = new Channel(base_, fd, kReadEvent);
listen_channel_->onRead([this]{ handleAccept(); });
}

void TcpServer::handleAccept() {
struct sockaddr_in raddr;
socklen_t rsz = sizeof(raddr);
int cfd;
while ((cfd = accept4(listen_channel_->fd(),(struct sockaddr *)&raddr,&rsz, SOCK_CLOEXEC))>=0) {
while ((cfd = accept(listen_channel_->fd(),(struct sockaddr *)&raddr,&rsz))>=0) {
sockaddr_in peer, local;
socklen_t alen = sizeof(peer);
int r = getpeername(cfd, (sockaddr*)&peer, &alen);
Expand All @@ -286,6 +290,8 @@ void TcpServer::handleAccept() {
error("getsockname failed %d %s", errno, strerror(errno));
continue;
}
r = util::addFdFlag(listen_channel_->fd(), FD_CLOEXEC);
fatalif(r, "addFdFlag FD_CLOEXEC failed");
EventBase* b = bases_->allocBase();
auto addcon = [=] {
TcpConnPtr con = createcb_();
Expand Down
Loading

0 comments on commit 3ea91fe

Please sign in to comment.