Пример #1
0
void	ManageRoom::handleConnectGame(void *buffer, Client *cl)
{
	t_TCPConnection *res;
	ISocket *sock;
	std::string name;
	res = (t_TCPConnection *)buffer;
	cl->setName(res->name);
	std::cout << res->name << std::endl;
	sock = cl->getSocket();
	cl->setIp(sock->getIp());
	if (!cl->getName().empty())
	{
		t_TCPHeader Header;
		Header.type = ESTABLISHED;
		Header.packetSize = sizeof(Header);
		sock->sendBinary(&Header, sizeof(Header));
	}
	else
	{
		t_TCPError Header;
		Header.header.type = PACKET_ERROR;
		Header.header.packetSize = sizeof(Header);
		Header.error = LOGIN;
		sock->sendBinary(&Header, sizeof(Header));
	}
}
Пример #2
0
void	ManageRoom::check_list()
{
	for (std::list<Client *>::iterator it = this->clList.begin(); it != this->clList.end(); ++it)
	{
		ISocket *socket;
		socket = (*it)->getSocket();
		char *buf = new char[1025];

		if (socket != NULL)
		{
			int fd = socket->getSocket();
			if (FD_ISSET(fd, &(this->read)))
			{
				std::string buffer;
				int len;
				t_TCPHeader *header;
				if ((len = socket->recBinary(buf, 1024)) < 0)
				{
					std::map<std::string, Room *>::iterator it2;
					std::string ip = socket->getIp();
					if ((it2 = this->_access.find(ip)) != this->_access.end())
					{
						std::cout << "quittons la partie "<< (int)(*it)->getIdPlayer() << std::endl;
						Room * room = it2->second;
						room->deleteClient((*it)->getIdPlayer());
						this->_access.erase(it2);
					}
					this->clList.erase(it++);
					socket->closeSocket();
				}
				else
				{
					header = (t_TCPHeader *)buf;
					if (this->TCPActions.find(header->type) != this->TCPActions.end())
					{
	        			 ptr p;
						 t_TCPConnection *con;
						 con = (t_TCPConnection *)buf;
						 p = this->TCPActions[header->type];
						 if ((*it)->getName().empty() == 1 && header->type == CONNECTION)
							(this->*p)(buf, *it);
						 else if (!(*it)->getName().empty() == 1)
							 (this->*p)(buf, *it);
						 else
							this->sendErrorPacket(socket, NOT_LOGGED);
					}
				}
			}
		}
	}
}
Пример #3
0
void	ManageRoom::handleCreateGame(void *buffer, Client *cl)
{
	t_TCPCreate *create;
	create = (t_TCPCreate *)buffer;

	Room *room = new Room;

	room->setGameId(this->currentGameId);
	room->setNbMax(create->nb_max);
	room->setName(create->name_game);
	std::cout << "Creating room=" << room->getName() << std::endl;
	char id = room->addClient(cl);
	std::cout << (int)id  << std::endl;
	this->listRoom[this->currentGameId++] = room;
	int i = 0;
	int len;
	const char *str = cl->getName().c_str();
	ISocket *sock;
	t_TCPPlayer Player;

	//On prépare le packet pour prévenir le joueur de sa connection à la partie créée

	while (str[i])
		Player.name[i] = str[i++];
	Player.name[i] = '\0';
	Player.player_id = id;
	Player.status = NOT_READY;
	Player.header.packetSize = sizeof(Player);
	Player.header.type = PLAYER;

	sock = cl->getSocket();
	this->_access[sock->getIp()] = room;
	IThread *th = new WinThread;

	std::cout <<"LOOOOOOOOOOOL" << std::endl;
	th->create(initGame, room);
	std::cout << "LOOOL2"<< std::endl;
	if ((len = sock->sendBinary(&Player, sizeof(Player))) < 0)
		std::cout << "Error while sending Create" << std::endl;
}