ThreadTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief Tests multiple readers and writers on the same instance of RakPeer.
  12. #include "RakPeerInterface.h"
  13. #include "GetTime.h"
  14. #include "RakNetStatistics.h"
  15. #include "MessageIdentifiers.h"
  16. #include "Kbhit.h"
  17. #include <stdio.h> // Printf
  18. #include "WindowsIncludes.h" // Sleep
  19. //#include <process.h>
  20. #include "RakThread.h"
  21. #include "RakSleep.h"
  22. using namespace RakNet;
  23. RakPeerInterface *peer1, *peer2;
  24. bool endThreads;
  25. RAK_THREAD_DECLARATION(ProducerThread)
  26. {
  27. char i = *((char *) arguments);
  28. char out[2];
  29. out[0]=(char) ID_USER_PACKET_ENUM;
  30. out[1]=i;
  31. while (endThreads==false)
  32. {
  33. // printf("Thread %i writing...\n", i);
  34. if (i&1)
  35. peer1->Send(out, 2, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  36. else
  37. peer2->Send(out, 2, HIGH_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  38. // printf("Thread %i done writing\n", i);
  39. RakSleep(30);
  40. }
  41. return 0;
  42. }
  43. RAK_THREAD_DECLARATION(ConsumerThread)
  44. {
  45. char i = *((char *) arguments);
  46. RakNet::Packet *p;
  47. while (endThreads==false)
  48. {
  49. // printf("Thread %i reading...\n", i);
  50. if (i&1)
  51. p=peer1->Receive();
  52. else
  53. p=peer2->Receive();
  54. // printf("Thread %i done reading...\n", i);
  55. if (p)
  56. {
  57. if (p->data[0]==ID_USER_PACKET_ENUM)
  58. printf("Got data from thread %i\n", p->data[1]);
  59. if (i&1)
  60. peer1->DeallocatePacket(p);
  61. else
  62. peer2->DeallocatePacket(p);
  63. }
  64. RakSleep(30);
  65. }
  66. return 0;
  67. }
  68. int main()
  69. {
  70. peer1=RakNet::RakPeerInterface::GetInstance();
  71. peer2=RakNet::RakPeerInterface::GetInstance();
  72. peer1->SetMaximumIncomingConnections(1);
  73. peer2->SetMaximumIncomingConnections(1);
  74. RakNet::SocketDescriptor socketDescriptor(1234,0);
  75. peer1->Startup(1,&socketDescriptor, 1);
  76. socketDescriptor.port=1235;
  77. peer2->Startup(1,&socketDescriptor, 1);
  78. RakSleep(500);
  79. peer1->Connect("127.0.0.1", 1235, 0, 0);
  80. peer2->Connect("127.0.0.1", 1234, 0, 0);
  81. printf("Tests multiple threads sharing the same instance of RakPeer\n");
  82. printf("Difficulty: Beginner\n\n");
  83. endThreads=false;
  84. unsigned i;
  85. char count[20];
  86. printf("Starting threads\n");
  87. for (i=0; i< 10; i++)
  88. {
  89. count[i]=i;
  90. RakNet::RakThread::Create(&ProducerThread, count+i);
  91. }
  92. for (; i < 20; i++)
  93. {
  94. count[i]=i;
  95. RakNet::RakThread::Create(&ConsumerThread, count+i );
  96. }
  97. printf("Running test\n");
  98. RakNet::TimeMS endTime = 60 * 1000 + RakNet::GetTimeMS();
  99. while (RakNet::GetTimeMS() < endTime)
  100. {
  101. RakSleep(0);
  102. }
  103. endThreads=true;
  104. printf("Test done!\n");
  105. RakNet::RakPeerInterface::DestroyInstance(peer1);
  106. RakNet::RakPeerInterface::DestroyInstance(peer2);
  107. return 0;
  108. }
粤ICP备19079148号