TitleValidationDB_PostgreSQLTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // Common includes
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "Kbhit.h"
  14. #include "GetTime.h"
  15. #include "FunctionThread.h"
  16. #include "TitleValidationDB_PostgreSQL.h"
  17. #include "EpochTimeToString.h"
  18. #ifdef _WIN32
  19. #include <windows.h> // Sleep
  20. #else
  21. #include <unistd.h> // usleep
  22. #endif
  23. // localtime
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <time.h>
  27. // Database queries are asynchronous so the results are read in Functor::HandleResult. The default implementation passes it to a callback
  28. class DBResultHandler : public TitleValidationDBCBInterface
  29. {
  30. virtual void AddTitle_CB(AddTitle_PostgreSQLImpl *callResult, bool wasCancelled, void *context)
  31. {
  32. if (wasCancelled)
  33. printf("AddTitle_CB call canceled:\n");
  34. else if (callResult->dbQuerySuccess==false)
  35. {
  36. printf("AddTitle_CB call DB failure:\n");
  37. printf("%s", callResult->titleValidationServer->GetLastError());
  38. }
  39. else
  40. {
  41. printf("AddTitle_CB success:\n");
  42. }
  43. printf("\n");
  44. }
  45. virtual void GetTitles_CB(GetTitles_PostgreSQLImpl *callResult, bool wasCancelled, void *context)
  46. {
  47. if (wasCancelled)
  48. printf("GetTitles_CB call canceled:\n");
  49. else if (callResult->dbQuerySuccess==false)
  50. {
  51. printf("GetTitles_CB call DB failure:\n");
  52. printf("%s", callResult->titleValidationServer->GetLastError());
  53. }
  54. else
  55. {
  56. printf("GetTitles_CB success:\n");
  57. unsigned i;
  58. for (i=0; i < callResult->titles.Size(); i++)
  59. {
  60. printf("%i. titleId=%i\n", i+1, callResult->titles[i]->titleId);
  61. printf("%i. titleName=%s\n", i+1, callResult->titles[i]->titleName.C_String());
  62. printf("%i. titlePasswordLength=%i\n", i+1, callResult->titles[i]->titlePasswordLength);
  63. printf("%i. allowClientAccountCreation=%i\n", i+1, callResult->titles[i]->allowClientAccountCreation);
  64. printf("%i. lobbyIsGameIndependent=%i\n", i+1, callResult->titles[i]->lobbyIsGameIndependent);
  65. printf("%i. requireUserKeyToLogon=%i\n", i+1, callResult->titles[i]->requireUserKeyToLogon);
  66. printf("%i. defaultAllowUpdateHandle=%i\n", i+1, callResult->titles[i]->defaultAllowUpdateHandle);
  67. printf("%i. defaultAllowUpdateCCInfo=%i\n", i+1, callResult->titles[i]->defaultAllowUpdateCCInfo);
  68. printf("%i. defaultAllowUpdateAccountNumber=%i\n", i+1, callResult->titles[i]->defaultAllowUpdateAccountNumber);
  69. printf("%i. defaultAllowUpdateAdminLevel=%i\n", i+1, callResult->titles[i]->defaultAllowUpdateAdminLevel);
  70. printf("%i. defaultAllowUpdateAccountBalance=%i\n", i+1, callResult->titles[i]->defaultAllowUpdateAccountBalance);
  71. printf("%i. defaultAllowClientsUploadActionHistory=%i\n", i+1, callResult->titles[i]->defaultAllowClientsUploadActionHistory);
  72. printf("%i. defaultPermissionsStr=%s\n", i+1, callResult->titles[i]->defaultPermissionsStr.C_String());
  73. printf("%i. creationDate=%s\n", i+1, EpochTimeToString(callResult->titles[i]->creationDate));
  74. }
  75. }
  76. printf("\n");
  77. }
  78. virtual void UpdateUserKey_CB(UpdateUserKey_PostgreSQLImpl *callResult, bool wasCancelled, void *context)
  79. {
  80. if (wasCancelled)
  81. printf("UpdateUserKey_CB call canceled:\n");
  82. else if (callResult->dbQuerySuccess==false)
  83. {
  84. printf("UpdateUserKey_CB call DB failure:\n");
  85. printf("%s", callResult->titleValidationServer->GetLastError());
  86. }
  87. else
  88. {
  89. printf("UpdateUserKey_CB success:\n");
  90. printf("[out] userKeyId=%i\n", callResult->userKeyId);
  91. }
  92. printf("\n");
  93. }
  94. virtual void GetUserKeys_CB(GetUserKeys_PostgreSQLImpl *callResult, bool wasCancelled, void *context)
  95. {
  96. if (wasCancelled)
  97. printf("GetUserKeys_CB call canceled:\n");
  98. else if (callResult->dbQuerySuccess==false)
  99. {
  100. printf("GetUserKeys_CB call DB failure:\n");
  101. printf("%s", callResult->titleValidationServer->GetLastError());
  102. }
  103. else
  104. {
  105. printf("GetUserKeys_CB success:\n");
  106. unsigned i;
  107. for (i=0; i < callResult->keys.Size(); i++)
  108. {
  109. printf("%i. userKeyId=%i\n", i+1, callResult->keys[i]->userKeyId);
  110. printf("%i. titleId=%i\n", i+1, callResult->keys[i]->titleId);
  111. printf("%i. keyPasswordLength=%i\n", i+1, callResult->keys[i]->keyPasswordLength);
  112. printf("%i. userId=%i\n", i+1, callResult->keys[i]->userId);
  113. printf("%i. userIP=%s\n", i+1, callResult->keys[i]->userIP.C_String());
  114. printf("%i. numRegistrations=%i\n", i+1, callResult->keys[i]->numRegistrations);
  115. printf("%i. invalidKey=%i\n", i+1, callResult->keys[i]->invalidKey);
  116. printf("%i. invalidKeyReason=%s\n", i+1, callResult->keys[i]->invalidKeyReason.C_String());
  117. printf("%i. binaryDataLength=%i\n", i+1, callResult->keys[i]->binaryDataLength);
  118. printf("%i. creationDate=%s\n", i+1, EpochTimeToString(callResult->keys[i]->creationDate));
  119. }
  120. }
  121. printf("\n");
  122. }
  123. virtual void ValidateUserKey_CB(ValidateUserKey_PostgreSQLImpl *callResult, bool wasCancelled, void *context)
  124. {
  125. if (wasCancelled)
  126. printf("ValidateUserKey_CB call canceled:\n");
  127. else if (callResult->dbQuerySuccess==false)
  128. {
  129. printf("ValidateUserKey_CB call DB failure:\n");
  130. printf("%s", callResult->titleValidationServer->GetLastError());
  131. }
  132. else if (callResult->successful==0)
  133. {
  134. printf("ValidateUserKey_CB success\n");
  135. }
  136. else
  137. printf("ValidateUserKey_CB failure. Reason=%s\n", callResult->invalidKeyReason.C_String());
  138. }
  139. };
  140. void main(int argc, char **argv)
  141. {
  142. printf("A sample on using the provided implementation\nof the Title Validation specification based on PostgreSQL\n");
  143. printf("Unlike the other samples, this is a server-only\nprocess so does not involve networking with RakNet.\n");
  144. printf("The goal of this class is to allow you to lookup\ngame titles based on a password, and verify CD keys.\n");
  145. printf("Difficulty: Intermediate\n\n");
  146. // A function thread is class that spawns a thread that operates on functors.
  147. // A functor is an instance of a class that has two pre-defined functions: One to perform processing, another to get the result.
  148. // One per application is enough to not block
  149. RakNet::FunctionThread ft;
  150. // Start one thread. Starting more than one may be advantageous for multi-core processors. However, in this scenario we are
  151. // blocking on database calls rather than processing.
  152. ft.StartThreads(1);
  153. // The real functionality of the TitleValidationDB class is contained in the functors defined in TitleValidationDB_PostgreSQL.h/.cpp.
  154. // However, this class contains some utility functions and members, such as the functionThread and a pointer to
  155. // the postgreSQL interface.
  156. TitleValidation_PostgreSQL titleValidationDB;
  157. // The class internally uses a functionThread so that database queries can happen asynchronously,
  158. // as opposed to blocking and slowing down the game.
  159. // If you don't assign one it will create one automatically and start it with one thread.
  160. titleValidationDB.AssignFunctionThread(&ft);
  161. // The default implementation of the functors pass Functor::HandleResult through to a callback, instantiated here.
  162. // Alternatively, you could derive from the *_PostgreSQLImpl functors found in TitleValidationDB_PostgreSQL.h/.cpp
  163. // and override the behavior of Functor::HandleResult
  164. DBResultHandler resultHandler;
  165. titleValidationDB.AssignCallback(&resultHandler);
  166. printf("Enter database password:\n");
  167. char connectionString[256],password[128];
  168. char username[256];
  169. strcpy(username, "postgres");
  170. gets(password);
  171. strcpy(connectionString, "user=");
  172. strcat(connectionString, username);
  173. strcat(connectionString, " password=");
  174. strcat(connectionString, password);
  175. // database=blah
  176. if (titleValidationDB.Connect(connectionString)==false)
  177. {
  178. printf("Database connection failed.\n");
  179. return;
  180. }
  181. printf("Database connection succeeded.\n");
  182. printf("(A) Drop tables\n"
  183. "(B) Create tables\n"
  184. "(C) Add title.\n"
  185. "(D) Get titles.\n"
  186. "(E) Update user key.\n"
  187. "(F) Get user keys.\n"
  188. "(F) Validate user key.\n"
  189. "(~) Quit\n");
  190. char inputStr[512];
  191. char ch;
  192. while (1)
  193. {
  194. if (kbhit())
  195. {
  196. ch=getch();
  197. if (ch=='~')
  198. break;
  199. else if (ch=='a')
  200. {
  201. if (titleValidationDB.DestroyTitleValidationTables()==false)
  202. printf("%s", titleValidationDB.GetLastError());
  203. else
  204. printf("Tables dropped.\n");
  205. }
  206. else if (ch=='b')
  207. {
  208. if (titleValidationDB.CreateTitleValidationTables()==false)
  209. printf("%s", titleValidationDB.GetLastError());
  210. else
  211. printf("Tables created.\n");
  212. }
  213. else if (ch=='c')
  214. {
  215. // We could do new and delete, but this ensures the class is allocated and deallocated in the same DLL, should we use one.
  216. // By default deallocation takes place in HandleResult()
  217. AddTitle_PostgreSQLImpl *functor = AddTitle_PostgreSQLImpl::Alloc();
  218. printf("Adds a title to the database\n");
  219. printf("Enter title name: ");
  220. gets(inputStr);
  221. functor->titleName = inputStr;
  222. printf("Enter title password (binary): ");
  223. gets(inputStr);
  224. functor->titlePassword = AddTitle_PostgreSQLImpl::AllocBytes((int) strlen(inputStr)+1);
  225. functor->titlePasswordLength = (int) strlen(inputStr)+1;
  226. memcpy(functor->titlePassword, inputStr, functor->titlePasswordLength);
  227. functor->allowClientAccountCreation=true;
  228. functor->lobbyIsGameIndependent=true;
  229. functor->requireUserKeyToLogon=false;
  230. functor->allowClientAccountCreation=true;
  231. functor->lobbyIsGameIndependent=true;
  232. functor->defaultAllowUpdateHandle=true;
  233. functor->defaultAllowUpdateCCInfo=true;
  234. functor->defaultAllowUpdateAccountNumber=true;
  235. functor->defaultAllowUpdateAdminLevel=true;
  236. functor->defaultAllowUpdateAccountBalance=true;
  237. functor->defaultAllowClientsUploadActionHistory=true;
  238. /// Puts this functor on the processing queue. It will process sometime later in a thread.
  239. titleValidationDB.PushFunctor(functor);
  240. }
  241. else if (ch=='d')
  242. {
  243. GetTitles_PostgreSQLImpl *functor = GetTitles_PostgreSQLImpl::Alloc();
  244. printf("Getting previously added titles\n");
  245. titleValidationDB.PushFunctor(functor);
  246. }
  247. else if (ch=='e')
  248. {
  249. UpdateUserKey_PostgreSQLImpl *functor = UpdateUserKey_PostgreSQLImpl::Alloc();
  250. printf("Update/Inserting a user key\n");
  251. printf("Insert a new key? (y/n): ");
  252. gets(inputStr);
  253. functor->userKeyIsSet = (inputStr[0]=='n');
  254. if (functor->userKeyIsSet)
  255. {
  256. printf("Enter existing key (integer): ");
  257. gets(inputStr);
  258. functor->userKeyId=atoi(inputStr);
  259. }
  260. printf("Enter title Id (integer, required): ");
  261. gets(inputStr);
  262. functor->titleId=atoi(inputStr);
  263. printf("Enter key password (binary): ");
  264. gets(inputStr);
  265. functor->keyPassword = UpdateUserKey_PostgreSQLImpl::AllocBytes((int) strlen(inputStr)+1);
  266. functor->keyPasswordLength = (int) strlen(inputStr)+1;
  267. memcpy(functor->keyPassword, inputStr, functor->keyPasswordLength);
  268. printf("Enter userId (integer, optional): ");
  269. gets(inputStr);
  270. functor->userId=atoi(inputStr);
  271. functor->userIP="127.0.0.1";
  272. functor->numRegistrations=0;
  273. functor->invalidKey=false;
  274. titleValidationDB.PushFunctor(functor);
  275. }
  276. else if (ch=='f')
  277. {
  278. GetUserKeys_PostgreSQLImpl *functor = GetUserKeys_PostgreSQLImpl::Alloc();
  279. printf("Getting previously added user keys\n");
  280. titleValidationDB.PushFunctor(functor);
  281. }
  282. else if (ch=='g')
  283. {
  284. ValidateUserKey_PostgreSQLImpl *functor = ValidateUserKey_PostgreSQLImpl::Alloc();
  285. printf("Validating key\n");
  286. printf("Enter key password (binary): ");
  287. gets(inputStr);
  288. functor->keyPassword = UpdateUserKey_PostgreSQLImpl::AllocBytes((int) strlen(inputStr)+1);
  289. functor->keyPasswordLength = (int) strlen(inputStr)+1;
  290. titleValidationDB.PushFunctor(functor);
  291. }
  292. }
  293. // Causes Functor::HandleResult calls. If this is forgotten you won't get processing result calls.
  294. ft.CallResultHandlers();
  295. Sleep(30);
  296. }
  297. }
粤ICP备19079148号