Fix another server failure (when second client in room disconnects)
authorunc0rr
Mon, 06 Oct 2008 18:37:47 +0000
changeset 1308 d5dcd6cfa5e2
parent 1307 ce26e16d18ab
child 1309 1a38a967bd48
Fix another server failure (when second client in room disconnects)
QTfrontend/newnetclient.cpp
netserver/HWProto.hs
netserver/newhwserv.hs
--- a/QTfrontend/newnetclient.cpp	Mon Oct 06 18:07:38 2008 +0000
+++ b/QTfrontend/newnetclient.cpp	Mon Oct 06 18:37:47 2008 +0000
@@ -109,7 +109,7 @@
 
 void HWNewNet::RawSendNet(const QByteArray & buf)
 {
-  qDebug() << "Client: " << buf;
+  qDebug() << "Client: " << QString(buf).split("\n");
   NetSocket.write(buf);
   NetSocket.write("\n\n", 2);
 }
@@ -171,6 +171,9 @@
 		return;
 	}
 
+	if ((lst[0] == "NICK") || (lst[0] == "PROTO"))
+		return ;
+
 	if (lst[0] == "ERROR") {
 		if (lst.size() == 2)
 			QMessageBox::information(0, 0, "Error: " + lst[1]);
@@ -187,24 +190,24 @@
 		return;
 	}
 
-  if (lst[0] == "CONNECTED") {
-    m_game_connected=true;
-    emit Connected();
-    emit EnteredGame();
-    return;
-  }
+	if (lst[0] == "CONNECTED") {
+		m_game_connected=true;
+		emit Connected();
+		emit EnteredGame();
+		return;
+	}
 
-  if (lst[0] == "CHAT_STRING") {
-    if(lst.size() < 3)
-    {
-	  qWarning("Net: Empty CHAT_STRING message");
-	  return;
-    }
-    QStringList tmp = lst;
-    tmp.removeFirst();
-    emit chatStringFromNet(tmp);
-    return;
-  }
+	if (lst[0] == "CHAT_STRING") {
+		if(lst.size() < 3)
+		{
+		qWarning("Net: Empty CHAT_STRING message");
+		return;
+		}
+		QStringList tmp = lst;
+		tmp.removeFirst();
+		emit chatStringFromNet(tmp);
+		return;
+	}
 
   if (lst[0] == "ADDTEAM:") {
     if(lst.size() < 22)
@@ -228,21 +231,22 @@
     return;
   }
 
-  if(lst[0]=="SLAVE") {
-    m_pGameCFGWidget->setEnabled(false);
-    m_pTeamSelWidget->setNonInteractive();
-    return;
-  }
+/*	if(lst[0] == "SLAVE") { // клиент знает CREATE он делал или JOIN
+		m_pGameCFGWidget->setEnabled(false);
+		m_pTeamSelWidget->setNonInteractive();
+		return;
+	}*/
 
-  if(lst[0]=="JOINED") {
-    if(lst.size() < 2)
-    {
-      qWarning("Net: Bad JOINED message");
-      return;
-    }
-    emit nickAdded(lst[1]);
-    return;
-  }
+	if(lst[0]=="JOINED") {
+		if(lst.size() < 2)
+		{
+			qWarning("Net: Bad JOINED message");
+			return;
+		}
+		for(int i = 1; i < lst.size(); ++i)
+			emit nickAdded(lst[i]);
+		return;
+	}
 
   if(lst[0]=="LEFT") {
     if(lst.size() < 2)
--- a/netserver/HWProto.hs	Mon Oct 06 18:07:38 2008 +0000
+++ b/netserver/HWProto.hs	Mon Oct 06 18:37:47 2008 +0000
@@ -9,7 +9,7 @@
 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])]
 answerQuit = [(clientOnly, ["BYE"])]
 answerAbandoned = [(sameRoom, ["BYE"])]
-answerQuitInform nick = [(sameRoom, ["QUIT", nick])]
+answerQuitInform nick = [(othersInRoom, ["QUIT", nick])]
 answerNickChosen = [(clientOnly, ["ERROR", "The nick already chosen"])]
 answerNickChooseAnother = [(clientOnly, ["WARNING", "Choose another nick"])]
 answerNick nick = [(clientOnly, ["NICK", nick])]
@@ -30,7 +30,7 @@
 	if null (room client) then
 		(noChangeClients, noChangeRooms, answerQuit)
 	else if isMaster client then
-		(noChangeClients, removeRoom (room client), answerAbandoned ++ (answerQuitInform $ nick client)) -- core disconnects clients on ROOMABANDONED answer
+		(noChangeClients, removeRoom (room client), answerAbandoned) -- core disconnects clients on ROOMABANDONED answer
 	else
 		(noChangeClients, noChangeRooms, answerQuitInform $ nick client)
 
@@ -86,15 +86,16 @@
 handleCmd_noRoom client clients rooms ["CREATE", newRoom] =
 	handleCmd_noRoom client clients rooms ["CREATE", newRoom, ""]
 	
-handleCmd_noRoom client _ rooms ["JOIN", roomName, roomPassword] =
+handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
 	if noSuchRoom then
 		(noChangeClients, noChangeRooms, answerNoRoom)
 	else if roomPassword /= password (roomByName roomName rooms) then
 		(noChangeClients, noChangeRooms, answerWrongPassword)
 	else
-		(modifyClient client{room = roomName}, noChangeRooms, answerJoined $ nick client)
+		(modifyClient client{room = roomName}, noChangeRooms, (answerJoined $ nick client) ++ answerNicks)
 	where
 		noSuchRoom = null $ filter (\room -> roomName == name room) rooms
+		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ filter (\ci -> room ci == roomName) clients))]
 
 handleCmd_noRoom client clients rooms ["JOIN", roomName] =
 	handleCmd_noRoom client clients rooms ["JOIN", roomName, ""]
--- a/netserver/newhwserv.hs	Mon Oct 06 18:07:38 2008 +0000
+++ b/netserver/newhwserv.hs	Mon Oct 06 18:37:47 2008 +0000
@@ -7,6 +7,7 @@
 import Control.Concurrent.STM
 import Control.Exception (setUncaughtExceptionHandler, handle, finally)
 import Control.Monad (forM, forM_, filterM, liftM)
+import Maybe (fromMaybe)
 import Data.List
 import Miscutils
 import HWProto
@@ -69,8 +70,10 @@
 
 			let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
 			let mrooms = roomsFunc rooms
+			let mclients = (clientsFunc clients)
+			let mclient = fromMaybe client $ find (== client) mclients
 
-			clientsIn <- sendAnswers answers client (clientsFunc clients) mrooms
+			clientsIn <- sendAnswers answers mclient mclients mrooms
 			
 			mainLoop servSock acceptChan clientsIn mrooms