Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Network.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. * \file test/Network.cpp
  3. * \brief Networking Singleton Unit test
  4. *
  5. * \author Mongoose
  6. */
  7. #include <Network.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. extern network_frame_t gPiggyBack;
  12. void from_network_layer(network_packet_t *p, unsigned int *last_id)
  13. {
  14. static unsigned int i = 0;
  15. if (!p)
  16. {
  17. return;
  18. }
  19. *last_id = i++;
  20. sleep(1);
  21. p->send = 1;
  22. p->pos[0] = i*3;
  23. p->pos[1] = i*3+1;
  24. p->pos[2] = i*3+2;
  25. printf("<S>ending { %f %f %f }\n", p->pos[0], p->pos[1], p->pos[2]);
  26. }
  27. void to_network_layer(network_packet_t p)
  28. {
  29. printf("<R>ecieved { %f %f %f }\n", p.pos[0], p.pos[1], p.pos[2]);
  30. gPiggyBack.data.pos[0] = gPiggyBack.seq*4;
  31. gPiggyBack.data.pos[1] = gPiggyBack.seq*4+1;
  32. gPiggyBack.data.pos[2] = gPiggyBack.seq*4+2;
  33. gPiggyBack.data.send = 1;
  34. gPiggyBack.data.yaw = 90.0f;
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. printf("\n\n[Network class test]\n");
  39. Network &test = *Network::Instance();
  40. if (argc > 3)
  41. {
  42. if (argv[1][1] == 'v')
  43. {
  44. test.setDebug(true);
  45. }
  46. switch (argv[1][0])
  47. {
  48. case 'c':
  49. test.setRemoteHost(argv[2]);
  50. test.setPort(atoi(argv[3]));
  51. test.runClient();
  52. break;
  53. case 's':
  54. test.setBindHost(argv[2]);
  55. test.setPort(atoi(argv[3]));
  56. test.runServer();
  57. break;
  58. default:
  59. printf("Error in command line, run %s for help\n", argv[0]);
  60. }
  61. }
  62. else if (argc > 2)
  63. {
  64. test.setPort(atoi(argv[2]));
  65. test.runServer();
  66. }
  67. else
  68. {
  69. printf("Server: %s s [bind_host_name] port\n", argv[0]);
  70. printf("Client: %s c remote_host_name remote_host_port\n", argv[0]);
  71. printf("Append 'v' behind c/s option for verbose. eg cv\n");
  72. }
  73. killNetworkSingleton();
  74. return 0;
  75. }