void Server::execute(){ /* Enable the server to begin accepting clients */ setListening(true); do{ handleConnections(); }while( current_connected_players != max_connected_players ); /* Randomly assign player ids */ assignIds(); /* Signal all players that the game is ready to Begin */ broadcastPacket(PacketFactory::GAMESTART); /* Send out the packets for the Dictionary Data */ broadcastPacket(PacketFactory::TROOPDEF); bool keep_playing = true; // send init packet for first turn sendPacket(PacketFactory::TURNSTART, 0); while( keep_playing ){ keep_playing = handlePacket(); // std::cout << "keep_playing" << keep_playing << std::endl; } }
void gameMap::broadcastCombatEntityChanges(combatEntity* entity) { RakNet::BitStream data; data.Write<RakNet::MessageID>(gamePackets::COMBAT_ENTITY); data.Write<RakNet::MessageID>(gamePackets::COMBAT_ENTITY_UPDATE); data.Write<int>(entity->getUID()); entity->writeCombatData(data); broadcastPacket(data); }
bool PacketHandler::handleAttentionPing(ENetPeer *peer, ENetPacket *packet) { AttentionPing *ping = reinterpret_cast<AttentionPing*>(packet->data); AttentionPingAns response(ping); Log::getMainInstance()->writeLine("Plong x: %f, y: %f, z: %f, type: %i\n", ping->x, ping->y, ping->z, ping->type); return broadcastPacket(reinterpret_cast<uint8*>(&response), sizeof(AttentionPing), 3); }
/** Sends a message to the server if this is a client, or to all * clients if this is the server. * \param data Message to sent. * \param reliable If the message is to be sent reliable. */ void STKHost::sendMessage(const NetworkString& data, bool reliable) { if (NetworkConfig::get()->isServer()) broadcastPacket(data, reliable); else { if (m_peers.size() > 1) Log::warn("ClientNetworkManager", "Ambiguous send of data."); m_peers[0]->sendPacket(data, reliable); } } // sendMessage
static void offlineNotify(EVENT *ev, PRES *pres) { PACKET p; p.cmd = CMD_SRV_USER_OFFLINE; p.from = ev->s->getUserName(); p.to = NULL; p.data = NULL; p.dataLen = 0; p.online = true; broadcastPacket(ev, pres, &p); }
bool PacketHandler::handleLoadPing(ENetPeer *peer, ENetPacket *packet) { PingLoadInfo *loadInfo = reinterpret_cast<PingLoadInfo*>(packet->data); PingLoadInfo response; memcpy(&response, packet->data, sizeof(PingLoadInfo)); response.header.cmd = PKT_S2C_Ping_Load_Info; response.userId = peerInfo(peer)->userId; Log::getMainInstance()->writeLine("Loading: loaded: %f, ping: %f, %i, %f\n", loadInfo->loaded, loadInfo->ping, loadInfo->unk4, loadInfo->f3); return broadcastPacket(reinterpret_cast<uint8*>(&response), sizeof(PingLoadInfo), 4, UNRELIABLE); }
static void statusNotify(EVENT *ev, PRES *pres) { OutPacket out; out << pres->status; PACKET p; p.cmd = CMD_SRV_USER_STATUS; p.data = out.data; p.dataLen = out.getLength(); p.from = ev->s->getUserName(); p.to = NULL; broadcastPacket(ev, pres, &p); }
static void onlineNotify(EVENT *ev, PRES *pres) { OutPacket out; createOnlinePacket(out, ev->s, pres); PACKET p; p.cmd = CMD_SRV_USER_ONLINE; p.data = out.data; p.dataLen = out.getLength(); p.from = ev->s->getUserName(); p.to = NULL; p.online = true; broadcastPacket(ev, pres, &p); }
void PacketHandler::broadcastServerMessage(std::string msg) { //This is kind of an very ugly hack! Could cause memory leaks :/ SystemChatMessage packet; packet.cmd = PKT_ChatBoxMessage; packet.lenght = msg.length(); strcpy(packet.msg,msg.c_str()); packet.netId = 0; packet.playerNo = 0; packet.type = CHAT_ALL; broadcastPacket(reinterpret_cast<uint8*>(&packet), sizeof(packet), CHL_COMMUNICATION); }
/** * TO-DO - deal with the incoming player info stuff */ bool Server::handlePacket(sf::Packet& pckt, RemotePlayer& incomingPlayer){ sf::Int32 type; int player_id; /* Peel off the first int, which will be the packet type */ pckt >> type; pckt >> player_id; std::cout << "Packet received from: " << player_id << " On turn: " << turn_count << std::endl; RemotePlayer * playa; switch(type) { case TURNEND : turn_count++; active_player = (active_player + 1) % max_connected_players; if(turn_count <= max_turns) { for(const auto& player : players) { if(player->id == active_player) playa = player; } std::cout << "Sending troop update packet to: " << playa->id << std::endl; sendPacket(PacketFactory::TROOPUPDATE, playa->id); std::cout << "Send TurnStart to: " << playa->id << std::endl; sendPacket(PacketFactory::TURNSTART, playa->id); return true; } else { broadcastPacket(PacketFactory::GAMEOVER); return false; } break; // incoming Troop update case INTROOPUPDATE: troopDAO->updateTroops(pckt); break; case INCOMINGCITYUPDATE: cityDAO->updateCities(pckt); break; } return true; }
void NetMaster::sendToHandlers(NetPacket * packet) { qDebug().nospace() << "[" << packet->receivedFrom()->peerAddress().toString() << "]" << " Processing packet: " << packet->name(); if (!handlerExists(packet->name())) return; QList<AbstractNetHandler*> * handlers = m_netHandlers.value(packet->name()); bool forward = false; for (int i = 0; i < handlers->count(); i++) if (handlers->at(i)->handle(*packet)) forward = true; if (forward) broadcastPacket(packet); }