QTfrontend/net/newnetclient.cpp
branchios-develop
changeset 13413 ba39a1d396c0
parent 13322 b77a9380dd0f
child 13658 73993abb85d7
equal deleted inserted replaced
13411:6e8b807bda4b 13413:ba39a1d396c0
    97     NetSocket.disconnectFromHost();
    97     NetSocket.disconnectFromHost();
    98 }
    98 }
    99 
    99 
   100 void HWNewNet::CreateRoom(const QString & room, const QString & password)
   100 void HWNewNet::CreateRoom(const QString & room, const QString & password)
   101 {
   101 {
   102     if(netClientState != InLobby)
   102     if(netClientState != InLobby || !ByteLength(room))
   103     {
   103     {
   104         qWarning("Illegal try to create room!");
   104         qWarning("Illegal try to create room!");
   105         return;
   105         return;
   106     }
   106     }
   107 
   107 
   172 void HWNewNet::SendNet(const QByteArray & buf)
   172 void HWNewNet::SendNet(const QByteArray & buf)
   173 {
   173 {
   174     QString msg = QString(buf.toBase64());
   174     QString msg = QString(buf.toBase64());
   175 
   175 
   176     RawSendNet(QString("EM%1%2").arg(delimiter).arg(msg));
   176     RawSendNet(QString("EM%1%2").arg(delimiter).arg(msg));
       
   177 }
       
   178 
       
   179 int HWNewNet::ByteLength(const QString & str)
       
   180 {
       
   181 	return str.toUtf8().size();
   177 }
   182 }
   178 
   183 
   179 void HWNewNet::RawSendNet(const QString & str)
   184 void HWNewNet::RawSendNet(const QString & str)
   180 {
   185 {
   181     RawSendNet(str.toUtf8());
   186     RawSendNet(str.toUtf8());
   240 }
   245 }
   241 
   246 
   242 void HWNewNet::SendPasswordHash(const QString & hash)
   247 void HWNewNet::SendPasswordHash(const QString & hash)
   243 {
   248 {
   244     // don't send it immediately, only store and check if server asked us for a password
   249     // don't send it immediately, only store and check if server asked us for a password
   245     m_passwordHash = hash.toAscii();
   250     m_passwordHash = hash.toLatin1();
   246 
   251 
   247     maybeSendPassword();
   252     maybeSendPassword();
   248 }
   253 }
   249 
   254 
   250 void HWNewNet::ParseCmd(const QStringList & lst)
   255 void HWNewNet::ParseCmd(const QStringList & lst)
   269         return ;
   274         return ;
   270 
   275 
   271     if (lst[0] == "ERROR")
   276     if (lst[0] == "ERROR")
   272     {
   277     {
   273         if (lst.size() == 2)
   278         if (lst.size() == 2)
   274             emit Error(HWApplication::translate("server", lst[1].toAscii().constData()));
   279             emit Error(HWApplication::translate("server", lst[1].toLatin1().constData()));
   275         else
   280         else
   276             emit Error("Unknown error");
   281             emit Error("Unknown error");
   277         return;
   282         return;
   278     }
   283     }
   279 
   284 
   280     if (lst[0] == "WARNING")
   285     if (lst[0] == "WARNING")
   281     {
   286     {
   282         if (lst.size() == 2)
   287         if (lst.size() == 2)
   283             emit Warning(HWApplication::translate("server", lst[1].toAscii().constData()));
   288             emit Warning(HWApplication::translate("server", lst[1].toLatin1().constData()));
   284         else
   289         else
   285             emit Warning("Unknown warning");
   290             emit Warning("Unknown warning");
   286         return;
   291         return;
   287     }
   292     }
   288 
   293 
   445         const QStringList nicks = lst.mid(2);
   450         const QStringList nicks = lst.mid(2);
   446 
   451 
   447         while(flags.size() > 1)
   452         while(flags.size() > 1)
   448         {
   453         {
   449             flags.remove(0, 1);
   454             flags.remove(0, 1);
   450             char c = flags[0].toAscii();
   455             char c = flags[0].toLatin1();
   451             bool inRoom = (netClientState == InRoom || netClientState == InGame);
   456             bool inRoom = (netClientState == InRoom || netClientState == InGame);
   452 
   457 
   453             switch(c)
   458             switch(c)
   454             {
   459             {
   455                 // flag indicating if a player is ready to start a game
   460                 // flag indicating if a player is ready to start a game
   671             //omitted 'emit disconnected()', we don't want the error message
   676             //omitted 'emit disconnected()', we don't want the error message
   672             return;
   677             return;
   673         }
   678         }
   674         m_game_connected = false;
   679         m_game_connected = false;
   675         Disconnect();
   680         Disconnect();
   676         emit disconnected(HWApplication::translate("server", lst[1].toAscii().constData()));
   681         emit disconnected(HWApplication::translate("server", lst[1].toLatin1().constData()));
   677         return;
   682         return;
   678     }
   683     }
   679 
   684 
   680     if(lst[0] == "JOINING")
   685     if(lst[0] == "JOINING")
   681     {
   686     {
   725                 qWarning("Net: Bad EM message");
   730                 qWarning("Net: Bad EM message");
   726                 return;
   731                 return;
   727             }
   732             }
   728             for(int i = 1; i < lst.size(); ++i)
   733             for(int i = 1; i < lst.size(); ++i)
   729             {
   734             {
   730                 QByteArray em = QByteArray::fromBase64(lst[i].toAscii());
   735                 QByteArray em = QByteArray::fromBase64(lst[i].toLatin1());
   731                 emit FromNet(em);
   736                 emit FromNet(em);
   732             }
   737             }
   733             return;
   738             return;
   734         }
   739         }
   735 
   740 
   907     }
   912     }
   908 }
   913 }
   909 
   914 
   910 void HWNewNet::chatLineToNet(const QString& str)
   915 void HWNewNet::chatLineToNet(const QString& str)
   911 {
   916 {
   912     if(str != "")
   917     if(ByteLength(str))
   913     {
   918     {
   914         RawSendNet(QString("CHAT") + delimiter + str);
   919         RawSendNet(QString("CHAT") + delimiter + str);
   915         QString action = HWProto::chatStringToAction(str);
   920         QString action = HWProto::chatStringToAction(str);
   916         if (action != NULL)
   921         if (action != NULL)
   917             emit(roomChatAction(mynick, action));
   922             emit(roomChatAction(mynick, action));
   920     }
   925     }
   921 }
   926 }
   922 
   927 
   923 void HWNewNet::chatLineToLobby(const QString& str)
   928 void HWNewNet::chatLineToLobby(const QString& str)
   924 {
   929 {
   925     if(str != "")
   930     if(ByteLength(str))
   926     {
   931     {
   927         RawSendNet(QString("CHAT") + delimiter + str);
   932         RawSendNet(QString("CHAT") + delimiter + str);
   928         QString action = HWProto::chatStringToAction(str);
   933         QString action = HWProto::chatStringToAction(str);
   929         if (action != NULL)
   934         if (action != NULL)
   930             emit(lobbyChatAction(mynick, action));
   935             emit(lobbyChatAction(mynick, action));
  1153 
  1158 
  1154     if(m_passwordHash.isEmpty() || m_serverSalt.isEmpty())
  1159     if(m_passwordHash.isEmpty() || m_serverSalt.isEmpty())
  1155         return;
  1160         return;
  1156 
  1161 
  1157     QString hash = QCryptographicHash::hash(
  1162     QString hash = QCryptographicHash::hash(
  1158                 m_clientSalt.toAscii()
  1163                 m_clientSalt.toLatin1()
  1159                 .append(m_serverSalt.toAscii())
  1164                 .append(m_serverSalt.toLatin1())
  1160                 .append(m_passwordHash)
  1165                 .append(m_passwordHash)
  1161                 .append(cProtoVer->toAscii())
  1166                 .append(cProtoVer->toLatin1())
  1162                 .append("!hedgewars")
  1167                 .append("!hedgewars")
  1163                 , QCryptographicHash::Sha1).toHex();
  1168                 , QCryptographicHash::Sha1).toHex();
  1164 
  1169 
  1165     m_serverHash = QCryptographicHash::hash(
  1170     m_serverHash = QCryptographicHash::hash(
  1166                 m_serverSalt.toAscii()
  1171                 m_serverSalt.toLatin1()
  1167                 .append(m_clientSalt.toAscii())
  1172                 .append(m_clientSalt.toLatin1())
  1168                 .append(m_passwordHash)
  1173                 .append(m_passwordHash)
  1169                 .append(cProtoVer->toAscii())
  1174                 .append(cProtoVer->toLatin1())
  1170                 .append("!hedgewars")
  1175                 .append("!hedgewars")
  1171                 , QCryptographicHash::Sha1).toHex();
  1176                 , QCryptographicHash::Sha1).toHex();
  1172 
  1177 
  1173     RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimiter).arg(hash).arg(m_clientSalt));
  1178     RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimiter).arg(hash).arg(m_clientSalt));
  1174 }
  1179 }