PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/netplay/netlobby.h

http://github.com/Warzone2100/warzone2100
C Header | 248 lines | 174 code | 44 blank | 30 comment | 4 complexity | ca87348acbfae485d9d0a0b077ee3338 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, CC-BY-SA-4.0, LGPL-2.0, BSD-3-Clause
  1. /*
  2. This file is part of Warzone 2100.
  3. Copyright (C) 1999-2004 Eidos Interactive
  4. Copyright (C) 2005-2011 Warzone 2100 Project
  5. Warzone 2100 is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. Warzone 2100 is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Warzone 2100; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifndef _netlobby_h
  18. #define _netlobby_h
  19. #if defined(_MSC_VER) && defined(NO_SSL)
  20. #pragma message ("Note: openSSL is needed for SSL")
  21. #endif
  22. #if defined(NO_SSL)
  23. #include <QtNetwork/QTcpSocket>
  24. #else
  25. #include <QtNetwork/QSslSocket>
  26. #endif
  27. #include <QtCore/QVariant>
  28. #include "lib/framework/frame.h"
  29. namespace Lobby
  30. {
  31. const qint32 PROTOCOL = 4;
  32. /* NOTE: You also need to change this value in the
  33. * masterservers settings - session_size!
  34. */
  35. const int SESSION_SIZE = 16+1;
  36. /* We limit usernames here to 40 chars,
  37. * while the forums allows usernames with up to 255 characters.
  38. */
  39. const int USERNAME_SIZE = 40+1;
  40. enum RETURN_CODES
  41. {
  42. LOBBY_NO_ERROR = 0,
  43. // Copied from XMLRPC for socketrpc
  44. PARSE_ERROR = -32700,
  45. SERVER_ERROR = -32600,
  46. APPLICATION_ERROR = -32500,
  47. TRANSPORT_ERROR = -32300,
  48. // Specific errors.
  49. UNSUPPORTED_ENCODING = -32701,
  50. METHOD_NOT_FOUND = -32601,
  51. // Custom error codes.
  52. INVALID_DATA = -500,
  53. LOGIN_REQUIRED = -405,
  54. WRONG_LOGIN = -404,
  55. NOT_ACCEPTABLE = -403,
  56. NO_GAME = -402,
  57. };
  58. // FIXME: Not sure if std::string is a good idea here,
  59. // as multiint is passing them to iV_DrawText.
  60. struct GAME
  61. {
  62. uint32_t port; ///< Port hosting on.
  63. std::string host; ///< IPv4, IPv6 or DNS Name of the host.
  64. std::string description; ///< Game Description.
  65. uint32_t currentPlayers; ///< Number of joined players.
  66. uint32_t maxPlayers; ///< Maximum number of players.
  67. std::string versionstring; ///< Version string.
  68. uint32_t game_version_major; ///< Minor NETCODE version.
  69. uint32_t game_version_minor; ///< Major NETCODE version.
  70. bool isPrivate; ///< Password protected?
  71. std::string modlist; ///< display string for mods.
  72. std::string mapname; ///< name of map hosted.
  73. std::string hostplayer; ///< hosts playername.
  74. };
  75. struct LOBBY_ERROR
  76. {
  77. RETURN_CODES code;
  78. QString message;
  79. };
  80. struct CALL_RESULT
  81. {
  82. RETURN_CODES code;
  83. QVariant result;
  84. };
  85. class Client {
  86. public:
  87. QList<GAME> games;
  88. Client();
  89. void stop();
  90. RETURN_CODES connect();
  91. bool disconnect();
  92. bool isConnected();
  93. RETURN_CODES login(const QString& password);
  94. RETURN_CODES logout();
  95. RETURN_CODES addGame(char** result, const uint32_t port, const uint32_t maxPlayers,
  96. const char* description, const char* versionstring,
  97. const uint32_t game_version_major, const uint32_t game_version_minor,
  98. const bool isPrivate, const char* modlist,
  99. const char* mapname, const char* hostplayer);
  100. RETURN_CODES delGame();
  101. RETURN_CODES addPlayer(const unsigned int index, const char* name, const char* username, const char* session);
  102. RETURN_CODES delPlayer(const unsigned int index);
  103. RETURN_CODES updatePlayer(const unsigned int index, const char* name);
  104. RETURN_CODES listGames(const int maxGames);
  105. Client& addCACertificate(const QString& path);
  106. bool useSSL() {
  107. return useSSL_;
  108. }
  109. bool useSSL(bool useSSL)
  110. {
  111. #if defined(NO_SSL)
  112. if (useSSL == true)
  113. {
  114. debug(LOG_ERROR, "Cannot use SSL as its not compiled in.");
  115. }
  116. return false;
  117. #else
  118. useSSL_ = useSSL;
  119. useAuth_ = useSSL;
  120. return useSSL_;
  121. #endif
  122. }
  123. bool useAuth() {
  124. return useAuth_;
  125. }
  126. Client& setHost(const QString& host) {
  127. host_ = host;
  128. return *this;
  129. }
  130. QString getHost() const {
  131. return host_;
  132. }
  133. Client& setPort(const quint16& port) {
  134. port_ = port;
  135. return *this;
  136. }
  137. quint16 getPort() {
  138. return port_;
  139. }
  140. bool isAuthenticated() {
  141. return isAuthenticated_;
  142. }
  143. bool hasAuthData() {
  144. return (!user_.isEmpty() && !token_.isEmpty());
  145. }
  146. Client& setUser(const QString& user) {
  147. user_ = user;
  148. return *this;
  149. }
  150. QString getUser() const {
  151. return user_;
  152. }
  153. Client& setToken(const QString& token) {
  154. token_ = token;
  155. return *this;
  156. }
  157. QString getToken() {
  158. return token_;
  159. }
  160. QString getSession() const {
  161. return session_;
  162. }
  163. LOBBY_ERROR* getError() {
  164. return &lastError_;
  165. }
  166. void freeError()
  167. {
  168. lastError_.code = LOBBY_NO_ERROR;
  169. lastError_.message.clear();
  170. }
  171. private:
  172. qint64 gameId_;
  173. qint64 callId_;
  174. bool useSSL_;
  175. #if !defined(NO_SSL)
  176. QList<QSslCertificate> cacerts_;
  177. #endif
  178. bool useAuth_;
  179. QString host_;
  180. quint16 port_;
  181. QString user_;
  182. QString token_;
  183. QString session_;
  184. #if defined(NO_SSL)
  185. QTcpSocket *socket_;
  186. #else
  187. QSslSocket *socket_;
  188. #endif
  189. bool isAuthenticated_;
  190. LOBBY_ERROR lastError_;
  191. CALL_RESULT callResult_;
  192. RETURN_CODES call_(const char* command, const QVariantMap& kwargs = QVariantMap());
  193. void freeCallResult_()
  194. {
  195. callResult_.code = LOBBY_NO_ERROR;
  196. callResult_.result.clear();
  197. }
  198. RETURN_CODES setError_(const RETURN_CODES code, const char * message, ...);
  199. }; // class Client
  200. } // namespace Lobby
  201. #endif // #ifndef _netlobby_h