subevent
1.0.0
C++ Event Driven and Network Application Library
-std=c++11
(or later option)-pthread
or -lpthread
-lssl
and -lcrypt
(for OpenSSL)#include <subevent/subevent.hpp>
SEV_USING_NS
//---------------------------------------------------------------------------//
// Main
//---------------------------------------------------------------------------//
SEV_IMPL_GLOBAL
int main(int, char**)
{
NetApplication app;
// your code here.
return app.run();
}
#include <subevent/subevent.hpp>
SEV_USING_NS
//---------------------------------------------------------------------------//
// MyApp
//---------------------------------------------------------------------------//
class MyApp : public NetApplication
{
protected:
bool onInit() override
{
NetApplication::onInit();
// Your initialization code here.
return true;
}
void onExit() override
{
// Your deinitialization code here.
NetApplication::onExit();
}
};
//---------------------------------------------------------------------------//
// Main
//---------------------------------------------------------------------------//
SEV_IMPL_GLOBAL
int main(int, char**)
{
MyApp app;
return app.run();
}
// call "void NetApplication::stop()"
stop();
class MyThread : public NetThread
{
protected:
bool onInit() override
{
NetThread::onInit();
// Your initialization code here.
return true;
}
void onExit() override
{
// Your deinitialization code here.
NetThread::onExit();
}
};
// Start
MyThread* myThread = new MyThread();
myThread->start();
// End
myThread->stop();
myThread->wait();
delete myThread;
// Declare MyEvent
// <Unique Id, ParamType1, ParamType2, ...>
typedef UserEvent<1, int, std::string> MyEvent;
// Send (to another thread or self)
int param1 = 20;
std::string param2 = "data";
myThread->post(new MyEvent(param1, param2));
// Receive
setUserEventHandler<MyEvent>([&](const MyEvent* myEvent) {
int param1;
std::string param2;
myEvent->getParams(param1, param2);
});
myThread->post([&]() {
// Executed on myThread.
});
Timer* timer = new Timer();
uint32_t msec = 60000;
bool repeat = false;
// start
timer->start(msec, repeat, [&](Timer*) {
// Called from the same thread when time out.
});
$ cmake .
$ make