CommonFunctions.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "CommonFunctions.h"
  11. CommonFunctions::CommonFunctions(void)
  12. {
  13. }
  14. CommonFunctions::~CommonFunctions(void)
  15. {
  16. }
  17. bool CommonFunctions::ConnectionStateMatchesOptions(RakPeerInterface *peer,SystemAddress currentSystem,bool isConnected,bool isConnecting,bool isPending,bool isDisconnecting,bool isNotConnected,bool isLoopBack , bool isSilentlyDisconnecting)
  18. {
  19. ConnectionState connectionState=peer->GetConnectionState(currentSystem);
  20. switch(connectionState)
  21. {
  22. case IS_CONNECTED:
  23. return isConnected;
  24. break;
  25. case IS_CONNECTING:
  26. return isConnecting;
  27. break;
  28. case IS_PENDING:
  29. return isPending;
  30. break;
  31. case IS_DISCONNECTING:
  32. return isDisconnecting;
  33. break;
  34. case IS_LOOPBACK:
  35. return isLoopBack;
  36. break;
  37. case IS_NOT_CONNECTED:
  38. return isNotConnected;
  39. break;
  40. case IS_SILENTLY_DISCONNECTING:
  41. return isSilentlyDisconnecting;
  42. break;
  43. default:
  44. return false;
  45. break;
  46. }
  47. }
  48. bool CommonFunctions::WaitAndConnect(RakPeerInterface *peer,char* ip,unsigned short int port,int millisecondsToWait)
  49. {
  50. SystemAddress connectToAddress;
  51. connectToAddress.SetBinaryAddress(ip);
  52. connectToAddress.port=port;
  53. TimeMS entryTime=GetTimeMS();
  54. while(!CommonFunctions::ConnectionStateMatchesOptions (peer,connectToAddress,true)&&GetTimeMS()-entryTime<millisecondsToWait)
  55. {
  56. if(!CommonFunctions::ConnectionStateMatchesOptions (peer,connectToAddress,true,true,true,true))
  57. {
  58. peer->Connect(ip,port,0,0);
  59. }
  60. RakSleep(100);
  61. }
  62. if (ConnectionStateMatchesOptions (peer,connectToAddress,true))
  63. {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. void CommonFunctions::DisconnectAndWait(RakPeerInterface *peer,char* ip,unsigned short int port)
  69. {
  70. SystemAddress targetAddress;
  71. targetAddress.SetBinaryAddress(ip);
  72. targetAddress.port=port;
  73. while(CommonFunctions::ConnectionStateMatchesOptions (peer,targetAddress,true,true,true,true))//disconnect client
  74. {
  75. peer->CloseConnection (targetAddress,true,0,LOW_PRIORITY);
  76. }
  77. }
  78. bool CommonFunctions::WaitForMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait)
  79. {
  80. RakTimer timer(millisecondsToWait);
  81. Packet *packet;
  82. while(!timer.IsExpired())
  83. {
  84. for (packet=reciever->Receive(); packet;reciever->DeallocatePacket(packet), packet=reciever->Receive())
  85. {
  86. //printf("Packet %i\n",packet->data[0]);
  87. if (packet->data[0]==id)
  88. {
  89. reciever->DeallocatePacket(packet);
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. Packet *CommonFunctions::WaitAndReturnMessageWithID(RakPeerInterface *reciever,int id,int millisecondsToWait)
  97. {
  98. RakTimer timer(millisecondsToWait);
  99. Packet *packet;
  100. while(!timer.IsExpired())
  101. {
  102. for (packet=reciever->Receive(); packet;reciever->DeallocatePacket(packet), packet=reciever->Receive())
  103. {
  104. // printf("Packet %i\n",packet->data[0]);
  105. if (packet->data[0]==id)
  106. {
  107. return packet;
  108. }
  109. }
  110. }
  111. return 0;
  112. }
粤ICP备19079148号