Packet::Packet(Identity& id) : _type(Packet::Id) { unsigned char *instr; unsigned char *port; char* _username; char* _ip; unsigned int _instruction; unsigned int _port; _instruction = id.getInstruct(); _port = id.getPort(); _username = id.getUsername(); _ip = id.getIp(); instr = reinterpret_cast<unsigned char *>(&_instruction); port = reinterpret_cast<unsigned char *>(&_port); for (unsigned int i = 0; i < 8; ++i) if (i < 4) _data.push_back(instr[i]); else _data.push_back(port[i - sizeof(int)]); for (unsigned int i = 0; i < 64; ++i) _data.push_back(_username[i]); for (unsigned int i = 0; i < 32; ++i) _data.push_back(_ip[i]); this->_encrypted = false; }
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; } }