Exemplo n.º 1
0
void
ISocket::write(const std::vector<unsigned char> &data, unsigned int id)
{
	IMutex *mutex;

	if (this->_type == ISocket::Client)
	{
		mutex = (*MutexVault::getMutexVault())["write" + MutexVault::toString(this->_id)];
		mutex->lock(true);
		for (unsigned int i = 0; i < data.size(); i++)
			this->_write_buffer.push_back(data[i]);
		mutex->unlock();
	}
	else
	{
		mutex = (*MutexVault::getMutexVault())["serverTargets"];
		mutex->lock(true);
		if (id != 0 && id < this->_targets.size() && this->_targets[id - 1]->getStatus() == ISocket::Running)
			this->_targets[id - 1]->write(data);
		else if (id == 0)
			for (unsigned int i = 0; i < this->_targets.size(); i++)
				this->_targets[i]->write(data);
		mutex->unlock();
	}
}
Exemplo n.º 2
0
bool
BabelClient::answer(ISocket *client)
{
    BabelClient *_this = BabelClient::getInstance();
    IMutex *mutex = (*MutexVault::getMutexVault())["peer"];

    mutex->lock(true);
    if (_this->_peer != NULL && _this->_peer->getStatus() == ISocket::Ready)
    {
        //attach handlers
        if (_this->_peer->start() == -1) {
            std::cout << "Connection failed" << std::endl;
            client->writePacket(new Packet(ENDCALL));
            _this->_peer->cancel();
			#ifdef _WIN_32
				Sleep(100);
			#else
				usleep(100);
			#endif
            delete _this->_peer;
            _this->_peer = NULL;
        }
        else
        {
            //send ok!
            client->writePacket(new Packet(CALLESTABLISHED));
            mutex->unlock();
            return (true);
        }
    }
    mutex->unlock();
    return (false);
}
Exemplo n.º 3
0
bool
RTypeServer::leaveRoom(User *user) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    GameRoom *room;
    bool mustSend = false;

    bool res = false;
    mutex->lock(true);
    if ((room = user->getRoom()) != NULL) {

        if (room->owner != user)
            mustSend = true;
        user->getRoom()->removeUser(user);
        if (mustSend)
        {
            std::vector<User *> tmp = room->getUsers();
            Instruction i = room->getUsersInstruction();
            for (std::vector<User *>::iterator it = tmp.begin(); it != tmp.end(); it++)
                RTypeServer::getInstance()->getInstance()->sendToClient(*it, i);
        }
        res = true;
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 4
0
bool
GameRoom::startGame(User *user) {

    RTypeServer *server = RTypeServer::getInstance();
    ISocket *sock;
    bool res = false;
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];

    std::cout << "starting" << std::endl;
    mutex->lock(true);
    if (this->getState() != GameRoom::Running && user == this->owner)
    {
        res = true;
		#ifdef _WIN_32
			this->th = new WinThread<void, GameRoom *>(GameRoom::gameLoop);
		#else
			this->th = new LinuxThread<void, GameRoom *>(GameRoom::gameLoop);
		#endif
        this->state = GameRoom::Running;
        (*this->th)(this);
        for (std::vector<User *>::iterator it = this->users.begin(); it != this->users.end(); it++)
        {
            if ((sock = server->getUserSocket(*it)) != NULL)
                sock->attachOnReceive(RTypeServer::tcpHold);
        }
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 5
0
	bool lock(bool wait)
	{
		IMutex *mutex;

		if ((mutex = (*this->_mutex_vault)[this->_id]) == NULL)
			return (false);
		this->setStatus(Paused);
		return (mutex->lock(wait));
	}
Exemplo n.º 6
0
void
BabelClient::addContact(Identity *id)
{
    IMutex *mutex = (*MutexVault::getMutexVault())["contacts"];

    mutex->lock(true);
    std::cout << "new contact : " << id->getUsername() << std::endl;
    this->_contacts.push_back(id);
    mutex->unlock();
}
Exemplo n.º 7
0
GameRoom::State
GameRoom::getState() const
{
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];
    State state;

    mutex->lock(true);
    state = this->state;
    mutex->unlock();
    return (state);
}
Exemplo n.º 8
0
bool
RTypeServer::startGame(User *user) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;

    mutex->lock(true);
    if (user->getRoom() != NULL)
        res = user->getRoom()->setState(GameRoom::Running, user);
    mutex->unlock();
    return (res);
}
Exemplo n.º 9
0
bool
GameRoom::hasUser(User *user) const
{
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];
    bool res = false;

    mutex->lock(true);
    if (std::find(this->users.begin(), this->users.end(), user) != this->users.end())
        res = true;
    mutex->unlock();
    return (res);
}
Exemplo n.º 10
0
void RTypeServer::tcpHold(ISocket *client) {

    IMutex *mutex = (*MutexVault::getMutexVault())["instantiation"];
    mutex->lock(true);
    #ifdef _WIN_32
        Sleep(1000);
    #else
        sleep(1);
    #endif
    client->attachOnReceive(RTypeServer::tcpGamePlay);
    mutex->unlock();
}
Exemplo n.º 11
0
Instruction
GameRoom::getUsersInstruction() const {

    Instruction instruct(Instruction::GETALLUSERSINROOM);
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];

    mutex->lock(true);
    for (std::vector<User *>::const_iterator it = this->users.begin(); it != this->users.end(); it++)
        instruct.addName((*it)->getName());
    mutex->unlock();
    return (instruct);
}
Exemplo n.º 12
0
std::vector<Identity>
BabelClient::getContacts()
{
    IMutex *mutex = (*MutexVault::getMutexVault())["contacts"];
    std::vector<Identity> contacts;

    mutex->lock(true);
    for (unsigned int i = 0; i < this->_contacts.size(); i++)
        if (this->_contacts[i] != NULL)
            contacts.push_back(*(this->_contacts[i]));
    mutex->unlock();
    return (contacts);
}
Exemplo n.º 13
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();
}
Exemplo n.º 14
0
bool
RTypeServer::roomNameExists(const std::string &roomName) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;
    mutex->lock(true);
    for (std::vector<GameRoom *>::iterator it = this->rooms.begin(); it != this->rooms.end(); it++)
        if ((*it)->getName() == roomName) {
            res = true;
            break;
        }
    mutex->unlock();
    return (res);
}
Exemplo n.º 15
0
void
BabelClient::waitingForAnswer(ISocket *client)
{
    Packet *packet;
    Instruct *instruct;
    Identity *id;
    BabelClient *_this = BabelClient::getInstance();

    std::cout << "Waiting for answer" << std::endl;
    if ((packet = client->readPacket()) != NULL) {

        if (packet->getType() == Packet::Inst &&
            (instruct = packet->unpack<Instruct>()) != NULL) {

            if (*instruct == KO) {
                std::cout << "No such user or whatever" << std::endl;
                client->attachOnReceive(BabelClient::onReceiveLogged);
            }
            delete instruct;
        }
        else if (packet->getType() == Packet::Id)
        {
            if ((id = packet->unpack<Identity>()) != NULL) {

                if (id->getInstruct() == OK) {
                    std::cout << "Creating server" << std::endl;

                    IMutex *mutex = (*MutexVault::getMutexVault())["peer"];
                    mutex->lock(true);
                    _this->_peer = ISocket::getServer(5555);
                    _this->_peer->attachOnReceive(BabelClient::receiveSound);
                    _this->getSound();
                    _this->_peer->start();

					#ifdef _WIN_32
						_this->_peerthread = new WinThread<void, ISocket *>(BabelClient::sendSound);
					#else
						_this->_peerthread = new LinuxThread<void, ISocket *>(BabelClient::sendSound);
					#endif
                    (*_this->_peerthread)(_this->_peer);

                    client->attachOnReceive(BabelClient::onReceiveLogged);
                    mutex->unlock();
                }
                delete id;
            }
        }
        delete packet;
    }
}
Exemplo n.º 16
0
bool
GameRoom::addUser(User *user)
{
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];
    bool res = false;

    mutex->lock(true);
    if (!this->hasUser(user) && this->users.size() < 4 && !this->hasUser(user)) {
        this->users.push_back(user);
        user->attachRoom(this);
        res = true;
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 17
0
bool
GameRoom::removeAllUsers() {

    RTypeServer *server = RTypeServer::getInstance();
    IMutex *mutex = (*MutexVault::getMutexVault())["room" + this->name];
    Instruction instruct(Instruction::LEAVE_ROOM);

    mutex->lock(true);
    while (this->users.size() > 0) {
        server->sendToClient(this->users[0], instruct);
        this->users[0]->detachRoom();
    }
    mutex->unlock();
    return (true);
}
Exemplo n.º 18
0
bool
RTypeServer::userNameExists(const std::string &username) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;

    mutex->lock(true);
    for (std::map<ISocket *, User *>::iterator it = this->userLinks.begin(); it != this->userLinks.end(); it++)
        if (it->second != NULL && it->second->getName() == username) {
            res = true;
            break;
        }
    mutex->unlock();
    return (res);
}
Exemplo n.º 19
0
ISocket *RTypeServer::getUserSocket(User *user) {

    ISocket *client = NULL;
    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    mutex->lock(true);

    for (std::map<ISocket *, User *>::iterator it = this->userLinks.begin(); it != this->userLinks.end(); it++)
        if (it->second == user)
        {
            client = it->first;
            break;
        }
    mutex->unlock();
    return (client);
}
Exemplo n.º 20
0
bool
GameRoom::setState(State state, User *user)
{
    if (this->owner != user)
        return (false);

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

    mutex->lock(true);
    if ((this->state = state) == Running)
        for (std::vector<User *>::iterator it = this->users.begin(); it != this->users.end(); it++)
            (*it)->setState(User::Playing);
    mutex->unlock();
    return (true);
}
Exemplo n.º 21
0
bool
RTypeServer::newUser(const std::string &userName, ISocket *client) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;

    mutex->lock(true);
    if (!userName.empty() && !this->userNameExists(userName))
    {
        std::cout << "New user : " << userName << std::endl;
        this->userLinks[client] = new User(userName);
        res = true;
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 22
0
bool RTypeServer::sendToClient(User *user, Instruction &instruct) {

    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    mutex->lock(true);
    bool res = false;

    for (std::map<ISocket *, User *>::iterator it = this->userLinks.begin(); it != this->userLinks.end(); it++)
        if (it->second == user)
        {
            it->first->writePacket(Packet::pack(instruct));
            res = true;
            break;
        }
    mutex->unlock();
    return (res);
}
Exemplo n.º 23
0
void
BabelClient::removeContact(Identity *id)
{
    IMutex *mutex = (*MutexVault::getMutexVault())["contacts"];

    mutex->lock(true);
    std::cout << "remove contact : " << id->getUsername() << std::endl;
    for (unsigned int i = 0; i < this->_contacts.size(); i++)
        if (this->_contacts[i] == id)
        {
            delete this->_contacts[i];
            this->_contacts[i] = NULL;
            break;
        }
    mutex->unlock();
}
Exemplo n.º 24
0
void
BabelClient::executeIdentity(Identity *id, ISocket *client)
{
    IMutex *mutex;
    BabelClient *_this = BabelClient::getInstance();

    if (id == NULL)
        return;
    switch (id->getInstruct())
    {
        case (ADDCONTACT) :
            _this->addContact(id);
            return; //no delete
        case (DELCONTACT) :
            _this->removeContact(id);
            break;
        case (ASKCALL) :
            mutex = (*MutexVault::getMutexVault())["peer"];
            mutex->lock(true);
            std::cout << "asked to call " << id->getIp() << std::endl;
            if (id->hasAdressAndName() && _this->_peer == NULL) {

                _this->_peer = ISocket::getClient(id->getIp(), id->getPort());
                _this->_peer->attachOnReceive(BabelClient::receiveSound);
                _this->_peer->attachOnDisconnect(BabelClient::endPeer);
                if (_this->_peer->start() == -1) {
                    std::cout << "Failed to connect!" << std::endl;
                    delete _this->_peer;
                    _this->_peer = NULL;
                    client->writePacket(new Packet(ENDCALL));
                }
                else {
                    _this->getSound();

					#ifdef _WIN_32
						_this->_peerthread = new WinThread<void, ISocket *>(BabelClient::sendSound);
					#else
						_this->_peerthread = new LinuxThread<void, ISocket *>(BabelClient::sendSound);
					#endif
                    (*_this->_peerthread)(_this->_peer);
                }
            }
            mutex->unlock();
    }
    delete id;
    return;
}
Exemplo n.º 25
0
bool RTypeServer::removeRoom(GameRoom *room) {

    std::vector<GameRoom *>::iterator it;
    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;

    mutex->lock(true);
    if ((it = std::find(this->rooms.begin(), this->rooms.end(), room)) != this->rooms.end())
    {
        std::cout << "Removing room " << (*it)->getName() << std::endl;
        delete *it;
        this->rooms.erase(it);
        res = true;
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 26
0
std::vector<ISocket *>
ISocket::getActiveClients()
{
    std::vector<ISocket *> tmp;
    IMutex *mutex;

    if (this->getType() == ISocket::Server)
    {
        mutex = (*MutexVault::getMutexVault())["serverTargets"];
        mutex->lock(true);
        for (unsigned int i = 0; i < this->_targets.size(); i++)
            if (this->_targets[i]->getStatus() == ISocket::Running)
                tmp.push_back(this->_targets[i]);
        mutex->unlock();
    }
    return tmp;
}
Exemplo n.º 27
0
    //! Decrements the reference count of an object and deletes it if both automaticDelete() is \p true the count reaches 0.
    void decReference()
    {
      // Save local copy in case of deletion.
      IMutex* mutex = mRefCountMutex;

      // Lock mutex.
      if (mutex)
        mutex->lock();

      VL_CHECK(mReferenceCount)
      --mReferenceCount;
      if (mReferenceCount == 0 && automaticDelete())
        delete this;

      // Unlock mutex.
      if (mutex)
        mutex->unlock();
    }
Exemplo n.º 28
0
bool RTypeServer::removeUser(ISocket *client) {

    std::map<ISocket *, User *>::iterator it;
    IMutex *mutex = (*MutexVault::getMutexVault())["serverType"];
    bool res = false;

    mutex->lock(true);
    if ((it = this->userLinks.find(client)) != this->userLinks.end())
    {
        std::cout << "Removing user : " << this->userLinks[client]->getName() << std::endl;
        RTypeServer::getInstance()->leaveRoom(RTypeServer::getInstance()->userLinks[client]);
        delete it->second;
        client->attachOnReceive(RTypeServer::tcpGuestRoom);
        this->userLinks.erase(it);
        res = true;
    }
    mutex->unlock();
    return (res);
}
Exemplo n.º 29
0
void
BabelClient::onDisconnect(ISocket *client)
{
    BabelClient *_this = BabelClient::getInstance();

    IMutex *mutex = (*MutexVault::getMutexVault())["peer"];
    mutex->lock(true);
    if (_this->_peer != NULL) {
        _this->_peer->cancel();
        BabelClient::getSound()->stopStream();
		#ifdef _WIN_32
			Sleep(100);
		#else
			usleep(100);
		#endif
        delete _this->_peer;
        _this->_peer = NULL;
    }
    mutex->unlock();
    std::cout << "Server on " << client->getIp() << " for client no : " << client->getId() << " disconnected!" << std::endl;
}
Exemplo n.º 30
0
bool
BabelClient::call(const std::string &peername, ISocket *server)
{
    bool ret = false;
    Identity *id;
    IMutex *mutex = (*MutexVault::getMutexVault())["peer"];

    mutex->lock(true);
    if (this->_peer == NULL)
    {
        id = new Identity(peername, ASKCALL);
        server->writePacket(Packet::pack<Identity>(*id));
        server->attachOnReceive(BabelClient::waitingForAnswer);
        delete id;
        ret = true;
    }
    else
        std::cout << "You cannot call 2 persons at the same time!" << std::endl;
    mutex->unlock();
    return (ret);
}