Beispiel #1
0
void ServerProcedures::update()
{
	if (network->acceptNewClient(client_id))
	{
		printf("client %d has been connected to the server\n", client_id);
		sendInitPacket();

		if (client_id % 2 == 1)
		{
			this->sendStartGamePackets();
		}

		//TODO: only to test here
		if (client_id == 0)
		{
			srand(time(NULL));
			this->bitmapId1 = (std::rand() % 7) + 112;
			this->bitmapId2 = (std::rand() % 7) + 112;
			this->bitmapId3 = (std::rand() % 7) + 112;
			this->bitmapId4 = (std::rand() % 7) + 112;
		}

		client_id++;
	}

	receiveFromClients();
}
// Update loop - accepts new connections
// Collects and stores data 
void ServerNetworkManager::update() {
	// get new clients
	do {
		unsigned int temp_c_id = client_id;
		if(acceptNewClient(temp_c_id)) {
			if(debugFlag) DC::get()->print("client %d has been connected to the server\n",client_id);
			PlayerSObj * o;
			if(temp_c_id == client_id) {
				// Create a Test Server Object for them (for now)
				SOM *som = SOM::get();
				// Ok, since we should only have one object on both sides, the id's will match
				// but how do we get them matching later? maybe the server should send
				// the client the id back or something?
				o = new PlayerSObj(som->genId());
				som->add(o);
				sessionsobjid.insert( pair<unsigned int, unsigned int>(temp_c_id, o->getId()) );
			} else {
				o = reinterpret_cast<PlayerSObj *>(SOM::get()->find(sessionsobjid.find(temp_c_id)->second));
			}

			this->getSendBuffer();	// Need to call this before each send, regardless of whether or not you have a message.
			this->sendToClient(sessions[temp_c_id], INIT_CONNECTION, o->getId(), 0);			

			if(debugFlag) DC::get()->print("client %d has been assigned client_id... Moving onto the rest of the loop.\n",client_id);
			if(temp_c_id == client_id) {
				client_id++;
			}
			EventManager::get()->fireEvent(EVENT_CONNECTION, o);
		}
	} while (sessions.empty());
	// Collect data from clients
    receiveFromClients();
	iteration++;
}
Packet ServerPaint::update(Packet _outgoing_packet)
{

	// get new clients
	if (network->acceptNewClient(client_id))
	{
		printf("client %d has been connected to the server\n", client_id);

		client_id++;
	}
	return receiveFromClients(_outgoing_packet);
}
Beispiel #4
0
void ServerGame::update()
{
    // get new clients
   if(network->acceptNewClient(client_id))
   {
        printf(">>>>> client %d has connected to the server <<<<<\n", client_id);

        client_id++;
   }

   receiveFromClients();
}
Beispiel #5
0
void ServerGame::update()
{
	// get new clients only if the game hasn't started
	if (!game_started)
	{
		if (network->acceptNewClient(client_id))
		{
			printf("client %d has been connected to the server\n", client_id);

			// This will be an INIT_CONNECTION packet
			receiveFromClients();
		}
	}	

	auto curr_time = chrono::high_resolution_clock::now();

	chrono::duration<double, milli> fp_stamp = curr_time - start_time;

	int diff = fp_stamp.count();

	if (game_over == false && eggs_spawned && diff >= 300000)
	{
		game_over = true;
		if (scores[0] == scores[1])
		{
			sendGameOverPacket(-1);
		}
		else if(scores[0] > scores[1])
		{
			sendGameOverPacket(0);
		}
		else
		{
			sendGameOverPacket(1);
		}
	}
	else
	{
		if (game_over == false && eggs_spawned && ((diff % 10000) <= 16))
		{
			sendTimeStampPacket(diff);
		}
	}

	receiveFromClients();

	// Check that all clients are ready

	if (game_started && ready_clients >= client_id)
	{
		if (game_over)
			return;

		if (spawned_clients == ready_clients && !eggs_spawned) {
			total_eggs = 2*ready_clients + 1;
			for (int i = 0; i < total_eggs; i++)
			{
				engine->SpawnRandomFlag();
			}
			eggs_spawned = true;
			Sleep(2000); // should wait for clients to respond
			start_time = chrono::high_resolution_clock::now();
		}
		if(!engine->hasInitialSpawned())
			engine->SendPreSpawn(ready_clients);

		// once eggs has spawned, everything has spawned and we can begin the world cycle
		auto t1 = chrono::high_resolution_clock::now();


		if(eggs_spawned)
			engine->GetWorld()->UpdateWorld();

		auto t2 = chrono::high_resolution_clock::now();

		float thresh = 16.67; // 60
        //float thresh = 33;   // 30 frames
		chrono::duration<double, milli> fp_ms = t2 - t1;
		//("DIFFERENCE: %f\n", fp_ms.count());

		if(thresh - fp_ms.count() < 0)
			printf("TIMING ERROR: %f\n", thresh - fp_ms.count());
		else
		{
			//printf("SLEEPING: %f\n", (thresh - fp_ms.count()));

			Sleep((thresh - fp_ms.count()));

			auto t3 = chrono::high_resolution_clock::now();

			chrono::duration<double, milli> fp_after = t3 - t1;

			//("TOTAL AFTER SLEEP: %f\n", fp_after.count());


		}
	}

}