From cc167180f692beeed8684938a05ed69b38ad9880 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 1 Dec 2014 22:33:15 +1100 Subject: [PATCH] Added support for cleaning up after rejection messages --- client/client_utilities/makefile | 37 ++++++++++++++++++++++ client/client_utilities/terminal_error.cpp | 23 ++++++++++++++ client/client_utilities/terminal_error.hpp | 34 ++++++++++++++++++++ client/makefile | 3 +- client/scenes/in_world.cpp | 13 ++++++++ client/scenes/makefile | 2 +- rsc/scripts/setup_server.lua | 3 +- server/server_methods.cpp | 29 ++++++++++++++--- 8 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 client/client_utilities/makefile create mode 100644 client/client_utilities/terminal_error.cpp create mode 100644 client/client_utilities/terminal_error.hpp diff --git a/client/client_utilities/makefile b/client/client_utilities/makefile new file mode 100644 index 0000000..4a01dcd --- /dev/null +++ b/client/client_utilities/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,client.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/client/client_utilities/terminal_error.cpp b/client/client_utilities/terminal_error.cpp new file mode 100644 index 0000000..d391add --- /dev/null +++ b/client/client_utilities/terminal_error.cpp @@ -0,0 +1,23 @@ +/* Copyright: (c) Kayne Ruse 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 "terminal_error.hpp" + diff --git a/client/client_utilities/terminal_error.hpp b/client/client_utilities/terminal_error.hpp new file mode 100644 index 0000000..980b4e3 --- /dev/null +++ b/client/client_utilities/terminal_error.hpp @@ -0,0 +1,34 @@ +/* Copyright: (c) Kayne Ruse 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 TERMINALERROR_HPP_ +#define TERMINALERROR_HPP_ + +#include +#include + +class terminal_error: public std::runtime_error { +public: + explicit terminal_error(const std::string& str): runtime_error(str) {} + explicit terminal_error(const char* cstr): runtime_error(cstr) {} +}; + +#endif diff --git a/client/makefile b/client/makefile index d7adf5a..68ce8d9 100644 --- a/client/makefile +++ b/client/makefile @@ -1,5 +1,5 @@ #include directories -INCLUDES+=. entities scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities +INCLUDES+=. client_utilities entities scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet_types ../common/ui ../common/utilities #libraries #the order of the $(LIBS) is important, at least for MinGW @@ -25,6 +25,7 @@ OUT=$(addprefix $(OUTDIR)/,client) #targets all: $(OBJ) $(OUT) + $(MAKE) -C client_utilities $(MAKE) -C entities $(MAKE) -C scenes $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 6167051..5873de7 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -24,6 +24,7 @@ #include "channels.hpp" #include "utility.hpp" +#include "terminal_error.hpp" #include #include #include @@ -103,6 +104,9 @@ void InWorld::Update() { HandlePacket(packetBuffer); } } + catch(terminal_error& e) { + throw(e); + } catch(std::exception& e) { std::cerr << "HandlePacket Error: " << e.what() << std::endl; } @@ -230,6 +234,15 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) { HandleRegionContent(static_cast(argPacket)); break; + //rejection messages + case SerialPacketType::REGION_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + throw(terminal_error(static_cast(argPacket)->text)); + break; + case SerialPacketType::SHUTDOWN_REJECTION: + throw(std::runtime_error(static_cast(argPacket)->text)); + break; + //errors default: { std::ostringstream msg; diff --git a/client/scenes/makefile b/client/scenes/makefile index 73a4950..c11a607 100644 --- a/client/scenes/makefile +++ b/client/scenes/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. ../entities ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities +INCLUDES+=. .. ../client_utilities ../entities ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet_types ../../common/ui ../../common/utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index 3b4c46b..7ac5afc 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -25,7 +25,8 @@ dumpTable(roomSystem) dumpTable(roomSystem.RoomManager) dumpTable(roomSystem.Room) -local overworld = roomSystem.RoomManager.CreateRoom("overworld") +--NOTE: room 0 is the first that the client asks for, therefore it must exist +local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld") roomSystem.Room.Initialize(overworld, "overworld.bmp", mapSaver.Load, mapSaver.Save, mapMaker.debugIsland, mapSaver.Save) print("Finished the lua script") diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 310bfba..863b0cf 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -107,7 +107,7 @@ void ServerApplication::HandleLoginRequest(ClientPacket* const argPacket) { network.SendTo(clientData->GetAddress(), static_cast(&newPacket)); //log the error - std::cerr << "Error message sent: " << msg << std::endl; + std::cerr << "Error message sent: " << newPacket.text << std::endl; return; } @@ -233,11 +233,30 @@ void ServerApplication::HandleShutdownRequest(ClientPacket* const argPacket) { } //check for fraud - if (clientData->GetAddress() != argPacket->srcAddress || accountData->GetAdministrator() != true) { + if (clientData->GetAddress() != argPacket->srcAddress) { std::cerr << "Falsified server shutdown detected from: " << accountData->GetUsername() << std::endl; return; } + //reject non-admin requests + if (accountData->GetAdministrator() != true) { + std::cerr << "Rejected server shutdown command from: " << accountData->GetUsername() << std::endl; + + //build the message + std::ostringstream msg; + msg << "Invalid admin status"; + + //build the packet + TextPacket newPacket; + newPacket.type = SerialPacketType::SHUTDOWN_REJECTION; + strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE); + + //send the rejection message + network.SendTo(argPacket->srcAddress, static_cast(&newPacket)); + + return; + } + //end the server running = false; @@ -260,8 +279,8 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) { if (!room) { //build the error message std::ostringstream msg; - msg << "Failed to find Region (" << argPacket->roomIndex << "," << argPacket->x << "," << argPacket->y << ");"; - msg << "Room " << argPacket->roomIndex << "does not exist"; + msg << "Failed to find Region (" << argPacket->roomIndex << "," << argPacket->x << "," << argPacket->y << "); "; + msg << "Room " << argPacket->roomIndex << " does not exist"; //build the packet TextPacket newPacket; @@ -270,7 +289,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) { network.SendTo(argPacket->srcAddress, static_cast(&newPacket)); //log the error - std::cerr << "Error message sent: " << msg << std::endl; + std::cerr << "Error message sent: " << newPacket.text << std::endl; return; } Region* region = room->GetPager()->GetRegion(argPacket->x, argPacket->y);