The client->server->client region protocol is working

This commit is contained in:
Kayne Ruse
2014-04-06 02:48:43 +11:00
parent 27bda5dc28
commit 99aecbfdbb
5 changed files with 37 additions and 43 deletions
+16 -5
View File
@@ -25,6 +25,11 @@
#include <stdexcept> #include <stdexcept>
//debugging
#include <iostream>
using std::cout;
using std::endl;
//------------------------- //-------------------------
//Public access members //Public access members
//------------------------- //-------------------------
@@ -80,7 +85,7 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
//debug //debug
mapPager.GetRegion(0, 0); RequestRegion(0, 0);
} }
InWorld::~InWorld() { InWorld::~InWorld() {
@@ -327,6 +332,15 @@ void InWorld::HandleRegionContent(NetworkPacket packet) {
} }
mapPager.PushRegion(packet.regionInfo.region); mapPager.PushRegion(packet.regionInfo.region);
packet.regionInfo.region = nullptr; packet.regionInfo.region = nullptr;
//debugging
cout << "Received region: " << packet.regionInfo.x << ", " << packet.regionInfo.y << endl;
if (mapPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
cout << "Success" << endl;
}
else {
cout << "Failure" << endl;
}
} }
//------------------------- //-------------------------
@@ -373,7 +387,7 @@ void InWorld::RequestShutDown() {
} }
void InWorld::UpdateMap() { void InWorld::UpdateMap() {
// //TODO
} }
void InWorld::RequestRegion(int x, int y) { void InWorld::RequestRegion(int x, int y) {
@@ -382,9 +396,6 @@ void InWorld::RequestRegion(int x, int y) {
//pack the region's data //pack the region's data
packet.meta.type = NetworkPacket::Type::REGION_REQUEST; packet.meta.type = NetworkPacket::Type::REGION_REQUEST;
packet.regionInfo.width = mapPager.GetRegionWidth();
packet.regionInfo.height = mapPager.GetRegionHeight();
packet.regionInfo.depth = mapPager.GetRegionDepth();
packet.regionInfo.x = x; packet.regionInfo.x = x;
packet.regionInfo.y = y; packet.regionInfo.y = y;
serialize(&packet, buffer); serialize(&packet, buffer);
+3 -9
View File
@@ -21,12 +21,6 @@
*/ */
#include "region.hpp" #include "region.hpp"
#include <stdexcept>
#include <iostream>
//decorator
#define TRY(x) try { x } catch(std::exception& e) { throw std::runtime_error(std::string() + e.what() + ": " + #x); }
Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY): Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
width(argWidth), width(argWidth),
height(argHeight), height(argHeight),
@@ -34,11 +28,11 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
x(argX), x(argX),
y(argY) y(argY)
{ {
TRY(tiles = new type_t**[width];) tiles = new type_t**[width];
for (register int i = 0; i < width; ++i) { for (register int i = 0; i < width; ++i) {
TRY(tiles[i] = new type_t*[height];) tiles[i] = new type_t*[height];
for (register int j = 0; j < height; ++j) { for (register int j = 0; j < height; ++j) {
TRY(tiles[i][j] = new type_t[depth];) tiles[i][j] = new type_t[depth];
for (register int k = 0; k < depth; ++k) { for (register int k = 0; k < depth; ++k) {
tiles[i][j][k] = 0; tiles[i][j][k] = 0;
} }
+5 -28
View File
@@ -23,11 +23,6 @@
#include "utility.hpp" #include "utility.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth): RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
regionWidth(argWidth), regionWidth(argWidth),
regionHeight(argHeight), regionHeight(argHeight),
@@ -59,29 +54,11 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
//TODO: revert this try/catch point //TODO: revert this try/catch point
Region* ptr = nullptr; Region* ptr = nullptr;
try { ptr = FindRegion(x, y);
ptr = FindRegion(x, y); if (ptr) return ptr;
if (ptr) return ptr; ptr = LoadRegion(x, y);
} if (ptr) return ptr;
catch(exception& e) { return CreateRegion(x, y);
cerr << "FindRegion Error: " << e.what() << endl;
}
try {
ptr = LoadRegion(x, y);
if (ptr) return ptr;
}
catch(exception& e) {
cerr << "LoadRegion Error: " << e.what() << endl;
}
try {
return CreateRegion(x, y);
}
catch(exception& e) {
cerr << "CreateRegion Error: " << e.what() << endl;
}
return nullptr;
} }
Region* RegionPagerBase::FindRegion(int x, int y) { Region* RegionPagerBase::FindRegion(int x, int y) {
+12 -1
View File
@@ -114,7 +114,7 @@ void ServerApplication::Init(int argc, char** argv) {
cout << "Startup completed successfully" << endl; cout << "Startup completed successfully" << endl;
//debugging //debugging
mapPager.GetRegion(0, 0); //
} }
void ServerApplication::Loop() { void ServerApplication::Loop() {
@@ -180,6 +180,9 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
case NetworkPacket::Type::PLAYER_UPDATE: case NetworkPacket::Type::PLAYER_UPDATE:
HandlePlayerUpdate(packet); HandlePlayerUpdate(packet);
break; break;
case NetworkPacket::Type::REGION_REQUEST:
HandleRegionRequest(packet);
break;
//handle errors //handle errors
default: default:
throw(runtime_error("Unknown NetworkPacket::Type encountered")); throw(runtime_error("Unknown NetworkPacket::Type encountered"));
@@ -340,6 +343,14 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
PumpPacket(packet); PumpPacket(packet);
} }
void ServerApplication::HandleRegionRequest(NetworkPacket packet) {
char buffer[PACKET_BUFFER_SIZE];
packet.meta.type = NetworkPacket::Type::REGION_CONTENT;
packet.regionInfo.region = mapPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
serialize(&packet, buffer);
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
}
void ServerApplication::PumpPacket(NetworkPacket packet) { void ServerApplication::PumpPacket(NetworkPacket packet) {
//I don't really like this, but it'll do for now //I don't really like this, but it'll do for now
char buffer[PACKET_BUFFER_SIZE]; char buffer[PACKET_BUFFER_SIZE];
+1
View File
@@ -81,6 +81,7 @@ private:
void HandlePlayerNew(NetworkPacket); void HandlePlayerNew(NetworkPacket);
void HandlePlayerDelete(NetworkPacket); void HandlePlayerDelete(NetworkPacket);
void HandlePlayerUpdate(NetworkPacket); void HandlePlayerUpdate(NetworkPacket);
void HandleRegionRequest(NetworkPacket);
void PumpPacket(NetworkPacket); void PumpPacket(NetworkPacket);