Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
d5ccda49
Commit
d5ccda49
authored
Jan 23, 2013
by
Max-Wilhelm Bruker
Browse files
server code cleanup
parent
60a1f5d7
Changes
8
Hide whitespace changes
Inline
Side-by-side
common/server.cpp
View file @
d5ccda49
...
@@ -186,6 +186,17 @@ QList<PlayerReference> Server::getPersistentPlayerReferences(const QString &user
...
@@ -186,6 +186,17 @@ QList<PlayerReference> Server::getPersistentPlayerReferences(const QString &user
return
persistentPlayers
.
values
(
userName
);
return
persistentPlayers
.
values
(
userName
);
}
}
Server_AbstractUserInterface
*
Server
::
findUser
(
const
QString
&
userName
)
const
{
// Call this only with clientsLock set.
Server_AbstractUserInterface
*
userHandler
=
users
.
value
(
userName
);
if
(
userHandler
)
return
userHandler
;
else
return
externalUsers
.
value
(
userName
);
}
void
Server
::
addClient
(
Server_ProtocolHandler
*
client
)
void
Server
::
addClient
(
Server_ProtocolHandler
*
client
)
{
{
QWriteLocker
locker
(
&
clientsLock
);
QWriteLocker
locker
(
&
clientsLock
);
...
...
common/server.h
View file @
d5ccda49
...
@@ -46,6 +46,7 @@ public:
...
@@ -46,6 +46,7 @@ public:
AuthenticationResult
loginUser
(
Server_ProtocolHandler
*
session
,
QString
&
name
,
const
QString
&
password
,
QString
&
reason
,
int
&
secondsLeft
);
AuthenticationResult
loginUser
(
Server_ProtocolHandler
*
session
,
QString
&
name
,
const
QString
&
password
,
QString
&
reason
,
int
&
secondsLeft
);
const
QMap
<
int
,
Server_Room
*>
&
getRooms
()
{
return
rooms
;
}
const
QMap
<
int
,
Server_Room
*>
&
getRooms
()
{
return
rooms
;
}
Server_AbstractUserInterface
*
findUser
(
const
QString
&
userName
)
const
;
const
QMap
<
QString
,
Server_ProtocolHandler
*>
&
getUsers
()
const
{
return
users
;
}
const
QMap
<
QString
,
Server_ProtocolHandler
*>
&
getUsers
()
const
{
return
users
;
}
const
QMap
<
qint64
,
Server_ProtocolHandler
*>
&
getUsersBySessionId
()
const
{
return
usersBySessionId
;
}
const
QMap
<
qint64
,
Server_ProtocolHandler
*>
&
getUsersBySessionId
()
const
{
return
usersBySessionId
;
}
void
addClient
(
Server_ProtocolHandler
*
player
);
void
addClient
(
Server_ProtocolHandler
*
player
);
...
@@ -63,7 +64,6 @@ public:
...
@@ -63,7 +64,6 @@ public:
Server_DatabaseInterface
*
getDatabaseInterface
()
const
;
Server_DatabaseInterface
*
getDatabaseInterface
()
const
;
int
getNextLocalGameId
()
{
QMutexLocker
locker
(
&
nextLocalGameIdMutex
);
return
++
nextLocalGameId
;
}
int
getNextLocalGameId
()
{
QMutexLocker
locker
(
&
nextLocalGameIdMutex
);
return
++
nextLocalGameId
;
}
virtual
void
storeGameInformation
(
int
secondsElapsed
,
const
QSet
<
QString
>
&
allPlayersEver
,
const
QSet
<
QString
>
&
allSpectatorsEver
,
const
QList
<
GameReplay
*>
&
replays
)
{
}
void
sendIsl_Response
(
const
Response
&
item
,
int
serverId
=
-
1
,
qint64
sessionId
=
-
1
);
void
sendIsl_Response
(
const
Response
&
item
,
int
serverId
=
-
1
,
qint64
sessionId
=
-
1
);
void
sendIsl_SessionEvent
(
const
SessionEvent
&
item
,
int
serverId
=
-
1
,
qint64
sessionId
=
-
1
);
void
sendIsl_SessionEvent
(
const
SessionEvent
&
item
,
int
serverId
=
-
1
,
qint64
sessionId
=
-
1
);
...
...
common/server_game.cpp
View file @
d5ccda49
...
@@ -42,6 +42,7 @@
...
@@ -42,6 +42,7 @@
#include
"pb/event_set_active_phase.pb.h"
#include
"pb/event_set_active_phase.pb.h"
#include
"pb/serverinfo_playerping.pb.h"
#include
"pb/serverinfo_playerping.pb.h"
#include
"pb/game_replay.pb.h"
#include
"pb/game_replay.pb.h"
#include
"pb/event_replay_added.pb.h"
#include
<google/protobuf/descriptor.h>
#include
<google/protobuf/descriptor.h>
#include
<QTimer>
#include
<QTimer>
#include
<QDebug>
#include
<QDebug>
...
@@ -110,7 +111,7 @@ Server_Game::~Server_Game()
...
@@ -110,7 +111,7 @@ Server_Game::~Server_Game()
currentReplay
->
set_duration_seconds
(
secondsElapsed
-
startTimeOfThisGame
);
currentReplay
->
set_duration_seconds
(
secondsElapsed
-
startTimeOfThisGame
);
replayList
.
append
(
currentReplay
);
replayList
.
append
(
currentReplay
);
room
->
getServer
()
->
storeGameInformation
(
secondsElapsed
,
allPlayersEver
,
allSpectatorsEver
,
replayList
);
storeGameInformation
();
for
(
int
i
=
0
;
i
<
replayList
.
size
();
++
i
)
for
(
int
i
=
0
;
i
<
replayList
.
size
();
++
i
)
delete
replayList
[
i
];
delete
replayList
[
i
];
...
@@ -118,6 +119,51 @@ Server_Game::~Server_Game()
...
@@ -118,6 +119,51 @@ Server_Game::~Server_Game()
qDebug
()
<<
"Server_Game destructor: gameId="
<<
gameId
;
qDebug
()
<<
"Server_Game destructor: gameId="
<<
gameId
;
}
}
void
Server_Game
::
storeGameInformation
()
{
const
ServerInfo_Game
&
gameInfo
=
replayList
.
first
()
->
game_info
();
Event_ReplayAdded
replayEvent
;
ServerInfo_ReplayMatch
*
replayMatchInfo
=
replayEvent
.
mutable_match_info
();
replayMatchInfo
->
set_game_id
(
gameInfo
.
game_id
());
replayMatchInfo
->
set_room_name
(
room
->
getName
().
toStdString
());
replayMatchInfo
->
set_time_started
(
QDateTime
::
currentDateTime
().
addSecs
(
-
secondsElapsed
).
toTime_t
());
replayMatchInfo
->
set_length
(
secondsElapsed
);
replayMatchInfo
->
set_game_name
(
gameInfo
.
description
());
const
QStringList
&
allGameTypes
=
room
->
getGameTypes
();
QStringList
gameTypes
;
for
(
int
i
=
gameInfo
.
game_types_size
()
-
1
;
i
>=
0
;
--
i
)
gameTypes
.
append
(
allGameTypes
[
gameInfo
.
game_types
(
i
)]);
QSetIterator
<
QString
>
playerIterator
(
allPlayersEver
);
while
(
playerIterator
.
hasNext
())
replayMatchInfo
->
add_player_names
(
playerIterator
.
next
().
toStdString
());
for
(
int
i
=
0
;
i
<
replayList
.
size
();
++
i
)
{
ServerInfo_Replay
*
replayInfo
=
replayMatchInfo
->
add_replay_list
();
replayInfo
->
set_replay_id
(
replayList
[
i
]
->
replay_id
());
replayInfo
->
set_replay_name
(
gameInfo
.
description
());
replayInfo
->
set_duration
(
replayList
[
i
]
->
duration_seconds
());
}
QSet
<
QString
>
allUsersInGame
=
allPlayersEver
+
allSpectatorsEver
;
QSetIterator
<
QString
>
allUsersIterator
(
allUsersInGame
);
SessionEvent
*
sessionEvent
=
Server_ProtocolHandler
::
prepareSessionEvent
(
replayEvent
);
Server
*
server
=
room
->
getServer
();
server
->
clientsLock
.
lockForRead
();
while
(
allUsersIterator
.
hasNext
())
{
Server_AbstractUserInterface
*
userHandler
=
server
->
findUser
(
allUsersIterator
.
next
());
if
(
userHandler
)
userHandler
->
sendProtocolItem
(
*
sessionEvent
);
}
server
->
clientsLock
.
unlock
();
delete
sessionEvent
;
server
->
getDatabaseInterface
()
->
storeGameInformation
(
room
->
getName
(),
gameTypes
,
gameInfo
,
allPlayersEver
,
allSpectatorsEver
,
replayList
);
}
void
Server_Game
::
pingClockTimeout
()
void
Server_Game
::
pingClockTimeout
()
{
{
QMutexLocker
locker
(
&
gameMutex
);
QMutexLocker
locker
(
&
gameMutex
);
...
...
common/server_game.h
View file @
d5ccda49
...
@@ -73,6 +73,7 @@ private:
...
@@ -73,6 +73,7 @@ private:
void
createGameStateChangedEvent
(
Event_GameStateChanged
*
event
,
Server_Player
*
playerWhosAsking
,
bool
omniscient
,
bool
withUserInfo
);
void
createGameStateChangedEvent
(
Event_GameStateChanged
*
event
,
Server_Player
*
playerWhosAsking
,
bool
omniscient
,
bool
withUserInfo
);
void
sendGameStateToPlayers
();
void
sendGameStateToPlayers
();
void
storeGameInformation
();
signals:
signals:
void
sigStartGameIfReady
();
void
sigStartGameIfReady
();
void
gameInfoChanged
(
ServerInfo_Game
gameInfo
);
void
gameInfoChanged
(
ServerInfo_Game
gameInfo
);
...
...
common/server_protocolhandler.cpp
View file @
d5ccda49
...
@@ -375,12 +375,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message
...
@@ -375,12 +375,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message
QReadLocker
locker
(
&
server
->
clientsLock
);
QReadLocker
locker
(
&
server
->
clientsLock
);
QString
receiver
=
QString
::
fromStdString
(
cmd
.
user_name
());
QString
receiver
=
QString
::
fromStdString
(
cmd
.
user_name
());
Server_AbstractUserInterface
*
userInterface
=
server
->
getUsers
().
value
(
receiver
);
Server_AbstractUserInterface
*
userInterface
=
server
->
findUser
(
receiver
);
if
(
!
userInterface
)
{
if
(
!
userInterface
)
userInterface
=
server
->
getExternalUsers
().
value
(
receiver
);
return
Response
::
RespNameNotFound
;
if
(
!
userInterface
)
return
Response
::
RespNameNotFound
;
}
if
(
databaseInterface
->
isInIgnoreList
(
receiver
,
QString
::
fromStdString
(
userInfo
->
name
())))
if
(
databaseInterface
->
isInIgnoreList
(
receiver
,
QString
::
fromStdString
(
userInfo
->
name
())))
return
Response
::
RespInIgnoreList
;
return
Response
::
RespInIgnoreList
;
...
@@ -401,13 +398,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G
...
@@ -401,13 +398,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G
if
(
authState
==
NotLoggedIn
)
if
(
authState
==
NotLoggedIn
)
return
Response
::
RespLoginNeeded
;
return
Response
::
RespLoginNeeded
;
// XXX This does not take external users into account.
// We don't need to check whether the user is logged in; persistent games should also work.
// XXX Maybe remove this check and test if the result list is empty at the end.
// The client needs to deal with an empty result list.
{
QReadLocker
clientsLocker
(
&
server
->
clientsLock
);
if
(
!
server
->
getUsers
().
contains
(
QString
::
fromStdString
(
cmd
.
user_name
())))
return
Response
::
RespNameNotFound
;
}
Response_GetGamesOfUser
*
re
=
new
Response_GetGamesOfUser
;
Response_GetGamesOfUser
*
re
=
new
Response_GetGamesOfUser
;
server
->
roomsLock
.
lockForRead
();
server
->
roomsLock
.
lockForRead
();
...
@@ -440,12 +432,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetU
...
@@ -440,12 +432,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetU
QReadLocker
locker
(
&
server
->
clientsLock
);
QReadLocker
locker
(
&
server
->
clientsLock
);
ServerInfo_User_Container
*
infoSource
;
ServerInfo_User_Container
*
infoSource
=
server
->
findUser
(
userName
);
if
(
server
->
getUsers
().
contains
(
userName
))
if
(
!
infoSource
)
infoSource
=
server
->
getUsers
().
value
(
userName
);
else
if
(
server
->
getExternalUsers
().
contains
(
userName
))
infoSource
=
server
->
getExternalUsers
().
value
(
userName
);
else
return
Response
::
RespNameNotFound
;
return
Response
::
RespNameNotFound
;
re
->
mutable_user_info
()
->
CopyFrom
(
infoSource
->
copyUserInfo
(
true
,
false
,
userInfo
->
user_level
()
&
ServerInfo_User
::
IsModerator
));
re
->
mutable_user_info
()
->
CopyFrom
(
infoSource
->
copyUserInfo
(
true
,
false
,
userInfo
->
user_level
()
&
ServerInfo_User
::
IsModerator
));
...
...
servatrice/src/servatrice.cpp
View file @
d5ccda49
...
@@ -33,8 +33,6 @@
...
@@ -33,8 +33,6 @@
#include
"server_logger.h"
#include
"server_logger.h"
#include
"main.h"
#include
"main.h"
#include
"decklist.h"
#include
"decklist.h"
#include
"pb/game_replay.pb.h"
#include
"pb/event_replay_added.pb.h"
#include
"pb/event_server_message.pb.h"
#include
"pb/event_server_message.pb.h"
#include
"pb/event_server_shutdown.pb.h"
#include
"pb/event_server_shutdown.pb.h"
#include
"pb/event_connection_closed.pb.h"
#include
"pb/event_connection_closed.pb.h"
...
@@ -355,55 +353,6 @@ QList<ServerSocketInterface *> Servatrice::getUsersWithAddressAsList(const QHost
...
@@ -355,55 +353,6 @@ QList<ServerSocketInterface *> Servatrice::getUsersWithAddressAsList(const QHost
return
result
;
return
result
;
}
}
void
Servatrice
::
storeGameInformation
(
int
secondsElapsed
,
const
QSet
<
QString
>
&
allPlayersEver
,
const
QSet
<
QString
>
&
allSpectatorsEver
,
const
QList
<
GameReplay
*>
&
replayList
)
{
const
ServerInfo_Game
&
gameInfo
=
replayList
.
first
()
->
game_info
();
Server_Room
*
room
=
rooms
.
value
(
gameInfo
.
room_id
());
Event_ReplayAdded
replayEvent
;
ServerInfo_ReplayMatch
*
replayMatchInfo
=
replayEvent
.
mutable_match_info
();
replayMatchInfo
->
set_game_id
(
gameInfo
.
game_id
());
replayMatchInfo
->
set_room_name
(
room
->
getName
().
toStdString
());
replayMatchInfo
->
set_time_started
(
QDateTime
::
currentDateTime
().
addSecs
(
-
secondsElapsed
).
toTime_t
());
replayMatchInfo
->
set_length
(
secondsElapsed
);
replayMatchInfo
->
set_game_name
(
gameInfo
.
description
());
const
QStringList
&
allGameTypes
=
room
->
getGameTypes
();
QStringList
gameTypes
;
for
(
int
i
=
gameInfo
.
game_types_size
()
-
1
;
i
>=
0
;
--
i
)
gameTypes
.
append
(
allGameTypes
[
gameInfo
.
game_types
(
i
)]);
QSetIterator
<
QString
>
playerIterator
(
allPlayersEver
);
while
(
playerIterator
.
hasNext
())
replayMatchInfo
->
add_player_names
(
playerIterator
.
next
().
toStdString
());
for
(
int
i
=
0
;
i
<
replayList
.
size
();
++
i
)
{
ServerInfo_Replay
*
replayInfo
=
replayMatchInfo
->
add_replay_list
();
replayInfo
->
set_replay_id
(
replayList
[
i
]
->
replay_id
());
replayInfo
->
set_replay_name
(
gameInfo
.
description
());
replayInfo
->
set_duration
(
replayList
[
i
]
->
duration_seconds
());
}
QSet
<
QString
>
allUsersInGame
=
allPlayersEver
+
allSpectatorsEver
;
QSetIterator
<
QString
>
allUsersIterator
(
allUsersInGame
);
SessionEvent
*
sessionEvent
=
Server_ProtocolHandler
::
prepareSessionEvent
(
replayEvent
);
clientsLock
.
lockForRead
();
while
(
allUsersIterator
.
hasNext
())
{
const
QString
userName
=
allUsersIterator
.
next
();
Server_AbstractUserInterface
*
userHandler
=
users
.
value
(
userName
);
if
(
!
userHandler
)
userHandler
=
externalUsers
.
value
(
userName
);
if
(
userHandler
)
userHandler
->
sendProtocolItem
(
*
sessionEvent
);
}
clientsLock
.
unlock
();
delete
sessionEvent
;
getDatabaseInterface
()
->
storeGameInformation
(
room
->
getName
(),
gameTypes
,
gameInfo
,
allPlayersEver
,
allSpectatorsEver
,
replayList
);
}
void
Servatrice
::
updateLoginMessage
()
void
Servatrice
::
updateLoginMessage
()
{
{
if
(
!
servatriceDatabaseInterface
->
checkSql
())
if
(
!
servatriceDatabaseInterface
->
checkSql
())
...
...
servatrice/src/servatrice.h
View file @
d5ccda49
...
@@ -122,6 +122,7 @@ private:
...
@@ -122,6 +122,7 @@ private:
QMap
<
int
,
IslInterface
*>
islInterfaces
;
QMap
<
int
,
IslInterface
*>
islInterfaces
;
public
slots
:
public
slots
:
void
scheduleShutdown
(
const
QString
&
reason
,
int
minutes
);
void
scheduleShutdown
(
const
QString
&
reason
,
int
minutes
);
void
updateLoginMessage
();
public:
public:
Servatrice
(
QSettings
*
_settings
,
QObject
*
parent
=
0
);
Servatrice
(
QSettings
*
_settings
,
QObject
*
parent
=
0
);
~
Servatrice
();
~
Servatrice
();
...
@@ -139,12 +140,10 @@ public:
...
@@ -139,12 +140,10 @@ public:
AuthenticationMethod
getAuthenticationMethod
()
const
{
return
authenticationMethod
;
}
AuthenticationMethod
getAuthenticationMethod
()
const
{
return
authenticationMethod
;
}
QString
getDbPrefix
()
const
{
return
dbPrefix
;
}
QString
getDbPrefix
()
const
{
return
dbPrefix
;
}
int
getServerId
()
const
{
return
serverId
;
}
int
getServerId
()
const
{
return
serverId
;
}
void
updateLoginMessage
();
int
getUsersWithAddress
(
const
QHostAddress
&
address
)
const
;
int
getUsersWithAddress
(
const
QHostAddress
&
address
)
const
;
QList
<
ServerSocketInterface
*>
getUsersWithAddressAsList
(
const
QHostAddress
&
address
)
const
;
QList
<
ServerSocketInterface
*>
getUsersWithAddressAsList
(
const
QHostAddress
&
address
)
const
;
void
incTxBytes
(
quint64
num
);
void
incTxBytes
(
quint64
num
);
void
incRxBytes
(
quint64
num
);
void
incRxBytes
(
quint64
num
);
void
storeGameInformation
(
int
secondsElapsed
,
const
QSet
<
QString
>
&
allPlayersEver
,
const
QSet
<
QString
>
&
allSpectatorsEver
,
const
QList
<
GameReplay
*>
&
replays
);
void
addDatabaseInterface
(
QThread
*
thread
,
Servatrice_DatabaseInterface
*
databaseInterface
);
void
addDatabaseInterface
(
QThread
*
thread
,
Servatrice_DatabaseInterface
*
databaseInterface
);
bool
islConnectionExists
(
int
serverId
)
const
;
bool
islConnectionExists
(
int
serverId
)
const
;
...
...
servatrice/src/serversocketinterface.cpp
View file @
d5ccda49
...
@@ -739,7 +739,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
...
@@ -739,7 +739,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
Response
::
ResponseCode
ServerSocketInterface
::
cmdUpdateServerMessage
(
const
Command_UpdateServerMessage
&
/*cmd*/
,
ResponseContainer
&
/*rc*/
)
Response
::
ResponseCode
ServerSocketInterface
::
cmdUpdateServerMessage
(
const
Command_UpdateServerMessage
&
/*cmd*/
,
ResponseContainer
&
/*rc*/
)
{
{
servatrice
->
updateLoginMessage
(
);
QMetaObject
::
invokeMethod
(
server
,
"
updateLoginMessage
"
);
return
Response
::
RespOk
;
return
Response
::
RespOk
;
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment