1 | /*************************************************************************** |
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | ***************************************************************************/ |
10 | #ifndef KBLOCKSNETSERVER_H |
11 | #define KBLOCKSNETSERVER_H |
12 | |
13 | #include <QObject> |
14 | #include <QUdpSocket> |
15 | #include <QMap> |
16 | #include <QList> |
17 | #include <QString> |
18 | #include <QByteArray> |
19 | |
20 | #include "KBlocksGameLogic.h" |
21 | #include "KBlocksScore.h" |
22 | |
23 | using namespace std; |
24 | |
25 | class KBlocksNetServer : public QObject |
26 | { |
27 | Q_OBJECT |
28 | |
29 | public: |
30 | KBlocksNetServer(KBlocksGameLogic * p, const QString& localIP); |
31 | ~KBlocksNetServer(); |
32 | |
33 | public: |
34 | int executeGame(int gameCount, bool waitForAll); |
35 | |
36 | void setSendLength(int initLen, int lvUpLen); |
37 | void setRecordFile(const char * fileName, bool binaryMode = true); |
38 | |
39 | private: |
40 | void recvRemoteData(QVector<QByteArray> * recvData, QVector<QString> * recvAddr); |
41 | int processGame(int gameIndex); |
42 | |
43 | void addPlayerIP(int gameIndex, const QByteArray& data, const QString& addr); |
44 | void delPlayerIP(const QString& addr); |
45 | |
46 | void sendPlayerActionLength(); |
47 | void sendPlayerData(int gameIndex); |
48 | void sendGameOver(); |
49 | void sendGuiData(const QString& addr); |
50 | |
51 | int parseRemoteData(const QByteArray& data, const QString& addr); |
52 | int parsePlayerReply(const QByteArray& data, const QString& addr); |
53 | |
54 | bool parseIPString(const QString& input, QHostAddress * ip, quint16 * port); |
55 | QString formIPString(const QHostAddress& inAddr, quint16 inPort); |
56 | void formByteFromInt(int value, char * data); |
57 | |
58 | void printGameResult(); |
59 | |
60 | private: |
61 | KBlocksGameLogic* mpGameLogic; |
62 | KBlocksScore** maGameScoreList; |
63 | |
64 | bool mWaitForAll; |
65 | bool mSpeedMode; |
66 | int mTimeOut; |
67 | int mTopGameLevel; |
68 | int mInitSendLength; |
69 | int mLvUpSendLength; |
70 | |
71 | QHostAddress mLocalAddress; |
72 | quint16 mLocalPort; |
73 | QUdpSocket * mpServerSocket; |
74 | |
75 | QHostAddress mRemoteAddress; |
76 | quint16 mRemotePort; |
77 | |
78 | bool mRunningFlag; |
79 | int mGameCount; |
80 | bool mGameStarted; |
81 | |
82 | QMap<QString, int> mPlayerMapping; |
83 | QMap<int, QString> mPlayerName; |
84 | QList<QString> mPlayerIPList; |
85 | |
86 | string mRecordFileName; |
87 | bool mRecordFileType; |
88 | }; |
89 | |
90 | #endif |
91 | |
92 | |