Advertisement

Socket Help

Started by November 21, 2005 05:57 PM
0 comments, last by Surg AKA Kunark 19 years, 3 months ago
Hey guys me again, Working on a multiplayer game. It involves a linked list of connections. Now when a client leaves I want the game to shift the players behind it to be in its postition. IE. Player1: Player1 Player2: Player2 Player3: Player3 Player4: Player4 Player 2 then leaves it should look like this Player1: Player1 Player2: Player3 Player3: Player4 Player4: Empty if no other player, or 5 This is the following code I am trying to use.

void CServer::OnClose(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	
	// Iterate through connections
	std::list<CServer*>::iterator scan = m_pDlg->m_plConnections.begin();
	for(unsigned int i(1); scan != m_pDlg->m_plConnections.end(); i++, scan++)
	{
		// Delete the connection that dropped
		if ((*scan)==this)
		{
			m_pDlg->m_plConnections.remove(this);
			m_pDlg->m_csMessages += "Client Disconnected\r\n";
			m_pDlg->UpdateData(false);
			m_pDlg->m_iConnections--;

			// (i) holds player that disconnected
            
			for(; i<m_pDlg->m_iConnections+1; i++){
				// Copy over data and allow next player know what their new 
				// Player Number Is
				m_pDlg->m_cstrPlayerNames = m_pDlg->m_cstrPlayerNames[i+1];

				SocketPacket spBuffer;
				spBuffer.iFlag = 2;
				spBuffer.data.MessagePacket.iFlag = 2;
				spBuffer.data.MessagePacket.iHighParam = 0;
				spBuffer.data.MessagePacket.iLowParam = i+1;

				(*scan)->Send(&spBuffer, sizeof(SocketPacket));
				scan++;
			}

			TRACE("Player1: %s\n", m_pDlg->m_cstrPlayerNames[0]);
			TRACE("Player2: %s\n", m_pDlg->m_cstrPlayerNames[1]);
			TRACE("Player3: %s\n", m_pDlg->m_cstrPlayerNames[2]);
			TRACE("Player4: %s\n", m_pDlg->m_cstrPlayerNames[3]);

			delete this;
			return;
		}
	}

	CAsyncSocket::OnClose(nErrorCode);
}


It seems to be going into the if statement even when it shouldn't be. Perhaps I don't have a good enough understanding of stl lists. But when you call remove, that shifts all the connections down concurrently so that the next time you run the connection that was there is now gone and the latter connections have taken it place.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Okay all that was, was an issue with visual studios. Closed the entire program down and reopened and worked just fine.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF

This topic is closed to new replies.

Advertisement