1 #ifndef SERVER_PROTOCOLHANDLER_H
2 #define SERVER_PROTOCOLHANDLER_H
3 
4 #include "pb/response.pb.h"
5 #include "pb/server_message.pb.h"
6 #include "server.h"
7 #include "server_abstractuserinterface.h"
8 
9 #include <QObject>
10 #include <QPair>
11 
12 class Features;
13 class Server_DatabaseInterface;
14 class Server_Player;
15 class ServerInfo_User;
16 class Server_Room;
17 class QTimer;
18 class FeatureSet;
19 
20 class ServerMessage;
21 class Response;
22 class SessionEvent;
23 class GameEventContainer;
24 class RoomEvent;
25 class ResponseContainer;
26 
27 class CommandContainer;
28 class SessionCommand;
29 class ModeratorCommand;
30 class AdminCommand;
31 
32 class Command_Ping;
33 class Command_Login;
34 class Command_Register;
35 class Command_Message;
36 class Command_ListUsers;
37 class Command_GetGamesOfUser;
38 class Command_GetUserInfo;
39 class Command_ListRooms;
40 class Command_JoinRoom;
41 class Command_LeaveRoom;
42 class Command_RoomSay;
43 class Command_CreateGame;
44 class Command_JoinGame;
45 
46 class Server_ProtocolHandler : public QObject, public Server_AbstractUserInterface
47 {
48     Q_OBJECT
49 protected:
50     QMap<int, Server_Room *> rooms;
51 
52     bool deleted;
53     Server_DatabaseInterface *databaseInterface;
54     AuthenticationResult authState;
55     bool acceptsUserListChanges;
56     bool acceptsRoomListChanges;
57     bool idleClientWarningSent;
logDebugMessage(const QString &)58     virtual void logDebugMessage(const QString & /* message */)
59     {
60     }
61 
62 private:
63     QList<int> messageSizeOverTime, messageCountOverTime, commandCountOverTime;
64     int timeRunning, lastDataReceived, lastActionReceived;
65     QTimer *pingClock;
66 
67     virtual void transmitProtocolItem(const ServerMessage &item) = 0;
68 
69     Response::ResponseCode cmdPing(const Command_Ping &cmd, ResponseContainer &rc);
70     Response::ResponseCode cmdLogin(const Command_Login &cmd, ResponseContainer &rc);
71     Response::ResponseCode cmdMessage(const Command_Message &cmd, ResponseContainer &rc);
72     Response::ResponseCode cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc);
73     Response::ResponseCode cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc);
74     Response::ResponseCode cmdListRooms(const Command_ListRooms &cmd, ResponseContainer &rc);
75     Response::ResponseCode cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc);
76     Response::ResponseCode cmdListUsers(const Command_ListUsers &cmd, ResponseContainer &rc);
77     Response::ResponseCode cmdLeaveRoom(const Command_LeaveRoom &cmd, Server_Room *room, ResponseContainer &rc);
78     Response::ResponseCode cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room, ResponseContainer &rc);
79     Response::ResponseCode cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc);
80     Response::ResponseCode cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc);
81 
82     Response::ResponseCode processSessionCommandContainer(const CommandContainer &cont, ResponseContainer &rc);
83     virtual Response::ResponseCode
processExtendedSessionCommand(int,const SessionCommand &,ResponseContainer &)84     processExtendedSessionCommand(int /* cmdType */, const SessionCommand & /* cmd */, ResponseContainer & /* rc */)
85     {
86         return Response::RespFunctionNotAllowed;
87     }
88     Response::ResponseCode processRoomCommandContainer(const CommandContainer &cont, ResponseContainer &rc);
89     Response::ResponseCode processGameCommandContainer(const CommandContainer &cont, ResponseContainer &rc);
90     Response::ResponseCode processModeratorCommandContainer(const CommandContainer &cont, ResponseContainer &rc);
91     virtual Response::ResponseCode
processExtendedModeratorCommand(int,const ModeratorCommand &,ResponseContainer &)92     processExtendedModeratorCommand(int /* cmdType */, const ModeratorCommand & /* cmd */, ResponseContainer & /* rc */)
93     {
94         return Response::RespFunctionNotAllowed;
95     }
96     Response::ResponseCode processAdminCommandContainer(const CommandContainer &cont, ResponseContainer &rc);
97     virtual Response::ResponseCode
processExtendedAdminCommand(int,const AdminCommand &,ResponseContainer &)98     processExtendedAdminCommand(int /* cmdType */, const AdminCommand & /* cmd */, ResponseContainer & /* rc */)
99     {
100         return Response::RespFunctionNotAllowed;
101     }
102 
103     void resetIdleTimer();
104 private slots:
105     void pingClockTimeout();
106 public slots:
107     void prepareDestroy();
108 
109 public:
110     Server_ProtocolHandler(Server *_server, Server_DatabaseInterface *_databaseInterface, QObject *parent = 0);
111     ~Server_ProtocolHandler();
112 
getAcceptsUserListChanges()113     bool getAcceptsUserListChanges() const
114     {
115         return acceptsUserListChanges;
116     }
getAcceptsRoomListChanges()117     bool getAcceptsRoomListChanges() const
118     {
119         return acceptsRoomListChanges;
120     }
121     virtual QString getAddress() const = 0;
122     virtual QString getConnectionType() const = 0;
getDatabaseInterface()123     Server_DatabaseInterface *getDatabaseInterface() const
124     {
125         return databaseInterface;
126     }
127 
getLastCommandTime()128     int getLastCommandTime() const
129     {
130         return timeRunning - lastDataReceived;
131     }
132     void processCommandContainer(const CommandContainer &cont);
133 
134     void sendProtocolItem(const Response &item);
135     void sendProtocolItem(const SessionEvent &item);
136     void sendProtocolItem(const GameEventContainer &item);
137     void sendProtocolItem(const RoomEvent &item);
138 };
139 
140 #endif
141