connections.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <cstdio>
  2. #include <cstring>
  3. #include "OS.h"
  4. #include "MallocAllocator.h"
  5. #include "BitMessage.h"
  6. #include "AsyncConnection.h"
  7. using namespace crown;
  8. const unsigned int server_port = 30000;
  9. const unsigned int client_port = 30001;
  10. const uint32_t protocol_id = 0x00;
  11. const float delta_time = 1.0f / 45.0f;
  12. const real send_rate = 1.0f / 45.0f;
  13. const float time_out = 10.0f;
  14. enum Mode
  15. {
  16. SERVER,
  17. CLIENT
  18. };
  19. int main(int argc, char** argv)
  20. {
  21. if (argc != 2)
  22. {
  23. os::printf("Usage: ./connections server/client.\n");
  24. return -1;
  25. }
  26. bool connected = false;
  27. real send_acc = 0.0f;
  28. Mode mode = SERVER;
  29. os::NetAddress addr(127, 0, 0, 1, server_port);
  30. // creates connection
  31. MallocAllocator alloc;
  32. network::AsyncConnection connection(alloc);
  33. if (strcmp(argv[1], "server") == 0)
  34. {
  35. mode = SERVER;
  36. }
  37. else if (strcmp(argv[1], "client") == 0)
  38. {
  39. mode = CLIENT;
  40. }
  41. else
  42. {
  43. os::printf("Usage: ./connections server/client.\n");
  44. return -1;
  45. }
  46. uint16_t port = mode == SERVER ? server_port : client_port;
  47. // connection init
  48. connection.init(protocol_id, 10.0f);
  49. //connection start
  50. if (!connection.start(port))
  51. {
  52. os::printf("Couldn't start connection on port %d\n", port);
  53. return -1;
  54. }
  55. if (mode == CLIENT)
  56. {
  57. connection.connect(addr);
  58. }
  59. else if (mode == SERVER)
  60. {
  61. connection.listen();
  62. }
  63. // main loop
  64. while (true)
  65. {
  66. if (!connected && connection.is_connected())
  67. {
  68. os::printf("Client connected to server\n");
  69. connected = true;
  70. }
  71. if (!connected && connection.is_connect_fail())
  72. {
  73. os::printf("Connection failed\n");
  74. break;
  75. }
  76. send_acc += delta_time;
  77. if (mode == SERVER)
  78. {
  79. while (true)
  80. {
  81. network::BitMessage received(alloc);
  82. received.init(6);
  83. int32_t bytes = connection.receive_message(received, delta_time);
  84. if (bytes > 0)
  85. {
  86. uint8_t* header = received.get_header();
  87. uint32_t protocol_id = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
  88. uint16_t sequence = header[4] << 8 | header[5];
  89. uint16_t ack = header[6] << 8 | header[7];
  90. uint32_t ack_bits = header[8] << 24 | header[9] << 16 | header[10] << 8 | header[11];
  91. char string[6];
  92. received.read_string(string, 6);
  93. os::printf("------------------\n");
  94. os::printf("protocol_id: %d\n", protocol_id);
  95. os::printf("sequence: %d\n", sequence);
  96. os::printf("ack: %d\n", ack);
  97. os::printf("ack_bits: %d\n", ack_bits);
  98. os::printf("data: %s\n", string);
  99. os::printf("------------------\n");
  100. }
  101. if (bytes <= 0)
  102. {
  103. break;
  104. }
  105. }
  106. }
  107. if (mode == CLIENT)
  108. {
  109. while (send_acc > 1 / send_rate)
  110. {
  111. network::BitMessage message(alloc);
  112. message.init(6);
  113. message.begin_writing();
  114. message.write_string("prova", 6);
  115. connection.send_message(message, delta_time);
  116. uint8_t* header = message.get_header();
  117. uint32_t protocol_id = header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3];
  118. uint16_t sequence = header[4] << 8 | header[5];
  119. uint16_t ack = header[6] << 8 | header[7];
  120. uint32_t ack_bits = header[8] << 24 | header[9] << 16 | header[10] << 8 | header[11];
  121. os::printf("------------------\n");
  122. os::printf("protocol_id: %d\n", protocol_id);
  123. os::printf("sequence: %d\n", sequence);
  124. os::printf("ack: %d\n", ack);
  125. os::printf("ack_bits: %d\n", ack_bits);
  126. os::printf("------------------\n");
  127. send_acc -= 1.0f / send_rate;
  128. }
  129. }
  130. }
  131. connection.stop();
  132. }