PHPDirectoryServer2.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// \file
  11. /// \brief Contains PHPDirectoryServer2, a client for communicating with a HTTP list of game servers
  12. ///
  13. /// Creative Commons Licensees are subject to the
  14. /// license found at
  15. /// http://creativecommons.org/licenses/by-nc/2.5/
  16. /// Single application licensees are subject to the license found at
  17. /// http://www.jenkinssoftware.com/SingleApplicationLicense.html
  18. /// Custom license users are subject to the terms therein.
  19. /// GPL license users are subject to the GNU General Public
  20. /// License as published by the Free
  21. /// Software Foundation; either version 2 of the License, or (at your
  22. /// option) any later version.
  23. #ifndef __PHP_DIRECTORY_SERVER_2
  24. #define __PHP_DIRECTORY_SERVER_2
  25. #include "Export.h"
  26. #include "RakString.h"
  27. #include "HTTPConnection.h"
  28. #include "RakNetTypes.h"
  29. #include "DS_Queue.h"
  30. #include "DS_Table.h"
  31. #include "DS_Map.h"
  32. namespace RakNet {
  33. struct SystemAddress;
  34. enum HTTPReadResult
  35. {
  36. HTTP_RESULT_GOT_TABLE,
  37. HTTP_RESULT_EMPTY
  38. };
  39. /// \brief Use PHPDirectoryServer2 as a C++ client to DirectoryServer.php
  40. ///
  41. /// PHPDirectoryServer2 works with the HTTPConnection class (which works with the TCPInterface class) in order to communiate with DirectoryServer.php found under Samples/PHPDirectoryServer2
  42. class RAK_DLL_EXPORT PHPDirectoryServer2
  43. {
  44. public:
  45. PHPDirectoryServer2();
  46. virtual ~PHPDirectoryServer2();
  47. /// Associate PHPDirectoryServer2 with the HTTPConnection class it will communicate through
  48. /// \param[in] _http The instance of HTTP connection we will communicate through
  49. /// \param[in] _path The path to the PHP file on the remote server. For example, if the path is mysite.com/raknet/DirectoryServer.php then you would enter raknet/DirectoryServer.php
  50. void Init(HTTPConnection *_http, const char *_path);
  51. /// Set a parameter (these are passed to the server)
  52. /// To delete a column, just pass an empty string for value
  53. /// Store the game name and port with UploadTable, rather than SetField, as these columns are required and use reserved column names
  54. /// \param[in] columnName The name of the column to store
  55. /// \param[in] value What value to hold for the uploaded row (only one row can be uploaded at a time)
  56. void SetField(RakNet::RakString columnName, RakNet::RakString value);
  57. /// Returns the number of fields set with SetField()
  58. unsigned int GetFieldCount(void) const;
  59. /// Returns a field set with SetField()
  60. /// \param[in] index The 0 based index into the field list
  61. /// \param[out] columnName The \a columnName parameter passed to SetField()
  62. /// \param[out] value The \a value parameter passed to SetField()
  63. void GetField(unsigned int index, RakNet::RakString &columnName, RakNet::RakString &value);
  64. /// Set all parameters at once from a table
  65. /// \param[in] table A table containing the values you want to send. Note that all values are stored as strings in PHP
  66. void SetFields(DataStructures::Table *table);
  67. /// Clear all fields
  68. void ClearFields(void);
  69. /// Upload the values set with SetFields() or SetField()
  70. /// On success:
  71. /// 1. HTTPConnection::HasRead() will return true.
  72. /// 2. Pass the value returned by HTTPConnection::Read() to PHPDirectoryServer2::ProcessHTTPRead().
  73. /// 3. The return value of PHPDirectoryServer2::ProcessHTTPRead() will be HTTP_RESULT_EMPTY
  74. /// \param[in] uploadPassword The upload password set in the PHP page itself when you first uploaded and viewed it in the webpage.
  75. /// \param[in] gameName Every entry must have a game name. Pass it here.
  76. /// \param[in] gamePort Every entry must have a game port. Pass it here. The IP address will be stored automatically, or you can manually set it by passing a field named _System_Address
  77. /// \param[in] autoRepost Tables must be uploaded every 60 seconds or they get dropped. Set autoRepost to true to automatically reupload the most recent table.
  78. void UploadTable(RakNet::RakString uploadPassword, RakNet::RakString gameName, unsigned short gamePort, bool autoRepost);
  79. /// Send a download request to the PHP server.
  80. /// On success:
  81. /// 1. HTTPConnection::HasRead() will return true.
  82. /// 2. Pass the value returned by HTTPConnection::Read() to PHPDirectoryServer2::ProcessHTTPRead().
  83. /// 3. The return value of PHPDirectoryServer2::ProcessHTTPRead() will be HTTP_RESULT_GOT_TABLE or HTTP_RESULT_EMPTY
  84. /// 4. On HTTP_RESULT_GOT_TABLE, use GetLastDownloadedTable() to read the results.
  85. /// \param[in] downloadPassword The download password set in the PHP page itself when you first uploaded and viewed it in the webpage.
  86. void DownloadTable(RakNet::RakString downloadPassword);
  87. /// Same as calling DownloadTable immediately followed by UploadTable, except only the download result is returned
  88. /// \param[in] uploadPassword The upload password set in the PHP page itself when you first uploaded and viewed it in the webpage.
  89. /// \param[in] downloadPassword The download password set in the PHP page itself when you first uploaded and viewed it in the webpage.
  90. /// \param[in] gameName Every entry must have a game name. Pass it here.
  91. /// \param[in] gamePort Every entry must have a game port. Pass it here. The IP address will be stored automatically, or you can manually set it by passing a field named _System_Address
  92. /// \param[in] autoRepost Tables must be uploaded every 60 seconds or they get dropped. Set autoRepost to true to automatically reupload the most recent table.
  93. void UploadAndDownloadTable(RakNet::RakString uploadPassword, RakNet::RakString downloadPassword, RakNet::RakString gameName, unsigned short gamePort, bool autoRepost);
  94. /// When HTTPConnection::ProcessDataPacket() returns true, and not an error, pass HTTPConnection::Read() to this function
  95. /// The message will be parsed into DataStructures::Table, and a copy stored internally which can be retrieved by GetLastDownloadedTable();
  96. /// \param[in] packetData Returned from HTTPInterface::Read()
  97. /// \return One of the values for HTTPReadResult
  98. HTTPReadResult ProcessHTTPRead(RakNet::RakString httpRead);
  99. /// Returns the last value returned from ProcessHTTPString
  100. /// Default columns are "__GAME_NAME", "__GAME_PORT", "_System_Address"
  101. /// \return The table created by parsing httpString
  102. const DataStructures::Table *GetLastDownloadedTable(void) const;
  103. /// Call this periodically - it will handle connection states and refreshing updates to the server
  104. void Update(void);
  105. private:
  106. HTTPConnection *http;
  107. RakNet::RakString pathToPHP;
  108. RakNet::RakString gameNameParam;
  109. unsigned short gamePortParam;
  110. void SendOperation(void);
  111. void PushColumnsAndValues(DataStructures::List<RakNet::RakString> &columns, DataStructures::List<RakNet::RakString> &values);
  112. DataStructures::Table lastDownloadedTable;
  113. DataStructures::Map<RakNet::RakString, RakNet::RakString> fields;
  114. RakNet::RakString currentOperation;
  115. RakNet::TimeMS nextRepost;
  116. };
  117. } // namespace RakNet
  118. #endif
粤ICP备19079148号