QTfrontend/netclient.h
author unc0rr
Thu, 05 Oct 2006 16:33:18 +0000
changeset 183 57c2ef19f719
parent 127 ca70467bd0a8
child 184 f97a7a3dc8f6
permissions -rw-r--r--
Relicense to GPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     1
/*
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    12
 * GNU General Public License for more details.
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 127
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    17
 */
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    18
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    19
#ifndef NET_H
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    20
#define NET_H
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    21
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    22
#include <QObject>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    23
#include <QTcpSocket>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    24
#include <QRegExp>
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    25
#include <QStringList>
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
    26
#include <QTimer>
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    27
#include "team.h"
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    28
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 85
diff changeset
    29
#define MAGIC_CHAR "\x2f"
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    30
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    31
struct netTeam
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    32
{
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    33
	QString nick;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    34
	QStringList hhs;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    35
};
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    36
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    37
class GameUIConfig;
84
0f6669da2fcb Start rewrite ui
unc0rr
parents: 36
diff changeset
    38
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    39
class HWNet : public QObject
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    40
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    41
	Q_OBJECT
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    42
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    43
public:
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    44
    HWNet(GameUIConfig * config);
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    45
	void Connect(const QString & hostName, quint16 port, const QString & nick);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    46
	void Disconnect();
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    47
	void JoinGame(const QString & game);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    48
	void AddTeam(const HWTeam & team);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    49
	void StartGame();
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    50
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    51
signals:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    52
	void Connected();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    53
	void AddGame(const QString & chan);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    54
	void EnteredGame();
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    55
	void FromNet(const QByteArray & buf);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    56
	void LocalCFG(const QString & team);
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
    57
	void ChangeInTeams(const QStringList & teams);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    58
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    59
public slots:
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    60
	void SendNet(const QByteArray & buf);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    61
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    62
private:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    63
	enum NetState {
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    64
		nsDisconnected	= 0,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    65
		nsConnecting	= 1,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    66
		nsConnected	= 3,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    67
		nsJoining	= 4,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    68
		nsJoined	= 5,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    69
		nsStarting	= 6,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    70
		nsGaming	= 7,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    71
		nsQuitting	= 8
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    72
	};
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    73
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    74
	QTcpSocket NetSocket;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    75
	NetState state;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    76
	QRegExp * IRCmsg_cmd_text;
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    77
	QRegExp * IRCmsg_number_param;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    78
	QRegExp * IRCmsg_who_cmd_target;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    79
	QRegExp * IRCmsg_who_cmd_target_text;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    80
	QRegExp * IRCmsg_who_cmd_text;
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    81
	QString mynick;
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    82
	QString opnick;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    83
	QString channel;
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 85
diff changeset
    84
	QString seed;
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    85
	bool isOp;
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    86
	quint32 opCount;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    87
	netTeam teams[5];
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    88
	quint8 teamsCount;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    89
	int playerscnt;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    90
	int configasks;
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
    91
	QByteArray NetBuffer;
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
    92
	QTimer * TimerFlusher;
85
44d9045b26ff New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents: 84
diff changeset
    93
	GameUIConfig * config;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    94
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    95
	void RawSendNet(const QString & buf);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    96
	void RawSendNet(const QByteArray & buf);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    97
125
0532e70b1ce4 Fix unicode names in net game
unc0rr
parents: 97
diff changeset
    98
	void ParseLine(const QByteArray & line);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    99
	void msgcmd_textHandler(const QString & msg);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   100
	void msgnumber_paramHandler(const QString & msg);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   101
	void msgwho_cmd_targetHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   102
	void msgwho_cmd_textHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   103
	void msgwho_cmd_target_textHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   104
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   105
	void hwp_opmsg(const QString & who, const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   106
	void hwp_chanmsg(const QString & who, const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   107
	void ConfigAsked();
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   108
	void NetTeamAdded(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   109
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 85
diff changeset
   110
	void RunGame();
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   111
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   112
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   113
private slots:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   114
	void ClientRead();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   115
	void OnConnect();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   116
	void OnDisconnect();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   117
	void Perform();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   118
	void displayError(QAbstractSocket::SocketError socketError);
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   119
	void FlushNetBuf();
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   120
};
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   121
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   122
#define SENDCFGSTRNET(a)   {\
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   123
							QByteArray strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   124
							strmsg.append(a); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   125
							quint8 sz = strmsg.size(); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   126
							QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
36
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   127
							QString _msg = MAGIC_CHAR MAGIC_CHAR + QString(enginemsg.toBase64()); \
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   128
							hwp_chanmsg(mynick, _msg); \
a803a00a3272 - Anti-flood and anti-lag changes
unc0rr
parents: 32
diff changeset
   129
							RawSendNet(QString("PRIVMSG %1 :").arg(channel) + _msg); \
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   130
						}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   131
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   132
#define SENDCFGSTRLOC(a)   {\
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   133
							QByteArray strmsg; \
126
93df479aa1b9 - Now really fix unicode issues
unc0rr
parents: 125
diff changeset
   134
							strmsg.append(QString(a).toUtf8()); \
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   135
							quint8 sz = strmsg.size(); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   136
							QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   137
							emit FromNet(enginemsg); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   138
						}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   139
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   140
#endif