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