TestMain.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Timers;
  8. using System.IO;
  9. using RakNet;
  10. namespace SwigTestApp
  11. {
  12. class TestMain
  13. {
  14. static void Main(string[] args)
  15. {
  16. if (!File.Exists("RakNet.dll"))
  17. {
  18. Console.WriteLine("The SWIG build of the DLL has not been copied to the executable directory\nCopy from Swig/SwigWindowsCSharpSample/SwigTestApp/bin/X86/Debug/RakNet.dll to\nSwigWindowsCSharpSample/SwigTestApp/bin/Debug/RakNet.dll\nPress enter to quit.");
  19. Console.Read();
  20. return;
  21. }
  22. try
  23. {
  24. RakString dllCallTest = new RakString();
  25. }
  26. catch (Exception e)
  27. {
  28. Console.WriteLine("DLL issue\nAdd SwigOutput/CplusDLLIncludes/RakNetWrap.cxx to the project\nDLL_Swig/RakNet.sln and rebuild.\nPress enter to quit.");
  29. Console.Read();
  30. return;
  31. }
  32. Packet testPacket;
  33. int loopNumber;
  34. BitStream stringTestSendBitStream = new BitStream();
  35. BitStream rakStringTestSendBitStream = new BitStream();
  36. BitStream receiveBitStream = new BitStream();
  37. String holdingString;
  38. TimeSpan startTimeSpan;
  39. RakString rakStringTest = new RakString();
  40. RakPeerInterface testClient = RakPeer.GetInstance();
  41. testClient.Startup(1, new SocketDescriptor(60000, "127.0.0.1"), 1);
  42. RakPeerInterface testServer = RakPeer.GetInstance();
  43. testServer.Startup(1, new SocketDescriptor(60001, "127.0.0.1"), 1);
  44. testServer.SetMaximumIncomingConnections(1);
  45. Console.WriteLine("Send and receive loop using BitStream.\nBitStream read done into RakString");
  46. testClient.Connect("127.0.0.1", 60001, "", 0);
  47. String sendString = "The test string";
  48. stringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
  49. stringTestSendBitStream.Write(sendString);
  50. RakString testRakString = new RakString("Test RakString");
  51. rakStringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
  52. rakStringTestSendBitStream.Write(testRakString);
  53. startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  54. loopNumber = 0;
  55. while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
  56. {
  57. testPacket = testServer.Receive();
  58. if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
  59. {
  60. receiveBitStream.Reset();
  61. receiveBitStream.Write(testPacket.data, testPacket.length);
  62. receiveBitStream.IgnoreBytes(1);
  63. receiveBitStream.Read(rakStringTest);
  64. Console.WriteLine("Loop number: " + loopNumber + "\nData: " + rakStringTest.C_String());
  65. }
  66. testServer.DeallocatePacket(testPacket);
  67. loopNumber++;
  68. System.Threading.Thread.Sleep(50);
  69. testClient.Send(rakStringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
  70. }
  71. Console.WriteLine("String send and receive loop using BitStream.\nBitStream read done into String");
  72. SystemAddress[] remoteSystems;
  73. ushort numberOfSystems=1;
  74. testServer.GetConnectionList(out remoteSystems, ref numberOfSystems);
  75. startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  76. loopNumber = 0;
  77. while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
  78. {
  79. testPacket = testServer.Receive();
  80. if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
  81. {
  82. receiveBitStream.Reset();
  83. receiveBitStream.Write(testPacket.data, testPacket.length);
  84. receiveBitStream.IgnoreBytes(1);
  85. receiveBitStream.Read(out holdingString);
  86. Console.WriteLine("Loop number: " + loopNumber + "\nData: " + holdingString);
  87. }
  88. testServer.DeallocatePacket(testPacket);
  89. loopNumber++;
  90. System.Threading.Thread.Sleep(50);
  91. SystemAddress sa = RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS;
  92. testClient.Send(stringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
  93. }
  94. //If RakString is not freed before program exit it will crash
  95. rakStringTest.Dispose();
  96. testRakString.Dispose();
  97. RakPeer.DestroyInstance(testClient);
  98. RakPeer.DestroyInstance(testServer);
  99. Console.WriteLine("Demo complete. Press Enter.");
  100. Console.Read();
  101. }
  102. }
  103. #if AUTOPATCHERMYSQLTESTS
  104. private static int TestAutoPatcherClient()
  105. {
  106. //Stick the restarter path here
  107. String restarterPath = "C:\\Rak4\\Samples\\AutopatcherClientRestarter\\Debug\\AutopatcherClientRestarter.exe";
  108. TestCB transferCallback = new TestCB();
  109. Console.Write("A simple client interface for the advanced autopatcher.\n");
  110. Console.Write("Use DirectoryDeltaTransfer for a simpler version of an autopatcher.\n");
  111. Console.Write("Difficulty: Intermediate\n\n");
  112. Console.Write("Client starting...");
  113. SystemAddress serverAddress = RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS;
  114. AutopatcherClient autopatcherClient = new AutopatcherClient();
  115. FileListTransfer fileListTransfer = new FileListTransfer();
  116. autopatcherClient.SetFileListTransferPlugin(fileListTransfer);
  117. RakPeerInterface rakPeer;
  118. rakPeer = RakPeerInterface.GetInstance();
  119. SocketDescriptor socketDescriptor = new SocketDescriptor(0, null);
  120. rakPeer.Startup(1, socketDescriptor, 1);
  121. // Plugin will send us downloading progress notifications if a file is split to fit under the MTU 10 or more times
  122. rakPeer.SetSplitMessageProgressInterval(10);
  123. rakPeer.AttachPlugin(autopatcherClient);
  124. rakPeer.AttachPlugin(fileListTransfer);
  125. Console.Write("started\n");
  126. String buff;
  127. Console.Write("Enter server IP: ");
  128. buff = Console.ReadLine();
  129. if (buff == "")
  130. buff = "127.0.0.1";
  131. rakPeer.Connect(buff, 60000, null, 0);
  132. Console.Write("Connecting...\n");
  133. String appDir;
  134. Console.Write("Enter application directory: ");
  135. appDir = Console.ReadLine();
  136. if (appDir == "")
  137. {
  138. appDir = "C:/temp2";
  139. }
  140. String appName;
  141. Console.Write("Enter application name: ");
  142. appName = Console.ReadLine();
  143. if (appName == "")
  144. appName = "TestApp";
  145. bool patchImmediately = false;
  146. if (patchImmediately == false)
  147. Console.Write("Hit 'q' to quit, 'p' to patch, 'c' to cancel the patch. 'r' to reconnect. 'd' to disconnect.\n");
  148. else
  149. Console.Write("Hit 'q' to quit, 'c' to cancel the patch.\n");
  150. char ch;
  151. Packet p;
  152. while (true)
  153. {
  154. p = rakPeer.Receive();
  155. while (p != null)
  156. {
  157. if (p.data[0] == (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION)
  158. Console.Write("ID_DISCONNECTION_NOTIFICATION\n");
  159. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST)
  160. Console.Write("ID_CONNECTION_LOST\n");
  161. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED)
  162. {
  163. Console.Write("ID_CONNECTION_REQUEST_ACCEPTED\n");
  164. serverAddress = p.systemAddress;
  165. }
  166. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED)
  167. Console.Write("ID_CONNECTION_ATTEMPT_FAILED\n");
  168. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR)
  169. {
  170. //String buff;
  171. //BitStream temp = new BitStream(p.data, p.length, false);
  172. //temp.IgnoreBits(8);
  173. //StringCompressor.Instance().DecodeString(buff, 256, temp);
  174. //Console.Write("ID_AUTOPATCHER_REPOSITORY_FATAL_ERROR\n");
  175. //Console.Write("%s\n", buff);
  176. }
  177. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_AUTOPATCHER_FINISHED)
  178. Console.Write("ID_AUTOPATCHER_FINISHED\n");
  179. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_AUTOPATCHER_RESTART_APPLICATION)
  180. Console.Write("Launch \"AutopatcherClientRestarter.exe autopatcherRestart.txt\"\nQuit this application immediately after to unlock files.\n");
  181. rakPeer.DeallocatePacket(p);
  182. p = rakPeer.Receive();
  183. }
  184. if (Console.KeyAvailable)
  185. ch = Console.ReadKey().KeyChar;
  186. else
  187. ch = (char)0;
  188. if (ch == 'q')
  189. break;
  190. else if (ch == 'r')
  191. {
  192. rakPeer.Connect(buff, 60000, null, 0);
  193. }
  194. else if (ch == 'd')
  195. {
  196. rakPeer.CloseConnection(serverAddress, true);
  197. }
  198. else if (ch == 'p' || (serverAddress != RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS && patchImmediately == true))
  199. {
  200. patchImmediately = false;
  201. String lastUpdateDate;
  202. String restartFile;
  203. restartFile = appDir;
  204. restartFile += "/autopatcherRestart.txt";
  205. // Console.Write("Enter last update date (only newer updates retrieved) or nothing to get all updates\n");
  206. // lastUpdateDate = Console.ReadLine();
  207. lastUpdateDate = "";
  208. if (autopatcherClient.PatchApplication(appName, appDir, lastUpdateDate, serverAddress, transferCallback, restartFile, restarterPath))
  209. {
  210. Console.Write("Patching process starting.\n");
  211. }
  212. else
  213. {
  214. Console.Write("Failed to start patching.\n");
  215. }
  216. }
  217. else if (ch == 'c')
  218. {
  219. autopatcherClient.Clear();
  220. Console.Write("Autopatcher cleared.\n");
  221. }
  222. Thread.Sleep(30);
  223. }
  224. // Dereference so the destructor doesn't crash
  225. autopatcherClient.SetFileListTransferPlugin(null);
  226. rakPeer.Shutdown(500, 0);
  227. RakPeerInterface.DestroyInstance(rakPeer);
  228. return 1;
  229. }
  230. private static void TestAutoPatcherServer()
  231. {
  232. TimeSpan startTimeSpan;
  233. // Avoids the Error: Got a packet bigger than 'max_allowed_packet' bytes
  234. Console.Write("Important: Requires that you first set the DB schema and the max packet size on the server.\n");
  235. Console.Write("See DependentExtensions/AutopatcherMySQLRepository/readme.txt\n");
  236. Console.Write("Server starting... ");
  237. AutopatcherServer autopatcherServer = new AutopatcherServer();
  238. FLP_Printf progressIndicator = new FLP_Printf();
  239. FileListTransfer fileListTransfer = new FileListTransfer();
  240. // So only one thread runs per connection, we create an array of connection objects, and tell the autopatcher server to use one thread per item
  241. const int sqlConnectionObjectCount = 4;
  242. AutopatcherMySQLRepository[] connectionObject = new AutopatcherMySQLRepository[sqlConnectionObjectCount];
  243. AutopatcherRepositoryInterface[] connectionObjectAddresses = new AutopatcherRepositoryInterface[sqlConnectionObjectCount];
  244. for (int i = 0; i < sqlConnectionObjectCount; i++)
  245. {
  246. connectionObject[i] = new AutopatcherMySQLRepository();
  247. connectionObjectAddresses[i] = connectionObject[i];
  248. }
  249. fileListTransfer.SetCallback(progressIndicator);
  250. autopatcherServer.SetFileListTransferPlugin(fileListTransfer);
  251. RakPeerInterface rakPeer;
  252. rakPeer = RakPeerInterface.GetInstance();
  253. SocketDescriptor socketDescriptor = new SocketDescriptor((ushort)LISTEN_PORT, null);
  254. rakPeer.Startup(8, socketDescriptor, 1);
  255. rakPeer.SetMaximumIncomingConnections(MAX_INCOMING_CONNECTIONS);
  256. rakPeer.AttachPlugin(autopatcherServer);
  257. rakPeer.AttachPlugin(fileListTransfer);
  258. Console.Write("started.\n");
  259. Console.Write("Enter database password:\n");
  260. String password;
  261. String username = "root";
  262. password = Console.ReadLine();
  263. if (password == "")
  264. password = "aaaa";
  265. string db;
  266. Console.Write("Enter DB schema: ");
  267. // To create the schema, go to the command line client and type create schema autopatcher;
  268. // You also have to add
  269. // max_allowed_packet=128M
  270. // Where 128 is the maximum size file in megabytes you'll ever add
  271. // to MySQL\MySQL Server 5.1\my.ini in the [mysqld] section
  272. // Be sure to restart the service after doing so
  273. db = Console.ReadLine(); ;
  274. if (db == "")
  275. db = "autopatcher";
  276. for (int conIdx = 0; conIdx < sqlConnectionObjectCount; conIdx++)
  277. {
  278. if (!connectionObject[conIdx].Connect("localhost", username, password, db, 0, null, 0))
  279. {
  280. Console.Write("Database connection failed.\n");
  281. return;
  282. }
  283. }
  284. Console.Write("Database connection suceeded.\n");
  285. Console.Write("Starting threads\n");
  286. autopatcherServer.StartThreads(sqlConnectionObjectCount, connectionObjectAddresses);
  287. Console.Write("System ready for connections\n");
  288. Console.Write("(D)rop database\n(C)reate database.\n(A)dd application\n(U)pdate revision.\n(R)emove application\n(Q)uit\n");
  289. char ch;
  290. Packet p;
  291. while (true)
  292. {
  293. p = rakPeer.Receive();
  294. while (p != null)
  295. {
  296. if (p.data[0] == (byte)DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION)
  297. Console.Write("ID_NEW_INCOMING_CONNECTION\n");
  298. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION)
  299. Console.Write("ID_DISCONNECTION_NOTIFICATION\n");
  300. else if (p.data[0] == (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST)
  301. Console.Write("ID_CONNECTION_LOST\n");
  302. rakPeer.DeallocatePacket(p);
  303. p = rakPeer.Receive();
  304. }
  305. if (Console.KeyAvailable)
  306. {
  307. ch = Console.ReadKey().KeyChar;
  308. if (ch == 'q')
  309. break;
  310. else if (ch == 'c')
  311. {
  312. if (connectionObject[0].CreateAutopatcherTables() == false)
  313. Console.Write("Error: %s\n", connectionObject[0].GetLastError());
  314. else
  315. Console.Write("Created\n");
  316. }
  317. else if (ch == 'd')
  318. {
  319. if (connectionObject[0].DestroyAutopatcherTables() == false)
  320. Console.Write("Error: %s\n", connectionObject[0].GetLastError());
  321. else
  322. Console.Write("Destroyed\n");
  323. }
  324. else if (ch == 'a')
  325. {
  326. Console.Write("Enter application name to add: ");
  327. string appName;
  328. appName = Console.ReadLine(); ;
  329. if (appName == "")
  330. appName = "TestApp";
  331. if (connectionObject[0].AddApplication(appName, username) == false)
  332. Console.Write("Error: %s\n", connectionObject[0].GetLastError());
  333. else
  334. Console.Write("Done\n");
  335. }
  336. else if (ch == 'r')
  337. {
  338. Console.Write("Enter application name to remove: ");
  339. string appName;
  340. appName = Console.ReadLine(); ;
  341. if (appName == "")
  342. appName = "TestApp";
  343. if (connectionObject[0].RemoveApplication(appName) == false)
  344. Console.Write("Error: %s\n", connectionObject[0].GetLastError());
  345. else
  346. Console.Write("Done\n");
  347. }
  348. else if (ch == 'u')
  349. {
  350. Console.Write("Enter application name: ");
  351. string appName;
  352. appName = Console.ReadLine(); ;
  353. if (appName == "")
  354. appName = "TestApp";
  355. Console.Write("Enter application directory: ");
  356. string appDir;
  357. appDir = Console.ReadLine(); ;
  358. if (appDir == "")
  359. appDir = "C:/temp";
  360. if (connectionObject[0].UpdateApplicationFiles(appName, appDir, username, progressIndicator) == false)
  361. {
  362. Console.Write("Error: %s\n", connectionObject[0].GetLastError());
  363. }
  364. else
  365. {
  366. Console.Write("Update success.\n");
  367. }
  368. }
  369. }
  370. Thread.Sleep(30);
  371. }
  372. RakPeerInterface.DestroyInstance(rakPeer);
  373. }
  374. #endif
  375. }
粤ICP备19079148号