From 6923c98dc24e4338b49cde6b07088cb2e47aaf58 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Fri, 30 Oct 2009 13:18:25 +0100 Subject: [PATCH] foo --- common/protocol.cpp | 37 +++++++++-------- common/protocol.h | 12 +++++- common/protocol_items.cpp | 11 +++++ common/protocol_items.dat | 1 + common/protocol_items.h | 11 +++++ common/protocol_mc.pl | 7 ++++ common/server_protocolhandler.h | 2 + servatrice/servatrice.pro | 52 ++++++++++++------------ servatrice/src/servatrice.cpp | 2 + servatrice/src/servatrice.h | 1 + servatrice/src/serversocketinterface.cpp | 14 ++++++- servatrice/src/serversocketinterface.h | 2 + 12 files changed, 107 insertions(+), 45 deletions(-) diff --git a/common/protocol.cpp b/common/protocol.cpp index c0375ffc..d420a282 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -11,47 +11,47 @@ ProtocolItem::ProtocolItem(const QString &_itemName) { } -bool ProtocolItem::read(QXmlStreamReader &xml) +bool ProtocolItem::read(QXmlStreamReader *xml) { - while (!xml.atEnd()) { - xml.readNext(); - if (xml.isStartElement()) { - qDebug() << "startElement: " << xml.name().toString(); - } else if (xml.isEndElement()) { - qDebug() << "endElement: " << xml.name().toString(); - if (xml.name() == getItemType()) { + while (!xml->atEnd()) { + xml->readNext(); + if (xml->isStartElement()) { + qDebug() << "startElement: " << xml->name().toString(); + } else if (xml->isEndElement()) { + qDebug() << "endElement: " << xml->name().toString(); + if (xml->name() == getItemType()) { extractParameters(); qDebug() << "FERTIG"; deleteLater(); return true; } else { - QString tagName = xml.name().toString(); + QString tagName = xml->name().toString(); if (!parameters.contains(tagName)) qDebug() << "unrecognized attribute"; else parameters[tagName] = currentElementText; } - } else if (xml.isCharacters() && !xml.isWhitespace()) { - currentElementText = xml.text().toString(); + } else if (xml->isCharacters() && !xml->isWhitespace()) { + currentElementText = xml->text().toString(); qDebug() << "text: " << currentElementText; } } return false; } -void ProtocolItem::write(QXmlStreamWriter &xml) +void ProtocolItem::write(QXmlStreamWriter *xml) { - xml.writeStartElement(getItemType()); + xml->writeStartElement(getItemType()); if (!itemName.isEmpty()) - xml.writeAttribute("name", itemName); + xml->writeAttribute("name", itemName); QMapIterator i(parameters); while (i.hasNext()) { i.next(); - xml.writeTextElement(i.key(), i.value()); + xml->writeTextElement(i.key(), i.value()); } - xml.writeEndElement(); + xml->writeEndElement(); } ProtocolItem *ProtocolItem::getNewItem(const QString &name) @@ -118,6 +118,11 @@ void ProtocolResponse::initializeHash() responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed); } +GenericEvent::GenericEvent(const QString &_eventName) + : ProtocolItem(_eventName) +{ +} + void GameEvent::extractParameters() { bool ok; diff --git a/common/protocol.h b/common/protocol.h index 1a27d500..fd3f15bd 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -32,8 +32,8 @@ public: ProtocolItem(const QString &_itemName); static void initializeHash(); static ProtocolItem *getNewItem(const QString &name); - virtual bool read(QXmlStreamReader &xml); - virtual void write(QXmlStreamWriter &xml); + virtual bool read(QXmlStreamReader *xml); + virtual void write(QXmlStreamWriter *xml); }; class Command : public ProtocolItem { @@ -101,6 +101,14 @@ public: static ProtocolItem *newItem() { return new ProtocolResponse; } }; +class GenericEvent : public ProtocolItem { + Q_OBJECT +protected: + QString getItemType() const { return "generic_event"; } +public: + GenericEvent(const QString &_eventName); +}; + class GameEvent : public ProtocolItem { Q_OBJECT private: diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 7caae1b5..96c97052 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -551,6 +551,16 @@ void Event_StopDumpZone::extractParameters() zoneOwnerId = parameters["zone_owner_id"].toInt(); zone = parameters["zone"]; } +Event_Welcome::Event_Welcome(const QString &_message) + : GenericEvent("welcome"), message(_message) +{ + setParameter("message", message); +} +void Event_Welcome::extractParameters() +{ + GenericEvent::extractParameters(); + message = parameters["message"]; +} void ProtocolItem::initializeHashAuto() { itemNameHash.insert("cmdping", Command_Ping::newItem); @@ -604,4 +614,5 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("game_eventset_active_phase", Event_SetActivePhase::newItem); itemNameHash.insert("game_eventdump_zone", Event_DumpZone::newItem); itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem); + itemNameHash.insert("generic_eventwelcome", Event_Welcome::newItem); } diff --git a/common/protocol_items.dat b/common/protocol_items.dat index 375dd93c..96e79b69 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -49,3 +49,4 @@ 3:set_active_phase:i,phase 3:dump_zone:i,zone_owner_id:s,zone:i,number_cards 3:stop_dump_zone:i,zone_owner_id:s,zone +4:welcome:s,message \ No newline at end of file diff --git a/common/protocol_items.h b/common/protocol_items.h index df73b049..e1dc3e12 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -634,5 +634,16 @@ public: protected: void extractParameters(); }; +class Event_Welcome : public GenericEvent { + Q_OBJECT +private: + QString message; +public: + Event_Welcome(const QString &_message = QString()); + QString getMessage() const { return message; } + static ProtocolItem *newItem() { return new Event_Welcome; } +protected: + void extractParameters(); +}; #endif diff --git a/common/protocol_mc.pl b/common/protocol_mc.pl index b3fcae98..5a787e75 100755 --- a/common/protocol_mc.pl +++ b/common/protocol_mc.pl @@ -47,6 +47,13 @@ while () { $parentConstructorCall = "$baseClass(\"$name1\", _gameId, _playerId)"; $constructorParamsH = "int _gameId = -1, int _playerId = -1"; $constructorParamsCpp = "int _gameId, int _playerId"; + } elsif ($type == 4) { + $type = 'generic_event'; + $namePrefix = 'Event'; + $baseClass = 'GenericEvent'; + $parentConstructorCall = "$baseClass(\"$name1\")"; + $constructorParamsH = ""; + $constructorParamsCpp = ""; } $className = $namePrefix . '_' . $name2; print headerfile "class $className : public $baseClass {\n" diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index ee340461..b8eccba3 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -7,6 +7,7 @@ class Server_Player; class Command; +class ProtocolItem; class Server_ProtocolHandler : public QObject { Q_OBJECT @@ -31,6 +32,7 @@ public: const QString &getPlayerName() const { return playerName; } void processCommand(Command *command); + virtual void sendProtocolItem(ProtocolItem *item) = 0; }; #endif diff --git a/servatrice/servatrice.pro b/servatrice/servatrice.pro index 9c0ff7c0..c705c8ef 100755 --- a/servatrice/servatrice.pro +++ b/servatrice/servatrice.pro @@ -4,8 +4,8 @@ TEMPLATE = app TARGET = -DEPENDPATH += . src ../common/src -INCLUDEPATH += . src ../common/src +DEPENDPATH += . src ../common +INCLUDEPATH += . src ../common MOC_DIR = build OBJECTS_DIR = build @@ -16,31 +16,31 @@ QT -= gui HEADERS += src/servatrice.h \ src/serversocketinterface.h \ src/version.h \ - ../common/src/protocol.h \ - ../common/src/protocol_items.h \ - ../common/src/rng_abstract.h \ - ../common/src/rng_qt.h \ - ../common/src/server.h \ - ../common/src/server_arrow.h \ - ../common/src/server_card.h \ - ../common/src/server_cardzone.h \ - ../common/src/server_chatchannel.h \ - ../common/src/server_counter.h \ - ../common/src/server_game.h \ - ../common/src/server_player.h \ - ../common/src/server_protocolhandler.h + ../common/protocol.h \ + ../common/protocol_items.h \ + ../common/rng_abstract.h \ + ../common/rng_qt.h \ + ../common/server.h \ + ../common/server_arrow.h \ + ../common/server_card.h \ + ../common/server_cardzone.h \ + ../common/server_chatchannel.h \ + ../common/server_counter.h \ + ../common/server_game.h \ + ../common/server_player.h \ + ../common/server_protocolhandler.h SOURCES += src/main.cpp \ src/servatrice.cpp \ src/serversocketinterface.cpp \ - ../common/src/protocol.cpp \ - ../common/src/protocol_items.cpp \ - ../common/src/rng_abstract.cpp \ - ../common/src/rng_qt.cpp \ - ../common/src/server.cpp \ - ../common/src/server_card.cpp \ - ../common/src/server_cardzone.cpp \ - ../common/src/server_chatchannel.cpp \ - ../common/src/server_game.cpp \ - ../common/src/server_player.cpp \ - ../common/src/server_protocolhandler.cpp + ../common/protocol.cpp \ + ../common/protocol_items.cpp \ + ../common/rng_abstract.cpp \ + ../common/rng_qt.cpp \ + ../common/server.cpp \ + ../common/server_card.cpp \ + ../common/server_cardzone.cpp \ + ../common/server_chatchannel.cpp \ + ../common/server_game.cpp \ + ../common/server_player.cpp \ + ../common/server_protocolhandler.cpp diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index 65cee166..10f25bfa 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -117,3 +117,5 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS } else return UnknownUser; } + +const QString Servatrice::versionString = "Servatrice 0.20091030"; diff --git a/servatrice/src/servatrice.h b/servatrice/src/servatrice.h index 14653ecc..61637cf7 100644 --- a/servatrice/src/servatrice.h +++ b/servatrice/src/servatrice.h @@ -32,6 +32,7 @@ class Servatrice : public Server private slots: void newConnection(); public: + static const QString versionString; Servatrice(QObject *parent = 0); ~Servatrice(); bool openDatabase(); diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index fc7d26eb..4d797edb 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -21,13 +21,16 @@ #include #include #include "serversocketinterface.h" +#include "servatrice.h" #include "protocol.h" +#include "protocol_items.h" ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent) : Server_ProtocolHandler(_server, parent), socket(_socket) { xmlWriter = new QXmlStreamWriter; xmlWriter->setDevice(socket); + xmlWriter->setAutoFormatting(true); xmlReader = new QXmlStreamReader; @@ -38,6 +41,8 @@ ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socke xmlWriter->writeStartDocument(); xmlWriter->writeStartElement("cockatrice_communication"); xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion)); + + sendProtocolItem(new Event_Welcome(Servatrice::versionString)); } ServerSocketInterface::~ServerSocketInterface() @@ -59,7 +64,9 @@ ServerSocketInterface::~ServerSocketInterface() void ServerSocketInterface::readClient() { -/* while (canReadLine()) { + xmlReader->addData(socket->readAll()); + + while (canReadLine()) { QString line = QString(readLine()).trimmed(); if (line.isNull()) break; @@ -91,3 +98,8 @@ void ServerSocketInterface::catchSocketError(QAbstractSocket::SocketError socket deleteLater(); } +void ServerSocketInterface::sendProtocolItem(ProtocolItem *item) +{ + item->write(xmlWriter); + delete item; +} diff --git a/servatrice/src/serversocketinterface.h b/servatrice/src/serversocketinterface.h index 0eadf64e..c496e36b 100644 --- a/servatrice/src/serversocketinterface.h +++ b/servatrice/src/serversocketinterface.h @@ -41,6 +41,8 @@ private: public: ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent = 0); ~ServerSocketInterface(); + + void sendProtocolItem(ProtocolItem *item); }; #endif