Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include "cnet.hpp"
  2.  
  3. CNet::CNet(CNetConf pConf)
  4. {
  5.     cConf = pConf;
  6.     cSock   = new QTcpSocket(this);
  7.  
  8.     connect(cSock, SIGNAL(readyRead()), this, SLOT(readData()));
  9.     connect(cSock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sockErr()));
  10.     connect(cSock, SIGNAL(disconnected()), this, SLOT(serverCosed()));
  11.  
  12.     connectToServer();
  13. }
  14.  
  15. void CNet::sendData(QList<QVariant> pData)
  16. {
  17.     QDataStream stream(cSock);
  18.     stream<<pData;
  19. }
  20.  
  21. void CNet::readData()
  22. {
  23.     QDataStream in(cSock);
  24.     QList<QVariant> data;
  25.     in>>data;
  26.  
  27.     switch (data.at(0).toInt())
  28.     {
  29.     case CNet::CAuthorisation:
  30.         emit authReceived(data.at(1).toBool());
  31.         break;
  32. //  case CNet::CPosition:
  33. //      break;
  34.     case CNet::CMessage:
  35.         emit messageReceived(data.at(1).toString());
  36.         break;
  37.     case CNet::CChatMessage:
  38.         int chan        = data.at(1).toInt();
  39.         QString name    = data.at(2).toString();
  40.         QString msg     = data.at(3).toString();
  41.         emit chatMessageReceived(chan, name, msg);
  42.         break;
  43. //  case CNet::CCastSpell:
  44. //      break;
  45. //  case CNet::CStatsUpdate:
  46. //      break;
  47. //  case CNet::CStatusUpdate:
  48. //      break;
  49. //  case CNet::CSurroundingInfo:
  50. //      break;
  51. //  default:
  52. //      break;
  53.     }
  54.  
  55. }
  56.  
  57. void CNet::sockErr()
  58. {
  59.     emit sockErr(QString("Could not connect to the Server:<br>&nbsp;&nbsp;&nbsp;&nbsp;%1").arg(cSock->errorString()));
  60. }
  61.  
  62. void CNet::disconnectAll()
  63. {
  64.     cSock->disconnectFromHost();
  65. }
  66.  
  67. QString CNet::md5(QString pTxt)
  68. {
  69.     return QString(QCryptographicHash::hash(pTxt.toStdString().c_str(),QCryptographicHash::Md5).toHex());
  70. }
  71.  
  72. void CNet::serverCosed()
  73. {
  74.     emit serverClosedConn();
  75. }
  76.  
  77. void CNet::connectToServer()
  78. {
  79.     cout<<"Connecting to "<<cConf.getHost().toString().toStdString().c_str()<<"..."<<endl;
  80.     cSock->connectToHost(cConf.getHost(), cConf.getPort());
  81. }
  82.  
  83. void CNet::tryReconnect()
  84. {
  85.     connectToServer();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement