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