forked from yedf2/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yedongfu
committed
Aug 2, 2014
0 parents
commit c341a5b
Showing
33 changed files
with
2,525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
handy.* | ||
*.o | ||
*.a | ||
a* | ||
*.mk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Copyright (c) 2014, yedf | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
include platform_config.mk | ||
|
||
default clean: | ||
@for d in $(SUBDIRS);\ | ||
do \ | ||
echo "making $@ in $$d"; \ | ||
(cd $$d && make $@ -j2) || exit 1; \ | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
handy | ||
==== | ||
|
||
a HANDY network C++11 libray on linux. | ||
|
||
reactor 模式 | ||
|
||
支持优雅退出 | ||
|
||
代码简短 | ||
|
||
examples | ||
==== | ||
<pre><code> | ||
#include "handy.h" | ||
#include "daemon.h" | ||
|
||
using namespace std; | ||
using namespace handy; | ||
|
||
|
||
int main(int argc, const char* argv[]) { | ||
EventBase base; | ||
Signal::signal(SIGINT, [&]{ base.exit(); }); | ||
|
||
TcpServer echo(&base, Ip4Addr(99)); | ||
echo.onConnRead( | ||
[](const TcpConnPtr& con) { | ||
con->send(con->getInput()); | ||
} | ||
); | ||
base.loop(); | ||
} | ||
</code></pre> | ||
license | ||
==== | ||
Use of this source code is governed by a BSD-style | ||
license that can be found in the License file. | ||
|
||
==== | ||
dongfuye at 163 dot com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#!/bin/sh | ||
# | ||
# 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. | ||
# | ||
LIBNAME=handy | ||
OPT="-g -std=c++11 -Wall -I. -I../$LIBNAME" | ||
#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 | ||
OUTPUT=platform_config.mk | ||
SRCS=srcs.mk | ||
SUBDIRS="handy examples protobuf" | ||
PREFIX= | ||
#git clone https://github.com/yedf/gtest.git ut/ | ||
if [ -e ut/gtest ]; then | ||
cd ut/gtest/make; make; cd ../../.. | ||
SUBDIRS="$SUBDIRS ut" | ||
fi | ||
|
||
for d in $SUBDIRS; | ||
do | ||
cd $d | ||
SOURCES=`ls *.cc | grep -v pb.cc | tr "\n" " "` | ||
echo "SOURCES=$SOURCES" > $SRCS | ||
cd .. | ||
done | ||
|
||
# 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 | ||
|
||
# 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 | ||
|
||
MEMCMP_FLAG= | ||
if [ "$CXX" = "g++" ]; then | ||
# Use libc's memcmp instead of GCC's memcmp. This results in ~40% | ||
# performance improvement on readrandom under gcc 4.4.3 on Linux/x86. | ||
MEMCMP_FLAG="-fno-builtin-memcmp" | ||
fi | ||
|
||
case "$TARGET_OS" in | ||
Darwin) | ||
PLATFORM=OS_MACOSX | ||
COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX" | ||
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 | ||
;; | ||
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," | ||
;; | ||
*) | ||
echo "Unknown platform!" >&2 | ||
exit 1 | ||
esac | ||
|
||
PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS" | ||
PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS" | ||
|
||
CFLAGS="$CFLAGS $PLATFORM_CCFLAGS $OPT" | ||
CXXFLAGS="$CXXFLAGS $PLATFORM_CXXFLAGS $OPT" | ||
LDFLAGS="$LDFLAGS $PLATFORM_LDFLAGS -L../$LIBNAME -l$LIBNAME" | ||
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 | ||
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
include ../platform_config.mk | ||
include srcs.mk | ||
|
||
PROGRAMS = $(SOURCES:.cc=) | ||
|
||
default: $(PROGRAMS) | ||
|
||
$(PROGRAMS): $(LIBFILE) | ||
|
||
clean: | ||
-rm -f $(PROGRAMS) | ||
-rm -f *.o | ||
|
||
.cc.o: | ||
$(CXX) $(CXXFLAGS) -c $< -o $@ | ||
|
||
.c.o: | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
.cc: | ||
$(CXX) -o $@ $< $(CXXFLAGS) $(LDFLAGS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "handy.h" | ||
#include "daemon.h" | ||
#include <signal.h> | ||
|
||
using namespace std; | ||
using namespace handy; | ||
|
||
|
||
int main(int argc, const char* argv[]) { | ||
if (argc == 1 || strcmp(argv[1], "-h") == 0) { | ||
printf("usage %s <port>\n", argv[0]); | ||
return 1; | ||
} | ||
int port = 99; | ||
if (argc > 1) { | ||
port = atoi(argv[1]); | ||
} | ||
if (argc > 2) { | ||
Logger::getLogger().setLogLevel(argv[2]); | ||
} | ||
|
||
intptr_t userid = 1; | ||
map<intptr_t, TcpConnPtr> users; | ||
string hlp = "<id> <msg>: send msg to <id>\n<msg>: send msg to all\n"; | ||
EventBase base; | ||
Signal::signal(SIGINT, [&]{ base.exit(); }); | ||
|
||
TcpServer chat(&base, Ip4Addr(port)); | ||
chat.onConnState([&](const TcpConnPtr& con) { | ||
if (con->getState() == TcpConn::Connected) { | ||
con->setContext((void*)userid); | ||
con->send(util::format("hello %d\n", userid)); | ||
con->send(hlp); | ||
users[userid] = con; | ||
userid ++; | ||
} else if (con->getState() == TcpConn::Closed) { | ||
intptr_t id = (intptr_t)con->getContext(); | ||
users.erase(id); | ||
} | ||
}); | ||
chat.onConnRead( | ||
[&](const TcpConnPtr& con) { | ||
intptr_t cid = (intptr_t)con->getContext(); | ||
Buffer& buf = con->getInput(); | ||
const char* ln = NULL; | ||
//one line per iteration | ||
while (buf.size() && (ln = strchr(buf.data(), '\n')) != NULL) { | ||
char* p = buf.data(); | ||
if (ln == p) { //empty line | ||
buf.consume(1); | ||
continue; | ||
} | ||
intptr_t id = strtol(p, &p, 10); | ||
auto p1 = users.find(id); | ||
if (p1 == users.end()) { //to all user | ||
for(auto& pc: users) { | ||
pc.second->send(util::format("%ld# %.*s", cid, ln+1-p, p)); | ||
} | ||
} else { //to one user | ||
p1->second->send(util::format("%ld#", (intptr_t)con->getContext())); | ||
p1->second->send(string(p, ln+1-p)); | ||
} | ||
buf.consume(ln - buf.data()+1); | ||
} | ||
} | ||
); | ||
base.loop(); | ||
info("program exited"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "handy.h" | ||
#include "daemon.h" | ||
|
||
using namespace std; | ||
using namespace handy; | ||
|
||
|
||
int main(int argc, const char* argv[]) { | ||
if (argc == 1 || strcmp(argv[1], "-h") == 0) { | ||
printf("usage %s <port>\n", argv[0]); | ||
return 1; | ||
} | ||
int port = 99; | ||
if (argc > 1) { | ||
port = atoi(argv[1]); | ||
} | ||
if (argc > 2) { | ||
Logger::getLogger().setLogLevel(argv[2]); | ||
} | ||
|
||
EventBase base; | ||
Signal::signal(SIGINT, [&]{ base.exit(); }); | ||
|
||
TcpServer echo(&base, Ip4Addr(port)); | ||
echo.onConnRead( | ||
[](const TcpConnPtr& con) { | ||
con->send(con->getInput()); | ||
} | ||
); | ||
base.loop(); | ||
info("program exited"); | ||
} |
Oops, something went wrong.