struct test_message : public AppFrame::actions::Message_<test_message>
{
irr::core::stringc data;
test_message()
{
nlog<<"Create test_message"<<nlendl;
}
test_message(const char* msg)
{
data = msg;
nlog<<"Create test_message"<<nlendl;
}
~test_message(void)
{
nlog<<"Destroy test_message"<<nlendl;
}
virtual void serialize(AppFrame::IDataStream* stream)
{
stream->Write(data);
}
virtual void deserialize(AppFrame::IDataStream* stream)
{
stream->Read(data);
}
void print()
{
nlog<<"test_message received with "<<data.c_str()<<nlendl;
}
};
struct test_message2 : public AppFrame::actions::Message_<test_message2>
{
irr::core::stringc data;
int health;
test_message2()
{
health = 0;
//nlog<<"Create test_message2"<<nlendl;
}
test_message2(const char* msg, int h)
{
data = msg;
health = h;
//nlog<<"Create test_message2"<<nlendl;
}
~test_message2(void)
{
//nlog<<"Destroy test_message2"<<nlendl;
}
virtual void serialize(AppFrame::IDataStream* stream)
{
stream->Write(data);
stream->Write(health);
}
virtual void deserialize(AppFrame::IDataStream* stream)
{
stream->Read(data);
stream->Read(health);
}
void print()
{
nlog<<"test_message2 received with "<<data.c_str()<<" and "<<health<<nlendl;
}
};
class testreceiver : public IConnectionMessageReceiver
{
public:
testreceiver(CConnectionManager* _m, CStreamManager* stream_manager_)
{
m = _m;
stream_manager = stream_manager_;
}
void onConnect(const ConnectionID& id)
{
nlog<<"Player connected"<<nlendl;
//stream_manager->getEventManager()->replicate(new test_message("Hello Events"), true);
//stream_manager->getEventManager()->replicate(new test_message2("Hello Events2", 567), true);
}
void onDisconnect(const ConnectionID& id)
{
nlog<<"Player disconnected"<<nlendl;
}
void onMessage(const ConnectionID& id, const CONNECTION_CHANNEL& channel, AppFrame::IDataStream* stream)
{
}
CConnectionManager* m;
CStreamManager* stream_manager;
};
void onMessage(test_message* test)
{
test->print();
}
int last = 0;
void onMessage2(test_message2* test)
{
if (test->health > last+1)
nlerror<<"Skipped "<<test->health-last<<" message(s)"<<nlendl;
last = test->health;
//test->print();
}
int main(int argc, char* argv[])
{
/*
CApp app(argc, argv);
app.Init();
app.Run();
*/
CConnectionManager connection;
CStreamManager stream_manager(&connection);
stream_manager.setEventReceiver(new testreceiver(&connection, &stream_manager));
stream_manager.setUpdateRate(20, 1);
stream_manager.getEventManager()->registerMessage<test_message>();
stream_manager.getEventManager()->registerMessage<test_message2>();
bool server = true;
if (argc > 1)
{
server = false;
nlog<<"Start Client"<<nlendl;
connection.init(0, 1);
connection.connect("127.0.0.1", 50000) ? nlog<<"Connecting..."<<nlendl : nlerror<<"Connection failed!"<<nlendl;
stream_manager.getEventManager()->Bind(onMessage);
stream_manager.getEventManager()->Bind(onMessage2);
stream_manager.getEventManager()->replicate(new test_message("Hello Events"), true);
stream_manager.getEventManager()->replicate(new test_message2("Hello Events2", 567), true);
}
else
{
nlog<<"Start Server"<<nlendl;
connection.init(50000, 10);
stream_manager.getEventManager()->Bind(onMessage2);
}
int id = 0;
while(true)
{
connection.update();
if (stream_manager.update() && server)
stream_manager.getEventManager()->replicate(new test_message2("Hello Events", id++), false);
}
return 0;
}