some server code

This commit is contained in:
Max-Wilhelm Bruker 2009-11-02 17:35:51 +01:00
parent 6923c98dc2
commit 29bf3d3774
18 changed files with 729 additions and 137 deletions

View file

@ -1,37 +0,0 @@
#include "returnmessage.h"
#include "serversocket.h"
bool ReturnMessage::send(ReturnCode code)
{
ServerSocket *s = qobject_cast<ServerSocket *>(parent());
if (!s)
return false;
QString returnCodeString;
switch (code) {
case ReturnNothing: return true;
case ReturnOk: returnCodeString = "ok"; break;
case ReturnNameNotFound: returnCodeString = "name_not_found"; break;
case ReturnLoginNeeded: returnCodeString = "login_needed"; break;
case ReturnSyntaxError: returnCodeString = "syntax"; break;
case ReturnContextError: returnCodeString = "context"; break;
case ReturnPasswordWrong: returnCodeString = "password"; break;
case ReturnSpectatorsNotAllowed: returnCodeString = "spectators_not_allowed"; break;
}
s->msg(QString("resp|%1|%2").arg(msg_id)
.arg(returnCodeString));
return (code == ReturnOk);
}
bool ReturnMessage::sendList(const QStringList &args, const QString &prefix)
{
ServerSocket *s = qobject_cast<ServerSocket *>(parent());
if (!s)
return false;
QString arg1 = prefix.isEmpty() ? cmd : prefix;
for (int i = 0; i < args.size(); i++)
s->msg(QString("%1|%2|%3").arg(arg1)
.arg(msg_id)
.arg(args[i]));
return true;
}

View file

@ -1,21 +0,0 @@
#ifndef RETURNMESSAGE_H
#define RETURNMESSAGE_H
#include <QStringList>
class ReturnMessage : public QObject {
Q_OBJECT
private:
unsigned int msg_id;
QString cmd;
public:
enum ReturnCode { ReturnNothing, ReturnOk, ReturnNameNotFound, ReturnLoginNeeded, ReturnSyntaxError, ReturnContextError, ReturnPasswordWrong, ReturnSpectatorsNotAllowed };
ReturnMessage(QObject *parent = 0) : QObject(parent), msg_id(0) { }
unsigned int getMsgId() const { return msg_id; }
void setMsgId(unsigned int _msg_id) { msg_id = _msg_id; }
void setCmd(const QString &_cmd) { cmd = _cmd; }
bool send(ReturnCode code);
bool sendList(const QStringList &args, const QString &prefix = QString());
};
#endif

View file

@ -22,10 +22,13 @@
#include "servatrice.h"
#include "server_chatchannel.h"
#include "serversocketinterface.h"
#include "protocol.h"
Servatrice::Servatrice(QObject *parent)
: Server(parent)
{
ProtocolItem::initializeHash();
tcpServer = new QTcpServer(this);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
tcpServer->listen(QHostAddress::Any, 4747); // XXX make customizable

View file

@ -26,7 +26,7 @@
#include "protocol_items.h"
ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent)
: Server_ProtocolHandler(_server, parent), socket(_socket)
: Server_ProtocolHandler(_server, parent), socket(_socket), currentItem(0)
{
xmlWriter = new QXmlStreamWriter;
xmlWriter->setDevice(socket);
@ -39,7 +39,7 @@ ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socke
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(catchSocketError(QAbstractSocket::SocketError)));
xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("cockatrice_communication");
xmlWriter->writeStartElement("cockatrice_server_stream");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
sendProtocolItem(new Event_Welcome(Servatrice::versionString));
@ -64,32 +64,38 @@ ServerSocketInterface::~ServerSocketInterface()
void ServerSocketInterface::readClient()
{
qDebug() << "readClient";
xmlReader->addData(socket->readAll());
while (canReadLine()) {
QString line = QString(readLine()).trimmed();
if (line.isNull())
break;
qDebug(QString("<<< %1").arg(line).toLatin1());
/* switch (PlayerStatus) {
case StatusNormal:
case StatusReadyStart:
case StatusPlaying:
parseCommand(line);
break;
case StatusSubmitDeck:
QString card = line;
if (card == ".") {
PlayerStatus = StatusNormal;
remsg->send(ReturnMessage::ReturnOk);
} else if (card.startsWith("SB:"))
SideboardList << card.mid(3);
if (currentItem) {
if (!currentItem->read(xmlReader))
return;
currentItem = 0;
}
while (!xmlReader->atEnd()) {
xmlReader->readNext();
if (xmlReader->isStartElement()) {
QString itemType = xmlReader->name().toString();
if (itemType == "cockatrice_client_stream")
continue;
QString itemName = xmlReader->attributes().value("name").toString();
qDebug() << "parseXml: startElement: " << "type =" << itemType << ", name =" << itemName;
currentItem = ProtocolItem::getNewItem(itemType + itemName);
if (!currentItem)
currentItem = new InvalidCommand;
if (!currentItem->read(xmlReader))
return;
else {
Command *command = qobject_cast<Command *>(currentItem);
if (qobject_cast<InvalidCommand *>(command))
sendProtocolItem(new ProtocolResponse(command->getCmdId(), ProtocolResponse::RespInvalidCommand));
else
DeckList << card;
processCommand(command);
currentItem = 0;
}
}
}
*/}
}
void ServerSocketInterface::catchSocketError(QAbstractSocket::SocketError socketError)
{

View file

@ -21,7 +21,7 @@
#define SERVERSOCKETINTERFACE_H
#include <QTcpSocket>
#include <server_protocolhandler.h>
#include "server_protocolhandler.h"
class QTcpSocket;
class Server;
@ -38,6 +38,7 @@ private:
QTcpSocket *socket;
QXmlStreamWriter *xmlWriter;
QXmlStreamReader *xmlReader;
ProtocolItem *currentItem;
public:
ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent = 0);
~ServerSocketInterface();

View file

@ -1,22 +0,0 @@
/***************************************************************************
* Copyright (C) 2008 by Max-Wilhelm Bruker *
* brukie@laptop *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
static const int PROTOCOL_VERSION = 2;
static const char *VERSION_STRING = "Servatrice 0.20091017";