QTfrontend/netclient.h
author unc0rr
Wed, 28 Dec 2005 22:18:11 +0000
changeset 32 78bff13b11c0
parent 31 99888245a4e8
child 36 a803a00a3272
permissions -rw-r--r--
With this patch the game doesn't crash when gaming by net
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
 *
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     6
 *
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
     9
 * with the Software without restriction, including without limitation the
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    12
 * furnished to do so, subject to the following conditions:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    13
 *
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    15
 *    this list of conditions and the following disclaimer.
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    18
 *    and/or other materials provided with the distribution.
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    20
 *    derived from this software without specific prior written permission.
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    21
 *
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    32
 */
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    33
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    34
#ifndef NET_H
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    35
#define NET_H
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    36
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    37
#include <QObject>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    38
#include <QTcpSocket>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    39
#include <QRegExp>
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    40
#include <QStringList>
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    41
#include "team.h"
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    42
#include "rndstr.h"
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    43
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    44
#define MAGIC_CHAR "\x02"
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    45
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    46
struct netTeam
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    47
{
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    48
	QString nick;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    49
	QStringList hhs;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    50
};
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    51
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    52
class HWNet : public QObject
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    53
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    54
	Q_OBJECT
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    55
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    56
public:
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    57
    HWNet(int Resolution, bool Fullscreen);
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    58
	void Connect(const QString & hostName, quint16 port, const QString & nick);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    59
	void Disconnect();
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    60
	void JoinGame(const QString & game);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    61
	void AddTeam(const HWTeam & team);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    62
	void StartGame();
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    63
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    64
signals:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    65
	void Connected();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    66
	void AddGame(const QString & chan);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    67
	void EnteredGame();
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    68
	void FromNet(const QByteArray & buf);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    69
	void LocalCFG(const QString & team);
32
78bff13b11c0 With this patch the game doesn't crash when gaming by net
unc0rr
parents: 31
diff changeset
    70
	void ChangeInTeams(const QStringList & teams);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    71
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    72
public slots:
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    73
	void SendNet(const QByteArray & buf);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    74
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    75
private:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    76
	enum NetState {
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    77
		nsDisconnected	= 0,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    78
		nsConnecting	= 1,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    79
		nsConnected	= 3,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    80
		nsJoining	= 4,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    81
		nsJoined	= 5,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    82
		nsStarting	= 6,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    83
		nsGaming	= 7,
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    84
		nsQuitting	= 8
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    85
	};
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    86
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    87
	QTcpSocket NetSocket;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    88
	NetState state;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    89
	QRegExp * IRCmsg_cmd_text;
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    90
	QRegExp * IRCmsg_number_param;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    91
	QRegExp * IRCmsg_who_cmd_target;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    92
	QRegExp * IRCmsg_who_cmd_target_text;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    93
	QRegExp * IRCmsg_who_cmd_text;
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    94
	QString mynick;
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    95
	QString opnick;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    96
	QString channel;
29
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    97
	bool isOp;
9311259d5242 More work on net game, still far from complete
unc0rr
parents: 28
diff changeset
    98
	quint32 opCount;
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
    99
	netTeam teams[5];
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   100
	quint8 teamsCount;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   101
	int gameResolution;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   102
	bool gameFullscreen;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   103
	RNDStr seedgen;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   104
	int playerscnt;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   105
	int configasks;
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   106
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   107
	void RawSendNet(const QString & buf);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   108
	void RawSendNet(const QByteArray & buf);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   109
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   110
	void ParseLine(const QString & msg);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   111
	void msgcmd_textHandler(const QString & msg);
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   112
	void msgnumber_paramHandler(const QString & msg);
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   113
	void msgwho_cmd_targetHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   114
	void msgwho_cmd_textHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   115
	void msgwho_cmd_target_textHandler(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   116
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   117
	void hwp_opmsg(const QString & who, const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   118
	void hwp_chanmsg(const QString & who, const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   119
	void ConfigAsked();
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   120
	void NetTeamAdded(const QString & msg);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   121
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   122
	void RunGame(const QString & seed);
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   123
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   124
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   125
private slots:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   126
	void ClientRead();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   127
	void OnConnect();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   128
	void OnDisconnect();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   129
	void Perform();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   130
	void displayError(QAbstractSocket::SocketError socketError);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   131
};
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   132
31
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   133
#define SENDCFGSTRNET(a)   {\
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   134
							QByteArray strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   135
							strmsg.append(a); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   136
							quint8 sz = strmsg.size(); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   137
							QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   138
							emit FromNet(enginemsg); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   139
							RawSendNet(QString("PRIVMSG %1 :"MAGIC_CHAR MAGIC_CHAR"%2").arg(channel, QString(enginemsg.toBase64()))); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   140
						}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   141
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   142
#define SENDCFGSTRLOC(a)   {\
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   143
							QByteArray strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   144
							strmsg.append(a); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   145
							quint8 sz = strmsg.size(); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   146
							QByteArray enginemsg = QByteArray((char *)&sz, 1) + strmsg; \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   147
							emit FromNet(enginemsg); \
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   148
						}
99888245a4e8 Now net game is near available, but exception occurs...
unc0rr
parents: 29
diff changeset
   149
28
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   150
#endif