main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <WinSock2.h>
  11. #include <windows.h>
  12. #include <Ws2tcpip.h>
  13. #include <stdio.h>
  14. #include "Gets.h"
  15. #include "TCPInterface.h"
  16. #include "RakString.h"
  17. #include "RakSleep.h"
  18. #include "DR_SHA1.h"
  19. #include "BitStream.h"
  20. #include "Base64Encoder.h"
  21. // See http://www.digip.org/jansson/doc/2.4/
  22. // This is used to make it easier to parse the JSON returned from the master server
  23. #include "jansson.h"
  24. using namespace RakNet;
  25. void main_RakNet(void)
  26. {
  27. const char *serverURL = "localhost";
  28. //const char *serverURL = "lobby3.raknet.com";
  29. //const unsigned int serverPort=80;
  30. //const unsigned int serverPort=8888;
  31. //const bool useSSL=true;
  32. const bool useSSL=false;
  33. //const unsigned int serverPort=443;
  34. const unsigned int serverPort=8080;
  35. json_t *jsonObject = json_object();
  36. json_object_set(jsonObject, "__devId", json_string("defaultDevId1"));
  37. json_object_set(jsonObject, "__userId", json_string("defaultUserId1"));
  38. json_object_set(jsonObject, "__userPw", json_string("defaultPw"));
  39. json_object_set(jsonObject, "__appId", json_string("defaultAppId1"));
  40. json_object_set(jsonObject, "__customTableId", json_string("defaultCustomTableId"));
  41. json_object_set(jsonObject, "__timeToLiveSec", json_integer(0));
  42. json_object_set(jsonObject, "__timeToIdleSec", json_integer(6000));
  43. json_object_set(jsonObject, "__key", json_integer(0));
  44. json_object_set(jsonObject, "__mergeMode", json_string("OVERWRITE_EXISTING"));
  45. //json_object_set(jsonObject, "__autoFields", json_string("svrTimestamp,svrIP,svrSerial,svrGeoIP"));
  46. json_object_set(jsonObject, "__fieldMetadata", json_string("sampleField1Key(_ownerRW,_putMin),sampleField2Key(_userRW,_putSum)"));
  47. json_object_set(jsonObject, "__protocol", json_integer(0));
  48. json_object_set(jsonObject, "sampleField1Key", json_integer(1));
  49. json_object_set(jsonObject, "sampleField2Key", json_integer(2));
  50. // JSON_COMPACT is required or it won't match json-lib
  51. char *jsonStr = json_dumps(jsonObject, JSON_COMPACT | JSON_PRESERVE_ORDER);
  52. printf(jsonStr);
  53. // For testing, see http://hash.online-convert.com/sha1-generator
  54. const char *__sharedKey="defaultSharedKey";
  55. unsigned char output[SHA1_LENGTH];
  56. CSHA1::HMAC((unsigned char*) __sharedKey, strlen(__sharedKey), (unsigned char*) jsonStr, strlen(jsonStr), output);
  57. char outputBase64[SHA1_LENGTH*2+6];
  58. int bytesWritten = Base64Encoding(output, sizeof(output), outputBase64);
  59. //outputBase64[bytesWritten]=0;
  60. json_object_set(jsonObject, "__hash", json_string(outputBase64));
  61. jsonStr = json_dumps(jsonObject, JSON_COMPACT | JSON_PRESERVE_ORDER);
  62. // GAE SSL https://developers.google.com/appengine/docs/ssl
  63. char URI[128];
  64. sprintf(URI, "%s/customTable/update", serverURL);
  65. TCPInterface *tcp = RakNet::OP_NEW<TCPInterface>(__FILE__,__LINE__); // Requires build with OPEN_SSL_CLIENT_SUPPORT
  66. tcp->Start(0, 64);
  67. tcp->Connect(serverURL, serverPort, true);
  68. RakString rspost = RakString::FormatForPOST(
  69. URI,
  70. RakString("text/plain; charset=UTF-8"),
  71. jsonStr
  72. );
  73. RakSleep(100);
  74. SystemAddress serverAddr = tcp->HasCompletedConnectionAttempt();
  75. RakAssert(serverAddr!=UNASSIGNED_SYSTEM_ADDRESS);
  76. if (useSSL)
  77. tcp->StartSSLClient(serverAddr);
  78. tcp->Send(rspost.C_String(), rspost.GetLength(), serverAddr, false);
  79. RakSleep(1000);
  80. Packet *p;
  81. while (1)
  82. {
  83. p = tcp->Receive();
  84. if (p)
  85. {
  86. printf((const char*) p->data);
  87. break;
  88. }
  89. }
  90. }
  91. void main(void)
  92. {
  93. // main_sockets();
  94. main_RakNet();
  95. }
粤ICP备19079148号