QTfrontend/netclient.cpp
author unc0rr
Fri, 16 Dec 2005 20:59:49 +0000
changeset 28 59f7db859b8a
child 29 9311259d5242
permissions -rw-r--r--
- clear demos list before adding items - start to implement net game
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
#include <QMessageBox>
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    35
#include "netclient.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
HWNet::HWNet()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    38
	: QObject()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    39
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    40
	state = nsDisconnected;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    41
	IRCmsg_cmd_param = new QRegExp("^[A-Z]+ :.+$");
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    42
	IRCmsg_number_param = new QRegExp("^:\\S+ [0-9]{3} .+$");
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    43
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    44
	connect(&NetSocket, SIGNAL(readyRead()), this, SLOT(ClientRead()));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    45
	connect(&NetSocket, SIGNAL(connected()), this, SLOT(OnConnect()));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    46
	connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    47
	connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    48
			SLOT(displayError(QAbstractSocket::SocketError)));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    49
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    50
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    51
void HWNet::ClientRead()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    52
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    53
	while (NetSocket.canReadLine())
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    54
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    55
		ParseLine(NetSocket.readLine().trimmed());
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    56
	}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    57
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    58
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    59
void HWNet::displayError(QAbstractSocket::SocketError socketError)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    60
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    61
	switch (socketError)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    62
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    63
		case QAbstractSocket::RemoteHostClosedError:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    64
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    65
		case QAbstractSocket::HostNotFoundError:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    66
			QMessageBox::information(0, tr("Error"),
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    67
					tr("The host was not found. Please check the host name and port settings."));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    68
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    69
		case QAbstractSocket::ConnectionRefusedError:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    70
			QMessageBox::information(0, tr("Error"),
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    71
					tr("Connection refused"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    72
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    73
		default:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    74
			QMessageBox::information(0, tr("Error"),
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    75
					NetSocket.errorString());
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    76
    }
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    77
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    78
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    79
void HWNet::Connect(const QString & hostName, quint16 port)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    80
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    81
	state = nsConnecting;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    82
	NetSocket.connectToHost(hostName, port);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    83
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    84
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    85
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    86
void HWNet::OnConnect()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    87
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    88
	state = nsConnected;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    89
	SendNet(QString("USER hwgame 1 2 Hedgewars game"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    90
	SendNet(QString("NICK Hedgewars"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    91
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    92
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    93
void HWNet::OnDisconnect()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    94
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    95
	state = nsDisconnected;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    96
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    97
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    98
void HWNet::Perform()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
    99
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   100
	SendNet(QString("LIST"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   101
	SendNet(QString("JOIN #hw"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   102
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   103
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   104
void HWNet::Disconnect()
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   105
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   106
	switch (state)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   107
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   108
		case nsDisconnected:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   109
		{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   110
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   111
		}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   112
		case nsConnecting:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   113
		case nsQuitting:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   114
		{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   115
			NetSocket.disconnect();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   116
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   117
		}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   118
		default:
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   119
		{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   120
			state = nsQuitting;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   121
			SendNet(QString("QUIT :oops"));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   122
		}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   123
	}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   124
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   125
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   126
void HWNet::SendNet(const QString & str)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   127
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   128
	SendNet(str.toLatin1());
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   129
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   130
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   131
void HWNet::SendNet(const QByteArray & buf)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   132
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   133
	if (buf.size() > 510) return;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   134
	NetSocket.write(buf);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   135
	NetSocket.write("\x0d\x0a", 2);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   136
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   137
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   138
void HWNet::ParseLine(const QString & msg)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   139
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   140
	if (IRCmsg_cmd_param->exactMatch(msg))
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   141
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   142
		msgcmd_paramHandler(msg);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   143
	} else
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   144
	if (IRCmsg_number_param->exactMatch(msg))
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   145
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   146
		msgnumber_paramHandler(msg);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   147
	}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   148
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   149
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   150
void HWNet::msgcmd_paramHandler(const QString & msg)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   151
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   152
	QStringList list = msg.split(" :");
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   153
	if (list[0] == "PING")
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   154
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   155
		SendNet(QString("PONG %1").arg(list[1]));
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   156
	}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   157
}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   158
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   159
void HWNet::msgnumber_paramHandler(const QString & msg)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   160
{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   161
	QStringList list = msg.split(" ");
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   162
	bool ok;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   163
	quint16 number = list[1].toInt(&ok);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   164
	if (!ok)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   165
		return ;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   166
	switch (number)
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   167
	{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   168
		case 001 :
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   169
		{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   170
			Perform();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   171
			emit Connected();
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   172
			break;
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   173
		}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   174
		case 322 :
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   175
		{
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   176
			emit AddGame(list[3]);
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   177
		}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   178
	}
59f7db859b8a - clear demos list before adding items
unc0rr
parents:
diff changeset
   179
}