File indexing completed on 2024-04-21 04:02:07

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0004 *                                                                         *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #ifndef KBLOCKSNETSERVER_H
0008 #define KBLOCKSNETSERVER_H
0009 
0010 #include <QObject>
0011 #include <QUdpSocket>
0012 #include <QMap>
0013 #include <QList>
0014 #include <QString>
0015 #include <QByteArray>
0016 
0017 #include "KBlocksGameLogic.h"
0018 #include "KBlocksScore.h"
0019 
0020 using namespace std;
0021 
0022 class KBlocksNetServer : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     KBlocksNetServer(KBlocksGameLogic *p, const QString &localIP);
0028     ~KBlocksNetServer() override;
0029 
0030 public:
0031     int  executeGame(int gameCount, bool waitForAll);
0032 
0033     void setSendLength(int initLen, int lvUpLen);
0034     void setRecordFile(const char *fileName, bool binaryMode = true);
0035 
0036 private:
0037     void recvRemoteData(QList<QByteArray> *recvData, QList<QString> *recvAddr);
0038     int  processGame(int gameIndex);
0039 
0040     void addPlayerIP(int gameIndex, const QByteArray &data, const QString &addr);
0041     void delPlayerIP(const QString &addr);
0042 
0043     void sendPlayerActionLength();
0044     void sendPlayerData(int gameIndex);
0045     void sendGameOver();
0046     void sendGuiData(const QString &addr);
0047 
0048     int  parseRemoteData(const QByteArray &data, const QString &addr);
0049     int  parsePlayerReply(const QByteArray &data, const QString &addr);
0050 
0051     bool parseIPString(const QString &input, QHostAddress *ip, quint16 *port);
0052     QString formIPString(const QHostAddress &inAddr, quint16 inPort);
0053     void formByteFromInt(int value, char *data);
0054 
0055     void printGameResult();
0056 
0057 private:
0058     KBlocksGameLogic *mpGameLogic;
0059     KBlocksScore **maGameScoreList;
0060 
0061     bool mWaitForAll;
0062     //bool mSpeedMode;
0063     //int mTimeOut;
0064     int mTopGameLevel;
0065     int mInitSendLength;
0066     int mLvUpSendLength;
0067 
0068     QHostAddress mLocalAddress;
0069     quint16 mLocalPort;
0070     QUdpSocket *mpServerSocket;
0071 
0072     QHostAddress mRemoteAddress;
0073     quint16 mRemotePort;
0074 
0075     bool mRunningFlag;
0076     int mGameCount;
0077     bool mGameStarted;
0078 
0079     QMap<QString, int> mPlayerMapping;
0080     QMap<int, QString> mPlayerName;
0081     QList<QString> mPlayerIPList;
0082 
0083     string mRecordFileName;
0084     bool mRecordFileType;
0085 };
0086 
0087 #endif
0088