58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#include "socketirc.h"
|
|
#include <iostream>
|
|
#include <QCoreApplication>
|
|
|
|
SocketIRC::SocketIRC() : server("irc.freenode.net"), port(6667), isConnected(false)
|
|
{
|
|
connect(&sock, SIGNAL(readyRead()), this, SLOT(readMsg()));
|
|
connect(&sock, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
|
|
}
|
|
|
|
SocketIRC::SocketIRC(QString server_, int port_) : server(server_), port(port_), isConnected(false)
|
|
{
|
|
connect(&sock, SIGNAL(readyRead()), this, SLOT(readMsg()));
|
|
connect(&sock, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
|
|
}
|
|
|
|
void SocketIRC::sendMsg(QString msg)
|
|
{
|
|
sock.write(msg.toStdString().c_str());
|
|
sock.flush();
|
|
}
|
|
|
|
int SocketIRC::connectToServer(QCoreApplication* app_)
|
|
{
|
|
app = app_;
|
|
|
|
sock.connectToHost(server, quint16(port));
|
|
if(!sock.waitForConnected(3000))
|
|
{
|
|
std::cerr << "failed to connect to " << server.toStdString()
|
|
<< " on port " << port
|
|
<< ": " << sock.errorString().toStdString() << std::endl;
|
|
|
|
if(app != NULL)
|
|
app->exit();
|
|
else
|
|
return 0;
|
|
}
|
|
return app->exec();
|
|
}
|
|
|
|
void SocketIRC::readMsg()
|
|
{
|
|
char buffer[512];
|
|
while(sock.canReadLine())
|
|
{
|
|
int numbytes = sock.readLine(buffer, 512);
|
|
buffer[numbytes - 2] = '\0';
|
|
std::cout << buffer << std::endl;
|
|
emit receivedMsg(QString(buffer));
|
|
}
|
|
}
|
|
|
|
void SocketIRC::onDisconnect()
|
|
{
|
|
app->exit();
|
|
}
|