From 4c882682ed49eb70cf8f5bcb94a2bd82a84b17a6 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 9 Sep 2014 08:05:50 +1000 Subject: [PATCH] Added TextPacket --- client/client_application.cpp | 3 ++ common/network/packet_types/text_packet.cpp | 40 +++++++++++++++++++++ common/network/packet_types/text_packet.hpp | 35 ++++++++++++++++++ common/network/serial_packet.hpp | 4 ++- common/network/serial_packet_type.hpp | 16 +++++++-- common/network/serial_utility.cpp | 17 ++++++--- server/server_logic.cpp | 3 ++ 7 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 common/network/packet_types/text_packet.cpp create mode 100644 common/network/packet_types/text_packet.hpp diff --git a/client/client_application.cpp b/client/client_application.cpp index 3c5ae4c..62e4d25 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -89,6 +89,7 @@ void ClientApplication::Init(int argc, char** argv) { std::cout << "Internal sizes:" << std::endl; + DEBUG_OUTPUT_VAR(NETWORK_VERSION); DEBUG_OUTPUT_VAR(sizeof(Region::type_t)); DEBUG_OUTPUT_VAR(sizeof(Region)); DEBUG_OUTPUT_VAR(REGION_WIDTH); @@ -96,8 +97,10 @@ void ClientApplication::Init(int argc, char** argv) { DEBUG_OUTPUT_VAR(REGION_DEPTH); DEBUG_OUTPUT_VAR(REGION_TILE_FOOTPRINT); DEBUG_OUTPUT_VAR(REGION_SOLID_FOOTPRINT); + DEBUG_OUTPUT_VAR(PACKET_STRING_SIZE); DEBUG_OUTPUT_VAR(PACKET_BUFFER_SIZE); DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE); + DEBUG_OUTPUT_VAR(static_cast(SerialPacketType::LAST)); #undef DEBUG_OUTPUT_VAR diff --git a/common/network/packet_types/text_packet.cpp b/common/network/packet_types/text_packet.cpp new file mode 100644 index 0000000..fbe0d9f --- /dev/null +++ b/common/network/packet_types/text_packet.cpp @@ -0,0 +1,40 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "text_packet.hpp" + +#include "serial_utility.hpp" + +void serializeText(void* buffer, TextPacket* packet) { + serialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //content + serialCopy(&buffer, packet->name, PACKET_STRING_SIZE); + serialCopy(&buffer, packet->text, PACKET_STRING_SIZE); +} + +void deserializeText(void* buffer, TextPacket* packet) { + deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType)); + + //content + deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE); + deserialCopy(&buffer, packet->text, PACKET_STRING_SIZE); +} \ No newline at end of file diff --git a/common/network/packet_types/text_packet.hpp b/common/network/packet_types/text_packet.hpp new file mode 100644 index 0000000..8dfbe22 --- /dev/null +++ b/common/network/packet_types/text_packet.hpp @@ -0,0 +1,35 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef TEXTPACKET_HPP_ +#define TEXTPACKET_HPP_ + +#include "serial_packet_base.hpp" + +struct TextPacket : SerialPacketBase { + char name[PACKET_STRING_SIZE]; + char text[PACKET_STRING_SIZE]; +}; + +void serializeText(void* buffer, TextPacket* packet); +void deserializeText(void* buffer, TextPacket* packet); + +#endif diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index c767f6a..7dd6c81 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -27,18 +27,20 @@ #include "client_packet.hpp" #include "region_packet.hpp" #include "server_packet.hpp" +#include "text_packet.hpp" //SerialPacketBase is defined in serial_packet_base.hpp typedef SerialPacketBase SerialPacket; //DOCS: NETWORK_VERSION is used to discern compatible servers and clients -constexpr int NETWORK_VERSION = 20140831; +constexpr int NETWORK_VERSION = 20140909; union MaxPacket { CharacterPacket a; ClientPacket b; RegionPacket c; ServerPacket d; + TextPacket e; }; constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket); diff --git a/common/network/serial_packet_type.hpp b/common/network/serial_packet_type.hpp index cf9a4dd..1c517a8 100644 --- a/common/network/serial_packet_type.hpp +++ b/common/network/serial_packet_type.hpp @@ -52,7 +52,6 @@ enum class SerialPacketType { //Connecting to a server as a client JOIN_REQUEST, JOIN_RESPONSE, - JOIN_REJECTION, //client requests all information from the server SYNCHRONIZE, @@ -88,10 +87,23 @@ enum class SerialPacketType { CHARACTER_STATS_REQUEST, CHARACTER_STATS_RESPONSE, - //reject a character request + //------------------------- + //TextPacket + // name, text + //------------------------- + + //general speech + TEXT_BROADCAST, + + //rejection/error messages + SHUTDOWN_REJECTION, + JOIN_REJECTION, CHARACTER_REJECTION, + //------------------------- //not used + //------------------------- + LAST }; diff --git a/common/network/serial_utility.cpp b/common/network/serial_utility.cpp index 6f70999..1f943a6 100644 --- a/common/network/serial_utility.cpp +++ b/common/network/serial_utility.cpp @@ -26,6 +26,7 @@ #include "client_packet.hpp" #include "region_packet.hpp" #include "server_packet.hpp" +#include "text_packet.hpp" #include @@ -53,7 +54,6 @@ void serializePacket(void* buffer, SerialPacketBase* packet) { break; case SerialPacketType::JOIN_REQUEST: case SerialPacketType::JOIN_RESPONSE: - case SerialPacketType::JOIN_REJECTION: case SerialPacketType::SYNCHRONIZE: case SerialPacketType::DISCONNECT: case SerialPacketType::SHUTDOWN: @@ -68,9 +68,14 @@ void serializePacket(void* buffer, SerialPacketBase* packet) { case SerialPacketType::CHARACTER_UPDATE: case SerialPacketType::CHARACTER_STATS_REQUEST: case SerialPacketType::CHARACTER_STATS_RESPONSE: - case SerialPacketType::CHARACTER_REJECTION: serializeCharacter(buffer, static_cast(packet)); break; + case SerialPacketType::TEXT_BROADCAST: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::SHUTDOWN_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + serializeText(buffer, static_cast(packet)); + break; } } @@ -88,7 +93,6 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) { break; case SerialPacketType::JOIN_REQUEST: case SerialPacketType::JOIN_RESPONSE: - case SerialPacketType::JOIN_REJECTION: case SerialPacketType::SYNCHRONIZE: case SerialPacketType::DISCONNECT: case SerialPacketType::SHUTDOWN: @@ -103,8 +107,13 @@ void deserializePacket(void* buffer, SerialPacketBase* packet) { case SerialPacketType::CHARACTER_UPDATE: case SerialPacketType::CHARACTER_STATS_REQUEST: case SerialPacketType::CHARACTER_STATS_RESPONSE: - case SerialPacketType::CHARACTER_REJECTION: deserializeCharacter(buffer, static_cast(packet)); break; + case SerialPacketType::TEXT_BROADCAST: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::SHUTDOWN_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + serializeText(buffer, static_cast(packet)); + break; } } \ No newline at end of file diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 281c5b2..bb86ed2 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -110,6 +110,7 @@ void ServerApplication::Init(int argc, char** argv) { std::cout << "Internal sizes:" << std::endl; + DEBUG_OUTPUT_VAR(NETWORK_VERSION); DEBUG_OUTPUT_VAR(sizeof(Region::type_t)); DEBUG_OUTPUT_VAR(sizeof(Region)); DEBUG_OUTPUT_VAR(REGION_WIDTH); @@ -117,8 +118,10 @@ void ServerApplication::Init(int argc, char** argv) { DEBUG_OUTPUT_VAR(REGION_DEPTH); DEBUG_OUTPUT_VAR(REGION_TILE_FOOTPRINT); DEBUG_OUTPUT_VAR(REGION_SOLID_FOOTPRINT); + DEBUG_OUTPUT_VAR(PACKET_STRING_SIZE); DEBUG_OUTPUT_VAR(PACKET_BUFFER_SIZE); DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE); + DEBUG_OUTPUT_VAR(static_cast(SerialPacketType::LAST)); #undef DEBUG_OUTPUT_VAR