cleanups, games work with ISL, now we just need to clean up better when the ISL connection dies

This commit is contained in:
Max-Wilhelm Bruker 2012-03-18 18:48:14 +01:00
parent c9a8429044
commit 5db0fcd6ae
28 changed files with 570 additions and 245 deletions

View file

@ -7,6 +7,7 @@
#include "get_pb_extension.h"
#include "pb/isl_message.pb.h"
#include "pb/event_game_joined.pb.h"
#include "pb/event_server_complete_list.pb.h"
#include "pb/event_user_message.pb.h"
#include "pb/event_user_joined.pb.h"
@ -280,15 +281,6 @@ void IslInterface::sessionEvent_ServerCompleteList(const Event_ServerCompleteLis
}
}
void IslInterface::sessionEvent_UserMessage(const SessionEvent &sessionEvent, const Event_UserMessage &event)
{
QReadLocker locker(&server->clientsLock);
Server_ProtocolHandler *userInterface = server->getUsers().value(QString::fromStdString(event.receiver_name()));
if (userInterface)
userInterface->sendProtocolItem(sessionEvent);
}
void IslInterface::sessionEvent_UserJoined(const Event_UserJoined &event)
{
ServerInfo_User userInfo(event.user_info());
@ -327,13 +319,41 @@ void IslInterface::roomEvent_ListGames(int roomId, const Event_ListGames &event)
}
}
void IslInterface::processSessionEvent(const SessionEvent &event)
void IslInterface::roomCommand_JoinGame(const Command_JoinGame &cmd, int cmdId, int roomId, qint64 sessionId)
{
emit joinGameCommandReceived(cmd, cmdId, roomId, serverId, sessionId);
}
void IslInterface::processSessionEvent(const SessionEvent &event, qint64 sessionId)
{
switch (getPbExtension(event)) {
case SessionEvent::SERVER_COMPLETE_LIST: sessionEvent_ServerCompleteList(event.GetExtension(Event_ServerCompleteList::ext)); break;
case SessionEvent::USER_MESSAGE: sessionEvent_UserMessage(event, event.GetExtension(Event_UserMessage::ext)); break;
case SessionEvent::USER_JOINED: sessionEvent_UserJoined(event.GetExtension(Event_UserJoined::ext)); break;
case SessionEvent::USER_LEFT: sessionEvent_UserLeft(event.GetExtension(Event_UserLeft::ext)); break;
case SessionEvent::GAME_JOINED: {
QReadLocker clientsLocker(&server->clientsLock);
Server_AbstractUserInterface *client = server->getUsersBySessionId().value(sessionId);
if (!client) {
qDebug() << "IslInterface::processSessionEvent: session id" << sessionId << "not found";
break;
}
const Event_GameJoined &gameJoined = event.GetExtension(Event_GameJoined::ext);
client->playerAddedToGame(gameJoined.game_id(), gameJoined.room_id(), gameJoined.player_id());
client->sendProtocolItem(event);
break;
}
case SessionEvent::USER_MESSAGE:
case SessionEvent::REPLAY_ADDED: {
QReadLocker clientsLocker(&server->clientsLock);
Server_AbstractUserInterface *client = server->getUsersBySessionId().value(sessionId);
if (!client) {
qDebug() << "IslInterface::processSessionEvent: session id" << sessionId << "not found";
break;
}
client->sendProtocolItem(event);
break;
}
default: ;
}
}
@ -349,19 +369,40 @@ void IslInterface::processRoomEvent(const RoomEvent &event)
}
}
void IslInterface::processRoomCommand(const CommandContainer &cont, qint64 sessionId)
{
for (int i = 0; i < cont.room_command_size(); ++i) {
const RoomCommand &roomCommand = cont.room_command(i);
switch (static_cast<RoomCommand::RoomCommandType>(getPbExtension(roomCommand))) {
case RoomCommand::JOIN_GAME: roomCommand_JoinGame(roomCommand.GetExtension(Command_JoinGame::ext), cont.cmd_id(), cont.room_id(), sessionId);
default: ;
}
}
}
void IslInterface::processMessage(const IslMessage &item)
{
qDebug() << QString::fromStdString(item.DebugString());
switch (item.message_type()) {
case IslMessage::SESSION_EVENT: processSessionEvent(item.session_event()); break;
case IslMessage::RESPONSE: {
case IslMessage::ROOM_COMMAND_CONTAINER: {
processRoomCommand(item.room_command(), item.session_id());
break;
}
case IslMessage::GAME_COMMAND_CONTAINER: {
emit gameCommandContainerReceived(item.game_command(), item.player_id(), serverId, item.session_id());
break;
}
case IslMessage::SESSION_EVENT: {
processSessionEvent(item.session_event(), item.session_id());
break;
}
case IslMessage::RESPONSE: {
emit responseReceived(item.response(), item.session_id());
break;
}
case IslMessage::GAME_EVENT_CONTAINER: {
emit gameEventContainerReceived(item.game_event_container(), item.session_id());
break;
}
case IslMessage::ROOM_EVENT: {

View file

@ -21,6 +21,7 @@ class Event_JoinRoom;
class Event_LeaveRoom;
class Event_RoomSay;
class Event_ListGames;
class Command_JoinGame;
class IslInterface : public QObject {
Q_OBJECT
@ -37,6 +38,10 @@ signals:
void externalRoomUserLeft(int roomId, QString userName);
void externalRoomSay(int roomId, QString userName, QString message);
void externalRoomGameListChanged(int roomId, ServerInfo_Game gameInfo);
void joinGameCommandReceived(const Command_JoinGame &cmd, int cmdId, int roomId, int serverId, qint64 sessionId);
void gameCommandContainerReceived(const CommandContainer &cont, int playerId, int serverId, qint64 sessionId);
void responseReceived(const Response &resp, qint64 sessionId);
void gameEventContainerReceived(const GameEventContainer &cont, qint64 sessionId);
private:
int serverId;
int socketDescriptor;
@ -53,7 +58,6 @@ private:
int messageLength;
void sessionEvent_ServerCompleteList(const Event_ServerCompleteList &event);
void sessionEvent_UserMessage(const SessionEvent &sessionEvent, const Event_UserMessage &event);
void sessionEvent_UserJoined(const Event_UserJoined &event);
void sessionEvent_UserLeft(const Event_UserLeft &event);
@ -62,8 +66,11 @@ private:
void roomEvent_Say(int roomId, const Event_RoomSay &event);
void roomEvent_ListGames(int roomId, const Event_ListGames &event);
void processSessionEvent(const SessionEvent &event);
void roomCommand_JoinGame(const Command_JoinGame &cmd, int cmdId, int roomId, qint64 sessionId);
void processSessionEvent(const SessionEvent &event, qint64 sessionId);
void processRoomEvent(const RoomEvent &event);
void processRoomCommand(const CommandContainer &cont, qint64 sessionId);
void processMessage(const IslMessage &item);
void sharedCtor(const QSslCertificate &cert, const QSslKey &privateKey);

View file

@ -118,6 +118,7 @@ int main(int argc, char *argv[])
QStringList args = app.arguments();
bool testRandom = args.contains("--test-random");
bool testHashFunction = args.contains("--test-hash");
bool logToConsole = args.contains("--log-to-console");
qRegisterMetaType<QList<int> >("QList<int>");
@ -126,13 +127,16 @@ int main(int argc, char *argv[])
QSettings *settings = new QSettings("servatrice.ini", QSettings::IniFormat);
loggerThread = new QThread;
logger = new ServerLogger;
logger = new ServerLogger(logToConsole);
logger->moveToThread(loggerThread);
loggerThread->start();
QMetaObject::invokeMethod(logger, "startLog", Qt::BlockingQueuedConnection, Q_ARG(QString, settings->value("server/logfile").toString()));
qInstallMsgHandler(myMessageOutput2);
if (logToConsole)
qInstallMsgHandler(myMessageOutput);
else
qInstallMsgHandler(myMessageOutput2);
#ifdef Q_OS_UNIX
struct sigaction hup;
hup.sa_handler = ServerLogger::hupSignalHandler;
@ -159,6 +163,7 @@ int main(int argc, char *argv[])
testHash();
Servatrice *server = new Servatrice(settings);
QObject::connect(server, SIGNAL(logDebugMessage(QString, void *)), logger, SLOT(logMessage(QString, void *)));
QObject::connect(server, SIGNAL(destroyed()), &app, SLOT(quit()), Qt::QueuedConnection);
std::cerr << "-------------------------" << std::endl;

View file

@ -65,8 +65,6 @@ void Servatrice_IslServer::incomingConnection(int socketDescriptor)
Servatrice::Servatrice(QSettings *_settings, QObject *parent)
: Server(parent), dbMutex(QMutex::Recursive), settings(_settings), uptime(0), shutdownTimer(0)
{
qRegisterMetaType<ServerInfo_User>("ServerInfo_User");
serverName = settings->value("server/name").toString();
serverId = settings->value("server/id", 0).toInt();
@ -558,7 +556,7 @@ bool Servatrice::userSessionExists(const QString &userName)
return query.next();
}
int Servatrice::startSession(const QString &userName, const QString &address)
qint64 Servatrice::startSession(const QString &userName, const QString &address)
{
if (authenticationMethod == AuthenticationNone)
return -1;
@ -577,7 +575,7 @@ int Servatrice::startSession(const QString &userName, const QString &address)
return -1;
}
void Servatrice::endSession(int sessionId)
void Servatrice::endSession(qint64 sessionId)
{
if (authenticationMethod == AuthenticationNone)
return;
@ -780,7 +778,10 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &a
allUsersIterator.toFront();
clientsLock.lockForRead();
while (allUsersIterator.hasNext()) {
Server_ProtocolHandler *userHandler = users.value(allUsersIterator.next());
const QString userName = allUsersIterator.next();
Server_AbstractUserInterface *userHandler = users.value(userName);
if (!userHandler)
userHandler = externalUsers.value(userName);
if (userHandler)
userHandler->sendProtocolItem(*sessionEvent);
}
@ -916,6 +917,10 @@ void Servatrice::addIslInterface(int serverId, IslInterface *interface)
connect(interface, SIGNAL(externalRoomUserLeft(int, QString)), this, SLOT(externalRoomUserLeft(int, QString)));
connect(interface, SIGNAL(externalRoomSay(int, QString, QString)), this, SLOT(externalRoomSay(int, QString, QString)));
connect(interface, SIGNAL(externalRoomGameListChanged(int, ServerInfo_Game)), this, SLOT(externalRoomGameListChanged(int, ServerInfo_Game)));
connect(interface, SIGNAL(joinGameCommandReceived(Command_JoinGame, int, int, int, qint64)), this, SLOT(externalJoinGameCommandReceived(Command_JoinGame, int, int, int, qint64)));
connect(interface, SIGNAL(gameCommandContainerReceived(CommandContainer, int, int, qint64)), this, SLOT(externalGameCommandContainerReceived(CommandContainer, int, int, qint64)));
connect(interface, SIGNAL(responseReceived(Response, qint64)), this, SLOT(externalResponseReceived(Response, qint64)));
connect(interface, SIGNAL(gameEventContainerReceived(GameEventContainer, qint64)), this, SLOT(externalGameEventContainerReceived(GameEventContainer, qint64)));
}
void Servatrice::removeIslInterface(int serverId)

View file

@ -127,8 +127,8 @@ public:
QList<ServerProperties> getServerList() const;
protected:
int startSession(const QString &userName, const QString &address);
void endSession(int sessionId);
qint64 startSession(const QString &userName, const QString &address);
void endSession(qint64 sessionId);
bool userExists(const QString &user);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);

View file

@ -3,13 +3,14 @@
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <iostream>
#ifdef Q_OS_UNIX
# include <sys/types.h>
# include <sys/socket.h>
#endif
ServerLogger::ServerLogger(QObject *parent)
: QObject(parent), flushRunning(false)
ServerLogger::ServerLogger(bool _logToConsole, QObject *parent)
: QObject(parent), logToConsole(_logToConsole), flushRunning(false)
{
}
@ -71,6 +72,9 @@ void ServerLogger::flushBuffer()
stream << message << "\n";
stream.flush();
if (logToConsole)
std::cout << message.toStdString() << std::endl;
}
}

View file

@ -14,7 +14,7 @@ class Server_ProtocolHandler;
class ServerLogger : public QObject {
Q_OBJECT
public:
ServerLogger(QObject *parent = 0);
ServerLogger(bool _logToConsole, QObject *parent = 0);
~ServerLogger();
static void hupSignalHandler(int unused);
public slots:
@ -26,6 +26,7 @@ private slots:
signals:
void sigFlushBuffer();
private:
bool logToConsole;
static int sigHupFD[2];
QSocketNotifier *snHup;
static QFile *logFile;