If condition, stuck inside switch? condition check not working


#1

I’m making a small networked game, still learning networking so I might be missing out on something.
I am using Visual Studio Community 2017.
I am using Allegro 5 for inputs and graphics.
I am using SDL_Net for networking.

  • The game is supposed to launch, works fine.
  • Every game tick, function HandleEvents(...) gets called in my code.
  • The game connects to the server if a mouse click was registered between 0-10 px on the screen, both x and y.

that worked fine, except that the client connected multiple times, so I set a condition so that it will only try and connect 1 client. I did this using a condition:

if(connected) {
std::cout << "Already connected.." << std::endl;
} else
...

Here is minimal viable code code, so you can see what code that generates error example.
function in MenuState.cpp:

void MenuState::HandleEvents(Input* input, ClientSocket* cs) {
	if (input->mouseX >= 0 && input->mouseX <= 10 && input->mouseY >= 0 && input->mouseY <= 10) {
		if (input->mouseB == MouseLDown) {
			//Set mouse state to none, so that it doesn't "hold it down" but just clicks once,
			input->set_mouse_state(MouseNDown);

			if (connected == true) {
					std::cout << "You're already connected." << std::endl;
				}
				else {
					switch (cs->connectToServer()) {
					case Packets::SERVER_NOT_FULL:
						connected = true;
						std::cout << "Hello" << std::endl; //This never prints, why?
						break;
					}
				}
		}
	}
}

Here is connectToServer function from ClientSocket.cpp

int ClientSocket::connectToServer()
{
	// Resolve hostname of server ip and put result in hostResolved integer
	int hostResolved = SDLNet_ResolveHost(&serverIP, serverHostname.c_str(), serverPort);

	// if resolving host fails, else if success.
	if (hostResolved == -1)
	{
		// failed to resolve host, doesn't really matter tho, we got ip.
		return Packets::SERVER_NOT_RESOLVED;
	}
	else // otherwise if we successfully resolved the hostname...
	{
		// get our IP address in dot-quad format (break up 32-bit unsigned host address)
		// split it into an array of four 8-bit unsigned numbers...
		Uint8 * dotQuad = (Uint8*)&serverIP.host;

		dotQuadString  = toString( (unsigned short)dotQuad[0] );
		dotQuadString += ".";
		dotQuadString += toString( (unsigned short)dotQuad[1] );
		dotQuadString += ".";
		dotQuadString += toString( (unsigned short)dotQuad[2] );
		dotQuadString += ".";
		dotQuadString += toString( (unsigned short)dotQuad[3] );

		//Output dotquad string (ip) and output 16bit port
		if (debug)
		{
			cout << "Successfully resolved server to IP: " << dotQuadString;
			cout << ", will use port " << SDLNet_Read16(&serverIP.port) << endl;
		}
	}

	// Open connection between client/server
	clientSocket = SDLNet_TCP_Open(&serverIP);
	if (!clientSocket)
	{
		//Fail, server is down
		return Packets::SERVER_DOWN;
	}
	else // If we successfully opened a connection then check for the server response to our connection
	{
		if (debug) { cout << "Connected, reading status from the server..." << endl; }

		// Add our socket to the socket set for polling
		SDLNet_TCP_AddSocket(socketSet, clientSocket);

		// Wait for up to five seconds for a response from the server.
		int activeSockets = SDLNet_CheckSockets(socketSet, ClientSocket::CONNECTION_TIMEOUT_PERIOD);

		if (debug)
		{
			cout << "There are " << activeSockets << " socket(s) with data on them at the moment." << endl;
		}

		// Check for response from server
		int gotServerResponse = SDLNet_SocketReady(clientSocket);

		if (gotServerResponse != 0)
		{
			// Read the message on the client socket
			int serverResponseByteCount = SDLNet_TCP_Recv(clientSocket, pBuffer, bufferSize);

			if (debug)
			{
				cout << "Message from server: " << pBuffer << "(" << serverResponseByteCount << " bytes)" << endl;
			}

			// Get the contents of the buffer as a string
			string bufferContents = pBuffer;

			// If we got the SERVER_NOT_FULL response from the server then we can join!
			if ( pBuffer == ClientSocket::SERVER_NOT_FULL )
			{
				if (debug) { cout << "Joining server now..." << endl; }
				return Packets::SERVER_NOT_FULL;
			}
			else // Otherwise we must have got the SERVER_FULL response so we can't connect.
			{
				//Tells us server is full
				return Packets::SERVER_FULL;
			}
		}
		else // If there's no activity on the client socket then we've failed to connect
		{
			return Packets::SERVER_NO_RESPONSE;
		}
	}
}

and last here is the gameloop that calls the state->HandleEvents(input, clientsocket);

void game::game_loop() {
	while (engine->engine_running()) {
		//handle gamestate
		
		state->HandleEvents(engine->getInput(), engine->getSocket());

		//if engine ticked
			//update
			//clear screen
			//draw state
			//flip display buffer
	}
}

Help is much appreciated.


#3

What is returing from the connect function. ?
Is it Packets::SERVER_NOT_FULL


#4

Yes it is SERVER_NOT_FULL returning.


#5

Unless I have a full working example its very hard to see whats happening
Here are some things to check.

  1. Is connected set to false by default, it may always be true ?
    if (connected == true) {

  2. Print out the return value from the int ClientSocket::connectToServer()
    int packetMsg = cs->connectToServer();
    Check if packetMsg == Packets::SERVER_NOT_FULL