MessageSizeTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "RakPeerInterface.h"
  11. #include "GetTime.h"
  12. #include "MessageIdentifiers.h"
  13. #include "BitStream.h"
  14. #include <cstdio>
  15. #include <memory.h>
  16. #include <cstring>
  17. #include <stdlib.h>
  18. #include "Rand.h"
  19. #include "RakNetStatistics.h"
  20. #include "RakSleep.h"
  21. #include "RakMemoryOverride.h"
  22. using namespace RakNet;
  23. int main(int argc, char **argv)
  24. {
  25. RakPeerInterface *sender, *receiver;
  26. printf("This project tests sending messages of various sizes.\n");
  27. sender = RakNet::RakPeerInterface::GetInstance();
  28. receiver = RakNet::RakPeerInterface::GetInstance();
  29. RakNet::SocketDescriptor sd1(1234,0),sd2(1235,0);
  30. receiver->Startup(32, &sd1, 1);
  31. receiver->SetMaximumIncomingConnections(32);
  32. sender->Startup(1, &sd2, 1);
  33. sender->Connect("127.0.0.1", 1234, 0, 0);
  34. RakSleep(100);
  35. unsigned char data[4000];
  36. data[0]=ID_USER_PACKET_ENUM;
  37. for (unsigned int i=1; i < 4000; i++)
  38. data[i]=i%256;
  39. int stride, sum;
  40. int sendCount, receiveCount;
  41. for (stride=1; stride < 2000; stride++)
  42. {
  43. sendCount=0;
  44. receiveCount=0;
  45. for (sum=0; sum < 4000; sum+=stride)
  46. {
  47. sender->Send((const char*) data,stride,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
  48. sendCount++;
  49. }
  50. RakNet::Packet *p;
  51. for (p=sender->Receive(); p; sender->DeallocatePacket(p), p=sender->Receive())
  52. ;
  53. RakNet::Time timeout=RakNet::GetTime()+1000;
  54. while (RakNet::GetTime()<timeout)
  55. {
  56. for (p=receiver->Receive(); p; receiver->DeallocatePacket(p), p=receiver->Receive())
  57. {
  58. if (p->data[0]==ID_USER_PACKET_ENUM)
  59. {
  60. receiveCount++;
  61. }
  62. for (unsigned int i=1; i < p->length; i++)
  63. {
  64. RakAssert(data[i]==i%256);
  65. }
  66. }
  67. RakSleep(30);
  68. if (receiveCount==sendCount)
  69. break;
  70. }
  71. if (sendCount==receiveCount)
  72. printf("Stride=%i Sends=%i Receives=%i\n", stride, sendCount, receiveCount);
  73. else
  74. printf("ERROR! Stride=%i Sends=%i Receives=%i\n", stride, sendCount, receiveCount);
  75. }
  76. if (sender)
  77. RakNet::RakPeerInterface::DestroyInstance(sender);
  78. if (receiver)
  79. RakNet::RakPeerInterface::DestroyInstance(receiver);
  80. return 1;
  81. }
粤ICP备19079148号