main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. void main_sockets(void)
  15. {
  16. WSADATA winsockInfo;
  17. WSAStartup( MAKEWORD( 2, 2 ), &winsockInfo );
  18. int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  19. struct sockaddr_in serverAddr;
  20. memset(&serverAddr,0,sizeof(sockaddr_in));
  21. serverAddr.sin_family = AF_INET;
  22. serverAddr.sin_port = 0;
  23. int j = bind(sock,(struct sockaddr *) &serverAddr,sizeof(serverAddr));
  24. struct hostent * phe = gethostbyname( "masterserver2.raknet.com" );
  25. memcpy( &serverAddr.sin_addr.s_addr, phe->h_addr_list[ 0 ], sizeof( struct in_addr ) );
  26. serverAddr.sin_port = htons(80);
  27. connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  28. const char *postRequest =
  29. "POST /testServer HTTP/1.1\r\n"
  30. "Content-Length: 83\r\n"
  31. "Content-Type: text/plain; charset=UTF-8\r\n"
  32. "Host: masterserver2.raknet.com\r\n"
  33. "Connection: Keep-Alive\r\n"
  34. "\r\n"
  35. "{'__gameId': 'myGame','__clientReqId': '0','__timeoutSec': '60','mapname': 'myMap'}\r\n";
  36. send(sock, postRequest, strlen(postRequest), 0);
  37. char outputBuffer[512];
  38. memset(outputBuffer,0,512);
  39. recv(sock, outputBuffer, 512, 0);
  40. printf(outputBuffer);
  41. }
  42. #include "TCPInterface.h"
  43. #include "RakString.h"
  44. #include "RakSleep.h"
  45. #include "jansson.h"
  46. #include "GetTime.h"
  47. #define MASTER_SERVER_ADDRESS "masterserver2.raknet.com"
  48. #define MASTER_SERVER_PORT 80
  49. //#define MASTER_SERVER_ADDRESS "localhost"
  50. //#define MASTER_SERVER_PORT 8080
  51. using namespace RakNet;
  52. void main_RakNet_Post(void)
  53. {
  54. TCPInterface *tcp = RakNet::OP_NEW<TCPInterface>(__FILE__,__LINE__);
  55. tcp->Start(0, 64);
  56. tcp->Connect(MASTER_SERVER_ADDRESS, MASTER_SERVER_PORT, true);
  57. json_t *jsonObject = json_object();
  58. json_object_set(jsonObject, "__gameId", json_string("MotoGP_13") );
  59. json_object_set(jsonObject, "__clientReqId", json_integer(0) );
  60. json_object_set(jsonObject, "__timeoutSec", json_integer(60) );
  61. //json_object_set(jsonObject, "mapname", json_string(RakString::NonVariadic("Joué-lés-tours").URLEncode().C_String()) );
  62. char *ds = json_dumps(jsonObject,0);
  63. RakString rspost = RakString::FormatForPOST(
  64. RakString(MASTER_SERVER_ADDRESS "/testServer"),
  65. RakString("application/json; charset=UTF-8"),
  66. ds);
  67. json_decref(jsonObject);
  68. RakSleep(100);
  69. SystemAddress serverAddr = tcp->HasCompletedConnectionAttempt();
  70. tcp->Send(rspost.C_String(), rspost.GetLength(), serverAddr, false);
  71. RakNet::Time timeout = RakNet::GetTime()+2000;
  72. while (RakNet::GetTime() < timeout)
  73. {
  74. Packet *p = tcp->Receive();
  75. if (p)
  76. {
  77. printf((const char*) p->data);
  78. break;
  79. }
  80. RakSleep(30);
  81. }
  82. //if (p) printf((const char*) p->data);
  83. tcp->Stop();
  84. }
  85. #include "HTTPConnection2.h"
  86. void main_RakNet_Get(void)
  87. {
  88. HTTPConnection2 *httpConnection2;
  89. httpConnection2 = HTTPConnection2::GetInstance();
  90. TCPInterface *tcp = RakNet::OP_NEW<TCPInterface>(__FILE__,__LINE__);
  91. tcp->Start(0, 64);
  92. tcp->AttachPlugin(httpConnection2);
  93. // tcp->Connect("masterserver2.raknet.com", MASTER_SERVER_PORT, true);
  94. RakString rsRequest = RakString::FormatForGET(
  95. RakString(MASTER_SERVER_ADDRESS "/testServer?__gameId=MotoGP_13"));
  96. httpConnection2->TransmitRequest(rsRequest, MASTER_SERVER_ADDRESS, MASTER_SERVER_PORT);
  97. while (1)
  98. {
  99. // The following code is TCP operations for talking to the master server, and parsing the reply
  100. SystemAddress sa;
  101. Packet *packet;
  102. // This is kind of crappy, but for TCP plugins, always do HasCompletedConnectionAttempt, then Receive(), then HasFailedConnectionAttempt(),HasLostConnection()
  103. sa = tcp->HasCompletedConnectionAttempt();
  104. if (sa != UNASSIGNED_SYSTEM_ADDRESS)
  105. {
  106. printf("Connected to %s\n", sa.ToString());
  107. }
  108. for (packet = tcp->Receive(); packet; tcp->DeallocatePacket(packet), packet = tcp->Receive())
  109. ;
  110. sa = tcp->HasFailedConnectionAttempt();
  111. if (sa != UNASSIGNED_SYSTEM_ADDRESS)
  112. {
  113. printf("Failed to connect to %s\n", sa.ToString());
  114. }
  115. sa = tcp->HasLostConnection();
  116. if (sa != UNASSIGNED_SYSTEM_ADDRESS)
  117. {
  118. printf("Lost connection to %s\n", sa.ToString());
  119. }
  120. RakString stringTransmitted;
  121. RakString hostTransmitted;
  122. RakString responseReceived;
  123. SystemAddress hostReceived;
  124. int contentOffset;
  125. if (httpConnection2->GetResponse(stringTransmitted, hostTransmitted, responseReceived, hostReceived, contentOffset))
  126. {
  127. if (responseReceived.IsEmpty()==false)
  128. {
  129. if (contentOffset==-1)
  130. {
  131. // No content
  132. printf(responseReceived.C_String());
  133. }
  134. else
  135. {
  136. // printf("\n--------------\n\n");
  137. // printf(responseReceived.C_String());
  138. json_error_t error;
  139. json_t *root = json_loads(responseReceived.C_String() + contentOffset, JSON_REJECT_DUPLICATES, &error);
  140. if (!root)
  141. {
  142. printf("Error parsing JSON\n", __LINE__);
  143. printf(responseReceived.C_String());
  144. }
  145. else
  146. {
  147. void *iter = json_object_iter(root);
  148. while (iter)
  149. {
  150. const char *firstKey = json_object_iter_key(iter);
  151. if (stricmp(firstKey, "GET")==0)
  152. {
  153. json_t* jsonArray = json_object_iter_value(iter);
  154. size_t arraySize = json_array_size(jsonArray);
  155. for (unsigned int i=0; i < arraySize; i++)
  156. {
  157. json_t* object = json_array_get(jsonArray, i);
  158. json_t* gameIdVal = json_object_get(object, "__gameId");
  159. //RakAssert(cityNameVal->type==JSON_STRING);
  160. //RakString val = RakString::NonVariadic(json_string_value(cityNameVal)).URLDecode();
  161. printf("gameId: %i.\n", json_integer_value(gameIdVal));
  162. }
  163. if (arraySize==0)
  164. printf("No results.\n");
  165. break;
  166. }
  167. else if (stricmp(firstKey, "POST")==0)
  168. {
  169. break;
  170. }
  171. else
  172. {
  173. iter = json_object_iter_next(root, iter);
  174. RakAssert(iter != 0);
  175. }
  176. }
  177. json_decref(root);
  178. }
  179. }
  180. }
  181. }
  182. RakSleep(30);
  183. }
  184. }
  185. void main(void)
  186. {
  187. // main_sockets();
  188. main_RakNet_Post();
  189. // main_RakNet_Get();
  190. }
粤ICP备19079148号