Commit d3b96b1a authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

PB: server compiles again, standalone RemoteClient is able to log in

parent 10018280
...@@ -88,8 +88,6 @@ HEADERS += src/abstractcounter.h \ ...@@ -88,8 +88,6 @@ HEADERS += src/abstractcounter.h \
../common/serializable_item.h \ ../common/serializable_item.h \
../common/decklist.h \ ../common/decklist.h \
../common/protocol.h \ ../common/protocol.h \
../common/protocol_items.h \
../common/protocol_datastructures.h \
../common/rng_abstract.h \ ../common/rng_abstract.h \
../common/rng_sfmt.h \ ../common/rng_sfmt.h \
../common/server.h \ ../common/server.h \
...@@ -170,12 +168,9 @@ SOURCES += src/abstractcounter.cpp \ ...@@ -170,12 +168,9 @@ SOURCES += src/abstractcounter.cpp \
src/localclient.cpp \ src/localclient.cpp \
src/priceupdater.cpp \ src/priceupdater.cpp \
src/soundengine.cpp \ src/soundengine.cpp \
src/pending_command.cpp \
../common/serializable_item.cpp \ ../common/serializable_item.cpp \
../common/decklist.cpp \ ../common/decklist.cpp \
../common/protocol.cpp \ ../common/protocol.cpp \
../common/protocol_items.cpp \
../common/protocol_datastructures.cpp \
../common/rng_abstract.cpp \ ../common/rng_abstract.cpp \
../common/rng_sfmt.cpp \ ../common/rng_sfmt.cpp \
../common/sfmt/SFMT.c \ ../common/sfmt/SFMT.c \
......
#include "abstractclient.h" #include "abstractclient.h"
#include "protocol.h" #include "protocol.h"
#include "protocol_items.h"
#include "pending_command.h" #include "pending_command.h"
#include "pb/commands.pb.h" #include "pb/commands.pb.h"
#include "pb/event_server_identification.pb.h"
#include "pb/event_server_message.pb.h"
#include "pb/event_server_shutdown.pb.h"
#include "pb/event_connection_closed.pb.h"
#include "pb/event_user_message.pb.h"
#include "pb/event_list_rooms.pb.h"
#include "pb/event_add_to_list.pb.h"
#include "pb/event_remove_from_list.pb.h"
#include "pb/event_user_joined.pb.h"
#include "pb/event_user_left.pb.h"
#include "pb/event_game_joined.pb.h"
#include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.h>
AbstractClient::AbstractClient(QObject *parent) AbstractClient::AbstractClient(QObject *parent)
...@@ -15,57 +25,54 @@ AbstractClient::~AbstractClient() ...@@ -15,57 +25,54 @@ AbstractClient::~AbstractClient()
{ {
} }
void AbstractClient::processProtocolItem(ProtocolItem *item) void AbstractClient::processProtocolItem(const ServerMessage &item)
{ {
ProtocolResponse *response = qobject_cast<ProtocolResponse *>(item); switch (item.message_type()) {
if (response) { case ServerMessage::RESPONSE: {
const int cmdId = response->getCmdId(); const Response &response = item.response();
PendingCommand *pend = pendingCommands.value(cmdId, 0); const int cmdId = response.cmd_id();
if (!pend) PendingCommand *pend = pendingCommands.value(cmdId, 0);
return; if (!pend)
return;
pendingCommands.remove(cmdId);
pend->processResponse(response); pendingCommands.remove(cmdId);
if (response->getReceiverMayDelete()) pend->processResponse(response);
delete response; pend->deleteLater();
pend->deleteLater(); break;
}
return; case ServerMessage::SESSION_EVENT: {
} const SessionEvent &event = item.session_event();
std::vector< const ::google::protobuf::FieldDescriptor * > fieldList;
GenericEvent *genericEvent = qobject_cast<GenericEvent *>(item); event.GetReflection()->ListFields(event, &fieldList);
if (genericEvent) { int num = 0;
switch (genericEvent->getItemId()) { for (unsigned int j = 0; j < fieldList.size(); ++j)
case ItemId_Event_ConnectionClosed: emit connectionClosedEventReceived(static_cast<Event_ConnectionClosed *>(item)); break; if (fieldList[j]->is_extension()) {
case ItemId_Event_ServerShutdown: emit serverShutdownEventReceived(static_cast<Event_ServerShutdown *>(item)); break; num = fieldList[j]->number();
case ItemId_Event_AddToList: emit addToListEventReceived(static_cast<Event_AddToList *>(item)); break; break;
case ItemId_Event_RemoveFromList: emit removeFromListEventReceived(static_cast<Event_RemoveFromList *>(item)); break; }
case ItemId_Event_UserJoined: emit userJoinedEventReceived(static_cast<Event_UserJoined *>(item)); break; switch ((SessionEvent::SessionEventType) num) {
case ItemId_Event_UserLeft: emit userLeftEventReceived(static_cast<Event_UserLeft *>(item)); break; case SessionEvent::SERVER_IDENTIFICATION: emit serverIdentificationEventReceived(event.GetExtension(Event_ServerIdentification::ext)); break;
case ItemId_Event_ServerMessage: emit serverMessageEventReceived(static_cast<Event_ServerMessage *>(item)); break; case SessionEvent::SERVER_MESSAGE: emit serverMessageEventReceived(event.GetExtension(Event_ServerMessage::ext)); break;
case ItemId_Event_ListRooms: emit listRoomsEventReceived(static_cast<Event_ListRooms *>(item)); break; case SessionEvent::SERVER_SHUTDOWN: emit serverShutdownEventReceived(event.GetExtension(Event_ServerShutdown::ext)); break;
case ItemId_Event_GameJoined: emit gameJoinedEventReceived(static_cast<Event_GameJoined *>(item)); break; case SessionEvent::CONNECTION_CLOSED: emit connectionClosedEventReceived(event.GetExtension(Event_ConnectionClosed::ext)); break;
case ItemId_Event_Message: emit messageEventReceived(static_cast<Event_Message *>(item)); break; case SessionEvent::USER_MESSAGE: emit userMessageEventReceived(event.GetExtension(Event_UserMessage::ext)); break;
case SessionEvent::LIST_ROOMS: emit listRoomsEventReceived(event.GetExtension(Event_ListRooms::ext)); break;
case SessionEvent::ADD_TO_LIST: emit addToListEventReceived(event.GetExtension(Event_AddToList::ext)); break;
case SessionEvent::REMOVE_FROM_LIST: emit removeFromListEventReceived(event.GetExtension(Event_RemoveFromList::ext)); break;
case SessionEvent::USER_JOINED: emit userJoinedEventReceived(event.GetExtension(Event_UserJoined::ext)); break;
case SessionEvent::USER_LEFT: emit userLeftEventReceived(event.GetExtension(Event_UserLeft::ext)); break;
case SessionEvent::GAME_JOINED: emit gameJoinedEventReceived(event.GetExtension(Event_GameJoined::ext)); break;
}
break;
}
case ServerMessage::GAME_EVENT_CONTAINER: {
emit gameEventContainerReceived(item.game_event_container());
break;
}
case ServerMessage::ROOM_EVENT: {
emit roomEventReceived(item.room_event());
break;
} }
if (genericEvent->getReceiverMayDelete())
delete genericEvent;
return;
}
GameEventContainer *gameEventContainer = qobject_cast<GameEventContainer *>(item);
if (gameEventContainer) {
emit gameEventContainerReceived(gameEventContainer);
if (gameEventContainer->getReceiverMayDelete())
delete gameEventContainer;
return;
}
RoomEvent *roomEvent = qobject_cast<RoomEvent *>(item);
if (roomEvent) {
emit roomEventReceived(roomEvent);
if (roomEvent->getReceiverMayDelete())
delete roomEvent;
return;
} }
} }
......
...@@ -3,17 +3,15 @@ ...@@ -3,17 +3,15 @@
#include <QObject> #include <QObject>
#include <QVariant> #include <QVariant>
#include <google/protobuf/message.h> #include "pb/response.pb.h"
#include "protocol_datastructures.h" #include "pb/serverinfo_user.pb.h"
class PendingCommand; class PendingCommand;
class CommandContainer; class CommandContainer;
class ProtocolItem;
class ProtocolResponse;
class TopLevelProtocolItem;
class CommandContainer;
class RoomEvent; class RoomEvent;
class GameEventContainer; class GameEventContainer;
class ServerMessage;
class Event_ServerIdentification;
class Event_AddToList; class Event_AddToList;
class Event_RemoveFromList; class Event_RemoveFromList;
class Event_UserJoined; class Event_UserJoined;
...@@ -21,7 +19,7 @@ class Event_UserLeft; ...@@ -21,7 +19,7 @@ class Event_UserLeft;
class Event_ServerMessage; class Event_ServerMessage;
class Event_ListRooms; class Event_ListRooms;
class Event_GameJoined; class Event_GameJoined;
class Event_Message; class Event_UserMessage;
class Event_ConnectionClosed; class Event_ConnectionClosed;
class Event_ServerShutdown; class Event_ServerShutdown;
...@@ -38,30 +36,31 @@ class AbstractClient : public QObject { ...@@ -38,30 +36,31 @@ class AbstractClient : public QObject {
Q_OBJECT Q_OBJECT
signals: signals:
void statusChanged(ClientStatus _status); void statusChanged(ClientStatus _status);
void serverError(ResponseCode resp); void serverError(Response::ResponseCode resp);
// Room events // Room events
void roomEventReceived(RoomEvent *event); void roomEventReceived(const RoomEvent &event);
// Game events // Game events
void gameEventContainerReceived(GameEventContainer *event); void gameEventContainerReceived(const GameEventContainer &event);
// Generic events // Session events
void connectionClosedEventReceived(Event_ConnectionClosed *event); void serverIdentificationEventReceived(const Event_ServerIdentification &event);
void serverShutdownEventReceived(Event_ServerShutdown *event); void connectionClosedEventReceived(const Event_ConnectionClosed &event);
void addToListEventReceived(Event_AddToList *event); void serverShutdownEventReceived(const Event_ServerShutdown &event);
void removeFromListEventReceived(Event_RemoveFromList *event); void addToListEventReceived(const Event_AddToList &event);
void userJoinedEventReceived(Event_UserJoined *event); void removeFromListEventReceived(const Event_RemoveFromList &event);
void userLeftEventReceived(Event_UserLeft *event); void userJoinedEventReceived(const Event_UserJoined &event);
void serverMessageEventReceived(Event_ServerMessage *event); void userLeftEventReceived(const Event_UserLeft &event);
void listRoomsEventReceived(Event_ListRooms *event); void serverMessageEventReceived(const Event_ServerMessage &event);
void gameJoinedEventReceived(Event_GameJoined *event); void listRoomsEventReceived(const Event_ListRooms &event);
void messageEventReceived(Event_Message *event); void gameJoinedEventReceived(const Event_GameJoined &event);
void userInfoChanged(ServerInfo_User *userInfo); void userMessageEventReceived(const Event_UserMessage &event);
void buddyListReceived(const QList<ServerInfo_User *> &buddyList); void userInfoChanged(const ServerInfo_User &userInfo);
void ignoreListReceived(const QList<ServerInfo_User *> &ignoreList); void buddyListReceived(const QList<ServerInfo_User> &buddyList);
void ignoreListReceived(const QList<ServerInfo_User> &ignoreList);
private: private:
int nextCmdId; int nextCmdId;
protected slots: protected slots:
void processProtocolItem(ProtocolItem *item); void processProtocolItem(const ServerMessage &item);
protected: protected:
QMap<int, PendingCommand *> pendingCommands; QMap<int, PendingCommand *> pendingCommands;
ClientStatus status; ClientStatus status;
......
...@@ -125,19 +125,19 @@ void DlgCreateGame::actOK() ...@@ -125,19 +125,19 @@ void DlgCreateGame::actOK()
} }
PendingCommand *pend = room->prepareRoomCommand(cmd); PendingCommand *pend = room->prepareRoomCommand(cmd);
connect(pend, SIGNAL(finished(ResponseCode)), this, SLOT(checkResponse(ResponseCode))); connect(pend, SIGNAL(finished(Response::ResponseCode)), this, SLOT(checkResponse(Response::ResponseCode)));
room->sendRoomCommand(pend); room->sendRoomCommand(pend);
okButton->setEnabled(false); okButton->setEnabled(false);
cancelButton->setEnabled(false); cancelButton->setEnabled(false);
} }
void DlgCreateGame::checkResponse(ResponseCode response) void DlgCreateGame::checkResponse(Response::ResponseCode response)
{ {
okButton->setEnabled(true); okButton->setEnabled(true);
cancelButton->setEnabled(true); cancelButton->setEnabled(true);
if (response == RespOk) if (response == Response::RespOk)
accept(); accept();
else { else {
QMessageBox::critical(this, tr("Error"), tr("Server error.")); QMessageBox::critical(this, tr("Error"), tr("Server error."));
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
#define DLG_CREATEGAME_H #define DLG_CREATEGAME_H
#include <QDialog> #include <QDialog>
#include "protocol_datastructures.h" #include <QMap>
#include "pb/response.pb.h"
class QLabel; class QLabel;
class QLineEdit; class QLineEdit;
...@@ -18,7 +19,7 @@ public: ...@@ -18,7 +19,7 @@ public:
DlgCreateGame(TabRoom *_room, const QMap<int, QString> &_gameTypes, QWidget *parent = 0); DlgCreateGame(TabRoom *_room, const QMap<int, QString> &_gameTypes, QWidget *parent = 0);
private slots: private slots:
void actOK(); void actOK();
void checkResponse(ResponseCode response); void checkResponse(Response::ResponseCode response);
void spectatorsAllowedChanged(int state); void spectatorsAllowedChanged(int state);
private: private:
TabRoom *room; TabRoom *room;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
LocalClient::LocalClient(LocalServerInterface *_lsi, const QString &_playerName, QObject *parent) LocalClient::LocalClient(LocalServerInterface *_lsi, const QString &_playerName, QObject *parent)
: AbstractClient(parent), lsi(_lsi) : AbstractClient(parent), lsi(_lsi)
{ {
connect(lsi, SIGNAL(itemToClient(ProtocolItem *)), this, SLOT(itemFromServer(ProtocolItem *))); connect(lsi, SIGNAL(itemToClient(const ServerMessage &)), this, SLOT(itemFromServer(const ServerMessage &)));
Command_Login loginCmd; Command_Login loginCmd;
loginCmd.set_user_name(_playerName.toStdString()); loginCmd.set_user_name(_playerName.toStdString());
...@@ -27,7 +27,7 @@ void LocalClient::sendCommandContainer(const CommandContainer &cont) ...@@ -27,7 +27,7 @@ void LocalClient::sendCommandContainer(const CommandContainer &cont)
lsi->itemFromClient(cont); lsi->itemFromClient(cont);
} }
void LocalClient::itemFromServer(ProtocolItem *item) void LocalClient::itemFromServer(const ServerMessage &item)
{ {
processProtocolItem(item); processProtocolItem(item);
} }
...@@ -14,11 +14,8 @@ public: ...@@ -14,11 +14,8 @@ public:
~LocalClient(); ~LocalClient();
void sendCommandContainer(const CommandContainer &cont); void sendCommandContainer(const CommandContainer &cont);
private slots: private slots:
void itemFromServer(ProtocolItem *item); void itemFromServer(const ServerMessage &item);
signals:
void itemToServer(ProtocolItem *item);
}; };
#endif #endif
\ No newline at end of file
...@@ -20,7 +20,9 @@ LocalServerInterface *LocalServer::newConnection() ...@@ -20,7 +20,9 @@ LocalServerInterface *LocalServer::newConnection()
return lsi; return lsi;
} }
ServerInfo_User *LocalServer::getUserData(const QString &name) ServerInfo_User LocalServer::getUserData(const QString &name)
{ {
return new ServerInfo_User(name); ServerInfo_User result;
result.set_name(name.toStdString());
return result;
} }
\ No newline at end of file
...@@ -23,9 +23,11 @@ protected: ...@@ -23,9 +23,11 @@ protected:
int startSession(const QString & /*userName*/, const QString & /*address*/) { return -1; } int startSession(const QString & /*userName*/, const QString & /*address*/) { return -1; }
void endSession(int /*sessionId*/) { } void endSession(int /*sessionId*/) { }
bool userExists(const QString & /*name*/) { return false; } bool userExists(const QString & /*name*/) { return false; }
ServerInfo_User *getUserData(const QString &name); ServerInfo_User getUserData(const QString &name);
QMap<QString, ServerInfo_User *> getBuddyList(const QString & /*name*/) { return QMap<QString, ServerInfo_User *>(); } QMap<QString, ServerInfo_User> getBuddyList(const QString & /*name*/) { return QMap<QString, ServerInfo_User>(); }
QMap<QString, ServerInfo_User *> getIgnoreList(const QString & /*name*/) { return QMap<QString, ServerInfo_User *>(); } QMap<QString, ServerInfo_User> getIgnoreList(const QString & /*name*/) { return QMap<QString, ServerInfo_User>(); }
bool isInBuddyList(const QString & /*whoseList*/, const QString & /*who*/) { return false; }
bool isInIgnoreList(const QString & /*whoseList*/, const QString & /*who*/) { return false; }
}; };
#endif #endif
\ No newline at end of file
...@@ -12,16 +12,12 @@ LocalServerInterface::~LocalServerInterface() ...@@ -12,16 +12,12 @@ LocalServerInterface::~LocalServerInterface()
prepareDestroy(); prepareDestroy();
} }
void LocalServerInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem) void LocalServerInterface::transmitProtocolItem(const ServerMessage &item)
{ {
item->setReceiverMayDelete(false);
emit itemToClient(item); emit itemToClient(item);
if (deleteItem)
delete item;
} }
#include "pb/commands.pb.h"
void LocalServerInterface::itemFromClient(const CommandContainer &item) void LocalServerInterface::itemFromClient(const CommandContainer &item)
{ {
qDebug() << "READ" << QString::fromStdString(item.ShortDebugString());
processCommandContainer(item); processCommandContainer(item);
} }
...@@ -10,27 +10,25 @@ class LocalServerInterface : public Server_ProtocolHandler ...@@ -10,27 +10,25 @@ class LocalServerInterface : public Server_ProtocolHandler
Q_OBJECT Q_OBJECT
private: private:
DeckList *getDeckFromDatabase(int /*deckId*/) { return 0; } DeckList *getDeckFromDatabase(int /*deckId*/) { return 0; }
ResponseCode cmdAddToList(const Command_AddToList & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdAddToList(const Command_AddToList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdRemoveFromList(const Command_RemoveFromList & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdRemoveFromList(const Command_RemoveFromList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckList(const Command_DeckList & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckList(const Command_DeckList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckNewDir(const Command_DeckNewDir & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckNewDir(const Command_DeckNewDir & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckDelDir(const Command_DeckDelDir & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckDelDir(const Command_DeckDelDir & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckDel(const Command_DeckDel & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckDel(const Command_DeckDel & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckUpload(const Command_DeckUpload & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckUpload(const Command_DeckUpload & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdDeckDownload(const Command_DeckDownload & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdDeckDownload(const Command_DeckDownload & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdBanFromServer(const Command_BanFromServer & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdBanFromServer(const Command_BanFromServer & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdShutdownServer(const Command_ShutdownServer & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdShutdownServer(const Command_ShutdownServer & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
ResponseCode cmdUpdateServerMessage(const Command_UpdateServerMessage & /*cmd*/, BlaContainer * /*bla*/) { return RespFunctionNotAllowed; } Response::ResponseCode cmdUpdateServerMessage(const Command_UpdateServerMessage & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; }
protected:
bool getCompressionSupport() const { return false; }
public: public:
LocalServerInterface(LocalServer *_server); LocalServerInterface(LocalServer *_server);
~LocalServerInterface(); ~LocalServerInterface();
QString getAddress() const { return QString(); } QString getAddress() const { return QString(); }
void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); void transmitProtocolItem(const ServerMessage &item);
signals: signals:
void itemToClient(ProtocolItem *item); void itemToClient(const ServerMessage &item);
public slots: public slots:
void itemFromClient(const CommandContainer &item); void itemFromClient(const CommandContainer &item);
}; };
......
#include "pending_command.h"
#include "protocol.h"
void PendingCommand::processResponse(ProtocolResponse *response)
{
emit finished(response);
emit finished(response->getResponseCode());
}
#ifndef PENDING_COMMAND_H #ifndef PENDING_COMMAND_H
#define PENDING_COMMAND_H #define PENDING_COMMAND_H
#include "protocol_datastructures.h"
#include "pb/commands.pb.h" #include "pb/commands.pb.h"
#include "pb/response.pb.h"
#include <QVariant> #include <QVariant>
class ProtocolResponse;
class PendingCommand : public QObject { class PendingCommand : public QObject {
Q_OBJECT Q_OBJECT
signals: signals:
void finished(ProtocolResponse *response); void finished(const Response &response);
void finished(ResponseCode response); void finished(Response::ResponseCode response);
private: private:
CommandContainer commandContainer; CommandContainer commandContainer;
QVariant extraData; QVariant extraData;
...@@ -22,7 +20,11 @@ public: ...@@ -22,7 +20,11 @@ public:
CommandContainer &getCommandContainer() { return commandContainer; } CommandContainer &getCommandContainer() { return commandContainer; }
void setExtraData(const QVariant &_extraData) { extraData = _extraData; } void setExtraData(const QVariant &_extraData) { extraData = _extraData; }
QVariant getExtraData() const { return extraData; } QVariant getExtraData() const { return extraData; }
void processResponse(ProtocolResponse *response); void processResponse(const Response &response)
{
emit finished(response);
emit finished(response.response_code());
}
int tick() { return ++ticks; } int tick() { return ++ticks; }
}; };
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "handcounter.h" #include "handcounter.h"
#include "cardlist.h" #include "cardlist.h"
#include "tab_game.h" #include "tab_game.h"
#include "protocol_items.h"
#include "gamescene.h" #include "gamescene.h"
#include "settingscache.h" #include "settingscache.h"
#include "dlg_create_token.h" #include "dlg_create_token.h"
......
#include <QTimer> #include <QTimer>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QCryptographicHash>
#include "remoteclient.h" #include "remoteclient.h"
#include "protocol.h" #include "protocol.h"
#include "protocol_items.h"
#include "pending_command.h" #include "pending_command.h"
#include "pb/commands.pb.h" #include "pb/commands.pb.h"
#include "pb/session_commands.pb.h" #include "pb/session_commands.pb.h"
#include "pb/response_login.pb.h"
RemoteClient::RemoteClient(QObject *parent) RemoteClient::RemoteClient(QObject *parent)
: AbstractClient(parent), timeRunning(0), lastDataReceived(0), topLevelItem(0) : AbstractClient(parent), timeRunning(0), lastDataReceived(0), messageInProgress(false), messageLength(0)
{ {
ProtocolItem::initializeHash();
timer = new QTimer(this); timer = new QTimer(this);
timer->setInterval(1000); timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(ping())); connect(timer, SIGNAL(timeout()), this, SLOT(ping()));
...@@ -24,7 +19,7 @@ RemoteClient::RemoteClient(QObject *parent) ...@@ -24,7 +19,7 @@ RemoteClient::RemoteClient(QObject *parent)
connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError))); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError)));
xmlReader = new QXmlStreamReader; connect(this, SIGNAL(serverIdentificationEventReceived(const Event_ServerIdentification &)), this, SLOT(processServerIdentificationEvent(const Event_ServerIdentification &)));
} }
RemoteClient::~RemoteClient() RemoteClient::~RemoteClient()
...@@ -46,61 +41,72 @@ void RemoteClient::slotConnected() ...@@ -46,61 +41,72 @@ void RemoteClient::slotConnected()
setStatus(StatusAwaitingWelcome); setStatus(StatusAwaitingWelcome);
} }
void RemoteClient::loginResponse(ProtocolResponse *response) void RemoteClient::processServerIdentificationEvent(const Event_ServerIdentification & /*event*/)
{ {
if (response->getResponseCode() == RespOk) { setStatus(StatusLoggingIn);
Response_Login *resp = qobject_cast<Response_Login *>(response);
if (!resp) { Command_Login cmdLogin;
disconnectFromServer(); cmdLogin.set_user_name(userName.toStdString());
return; cmdLogin.set_password(password.toStdString());
}
PendingCommand *pend = prepareSessionCommand(cmdLogin);
connect(pend, SIGNAL(finished(const Response &)), this, SLOT(loginResponse(const Response &)));
sendCommand(pend);
}
void RemoteClient::loginResponse(const Response &response)
{
if (response.response_code() == Response::RespOk) {
const Response_Login &resp = response.GetExtension(Response_Login::ext);
setStatus(StatusLoggedIn); setStatus(StatusLoggedIn);
emit userInfoChanged(resp->getUserInfo()); emit userInfoChanged(resp.user_info());
emit buddyListReceived(resp->getBuddyList());
emit ignoreListReceived(resp->getIgnoreList()); QList<ServerInfo_User> buddyList;
for (int i = resp.buddy_list_size() - 1; i >= 0; --i)
buddyList.append(resp.buddy_list(i));
emit buddyListReceived(buddyList);
QList<ServerInfo_User> ignoreList;
for (int i = resp.ignore_list_size() - 1; i >= 0; --i)
ignoreList.append(resp.ignore_list(i));
emit ignoreListReceived(ignoreList);
} else { } else {
emit serverError(response->getResponseCode()); emit serverError(response.response_code());
setStatus(StatusDisconnecting); setStatus(StatusDisconnecting);
} }
} }
void RemoteClient::readData() void RemoteClient::readData()
{ {
QByteArray data = socket->readAll();
qDebug() << data;
xmlReader->addData(data);
lastDataReceived = timeRunning; lastDataReceived = timeRunning;
QByteArray data = socket->readAll();
while (!xmlReader->atEnd()) { inputBuffer.append(data);
xmlReader->readNext();
if (topLevelItem) do {
topLevelItem->readElement(xmlReader); if (!messageInProgress) {
else if (xmlReader->isStartElement() && (xmlReader->name().toString() == "cockatrice_server_stream")) { if (inputBuffer.size() >= 4) {
int serverVersion = xmlReader->attributes().value("version").toString().toInt(); messageLength = (((quint32) (unsigned char) inputBuffer[0]) << 24)
if (serverVersion != ProtocolItem::protocolVersion) { + (((quint32) (unsigned char) inputBuffer[1]) << 16)
emit protocolVersionMismatch(ProtocolItem::protocolVersion, serverVersion); + (((quint32) (unsigned char) inputBuffer[2]) << 8)
disconnectFromServer(); + ((quint32) (unsigned char) inputBuffer[3]);
inputBuffer.remove(0, 4);
messageInProgress = true;
} else
return; return;
}
/* xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("cockatrice_client_stream");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
xmlWriter->writeAttribute("comp", "1");
*/
topLevelItem = new TopLevelProtocolItem;
connect(topLevelItem, SIGNAL(protocolItemReceived(ProtocolItem *)), this, SLOT(processProtocolItem(ProtocolItem *)));
setStatus(StatusLoggingIn);
Command_Login cmdLogin;
cmdLogin.set_user_name(userName.toStdString());
cmdLogin.set_password(password.toStdString());
PendingCommand *pend = prepareSessionCommand(cmdLogin);
connect(pend, SIGNAL(finished(ProtocolResponse *)), this, SLOT(loginResponse(ProtocolResponse *)));
sendCommand(pend);
} }
} if (inputBuffer.size() < messageLength)
return;
ServerMessage newServerMessage;
newServerMessage.ParseFromArray(inputBuffer.data(), messageLength);
qDebug(QString::fromStdString(newServerMessage.ShortDebugString()).toUtf8());
inputBuffer.remove(0, messageLength);
messageInProgress = false;
processProtocolItem(newServerMessage);
} while (!inputBuffer.isEmpty());
if (status == StatusDisconnecting) if (status == StatusDisconnecting)
disconnectFromServer(); disconnectFromServer();
} }
...@@ -131,12 +137,10 @@ void RemoteClient::connectToServer(const QString &hostname, unsigned int port, c ...@@ -131,12 +137,10 @@ void RemoteClient::connectToServer(const QString &hostname, unsigned int port, c
void RemoteClient::disconnectFromServer() void RemoteClient::disconnectFromServer()
{ {
delete topLevelItem;
topLevelItem = 0;
xmlReader->clear();
timer->stop(); timer->stop();
messageInProgress = false;
messageLength = 0;
QList<PendingCommand *> pc = pendingCommands.values(); QList<PendingCommand *> pc = pendingCommands.values();
for (int i = 0; i < pc.size(); i++) for (int i = 0; i < pc.size(); i++)
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#include "abstractclient.h" #include "abstractclient.h"
class QTimer; class QTimer;
class QXmlStreamReader;
class QXmlStreamWriter;
class RemoteClient : public AbstractClient { class RemoteClient : public AbstractClient {
Q_OBJECT Q_OBJECT
...@@ -21,15 +19,18 @@ private slots: ...@@ -21,15 +19,18 @@ private slots:
void readData(); void readData();
void slotSocketError(QAbstractSocket::SocketError error); void slotSocketError(QAbstractSocket::SocketError error);
void ping(); void ping();
void loginResponse(ProtocolResponse *response); void processServerIdentificationEvent(const Event_ServerIdentification &event);
void loginResponse(const Response &response);
private: private:
static const int maxTimeout = 10; static const int maxTimeout = 10;
int timeRunning, lastDataReceived; int timeRunning, lastDataReceived;
QByteArray inputBuffer;
bool messageInProgress;
int messageLength;
QTimer *timer; QTimer *timer;
QTcpSocket *socket; QTcpSocket *socket;
QXmlStreamReader *xmlReader;
TopLevelProtocolItem *topLevelItem;
void sendCommandContainer(const CommandContainer &cont); void sendCommandContainer(const CommandContainer &cont);
public: public:
......
...@@ -51,7 +51,7 @@ void MainWindow::updateTabMenu(QMenu *menu) ...@@ -51,7 +51,7 @@ void MainWindow::updateTabMenu(QMenu *menu)
void MainWindow::processConnectionClosedEvent(Event_ConnectionClosed *event) void MainWindow::processConnectionClosedEvent(Event_ConnectionClosed *event)
{ {
QString reason = event->getReason(); /* QString reason = event->getReason();
client->disconnectFromServer(); client->disconnectFromServer();
QString reasonStr; QString reasonStr;
if (reason == "too_many_connections") if (reason == "too_many_connections")
...@@ -63,11 +63,13 @@ void MainWindow::processConnectionClosedEvent(Event_ConnectionClosed *event) ...@@ -63,11 +63,13 @@ void MainWindow::processConnectionClosedEvent(Event_ConnectionClosed *event)
else else
reasonStr = tr("Unknown reason."); reasonStr = tr("Unknown reason.");
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr)); QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
*/
} }
void MainWindow::processServerShutdownEvent(Event_ServerShutdown *event) void MainWindow::processServerShutdownEvent(Event_ServerShutdown *event)
{ {
QMessageBox::information(this, tr("Scheduled server shutdown"), tr("The server is going to be restarted in %n minute(s).\nAll running games will be lost.\nReason for shutdown: %1", "", event->getMinutes()).arg(event->getReason())); /* QMessageBox::information(this, tr("Scheduled server shutdown"), tr("The server is going to be restarted in %n minute(s).\nAll running games will be lost.\nReason for shutdown: %1", "", event->getMinutes()).arg(event->getReason()));
* */
} }
void MainWindow::statusChanged(ClientStatus _status) void MainWindow::statusChanged(ClientStatus _status)
...@@ -199,11 +201,11 @@ void MainWindow::serverTimeout() ...@@ -199,11 +201,11 @@ void MainWindow::serverTimeout()
QMessageBox::critical(this, tr("Error"), tr("Server timeout")); QMessageBox::critical(this, tr("Error"), tr("Server timeout"));
} }
void MainWindow::serverError(ResponseCode r) void MainWindow::serverError(Response::ResponseCode r)
{ {
switch (r) { switch (r) {
case RespWrongPassword: QMessageBox::critical(this, tr("Error"), tr("Invalid login data.")); break; case Response::RespWrongPassword: QMessageBox::critical(this, tr("Error"), tr("Invalid login data.")); break;
case RespWouldOverwriteOldSession: QMessageBox::critical(this, tr("Error"), tr("There is already an active session using this user name.\nPlease close that session first and re-login.")); break; case Response::RespWouldOverwriteOldSession: QMessageBox::critical(this, tr("Error"), tr("There is already an active session using this user name.\nPlease close that session first and re-login.")); break;
default: ; default: ;
} }
} }
...@@ -302,7 +304,7 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -302,7 +304,7 @@ MainWindow::MainWindow(QWidget *parent)
client = new RemoteClient(this); client = new RemoteClient(this);
connect(client, SIGNAL(connectionClosedEventReceived(Event_ConnectionClosed *)), this, SLOT(processConnectionClosedEvent(Event_ConnectionClosed *))); connect(client, SIGNAL(connectionClosedEventReceived(Event_ConnectionClosed *)), this, SLOT(processConnectionClosedEvent(Event_ConnectionClosed *)));
connect(client, SIGNAL(serverShutdownEventReceived(Event_ServerShutdown *)), this, SLOT(processServerShutdownEvent(Event_ServerShutdown *))); connect(client, SIGNAL(serverShutdownEventReceived(Event_ServerShutdown *)), this, SLOT(processServerShutdownEvent(Event_ServerShutdown *)));
connect(client, SIGNAL(serverError(ResponseCode)), this, SLOT(serverError(ResponseCode))); connect(client, SIGNAL(serverError(Response::ResponseCode)), this, SLOT(serverError(Response::ResponseCode)));
connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &))); connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &)));
connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout())); connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout()));
connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus))); connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus)));
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include <QMainWindow> #include <QMainWindow>
#include "abstractclient.h" #include "abstractclient.h"
#include "protocol_datastructures.h" #include "pb/response.pb.h"
class TabSupervisor; class TabSupervisor;
class RemoteClient; class RemoteClient;
...@@ -38,7 +38,7 @@ private slots: ...@@ -38,7 +38,7 @@ private slots:
void processConnectionClosedEvent(Event_ConnectionClosed *event); void processConnectionClosedEvent(Event_ConnectionClosed *event);
void processServerShutdownEvent(Event_ServerShutdown *event); void processServerShutdownEvent(Event_ServerShutdown *event);
void serverTimeout(); void serverTimeout();
void serverError(ResponseCode r); void serverError(Response::ResponseCode r);
void socketError(const QString &errorStr); void socketError(const QString &errorStr);
void protocolVersionMismatch(int localVersion, int remoteVersion); void protocolVersionMismatch(int localVersion, int remoteVersion);
void userInfoReceived(ServerInfo_User *userInfo); void userInfoReceived(ServerInfo_User *userInfo);
......
...@@ -24,6 +24,7 @@ public: ...@@ -24,6 +24,7 @@ public:
{ {
return QColor(value / 65536, (value % 65536) / 256, value % 256); return QColor(value / 65536, (value % 65536) / 256, value % 256);
} }
#endif
color get_color() const // HACK color get_color() const // HACK
{ {
color c; color c;
...@@ -32,7 +33,6 @@ public: ...@@ -32,7 +33,6 @@ public:
c.set_b(value % 256); c.set_b(value % 256);
return c; return c;
} }
#endif
}; };
#endif #endif
\ No newline at end of file
import "session_event.proto";
message Event_ServerIdentification {
extend SessionEvent {
optional Event_ServerIdentification ext = 500;
}
optional string server_name = 1;
optional string server_version = 2;
optional uint32 protocol_version = 3;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment