Uses a namespace "Lobby" instead of that annoying "LOBBY_" prefixes.

master
Rene Jochum 2011-04-29 14:14:48 +02:00
parent 739db7c881
commit c7497bf005
4 changed files with 668 additions and 650 deletions

File diff suppressed because it is too large Load Diff

View File

@ -29,157 +29,161 @@
#include "netsocket.h"
#include "bson/bson.h"
#define LOBBY_VERSION 4
/* NOTE: You also need to change this value in the
* masterservers settings - session_size!
*/
#define LOBBY_SESSION_SIZE 16+1
/* We limit usernames here to 40 chars,
* while the forums allow usernames with up to 255 characters.
*/
#define LOBBY_USERNAME_SIZE 40+1
enum LOBBY_ERROR
namespace Lobby
{
LOBBY_NO_ERROR = 0,
const int PROTOCOL = 4;
// Copied from XMLRPC for socketrpc
LOBBY_PARSE_ERROR = -32700,
LOBBY_SERVER_ERROR = -32600,
LOBBY_APPLICATION_ERROR = -32500,
LOBBY_TRANSPORT_ERROR = -32300,
/* NOTE: You also need to change this value in the
* masterservers settings - session_size!
*/
const int SESSION_SIZE = 16+1;
// Specific errors.
LOBBY_UNSUPPORTED_ENCODING = -32701,
LOBBY_METHOD_NOT_FOUND = -32601,
/* We limit usernames here to 40 chars,
* while the forums allows usernames with up to 255 characters.
*/
const int USERNAME_SIZE = 40+1;
// Custom error codes.
LOBBY_INVALID_DATA = -500,
LOBBY_LOGIN_REQUIRED = -405,
LOBBY_WRONG_LOGIN = -404,
LOBBY_NOT_ACCEPTABLE = -403,
LOBBY_NO_GAME = -402,
};
enum RETURN_CODES
{
NO_ERROR = 0,
// FIXME: Not sure if std::string is a good idea here,
// as multitint is passing them direct to iV_DrawText.
struct LOBBY_GAME
{
uint32_t port; ///< Port hosting on.
std::string host; ///< IPv4, IPv6 or DNS Name of the host.
std::string description; ///< Game Description.
uint32_t currentPlayers; ///< Number of joined players.
uint32_t maxPlayers; ///< Maximum number of players.
std::string versionstring; ///< Version string.
uint32_t game_version_major; ///< Minor NETCODE version.
uint32_t game_version_minor; ///< Major NETCODE version.
bool isPrivate; ///< Password protected?
std::string modlist; ///< display string for mods.
std::string mapname; ///< name of map hosted.
std::string hostplayer; ///< hosts playername.
};
// Copied from XMLRPC for socketrpc
PARSE_ERROR = -32700,
SERVER_ERROR = -32600,
APPLICATION_ERROR = -32500,
TRANSPORT_ERROR = -32300,
struct lobbyError
{
LOBBY_ERROR code;
char* message;
};
// Specific errors.
UNSUPPORTED_ENCODING = -32701,
METHOD_NOT_FOUND = -32601,
struct lobbyCallResult
{
LOBBY_ERROR code;
char* buffer;
const char* result;
};
// Custom error codes.
INVALID_DATA = -500,
LOGIN_REQUIRED = -405,
WRONG_LOGIN = -404,
NOT_ACCEPTABLE = -403,
NO_GAME = -402,
};
class LobbyClient {
public:
std::vector<LOBBY_GAME> games;
// FIXME: Not sure if std::string is a good idea here,
// as multiint is passing them to iV_DrawText.
struct GAME
{
uint32_t port; ///< Port hosting on.
std::string host; ///< IPv4, IPv6 or DNS Name of the host.
std::string description; ///< Game Description.
uint32_t currentPlayers; ///< Number of joined players.
uint32_t maxPlayers; ///< Maximum number of players.
std::string versionstring; ///< Version string.
uint32_t game_version_major; ///< Minor NETCODE version.
uint32_t game_version_minor; ///< Major NETCODE version.
bool isPrivate; ///< Password protected?
std::string modlist; ///< display string for mods.
std::string mapname; ///< name of map hosted.
std::string hostplayer; ///< hosts playername.
};
LobbyClient();
void stop();
struct ERROR
{
RETURN_CODES code;
char* message;
};
LOBBY_ERROR connect();
bool disconnect();
bool isConnected();
LOBBY_ERROR login(const std::string& password);
struct CALL_RESULT
{
RETURN_CODES code;
char* buffer;
const char* result;
};
LOBBY_ERROR addGame(char** result, const uint32_t port, const uint32_t maxPlayers,
const char* description, const char* versionstring,
const uint32_t game_version_major, const uint32_t game_version_minor,
const bool isPrivate, const char* modlist,
const char* mapname, const char* hostplayer);
class Client {
public:
std::vector<GAME> games;
LOBBY_ERROR delGame();
LOBBY_ERROR addPlayer(const unsigned int index, const char* name, const char* username, const char* session);
LOBBY_ERROR delPlayer(const unsigned int index);
LOBBY_ERROR updatePlayer(const unsigned int index, const char* name);
LOBBY_ERROR listGames(const int maxGames);
Client();
void stop();
LobbyClient& setHost(const std::string& host) { host_ = host; return *this; }
std::string getHost() const { return host_; }
RETURN_CODES connect();
bool disconnect();
bool isConnected();
RETURN_CODES login(const std::string& password);
LobbyClient& setPort(const uint32_t& port) { port_ = port; return *this; }
uint32_t getPort() { return port_; }
RETURN_CODES addGame(char** result, const uint32_t port, const uint32_t maxPlayers,
const char* description, const char* versionstring,
const uint32_t game_version_major, const uint32_t game_version_minor,
const bool isPrivate, const char* modlist,
const char* mapname, const char* hostplayer);
bool isAuthenticated() { return isAuthenticated_; }
RETURN_CODES delGame();
RETURN_CODES addPlayer(const unsigned int index, const char* name, const char* username, const char* session);
RETURN_CODES delPlayer(const unsigned int index);
RETURN_CODES updatePlayer(const unsigned int index, const char* name);
RETURN_CODES listGames(const int maxGames);
LobbyClient& setUser(const std::string& user) { user_ = user; return *this; }
std::string getUser() const { return user_; }
Client& setHost(const std::string& host) { host_ = host; return *this; }
std::string getHost() const { return host_; }
LobbyClient& setToken(const std::string& token) { token_ = token; return *this; }
std::string getToken() { return token_; }
Client& setPort(const uint32_t& port) { port_ = port; return *this; }
uint32_t getPort() { return port_; }
std::string getSession() const { return session_; }
bool isAuthenticated() { return isAuthenticated_; }
lobbyError* getError() { return &lastError_; }
Client& setUser(const std::string& user) { user_ = user; return *this; }
std::string getUser() const { return user_; }
void freeError()
{
if (lastError_.code == LOBBY_NO_ERROR)
Client& setToken(const std::string& token) { token_ = token; return *this; }
std::string getToken() { return token_; }
std::string getSession() const { return session_; }
ERROR* getError() { return &lastError_; }
void freeError()
{
return;
if (lastError_.code == NO_ERROR)
{
return;
}
lastError_.code = NO_ERROR;
free(lastError_.message);
lastError_.message = NULL;
}
lastError_.code = LOBBY_NO_ERROR;
free(lastError_.message);
lastError_.message = NULL;
}
private:
int64_t gameId_;
private:
int64_t gameId_;
uint32_t callId_;
uint32_t callId_;
std::string host_;
uint32_t port_;
std::string host_;
uint32_t port_;
std::string user_;
std::string token_;
std::string session_;
std::string user_;
std::string token_;
std::string session_;
Socket* socket_;
Socket* socket_;
bool isAuthenticated_;
bool isAuthenticated_;
ERROR lastError_;
CALL_RESULT callResult_;
lobbyError lastError_;
lobbyCallResult callResult_;
RETURN_CODES call_(const char* command, const bson* args, const bson* kwargs);
LOBBY_ERROR call_(const char* command, const bson* args, const bson* kwargs);
void freeCallResult_()
{
callResult_.code = NO_ERROR;
callResult_.result = NULL;
free(callResult_.buffer);
callResult_.buffer = NULL;
}
void freeCallResult_()
{
callResult_.code = LOBBY_NO_ERROR;
callResult_.result = NULL;
free(callResult_.buffer);
callResult_.buffer = NULL;
}
RETURN_CODES setError_(const RETURN_CODES code, char * message, ...);
}; // class Client
LOBBY_ERROR setError_(const LOBBY_ERROR code, char * message, ...);
};
} // namespace Lobby
extern Lobby::Client lobbyclient;
#endif // #ifndef _netlobby_h
extern LobbyClient lobbyclient;

View File

@ -1798,15 +1798,15 @@ static void NETallowJoining(void)
char ModList[modlist_string_size] = { '\0' };
char GamePassword[password_string_size] = { '\0' };
char *username = (char *)malloc(LOBBY_USERNAME_SIZE);
char *session = (char *)malloc(LOBBY_SESSION_SIZE);
char *username = (char *)malloc(Lobby::USERNAME_SIZE);
char *session = (char *)malloc(Lobby::SESSION_SIZE);
NETbeginDecode(NETnetTmpQueue(i), NET_JOIN);
NETstring(name, sizeof(name));
NETstring(ModList, sizeof(ModList));
NETstring(GamePassword, sizeof(GamePassword));
NETstring(username, LOBBY_USERNAME_SIZE);
NETstring(session, LOBBY_SESSION_SIZE);
NETstring(username, Lobby::USERNAME_SIZE);
NETstring(session, Lobby::SESSION_SIZE);
NETend();
tmp = NET_CreatePlayer(name);
@ -1872,7 +1872,7 @@ static void NETallowJoining(void)
// and we are authenticated.
if (rejected == 0 && lobbyclient.isAuthenticated())
{
if (lobbyclient.addPlayer(index, name, username, session) != LOBBY_NO_ERROR)
if (lobbyclient.addPlayer(index, name, username, session) != Lobby::NO_ERROR)
{
debug(LOG_INFO, "Lobby rejected player \"%s\", username \"%s\", session \"%s\", reason: %s",
name, username, session, lobbyclient.getError()->message);
@ -2046,10 +2046,10 @@ bool NEThostGame(const char* SessionName, const char* PlayerName,
// Register the game on the masterserver
if (lobbyclient.addGame(&motd, (uint32_t)gameserver_port, (uint32_t)NetPlay.maxPlayers,
SessionName, VersionString, NETCODE_VERSION_MAJOR, NETCODE_VERSION_MINOR,
NetPlay.GamePassworded, modlist, game.map, PlayerName) != LOBBY_NO_ERROR)
NetPlay.GamePassworded, modlist, game.map, PlayerName) != Lobby::NO_ERROR)
{
lobbyError* error = lobbyclient.getError();
if (error->code == LOBBY_LOGIN_REQUIRED)
Lobby::ERROR* error = lobbyclient.getError();
if (error->code == Lobby::LOGIN_REQUIRED)
{
asprintf(&motd, _("Game not in the lobby, please login first!"));
}
@ -2082,7 +2082,7 @@ bool NEThaltJoining(void)
debug(LOG_NET, "temporarily locking game to prevent more players");
allow_joining = false;
if (lobbyclient.delGame() != LOBBY_NO_ERROR)
if (lobbyclient.delGame() != Lobby::NO_ERROR)
{
lobbyclient.freeError();
}
@ -2100,10 +2100,10 @@ bool NETfindGame(void)
}
setLobbyError(ERROR_NOERROR);
if (lobbyclient.listGames(MaxGames) != LOBBY_NO_ERROR)
if (lobbyclient.listGames(MaxGames) != Lobby::NO_ERROR)
{
debug(LOG_ERROR, lobbyclient.getError()->message);
if (lobbyclient.getError()->code == LOBBY_LOGIN_REQUIRED)
if (lobbyclient.getError()->code == Lobby::LOGIN_REQUIRED)
{
setLobbyError(ERROR_AUTHENTICATION);
}
@ -2210,10 +2210,10 @@ bool NETjoinGame(const char* host, uint32_t port, const char* playername)
bsocket = tcp_socket;
socketBeginCompression(bsocket);
char *username = (char *)malloc(LOBBY_USERNAME_SIZE);
char *session = (char *)malloc(LOBBY_SESSION_SIZE);
strlcpy(username, lobbyclient.getUser().c_str(), LOBBY_USERNAME_SIZE);
strlcpy(session, lobbyclient.getSession().c_str(), LOBBY_SESSION_SIZE);
char *username = (char *)malloc(Lobby::USERNAME_SIZE);
char *session = (char *)malloc(Lobby::SESSION_SIZE);
strlcpy(username, lobbyclient.getUser().c_str(), Lobby::USERNAME_SIZE);
strlcpy(session, lobbyclient.getSession().c_str(), Lobby::SESSION_SIZE);
debug(LOG_NET, "Sending username \"%s\", session \"%s\"", username, lobbyclient.getSession().c_str());
@ -2222,8 +2222,8 @@ bool NETjoinGame(const char* host, uint32_t port, const char* playername)
NETstring(playername, 64);
NETstring(getModList(), modlist_string_size);
NETstring(NetPlay.gamePassword, sizeof(NetPlay.gamePassword));
NETstring(username, LOBBY_USERNAME_SIZE);
NETstring(session, LOBBY_SESSION_SIZE);
NETstring(username, Lobby::USERNAME_SIZE);
NETstring(session, Lobby::SESSION_SIZE);
NETend();
free(username);

View File

@ -984,7 +984,7 @@ void runGameFind(void )
UDWORD id;
static UDWORD lastupdate=0;
static char game_password[64]; // check if StringSize is available
LOBBY_GAME* lGame;
Lobby::GAME* lGame;
if (!loginDone && !EnablePasswordPrompt)
{
@ -1045,7 +1045,7 @@ void runGameFind(void )
case CON_LOGIN_YES:
loginDone = true;
if (lobbyclient.setUser(lobbyUser).login(lobbyPass) != LOBBY_NO_ERROR)
if (lobbyclient.setUser(lobbyUser).login(lobbyPass) != Lobby::NO_ERROR)
{
setLobbyError(ERROR_AUTHENTICATION);
addGames(); // Draws the error
@ -3813,7 +3813,7 @@ void displayRemoteGame(WIDGET *psWidget, UDWORD xOffset, UDWORD yOffset, PIELIGH
return;
}
LOBBY_GAME* lGame = &lobbyclient.games[gameNumber];
Lobby::GAME* lGame = &lobbyclient.games[gameNumber];
// Draw blue boxes.
drawBlueBox(x,y,psWidget->width,psWidget->height);