QTfrontend/newnetclient.cpp
changeset 1082 596b1dcdc1df
parent 1081 5be338fa4e2c
child 1083 3448dd03483f
equal deleted inserted replaced
1081:5be338fa4e2c 1082:596b1dcdc1df
    24 #include "gameuiconfig.h"
    24 #include "gameuiconfig.h"
    25 #include "game.h"
    25 #include "game.h"
    26 #include "gamecfgwidget.h"
    26 #include "gamecfgwidget.h"
    27 #include "teamselect.h"
    27 #include "teamselect.h"
    28 
    28 
    29 char delimeter=0x17;
    29 char delimeter='\n';
    30 
    30 
    31 HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) :
    31 HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) :
    32   config(config),
    32   config(config),
    33   m_pGameCFGWidget(pGameCFGWidget),
    33   m_pGameCFGWidget(pGameCFGWidget),
    34   m_pTeamSelWidget(pTeamSelWidget),
    34   m_pTeamSelWidget(pTeamSelWidget),
    52 {
    52 {
    53   m_game_connected=false;
    53   m_game_connected=false;
    54   NetSocket.disconnectFromHost();
    54   NetSocket.disconnectFromHost();
    55 }
    55 }
    56 
    56 
    57 void HWNewNet::JoinGame(const QString & game)
    57 void HWNewNet::CreateRoom(const QString & room)
    58 {
    58 {
    59   RawSendNet(QString("JOIN%1%2").arg(delimeter).arg(game));
    59 	RawSendNet(QString("CREATE%1%2").arg(delimeter).arg(room));
       
    60 }
       
    61 
       
    62 void HWNewNet::JoinRoom(const QString & room)
       
    63 {
       
    64 	RawSendNet(QString("JOIN%1%2").arg(delimeter).arg(room));
    60 }
    65 }
    61 
    66 
    62 void HWNewNet::AddTeam(const HWTeam & team)
    67 void HWNewNet::AddTeam(const HWTeam & team)
    63 {
    68 {
    64   RawSendNet(QString("ADDTEAM:") + delimeter +
    69   RawSendNet(QString("ADDTEAM:") + delimeter +
    85 
    90 
    86 void HWNewNet::SendNet(const QByteArray & buf)
    91 void HWNewNet::SendNet(const QByteArray & buf)
    87 {
    92 {
    88   QString msg = QString(buf.toBase64());
    93   QString msg = QString(buf.toBase64());
    89 
    94 
    90   //NetBuffer += buf;
       
    91   RawSendNet(QString("GAMEMSG:%1%2").arg(delimeter).arg(msg));
    95   RawSendNet(QString("GAMEMSG:%1%2").arg(delimeter).arg(msg));
    92 }
    96 }
    93 
    97 
    94 void HWNewNet::RawSendNet(const QString & str)
    98 void HWNewNet::RawSendNet(const QString & str)
    95 {
    99 {
    98 
   102 
    99 void HWNewNet::RawSendNet(const QByteArray & buf)
   103 void HWNewNet::RawSendNet(const QByteArray & buf)
   100 {
   104 {
   101 qDebug() << "Client: " << buf;
   105 qDebug() << "Client: " << buf;
   102   NetSocket.write(buf);
   106   NetSocket.write(buf);
   103   NetSocket.write("\n", 1);
   107   NetSocket.write("\n\n", 2);
   104 }
   108 }
   105 
   109 
   106 void HWNewNet::ClientRead()
   110 void HWNewNet::ClientRead()
   107 {
   111 {
   108   while (NetSocket.canReadLine()) {
   112 	while (NetSocket.canReadLine()) {
   109     ParseLine(NetSocket.readLine().trimmed());
   113 		QString s = QString::fromUtf8(NetSocket.readLine().trimmed());
   110   }
   114 
       
   115 		if (s.size() == 0) {
       
   116 			ParseCmd(cmdbuf);
       
   117 			cmdbuf.clear();
       
   118 		} else
       
   119 			cmdbuf << s;
       
   120 	}
   111 }
   121 }
   112 
   122 
   113 void HWNewNet::OnConnect()
   123 void HWNewNet::OnConnect()
   114 {
   124 {
   115   RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
   125   RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
       
   126   RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
       
   127   RawSendNet(QString("CREATE%1%2").arg(delimeter).arg("myroom"));
   116 }
   128 }
   117 
   129 
   118 void HWNewNet::OnDisconnect()
   130 void HWNewNet::OnDisconnect()
   119 {
   131 {
   120   //emit ChangeInTeams(QStringList());
   132   //emit ChangeInTeams(QStringList());
   139     QMessageBox::information(0, tr("Error"),
   151     QMessageBox::information(0, tr("Error"),
   140 			     NetSocket.errorString());
   152 			     NetSocket.errorString());
   141   }
   153   }
   142 }
   154 }
   143 
   155 
   144 void HWNewNet::ParseLine(const QByteArray & line)
   156 void HWNewNet::ParseCmd(const QStringList & lst)
   145 {
   157 {
   146 qDebug() << "Server: " << line;
   158 qDebug() << "Server: " << lst;
   147   QString msg = QString::fromUtf8 (line.data(), line.size());
   159 
   148 
   160   if(!lst.size())
   149   QStringList lst = msg.split(delimeter);
   161   {
   150 //qDebug() << "Parsing: " << lst;
   162     qWarning("Net client: Bad message");
       
   163     return;
       
   164   }
       
   165 
   151   if (lst[0] == "ERRONEUSNICKNAME") {
   166   if (lst[0] == "ERRONEUSNICKNAME") {
   152     QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used");
   167     QMessageBox::information(0, 0, "Your net nickname is in use or cannot be used");
   153     return;
   168     return;
   154   }
   169   }
   155 
   170 
   159     emit EnteredGame();
   174     emit EnteredGame();
   160     return;
   175     return;
   161   }
   176   }
   162 
   177 
   163   if (lst[0] == "CHAT_STRING") {
   178   if (lst[0] == "CHAT_STRING") {
   164     lst.pop_front();
   179     if(lst.size() < 3)
   165     if(lst.size() < 2)
       
   166     {
   180     {
   167 	  qWarning("Net: Empty CHAT_STRING message");
   181 	  qWarning("Net: Empty CHAT_STRING message");
   168 	  return;
   182 	  return;
   169     }
   183     }
   170     emit chatStringFromNet(lst);
   184     QStringList tmp = lst;
       
   185     tmp.removeFirst();
       
   186     emit chatStringFromNet(tmp);
   171     return;
   187     return;
   172   }
   188   }
   173 
   189 
   174   if (lst[0] == "ADDTEAM:") {
   190   if (lst[0] == "ADDTEAM:") {
   175     if(lst.size() < 14)
   191     if(lst.size() < 14)
   176     {
   192     {
   177 	  qWarning("Net: Too short ADDTEAM message");
   193 	  qWarning("Net: Too short ADDTEAM message");
   178 	  return;
   194 	  return;
   179     }
   195     }
   180     lst.pop_front();
   196     QStringList tmp = lst;
   181     emit AddNetTeam(lst);
   197     tmp.removeFirst();
       
   198     emit AddNetTeam(tmp);
   182     return;
   199     return;
   183   }
   200   }
   184 
   201 
   185   if (lst[0] == "REMOVETEAM:") {
   202   if (lst[0] == "REMOVETEAM:") {
   186     if(lst.size() < 3)
   203     if(lst.size() < 3)
   228     RunGame();
   245     RunGame();
   229     return;
   246     return;
   230   }
   247   }
   231 
   248 
   232   if (lst[0] == "CONFIGURED") {
   249   if (lst[0] == "CONFIGURED") {
   233     lst.pop_front();
   250     QStringList tmp = lst;
   234     if(lst.size() < 6)
   251     tmp.removeFirst();
       
   252     if(tmp.size() < 6)
   235     {
   253     {
   236       qWarning("Net: Bad CONFIGURED message");
   254       qWarning("Net: Bad CONFIGURED message");
   237       return;
   255       return;
   238     }
   256     }
   239     emit seedChanged(lst[0]);
   257     emit seedChanged(tmp[0]);
   240     emit mapChanged(lst[1]);
   258     emit mapChanged(tmp[1]);
   241     emit themeChanged(lst[2]);
   259     emit themeChanged(tmp[2]);
   242     emit initHealthChanged(lst[3].toUInt());
   260     emit initHealthChanged(tmp[3].toUInt());
   243     emit turnTimeChanged(lst[4].toUInt());
   261     emit turnTimeChanged(tmp[4].toUInt());
   244     emit fortsModeChanged(lst[5].toInt() != 0);
   262     emit fortsModeChanged(tmp[5].toInt() != 0);
   245     return;
   263     return;
   246   }
   264   }
   247 
   265 
   248   if(lst[0]=="TEAM_ACCEPTED") {
   266   if(lst[0]=="TEAM_ACCEPTED") {
   249     if(lst.size() < 3)
   267     if(lst.size() < 3)
   308 	  }
   326 	  }
   309 	  tmptm.numHedgehogs=lst[2].toUInt();
   327 	  tmptm.numHedgehogs=lst[2].toUInt();
   310 	  emit hhnumChanged(tmptm);
   328 	  emit hhnumChanged(tmptm);
   311 	  return;
   329 	  return;
   312   	}
   330   	}
   313     qWarning(QString("Net: Unknown 'CONFIG_PARAM' message: '%1'").arg(msg).toAscii().data());
   331     qWarning() << "Net: Unknown 'CONFIG_PARAM' message:" << lst;
   314     return;
   332     return;
   315   }
   333   }
   316 
   334 
   317 
   335 
   318   // should be kinda game states, which don't allow "GAMEMSG:" at configure step,
   336   // should be kinda game states, which don't allow "GAMEMSG:" at configure step,
   326     QByteArray em = QByteArray::fromBase64(lst[1].toAscii());
   344     QByteArray em = QByteArray::fromBase64(lst[1].toAscii());
   327     emit FromNet(em);
   345     emit FromNet(em);
   328     return;
   346     return;
   329   }
   347   }
   330 
   348 
   331   qWarning(QString("Net: Unknown message: '%1'").arg(msg).toAscii().data());
   349   qWarning() << "Net: Unknown message:" << lst;
   332 }
   350 }
   333 
   351 
   334 
   352 
   335 void HWNewNet::ConfigAsked()
   353 void HWNewNet::ConfigAsked()
   336 {
   354 {