mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-14 14:32:15 -07:00
Turn Card, Deck_List, Protocol, RNG, Network (Client, Server), Settings and Utility into libraries and remove cockatrice_common. (#6212)
--------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de> Co-authored-by: ebbit1q <ebbit1q@gmail.com>
This commit is contained in:
parent
be1403c920
commit
1ef07309d6
605 changed files with 3812 additions and 3408 deletions
|
|
@ -0,0 +1,24 @@
|
|||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(HEADERS abstract_client.h)
|
||||
|
||||
set(SOURCES abstract_client.cpp)
|
||||
|
||||
if(Qt6_FOUND)
|
||||
qt6_wrap_cpp(MOC_SOURCES ${HEADERS})
|
||||
elseif(Qt5_FOUND)
|
||||
qt5_wrap_cpp(MOC_SOURCES ${HEADERS})
|
||||
endif()
|
||||
|
||||
add_library(libcockatrice_network_client_abstract STATIC ${MOC_SOURCES} ${SOURCES})
|
||||
|
||||
add_dependencies(libcockatrice_network_client_abstract libcockatrice_protocol libcockatrice_network_server_remote)
|
||||
|
||||
target_include_directories(libcockatrice_network_client_abstract PUBLIC .)
|
||||
|
||||
target_link_libraries(
|
||||
libcockatrice_network_client_abstract PUBLIC ${COCKATRICE_QT_MODULES} libcockatrice_protocol
|
||||
libcockatrice_network_server_remote
|
||||
)
|
||||
|
|
@ -0,0 +1,197 @@
|
|||
#include "abstract_client.h"
|
||||
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <libcockatrice/protocol/featureset.h>
|
||||
#include <libcockatrice/protocol/get_pb_extension.h>
|
||||
#include <libcockatrice/protocol/pb/commands.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_add_to_list.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_connection_closed.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_game_joined.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_list_rooms.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_notify_user.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_remove_from_list.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_replay_added.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_server_identification.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_server_message.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_server_shutdown.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_user_joined.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_user_left.pb.h>
|
||||
#include <libcockatrice/protocol/pb/event_user_message.pb.h>
|
||||
#include <libcockatrice/protocol/pb/server_message.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
|
||||
AbstractClient::AbstractClient(QObject *parent)
|
||||
: QObject(parent), nextCmdId(0), status(StatusDisconnected), serverSupportsPasswordHash(false)
|
||||
{
|
||||
qRegisterMetaType<QVariant>("QVariant");
|
||||
qRegisterMetaType<CommandContainer>("CommandContainer");
|
||||
qRegisterMetaType<Response>("Response");
|
||||
qRegisterMetaType<Response::ResponseCode>("Response::ResponseCode");
|
||||
qRegisterMetaType<ClientStatus>("ClientStatus");
|
||||
qRegisterMetaType<RoomEvent>("RoomEvent");
|
||||
qRegisterMetaType<GameEventContainer>("GameEventContainer");
|
||||
qRegisterMetaType<Event_ServerIdentification>("Event_ServerIdentification");
|
||||
qRegisterMetaType<Event_ConnectionClosed>("Event_ConnectionClosed");
|
||||
qRegisterMetaType<Event_ServerShutdown>("Event_ServerShutdown");
|
||||
qRegisterMetaType<Event_AddToList>("Event_AddToList");
|
||||
qRegisterMetaType<Event_RemoveFromList>("Event_RemoveFromList");
|
||||
qRegisterMetaType<Event_UserJoined>("Event_UserJoined");
|
||||
qRegisterMetaType<Event_UserLeft>("Event_UserLeft");
|
||||
qRegisterMetaType<Event_ServerMessage>("Event_ServerMessage");
|
||||
qRegisterMetaType<Event_ListRooms>("Event_ListRooms");
|
||||
qRegisterMetaType<Event_GameJoined>("Event_GameJoined");
|
||||
qRegisterMetaType<Event_UserMessage>("Event_UserMessage");
|
||||
qRegisterMetaType<Event_NotifyUser>("Event_NotifyUser");
|
||||
qRegisterMetaType<ServerInfo_User>("ServerInfo_User");
|
||||
qRegisterMetaType<QList<ServerInfo_User>>("QList<ServerInfo_User>");
|
||||
qRegisterMetaType<Event_ReplayAdded>("Event_ReplayAdded");
|
||||
qRegisterMetaType<QList<QString>>("missingFeatures");
|
||||
qRegisterMetaType<PendingCommand *>("pendingCommand");
|
||||
|
||||
FeatureSet features;
|
||||
features.initalizeFeatureList(clientFeatures);
|
||||
|
||||
connect(this, &AbstractClient::sigQueuePendingCommand, this, &AbstractClient::queuePendingCommand);
|
||||
}
|
||||
|
||||
AbstractClient::~AbstractClient()
|
||||
{
|
||||
}
|
||||
|
||||
void AbstractClient::processProtocolItem(const ServerMessage &item)
|
||||
{
|
||||
switch (item.message_type()) {
|
||||
case ServerMessage::RESPONSE: {
|
||||
const Response &response = item.response();
|
||||
const int cmdId = response.cmd_id();
|
||||
|
||||
PendingCommand *pend = pendingCommands.value(cmdId, 0);
|
||||
if (!pend)
|
||||
return;
|
||||
pendingCommands.remove(cmdId);
|
||||
|
||||
pend->processResponse(response);
|
||||
pend->deleteLater();
|
||||
break;
|
||||
}
|
||||
case ServerMessage::SESSION_EVENT: {
|
||||
const SessionEvent &event = item.session_event();
|
||||
switch ((SessionEvent::SessionEventType)getPbExtension(event)) {
|
||||
case SessionEvent::SERVER_IDENTIFICATION:
|
||||
emit serverIdentificationEventReceived(event.GetExtension(Event_ServerIdentification::ext));
|
||||
break;
|
||||
case SessionEvent::SERVER_MESSAGE:
|
||||
emit serverMessageEventReceived(event.GetExtension(Event_ServerMessage::ext));
|
||||
break;
|
||||
case SessionEvent::SERVER_SHUTDOWN:
|
||||
emit serverShutdownEventReceived(event.GetExtension(Event_ServerShutdown::ext));
|
||||
break;
|
||||
case SessionEvent::CONNECTION_CLOSED:
|
||||
emit connectionClosedEventReceived(event.GetExtension(Event_ConnectionClosed::ext));
|
||||
break;
|
||||
case SessionEvent::USER_MESSAGE:
|
||||
emit userMessageEventReceived(event.GetExtension(Event_UserMessage::ext));
|
||||
break;
|
||||
case SessionEvent::NOTIFY_USER:
|
||||
emit notifyUserEventReceived(event.GetExtension(Event_NotifyUser::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;
|
||||
case SessionEvent::REPLAY_ADDED:
|
||||
emit replayAddedEventReceived(event.GetExtension(Event_ReplayAdded::ext));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ServerMessage::GAME_EVENT_CONTAINER: {
|
||||
emit gameEventContainerReceived(item.game_event_container());
|
||||
break;
|
||||
}
|
||||
case ServerMessage::ROOM_EVENT: {
|
||||
emit roomEventReceived(item.room_event());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractClient::setStatus(const ClientStatus _status)
|
||||
{
|
||||
QMutexLocker locker(&clientMutex);
|
||||
if (_status != status) {
|
||||
status = _status;
|
||||
emit statusChanged(_status);
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractClient::sendCommand(const CommandContainer &cont)
|
||||
{
|
||||
sendCommand(new PendingCommand(cont));
|
||||
}
|
||||
|
||||
void AbstractClient::sendCommand(PendingCommand *pend)
|
||||
{
|
||||
pend->moveToThread(thread());
|
||||
emit sigQueuePendingCommand(pend);
|
||||
}
|
||||
|
||||
void AbstractClient::queuePendingCommand(PendingCommand *pend)
|
||||
{
|
||||
// This function is always called from the client thread via signal/slot.
|
||||
const int cmdId = getNewCmdId();
|
||||
pend->getCommandContainer().set_cmd_id(cmdId);
|
||||
|
||||
pendingCommands.insert(cmdId, pend);
|
||||
|
||||
sendCommandContainer(pend->getCommandContainer());
|
||||
}
|
||||
|
||||
PendingCommand *AbstractClient::prepareSessionCommand(const ::google::protobuf::Message &cmd)
|
||||
{
|
||||
CommandContainer cont;
|
||||
SessionCommand *c = cont.add_session_command();
|
||||
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
|
||||
return new PendingCommand(cont);
|
||||
}
|
||||
|
||||
PendingCommand *AbstractClient::prepareRoomCommand(const ::google::protobuf::Message &cmd, int roomId)
|
||||
{
|
||||
CommandContainer cont;
|
||||
RoomCommand *c = cont.add_room_command();
|
||||
cont.set_room_id(roomId);
|
||||
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
|
||||
return new PendingCommand(cont);
|
||||
}
|
||||
|
||||
PendingCommand *AbstractClient::prepareModeratorCommand(const ::google::protobuf::Message &cmd)
|
||||
{
|
||||
CommandContainer cont;
|
||||
ModeratorCommand *c = cont.add_moderator_command();
|
||||
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
|
||||
return new PendingCommand(cont);
|
||||
}
|
||||
|
||||
PendingCommand *AbstractClient::prepareAdminCommand(const ::google::protobuf::Message &cmd)
|
||||
{
|
||||
CommandContainer cont;
|
||||
AdminCommand *c = cont.add_admin_command();
|
||||
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
|
||||
return new PendingCommand(cont);
|
||||
}
|
||||
|
|
@ -0,0 +1,134 @@
|
|||
/**
|
||||
* @file abstract_client.h
|
||||
* @ingroup Client
|
||||
* @brief TODO: Document this.
|
||||
*/
|
||||
|
||||
#ifndef ABSTRACTCLIENT_H
|
||||
#define ABSTRACTCLIENT_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
#include <libcockatrice/protocol/pb/response.pb.h>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_user.pb.h>
|
||||
|
||||
class PendingCommand;
|
||||
class CommandContainer;
|
||||
class RoomEvent;
|
||||
class GameEventContainer;
|
||||
class ServerMessage;
|
||||
class Event_ServerIdentification;
|
||||
class Event_AddToList;
|
||||
class Event_RemoveFromList;
|
||||
class Event_UserJoined;
|
||||
class Event_UserLeft;
|
||||
class Event_ServerMessage;
|
||||
class Event_ListRooms;
|
||||
class Event_GameJoined;
|
||||
class Event_UserMessage;
|
||||
class Event_NotifyUser;
|
||||
class Event_ConnectionClosed;
|
||||
class Event_ServerShutdown;
|
||||
class Event_ReplayAdded;
|
||||
class FeatureSet;
|
||||
|
||||
enum ClientStatus
|
||||
{
|
||||
StatusDisconnected,
|
||||
StatusDisconnecting,
|
||||
StatusConnecting,
|
||||
StatusRegistering,
|
||||
StatusActivating,
|
||||
StatusLoggingIn,
|
||||
StatusLoggedIn,
|
||||
StatusRequestingForgotPassword,
|
||||
StatusSubmitForgotPasswordReset,
|
||||
StatusSubmitForgotPasswordChallenge,
|
||||
StatusGettingPasswordSalt,
|
||||
};
|
||||
|
||||
class AbstractClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void statusChanged(ClientStatus _status);
|
||||
void maxPingTime(int seconds, int maxSeconds);
|
||||
|
||||
// Room events
|
||||
void roomEventReceived(const RoomEvent &event);
|
||||
// Game events
|
||||
void gameEventContainerReceived(const GameEventContainer &event);
|
||||
// Session events
|
||||
void serverIdentificationEventReceived(const Event_ServerIdentification &event);
|
||||
void connectionClosedEventReceived(const Event_ConnectionClosed &event);
|
||||
void serverShutdownEventReceived(const Event_ServerShutdown &event);
|
||||
void addToListEventReceived(const Event_AddToList &event);
|
||||
void removeFromListEventReceived(const Event_RemoveFromList &event);
|
||||
void userJoinedEventReceived(const Event_UserJoined &event);
|
||||
void userLeftEventReceived(const Event_UserLeft &event);
|
||||
void serverMessageEventReceived(const Event_ServerMessage &event);
|
||||
void listRoomsEventReceived(const Event_ListRooms &event);
|
||||
void gameJoinedEventReceived(const Event_GameJoined &event);
|
||||
void userMessageEventReceived(const Event_UserMessage &event);
|
||||
void notifyUserEventReceived(const Event_NotifyUser &event);
|
||||
void userInfoChanged(const ServerInfo_User &userInfo);
|
||||
void buddyListReceived(const QList<ServerInfo_User> &buddyList);
|
||||
void ignoreListReceived(const QList<ServerInfo_User> &ignoreList);
|
||||
void replayAddedEventReceived(const Event_ReplayAdded &event);
|
||||
void registerAccepted();
|
||||
void registerAcceptedNeedsActivate();
|
||||
void activateAccepted();
|
||||
|
||||
void sigQueuePendingCommand(PendingCommand *pend);
|
||||
|
||||
private:
|
||||
int nextCmdId;
|
||||
mutable QMutex clientMutex;
|
||||
ClientStatus status;
|
||||
private slots:
|
||||
void queuePendingCommand(PendingCommand *pend);
|
||||
protected slots:
|
||||
void processProtocolItem(const ServerMessage &item);
|
||||
|
||||
protected:
|
||||
QMap<int, PendingCommand *> pendingCommands;
|
||||
QString userName, password, email, country, realName, token;
|
||||
bool serverSupportsPasswordHash;
|
||||
void setStatus(ClientStatus _status);
|
||||
int getNewCmdId()
|
||||
{
|
||||
return nextCmdId++;
|
||||
}
|
||||
virtual void sendCommandContainer(const CommandContainer &cont) = 0;
|
||||
|
||||
public:
|
||||
explicit AbstractClient(QObject *parent = nullptr);
|
||||
~AbstractClient() override;
|
||||
|
||||
ClientStatus getStatus() const
|
||||
{
|
||||
QMutexLocker locker(&clientMutex);
|
||||
return status;
|
||||
}
|
||||
void sendCommand(const CommandContainer &cont);
|
||||
void sendCommand(PendingCommand *pend);
|
||||
|
||||
bool getServerSupportsPasswordHash() const
|
||||
{
|
||||
return serverSupportsPasswordHash;
|
||||
}
|
||||
const QString &getUserName() const
|
||||
{
|
||||
return userName;
|
||||
}
|
||||
|
||||
static PendingCommand *prepareSessionCommand(const ::google::protobuf::Message &cmd);
|
||||
static PendingCommand *prepareRoomCommand(const ::google::protobuf::Message &cmd, int roomId);
|
||||
static PendingCommand *prepareModeratorCommand(const ::google::protobuf::Message &cmd);
|
||||
static PendingCommand *prepareAdminCommand(const ::google::protobuf::Message &cmd);
|
||||
|
||||
QMap<QString, bool> clientFeatures;
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue