Example #1
0
void
GameRoom::sendToEveryUser(Packet *p) {

    ISocket *sock;
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];

    mutex->lock(true);
    for (std::vector<User *>::iterator it = this->users.begin(); it != this->users.end(); it++) {
        if ((sock = RTypeServer::getInstance()->getUserSocket(*it)) != NULL)
            sock->writePacket(p, 0, false);
    }
    delete p;
    mutex->unlock();
}
Example #2
0
int             main(int ac, char **av)
{
	std::string s;
	std::string token;
	size_t pos;
	Instruction instruct(Instruction::KO);
	ISocket *servTcp = getClient();

	/*
	try
	{
	servTcp->attachOnReceive(&recvHandler);
	instruct.setInstruct(Instruction::CONNEXION);
	instruct.addName("fabY");
	servTcp->writePacket(Packet::pack(instruct));
	#ifdef _WIN_32
	Sleep(100);
	#else
	usleep(10000);
	#endif

	instruct.setInstruct(Instruction::CREATE_ROOM);
	servTcp->writePacket(Packet::pack(instruct));
	#ifdef _WIN_32
	Sleep(100);
	#else
	usleep(10000);
	#endif

	instruct.setInstruct(Instruction::START_GAME);
	servTcp->writePacket(Packet::pack(instruct));
	#ifdef _WIN_32
	Sleep(100);
	#else
	usleep(10000);
	#endif

	game();
	}
	catch (BBException &err)
	{
	std::cerr << err.what() << std::endl;
	system("pause");
	}
	*/

	while (s != "quit") {

		instruct.eraseNames();
		std::getline(std::cin, s);

		if ((pos = s.find(" ")) != s.npos)
		{
			token = s.substr(0, pos);
			if (token == "connect")
				instruct.setInstruct(Instruction::CONNEXION);
			else if (token == "join")
				instruct.setInstruct(Instruction::JOIN_ROOM);
			else if (token == "create")
				instruct.setInstruct(Instruction::CREATE_ROOM);
			s.erase(0, pos + 1);
			if (s.size() > 0) {
				instruct.addName(s);
				servTcp->writePacket(Packet::pack(instruct));
			}
		}
		else if (s == "roomusers") {

			instruct.setInstruct(Instruction::GETALLUSERSINROOM);
			servTcp->writePacket(Packet::pack(instruct));
		}
		else if (s == "users")
		{
			instruct.setInstruct(Instruction::GETALLUSERNAMES);
			servTcp->writePacket(Packet::pack(instruct));
		}
		else if (s == "rooms")
		{
			instruct.setInstruct(Instruction::GETALLROOMNAMES);
			servTcp->writePacket(Packet::pack(instruct));
		}
		else if (s == "disconnect")
		{
			instruct.setInstruct(Instruction::DECONNEXION);
			servTcp->writePacket(Packet::pack(instruct));
		}
		else if (s == "leave")
		{
			instruct.setInstruct(Instruction::LEAVE_ROOM);
			servTcp->writePacket(Packet::pack(instruct));
		}
		else if (s == "start")
		{
			instruct.setInstruct(Instruction::START_GAME);
			servTcp->writePacket(Packet::pack(instruct));
			game();
			break;
		}
		//servUdp->writePacket(Packet::pack<std::string>(s));
	}
	servTcp->cancel();
#ifdef _WIN_32
	Sleep(1000);
#else
	sleep(1);
#endif

	delete servTcp;
	return (0);
}