From 0a03535ecb7835035c945732d4d2b17bdb70f136 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 2 Jun 2014 22:35:55 +1000 Subject: [PATCH] Replaced several lookups to the same object with a reference --- common/network/serial_packet.hpp | 1 + server/server_connections.cpp | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index 540494b..d378fde 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -32,6 +32,7 @@ #define NETWORK_VERSION 20140601 #define PACKET_STRING_SIZE 100 +//TODO: would it be possible to serialize structures directly? union SerialPacket { //types of packets enum class Type { diff --git a/server/server_connections.cpp b/server/server_connections.cpp index 6b7f296..047462f 100644 --- a/server/server_connections.cpp +++ b/server/server_connections.cpp @@ -70,13 +70,21 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { //bounce this packet network.SendTo(&newClient.address, &packet); + //reference to prevent multiple lookups + //TODO: I need a way to pack structures unto packets more easily + //NOTE: this chunk of code is similar to HandleSynchronize + CharacterData& character = characterMap[characterIndex]; + //send the new character to all clients packet.meta.type = SerialPacket::Type::CHARACTER_NEW; packet.characterInfo.characterIndex = characterIndex; - strncpy(packet.characterInfo.handle, characterMap[characterIndex].handle.c_str(), PACKET_STRING_SIZE); - strncpy(packet.characterInfo.avatar, characterMap[characterIndex].avatar.c_str(), PACKET_STRING_SIZE); - packet.characterInfo.origin = characterMap[characterIndex].origin; - packet.characterInfo.motion = characterMap[characterIndex].motion; + strncpy(packet.characterInfo.handle, character.handle.c_str(), PACKET_STRING_SIZE); + strncpy(packet.characterInfo.avatar, character.avatar.c_str(), PACKET_STRING_SIZE); + packet.characterInfo.mapIndex = character.mapIndex; + packet.characterInfo.origin = character.origin; + packet.characterInfo.motion = character.motion; + packet.characterInfo.stats = character.stats; + PumpPacket(packet); //TODO: don't send anything to a certain client until they send the OK (the sync packet? or ignore client side?)