fork(2) download
  1. struct test_message : public AppFrame::actions::Message_<test_message>
  2. {
  3. irr::core::stringc data;
  4. test_message()
  5. {
  6. nlog<<"Create test_message"<<nlendl;
  7. }
  8. test_message(const char* msg)
  9. {
  10. data = msg;
  11. nlog<<"Create test_message"<<nlendl;
  12. }
  13. ~test_message(void)
  14. {
  15. nlog<<"Destroy test_message"<<nlendl;
  16. }
  17. virtual void serialize(AppFrame::IDataStream* stream)
  18. {
  19. stream->Write(data);
  20. }
  21.  
  22. virtual void deserialize(AppFrame::IDataStream* stream)
  23. {
  24. stream->Read(data);
  25. }
  26.  
  27. void print()
  28. {
  29. nlog<<"test_message received with "<<data.c_str()<<nlendl;
  30. }
  31. };
  32.  
  33.  
  34. struct test_message2 : public AppFrame::actions::Message_<test_message2>
  35. {
  36. irr::core::stringc data;
  37. int health;
  38. test_message2()
  39. {
  40. health = 0;
  41. //nlog<<"Create test_message2"<<nlendl;
  42. }
  43. test_message2(const char* msg, int h)
  44. {
  45. data = msg;
  46. health = h;
  47. //nlog<<"Create test_message2"<<nlendl;
  48. }
  49. ~test_message2(void)
  50. {
  51. //nlog<<"Destroy test_message2"<<nlendl;
  52. }
  53. virtual void serialize(AppFrame::IDataStream* stream)
  54. {
  55. stream->Write(data);
  56. stream->Write(health);
  57. }
  58.  
  59. virtual void deserialize(AppFrame::IDataStream* stream)
  60. {
  61. stream->Read(data);
  62. stream->Read(health);
  63. }
  64.  
  65. void print()
  66. {
  67. nlog<<"test_message2 received with "<<data.c_str()<<" and "<<health<<nlendl;
  68. }
  69. };
  70.  
  71. class testreceiver : public IConnectionMessageReceiver
  72. {
  73. public:
  74. testreceiver(CConnectionManager* _m, CStreamManager* stream_manager_)
  75. {
  76. m = _m;
  77. stream_manager = stream_manager_;
  78. }
  79. void onConnect(const ConnectionID& id)
  80. {
  81. nlog<<"Player connected"<<nlendl;
  82. //stream_manager->getEventManager()->replicate(new test_message("Hello Events"), true);
  83. //stream_manager->getEventManager()->replicate(new test_message2("Hello Events2", 567), true);
  84. }
  85. void onDisconnect(const ConnectionID& id)
  86. {
  87. nlog<<"Player disconnected"<<nlendl;
  88. }
  89. void onMessage(const ConnectionID& id, const CONNECTION_CHANNEL& channel, AppFrame::IDataStream* stream)
  90. {
  91. }
  92. CConnectionManager* m;
  93. CStreamManager* stream_manager;
  94. };
  95.  
  96. void onMessage(test_message* test)
  97. {
  98. test->print();
  99. }
  100.  
  101. int last = 0;
  102. void onMessage2(test_message2* test)
  103. {
  104. if (test->health > last+1)
  105. nlerror<<"Skipped "<<test->health-last<<" message(s)"<<nlendl;
  106. last = test->health;
  107. //test->print();
  108. }
  109.  
  110. int main(int argc, char* argv[])
  111. {
  112. /*
  113.   CApp app(argc, argv);
  114.   app.Init();
  115.   app.Run();
  116.   */
  117. CConnectionManager connection;
  118. CStreamManager stream_manager(&connection);
  119. stream_manager.setEventReceiver(new testreceiver(&connection, &stream_manager));
  120. stream_manager.setUpdateRate(20, 1);
  121.  
  122. stream_manager.getEventManager()->registerMessage<test_message>();
  123. stream_manager.getEventManager()->registerMessage<test_message2>();
  124.  
  125. bool server = true;
  126.  
  127. if (argc > 1)
  128. {
  129. server = false;
  130. nlog<<"Start Client"<<nlendl;
  131. connection.init(0, 1);
  132. connection.connect("127.0.0.1", 50000) ? nlog<<"Connecting..."<<nlendl : nlerror<<"Connection failed!"<<nlendl;
  133.  
  134. stream_manager.getEventManager()->Bind(onMessage);
  135. stream_manager.getEventManager()->Bind(onMessage2);
  136.  
  137. stream_manager.getEventManager()->replicate(new test_message("Hello Events"), true);
  138. stream_manager.getEventManager()->replicate(new test_message2("Hello Events2", 567), true);
  139. }
  140. else
  141. {
  142. nlog<<"Start Server"<<nlendl;
  143. connection.init(50000, 10);
  144. stream_manager.getEventManager()->Bind(onMessage2);
  145. }
  146.  
  147. int id = 0;
  148. while(true)
  149. {
  150. connection.update();
  151. if (stream_manager.update() && server)
  152. stream_manager.getEventManager()->replicate(new test_message2("Hello Events", id++), false);
  153.  
  154. }
  155. return 0;
  156. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty