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>
//debugging
#include <iostream>
using std::cout;
using std::endl;
//-------------------------
//Public access members
//-------------------------
@@ -80,7 +85,7 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
//debug
mapPager.GetRegion(0, 0);
RequestRegion(0, 0);
}
InWorld::~InWorld() {
@@ -327,6 +332,15 @@ void InWorld::HandleRegionContent(NetworkPacket packet) {
}
mapPager.PushRegion(packet.regionInfo.region);
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() {
//
//TODO
}
void InWorld::RequestRegion(int x, int y) {
@@ -382,9 +396,6 @@ void InWorld::RequestRegion(int x, int y) {
//pack the region's data
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.y = y;
serialize(&packet, buffer);
+3 -9
View File
@@ -21,12 +21,6 @@
*/
#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):
width(argWidth),
height(argHeight),
@@ -34,11 +28,11 @@ Region::Region(int argWidth, int argHeight, int argDepth, int argX, int argY):
x(argX),
y(argY)
{
TRY(tiles = new type_t**[width];)
tiles = new type_t**[width];
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) {
TRY(tiles[i][j] = new type_t[depth];)
tiles[i][j] = new type_t[depth];
for (register int k = 0; k < depth; ++k) {
tiles[i][j][k] = 0;
}
+5 -28
View File
@@ -23,11 +23,6 @@
#include "utility.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
RegionPagerBase::RegionPagerBase(int argWidth, int argHeight, int argDepth):
regionWidth(argWidth),
regionHeight(argHeight),
@@ -59,29 +54,11 @@ Region* RegionPagerBase::GetRegion(int x, int y) {
//TODO: revert this try/catch point
Region* ptr = nullptr;
try {
ptr = FindRegion(x, y);
if (ptr) return ptr;
}
catch(exception& e) {
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;
ptr = FindRegion(x, y);
if (ptr) return ptr;
ptr = LoadRegion(x, y);
if (ptr) return ptr;
return CreateRegion(x, 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;
//debugging
mapPager.GetRegion(0, 0);
//
}
void ServerApplication::Loop() {
@@ -180,6 +180,9 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
case NetworkPacket::Type::PLAYER_UPDATE:
HandlePlayerUpdate(packet);
break;
case NetworkPacket::Type::REGION_REQUEST:
HandleRegionRequest(packet);
break;
//handle errors
default:
throw(runtime_error("Unknown NetworkPacket::Type encountered"));
@@ -340,6 +343,14 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket 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) {
//I don't really like this, but it'll do for now
char buffer[PACKET_BUFFER_SIZE];
+1
View File
@@ -81,6 +81,7 @@ private:
void HandlePlayerNew(NetworkPacket);
void HandlePlayerDelete(NetworkPacket);
void HandlePlayerUpdate(NetworkPacket);
void HandleRegionRequest(NetworkPacket);
void PumpPacket(NetworkPacket);