-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchessclient.cpp
More file actions
63 lines (48 loc) · 1.24 KB
/
chessclient.cpp
File metadata and controls
63 lines (48 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "chessclient.h"
#include "chesslog.h"
#include "chessinformation.h"
#include <QTcpSocket>
ChessClient * ChessClient::INSTANCE = 0;
ChessClient * ChessClient::instance()
{
if(!INSTANCE)
{
INSTANCE = new ChessClient;
}
return INSTANCE;
}
ChessClient::ChessClient(QObject *parent) :
ChessOpposition(parent), socket(0)
{
QString ip = ChessInformation::instance()->getServerIp();
quint16 port = ChessInformation::instance()->getServerPort();
socket = new QTcpSocket(this);
socket->connectToHost(ip, port);
connect(socket, SIGNAL(readyRead()), this, SLOT(read()));
Chess_Trace(tr("new ChessClient"));
}
ChessClient::~ChessClient()
{
Chess_Trace(tr("delete ChessClient"));
}
bool ChessClient::isValid()
{
if(socket)
{
if(socket->state() != QAbstractSocket::ConnectedState) return false;
if(socket->isValid()) return true;
}
return false;
}
void ChessClient::send(const QString &message)
{
if(isValid())
{
socket->write(message.toLocal8Bit());
}
}
void ChessClient::read()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
receive(QString(socket->readAll()));
}