CrossConnectionTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * This file is part of the Airplay SDK Code Samples.
  3. *
  4. * Copyright (C) 2001-2011 Ideaworks3D Ltd.
  5. * All Rights Reserved.
  6. *
  7. * This source code is intended only as a supplement to Ideaworks Labs
  8. * Development Tools and/or on-line documentation.
  9. *
  10. * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  11. * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12. * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  13. * PARTICULAR PURPOSE.
  14. */
  15. /**
  16. * @page ExampleS3EHelloWorld S3E Hello World Example
  17. *
  18. * The following example, in typical Hello World style, displays the phrase
  19. * "Hello, World!" on screen.
  20. *
  21. * The functions required to achieve this are:
  22. * <ul>
  23. * <li>s3eDebugPrint()
  24. * </ul>
  25. *
  26. * All examples will follow this basic pattern; a brief description of what
  27. * the example does will be given followed by a list of all the important
  28. * functions and, perhaps, classes.
  29. *
  30. * Should the example be more complex, a more detailed explanation of what the
  31. * example does and how it does it will be added. Note that most examples
  32. * use an example framework to remove boilerplate code and allow the projects
  33. * to be made up of a single source file for easy viewing. This framework can
  34. * be found in the examples/s3e/ExamplesMain directory.
  35. *
  36. * @include s3eHelloWorld.cpp
  37. */
  38. #include "s3e.h"
  39. // ------- RAKNET INCLUDES ------------
  40. #include "RakPeerInterface.h"
  41. #include "PacketLogger.h"
  42. #include "Rand.h"
  43. #include "Kbhit.h"
  44. #include <stdio.h>
  45. #include "RakSleep.h"
  46. #include "MessageIdentifiers.h"
  47. #include "BitStream.h"
  48. #include "GetTime.h"
  49. using namespace RakNet;
  50. // main entry point for the application
  51. int main()
  52. {
  53. // ------- RAKNET CODE ------------
  54. RakPeerInterface *rakPeer1, *rakPeer2;
  55. rakPeer1=RakPeerInterface::GetInstance();
  56. rakPeer2=RakPeerInterface::GetInstance();
  57. rakPeer1->SetMaximumIncomingConnections(8);
  58. rakPeer2->SetMaximumIncomingConnections(8);
  59. bool gotConnectionRequestAccepted[2];
  60. bool gotNewIncomingConnection[2];
  61. Packet *packet;
  62. SocketDescriptor sd1(60000,0);
  63. SocketDescriptor sd2(2000,0);
  64. unsigned short numSystems[2];
  65. // Wait for a quit request from the host OS
  66. while (!s3eDeviceCheckQuitRequest())
  67. {
  68. if (s3eTimerGetMs() % 3000 < 1000)
  69. s3eSurfaceClear(255, 0, 0);
  70. else if (s3eTimerGetMs() % 3000 < 2000)
  71. s3eSurfaceClear(0, 255, 0);
  72. else
  73. s3eSurfaceClear(0, 0, 255);
  74. // ------- RAKNET CODE ------------
  75. gotConnectionRequestAccepted[0]=false;
  76. gotConnectionRequestAccepted[1]=false;
  77. gotNewIncomingConnection[0]=false;
  78. gotNewIncomingConnection[1]=false;
  79. numSystems[0]=0;
  80. numSystems[1]=0;
  81. rakPeer1->Startup(1,&sd1, 1);
  82. rakPeer2->Startup(1,&sd2, 1);
  83. RakSleep(100);
  84. rakPeer1->Connect("127.0.0.1", 2000, 0, 0);
  85. rakPeer2->Connect("127.0.0.1", 60000, 0, 0);
  86. RakSleep(100);
  87. for (packet=rakPeer1->Receive(); packet; rakPeer1->DeallocatePacket(packet), packet=rakPeer1->Receive())
  88. {
  89. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  90. gotNewIncomingConnection[0]=true;
  91. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  92. gotConnectionRequestAccepted[0]=true;
  93. else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  94. printf("Error on rakPeer1, got ID_CONNECTION_ATTEMPT_FAILED\n");
  95. }
  96. for (packet=rakPeer2->Receive(); packet; rakPeer2->DeallocatePacket(packet), packet=rakPeer2->Receive())
  97. {
  98. if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
  99. gotNewIncomingConnection[1]=true;
  100. else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
  101. gotConnectionRequestAccepted[1]=true;
  102. else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  103. printf("Error on rakPeer2, got ID_CONNECTION_ATTEMPT_FAILED\n");
  104. }
  105. rakPeer1->GetConnectionList(0,&numSystems[0]);
  106. rakPeer2->GetConnectionList(0,&numSystems[1]);
  107. if (gotConnectionRequestAccepted[0]==true && gotConnectionRequestAccepted[1]==true)
  108. {
  109. printf("Test passed\n");
  110. }
  111. else if (numSystems[0]!=1 || numSystems[1]!=1)
  112. {
  113. printf("Test failed, system 1 has %i connections and system 2 has %i connections.\n", numSystems[0], numSystems[1]);
  114. }
  115. else if (gotConnectionRequestAccepted[0]==false && gotConnectionRequestAccepted[1]==false)
  116. {
  117. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED is false for both instances\n");
  118. }
  119. else if (gotNewIncomingConnection[0]==true && gotNewIncomingConnection[1]==true)
  120. {
  121. printf("Test failed, ID_NEW_INCOMING_CONNECTION is true for both instances\n");
  122. }
  123. else if (gotConnectionRequestAccepted[0]==false && gotConnectionRequestAccepted[1]==false)
  124. {
  125. printf("Test failed, ID_NEW_INCOMING_CONNECTION is false for both instances\n");
  126. }
  127. else if (gotConnectionRequestAccepted[0]==true && gotNewIncomingConnection[1]==false)
  128. {
  129. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for first instance, but not ID_NEW_INCOMING_CONNECTION for second\n");
  130. }
  131. else if (gotConnectionRequestAccepted[1]==true && gotNewIncomingConnection[0]==false)
  132. {
  133. printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for second instance, but not ID_NEW_INCOMING_CONNECTION for first\n");
  134. }
  135. else if ((int)gotConnectionRequestAccepted[0]+
  136. (int)gotConnectionRequestAccepted[1]!=1)
  137. {
  138. printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED\n");
  139. }
  140. else if ((int)gotNewIncomingConnection[0]+
  141. (int)gotNewIncomingConnection[1]!=1)
  142. {
  143. printf("Test failed, does not have exactly one instance of ID_NEW_INCOMING_CONNECTION\n");
  144. }
  145. else if ((int)gotConnectionRequestAccepted[0]+
  146. (int)gotConnectionRequestAccepted[1]+
  147. (int)gotNewIncomingConnection[0]+
  148. (int)gotNewIncomingConnection[1]!=2)
  149. {
  150. printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED and one instance of ID_NEW_INCOMING_CONNECTION\n");
  151. }
  152. else
  153. printf("Test passed\n");
  154. rakPeer1->Shutdown(0);
  155. rakPeer2->Shutdown(0);
  156. // flip the surface buffer
  157. s3eSurfaceShow();
  158. // sleep for 0ms to allow the OS to process events etc
  159. s3eDeviceYield(0);
  160. //Update keyboard and touchscreen event queues
  161. s3eKeyboardUpdate();
  162. s3ePointerUpdate();
  163. //Quit if there was any activity since the last time the s3e*Update() functions
  164. //were called
  165. if (s3eKeyboardAnyKey())
  166. break;
  167. if (s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_PRESSED)
  168. break;
  169. }
  170. return 0;
  171. }
粤ICP备19079148号