Fix memory corruption in ConnectScene

master
Nicole Collings 2019-07-22 14:14:25 -07:00
parent 1eeacfb102
commit 5fcbc5e75e
8 changed files with 27 additions and 38 deletions

View File

@ -74,7 +74,7 @@ private:
GuiUniforms gu;
GLint currentModelUniform;
double elapsedTime;
double elapsedTime = 0;
};

View File

@ -6,9 +6,9 @@
#include "InputManager.h"
InputManager::InputManager() {
for (bool &key : keysDown) key = false;
for (bool &key : keysPressed) key = false;
for (bool &key : keysReleased) key = false;
for (bool& key : keysDown) key = false;
for (bool& key : keysPressed) key = false;
for (bool& key : keysReleased) key = false;
leftDown = false;
rightDown = false;

View File

@ -22,9 +22,9 @@ public:
bool isMousePressed(int button);
bool isMouseReleased(int button);
private:
bool keysDown[1024];
bool keysPressed[1024];
bool keysReleased[1024];
bool keysDown[1024] {false};
bool keysPressed[1024] {false};
bool keysReleased[1024] {false};
bool leftPressed, leftDown, leftReleased;
bool rightReleased, rightDown, rightPressed;

View File

@ -30,7 +30,7 @@ void ConnectScene::update() {
case State::IDENTIFIER_LIST: {
ENetEvent e;
if (connection.pollEvents(&e) && e.type == ENET_EVENT_TYPE_RECEIVE) {
while (connection.pollEvents(&e) && e.type == ENET_EVENT_TYPE_RECEIVE) {
Packet p(e.packet);
statusText.set(statusText.get() + "Recieved block index-identifier table.\n");
@ -43,15 +43,13 @@ void ConnectScene::update() {
auto len = Serializer::decodeInt(&p.data[ind]);
indexIdentifierTable.emplace_back(&p.data[ind + 4], &p.data[ind + 4 + len]);
ind += 4 + len;
if (ind >= p.data.length()) break;
if (ind >= p.data.length() - 4) break;
}
statusText.set(statusText.get() + "Joining World...");
state.defs.blocks().setIdentifiers(indexIdentifierTable);
Packet r(PacketType::CONNECT_DATA_RECVD);
r.sendTo(connection.getPeer(), PacketChannel::CONNECT);
state.desiredState = "game";
return;
}
}
}

View File

@ -6,8 +6,7 @@
GameScene::GameScene(ClientState& state) : Scene(state),
defs(state.defs),
//TODO: Give `server` `state.connection` instead of a NetHandler.
server({"127.0.0.1", 12345}, defs),
server(state.connection, defs),
world(defs, &playerPos, &server),
player(world, defs, state.renderer.getCamera()),
@ -26,6 +25,9 @@ GameScene::GameScene(ClientState& state) : Scene(state),
entities.push_back(&player);
server.init(entities, &world);
Packet r(PacketType::CONNECT_DATA_RECVD);
r.sendTo(state.connection.getPeer(), PacketChannel::CONNECT);
}

View File

@ -4,9 +4,8 @@
#include "ClientNetworkInterpreter.h"
ClientNetworkInterpreter::ClientNetworkInterpreter(Address address, LocalDefs& defs) :
address(std::move(address)),
ClientNetworkInterpreter::ClientNetworkInterpreter(ServerConnection &connection, LocalDefs &defs) :
connection(connection),
entities(new DrawableGroup()),
playerFrontTex(defs.textures().getTextureRef("player_front")),
@ -16,19 +15,13 @@ ClientNetworkInterpreter::ClientNetworkInterpreter(Address address, LocalDefs& d
void ClientNetworkInterpreter::init(std::vector<Drawable *> &entities, LocalWorld *world) {
entities.push_back(this->entities);
this->world = world;
handler = NetHandler(address, 3, 3000);
if (handler.getState() != NetState::CLIENT) {
exit(EXIT_FAILURE);
}
}
void ClientNetworkInterpreter::update(Player &player) {
recvPackets = 0;
ENetEvent event;
while (handler.update(&event)) {
while (connection.pollEvents(&event)) {
recvPackets++;
switch (event.type) {
@ -111,12 +104,11 @@ void ClientNetworkInterpreter::update(Player &player) {
Serializer::encodeFloat(p.data, player.getPos().y - Player::EYE_HEIGHT);
Serializer::encodeFloat(p.data, player.getPos().z);
Serializer::encodeFloat(p.data, player.getYaw());
p.sendTo(handler.getPeer(), PacketChannel::PLAYER);
p.sendTo(connection.getPeer(), PacketChannel::PLAYER);
}
void ClientNetworkInterpreter::cleanup() {
handler.disconnect();
connected = false;
connection.disconnect();
}
ClientNetworkInterpreter::~ClientNetworkInterpreter() {
@ -127,5 +119,5 @@ void ClientNetworkInterpreter::setBlock(glm::vec3 pos, int block) {
Packet p(PacketType::BLOCK_SET);
Serializer::encodeIntVec3(p.data, pos);
Serializer::encodeInt(p.data, block);
p.sendTo(handler.getPeer(), PacketChannel::BLOCK);
p.sendTo(connection.getPeer(), PacketChannel::BLOCK);
}

View File

@ -17,10 +17,11 @@
#include "../world/Player.h"
#include "../world/LocalWorld.h"
#include "../../../util/net/Address.h"
#include "ServerConnection.h"
class ClientNetworkInterpreter {
public:
ClientNetworkInterpreter(Address address, LocalDefs& defs);
ClientNetworkInterpreter(ServerConnection& connection, LocalDefs& defs);
void init(std::vector<Drawable*> &entities, LocalWorld* world);
void update(Player &player);
@ -35,15 +36,11 @@ public:
int recvPackets = 0;
private:
std::shared_ptr<AtlasRef> playerFrontTex, playerBackTex, shadowTex;
bool connected = false;
int id = 0;
DrawableGroup* entities;
LocalWorld* world;
NetHandler handler;
Address address;
ServerConnection& connection;
};

View File

@ -38,10 +38,10 @@ private:
ServerClients clientList;
ServerConfig config;
double elapsedSeconds;
double deltaTime;
double elapsedSeconds = 0;
double deltaTime = 0;
unsigned short port;
unsigned short port = 0;
};