Coconut is C++ network framework with briefly, clearly, high performance.
- Using libevent (available for switching another network library)
- Available cross platform linux, windows (by using libevent)
- Easy multithread service
- Tcp Socket
- Unix Domain Socket(only tcp)
- Udp Socket
- Http Client (GET, POST)
- Simple Http Server
- Redis Request (optional)
- JSON Stream Protocol (optional, now unstable)
- fix -without-json option in configure.ac
- fix boost include path
total 26 lines. (It's not iocp sample.)
#include "Coconut.h"
using namespace coconut;
class FooSession : public BinaryController {
virtual void onReceivedData( const void *data, int size ) {
socket()->write(data, size);
}
};
class FooServer : public ServerController {
virtual boost::shared_ptr<ClientController> onAccept( boost::shared_ptr<TcpSocket> socket ) {
boost::shared_ptr<FooSession> newSession( new FooSession );
return newSession;
}
};
int main( int argc, char **argv ) {
IOServiceContainer container( 16 /* thread count */ );
container.initialize();
try {
boost::shared_ptr<FooServer> fooServer( new FooServer );
NetworkHelper::listenTcp( &container, 8000, fooServer );
container.run();
} catch(Exception &e) {
// Error
}
return 0;
}
Environment
-
Server : single thread, Linux, Intel(R) Xeon(R) CPU L5520 @ 2.27GHz 16 cpu core
-
Client : 16 thread, 1000 clients, 10000 count per a client with 256 byte, Linux, Intel(R) Xeon(R) CPU L5520 @ 2.27GHz 12 cpu core
-
All server sample codes are here : https://github.com/gunoodaddy/coconut/tree/master/test/echotest
-
client code is here : https://github.com/gunoodaddy/coconut/blob/master/examples/tcpclient.cpp
- If you use Win32 IOCP, you must use this libevent.
- Original libevent >= 2.0.15 has a bug in
evutil_tv_to_msec
function. (need to add tv pointer null check code) - And we've added
free callback feature
for struct evhttp_request to know which evhttp_request pointer will be freed. It needs if you should store http request and then later send responses. (like 'Commet'). You can see our fixed codes by findinggunoodaddy
comment in this libevent sources. - By running
configure
, it will check ourfree callback feature
exists in your own libevent and then markHAVE_LIBEVENT_GUNOODADDY_FIX
in config.h.
- You must build boost library (run b2), we need boost stage library for using boost::thread
https://github.com/antirez/hiredis
- On Win32, you must use this hiredis instead of orginal hiredis so that the build is successful.
- If you do not want to use redis, run configure
--without-redis
.
- In order to build successful, you must use
3rdParty/src/libjson
instead upper link's libjson. - We modified JSONStream class to parse correctly. (7.4.1 libjson has a few bug in JSONStream)
- You can find 'gunoodaddy' comment in
3rdParty/src/libjson
modified codes. - And if you use linux 64bit os, must edit <lib_json_root>/makefile to add
-fPIC
to cxxflags_default. - If you do not want to use json feature, run configure
--without-json
.
export BOOST_ROOT=<BOOST ROOT DIR>
export LD_LIBRARY_PATH=<BOOST STAGE LIB DIR>:$LD_LIBRARY_PATH
configure
(configure --with-boost-libdir={BOOST_HOME}/stage/lib)
make
cd test
unittest
visual studio >= 2008 needs.
open Win32/Coconut.sln
build
autoheader
(optional) cp /usr/share/libtool/config/ltmain.sh .
automake -a --copy
autoconf
DO NOT run aclocal
libtool 2.4.2, automake 1.11, autoconf 2.68 need
http:https://ftp.gnu.org/gnu/autoconf/autoconf-2.68.tar.gz
http:https://ftp.jaist.ac.jp/pub/GNU/libtool/libtool-2.4.2.tar.gz
http:https://ftp.gnu.org/gnu/automake/automake-1.11.tar.gz
copy libevent source to 3rdParty/src/libevent
copy hiredis source to 3rdParty/src/hiredis