Simple Event Driven Network Library
Version 1.0 is now in Beta!
SedNL is a C++11 network library with a high level API. It is event driven, which means you bind functions on events and your callback will be asynchronously called in a multi-threaded environment. This reduce network to 'designing good events' and 'reacting to events'.
SedNL use TCP to send and receive events.
32 Bits beta release is available here. 64 Bits beta release is available here.
Installation is simple. First, you have to git clone https://github.com/Zenol/sednl.git
.
Then, create a build directory. We will use a build directory in the clone : cd sednl ; mkdir build
.
And now, we generate all the makefiles with cmake ..
.
Then, you are home, and you can type make ; sudo make install
.
You should use cmake.exe / cmake-gui.exe.
If you want to build in sednl/build, to generate MinGW makefiles you
can type cmake -G "MinGW Makefiles" ..
.
Notice that Visual Studio's compiler isn't C++11 compliant, and it's probably that SedNL won't build. Seams like VC++ 2013 will solve it. You can use Visual Studio with Clang++ or MinGW (or any C++11 compiler).
Binding on events is really easy. Here is a small sample :
// Your callbacks :
void my_on_connect(Connection& c);
void my_on_disconnect(Connection& c);
void my_hello_msg(Connection& c, const Event& e);
void my_say_something(Connection& c, const Event& e);
void my_apples(Connection& c, const Event& e);
// Create a server
TCPServer server(SocketAddress(7777));
// Bind my_on_connect and listen from server
EventListener listener(server);
listener.on_connect().set_function(my_on_connect);
// Bind some events
EventConsumer consumer(listener);
consumer.on_disconnect(my_on_disconnect);
consumer.bind("hello_msg").set_function(my_hello_msg);
consumer.bind("say_something").set_function(my_say_something);
consumer.bind("apples").set_function(my_apples);
// Launch listener thread and consumer thread
listener.run();
consumer.run();
// Do what you want in the main thread ...
// Stop listener, close connections, and wait for the end of event processing.
listener.join();
consumer.join();
You can look at examples to have simple examples of use.
Documentation (very detailed) is available here : http://zenol.fr/sednl/
SedNL use queue to store events when they are received. That's the job of the EventListener class. Then, queues are read by consumers thread. Creating a thread and setting callbacks is the work of the EventConsumer class.
For easy building of message, we provide a serialisation macro (although you can write yourself the small template code generated) and rely on variadic templates. Events are instances of the Event class, which is nothing more than a Packet instance with a name. The packet class provide way to stack data and build an Event.
For short events, it's easier to write the packet by hand. For more complex object, the good choice is serialising big objects so that your packet reduce to few serialized object. This way, your code is more flexible.
You may notice that most of the library code isn't networking, but providing a hight level API so that you don't have to worry (too much) about concurency, and hide the complexity of using condition variables, mutexes, etc.