Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cnet.hpp"
- CNet::CNet(CNetConf pConf)
- {
- cConf = pConf;
- cSock = new QTcpSocket(this);
- connect(cSock, SIGNAL(readyRead()), this, SLOT(readData()));
- connect(cSock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sockErr()));
- connect(cSock, SIGNAL(disconnected()), this, SLOT(serverCosed()));
- connectToServer();
- }
- void CNet::sendData(QList<QVariant> pData)
- {
- QDataStream stream(cSock);
- stream<<pData;
- }
- void CNet::readData()
- {
- QDataStream in(cSock);
- QList<QVariant> data;
- in>>data;
- switch (data.at(0).toInt())
- {
- case CNet::CAuthorisation:
- emit authReceived(data.at(1).toBool());
- break;
- // case CNet::CPosition:
- // break;
- case CNet::CMessage:
- emit messageReceived(data.at(1).toString());
- break;
- case CNet::CChatMessage:
- int chan = data.at(1).toInt();
- QString name = data.at(2).toString();
- QString msg = data.at(3).toString();
- emit chatMessageReceived(chan, name, msg);
- break;
- // case CNet::CCastSpell:
- // break;
- // case CNet::CStatsUpdate:
- // break;
- // case CNet::CStatusUpdate:
- // break;
- // case CNet::CSurroundingInfo:
- // break;
- // default:
- // break;
- }
- }
- void CNet::sockErr()
- {
- emit sockErr(QString("Could not connect to the Server:<br> %1").arg(cSock->errorString()));
- }
- void CNet::disconnectAll()
- {
- cSock->disconnectFromHost();
- }
- QString CNet::md5(QString pTxt)
- {
- return QString(QCryptographicHash::hash(pTxt.toStdString().c_str(),QCryptographicHash::Md5).toHex());
- }
- void CNet::serverCosed()
- {
- emit serverClosedConn();
- }
- void CNet::connectToServer()
- {
- cout<<"Connecting to "<<cConf.getHost().toString().toStdString().c_str()<<"..."<<endl;
- cSock->connectToHost(cConf.getHost(), cConf.getPort());
- }
- void CNet::tryReconnect()
- {
- connectToServer();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement