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