23 #include "ircclient.h"
25 #include "lookuphost.h"
27 IRCClient::IRCClient()
29 this->bIsInHostLookupMode =
false;
31 recvTimer.setSingleShot(
true);
34 QObject::connect(&socket, SIGNAL(readyRead()),
this, SLOT(receiveSocketDataDelayed()));
35 QObject::connect(&recvTimer, SIGNAL(timeout()),
this, SLOT(receiveSocketData()));
38 IRCClient::~IRCClient()
43 void IRCClient::connect(
const QString& address,
unsigned short port)
45 emit
infoMessage(tr(
"IRC: attempting host lookup: %1").arg(address));
47 this->bIsInHostLookupMode =
true;
49 this->hostName = address;
51 LookupHost::lookupHost(address,
this, SLOT( hostLookupFinished(
const QHostInfo&) ) );
56 pAdapter->pSocket = &socket;
57 pAdapter->connect(&socket, SIGNAL( connected() ), SLOT( connected() ));
58 pAdapter->connect(&socket, SIGNAL( disconnected() ), SLOT( disconnected() ));
59 pAdapter->connect(&socket, SIGNAL( error(QAbstractSocket::SocketError) ), SLOT( errorReceived(QAbstractSocket::SocketError) ));
60 pAdapter->connect(
this, SIGNAL( hostLookupError(QHostInfo::HostInfoError) ), SLOT( hostLookupError(QHostInfo::HostInfoError) ));
64 void IRCClient::disconnect()
72 void IRCClient::hostLookupFinished(
const QHostInfo& hostInfo)
74 this->bIsInHostLookupMode =
false;
76 if (hostInfo.error() != QHostInfo::NoError)
78 emit hostLookupError(hostInfo.error());
82 const QHostAddress* pIp = this->pickAddress(hostInfo.addresses());
86 emit hostLookupError(QHostInfo::HostNotFound);
90 emit
infoMessage(tr(
"IRC: Connecting: %1:%2 [IP: %3]").arg(this->hostName).arg(this->port).arg(pIp->toString()));
91 socket.connectToHost(*pIp, port);
96 bool IRCClient::isConnected()
const
98 return socket.state() == QTcpSocket::ConnectedState && !this->bIsInHostLookupMode;
101 const QHostAddress* IRCClient::pickAddress(
const QList<QHostAddress>& addressesList)
103 const QHostAddress* pIPv6 = NULL;;
105 foreach (
const QHostAddress& addr, addressesList)
107 if (addr.protocol() != QAbstractSocket::IPv4Protocol)
109 if (addr.protocol() == QAbstractSocket::IPv6Protocol
126 void IRCClient::receiveSocketData()
128 while (socket.canReadLine())
130 QByteArray socketData = socket.readLine();
131 QString responseLine = QString::fromUtf8(socketData.constData(), socketData.size());
132 emit ircServerResponse(responseLine);
136 void IRCClient::receiveSocketDataDelayed()
138 if (!recvTimer.isActive())
144 bool IRCClient::sendMessage(
const QString& message)
151 QByteArray messageContent = message.toUtf8();
152 messageContent.append(
"\r\n");
154 qint64 numBytesWritten = socket.write(messageContent);
157 return numBytesWritten == messageContent.size();
160 void IRCClient::setFakeRecvLag(
int lagMs)
162 recvTimer.setInterval(lagMs);
void infoMessage(const QString &message)
These are the messages that IRCClient class sends to inform the upper layers of progress.