Fix race conditions in *Streams, fix deserializer SegFault.

master
Nicole Collings 2020-04-17 17:19:01 -07:00
parent e0a3dce73e
commit 0359ada6b3
10 changed files with 33 additions and 49 deletions

View File

@ -161,16 +161,16 @@ void ClientNetworkInterpreter::watchInv(const std::string& inv, const std::strin
}
void ClientNetworkInterpreter::unwatchInv(const std::string& inv, const std::string& list) {
Serializer().append(inv).append(list)
.packet(PacketType::INV_UNWATCH).sendTo(connection.getPeer(), PacketChannel::INVENTORY);
auto packet = Serializer().append(inv).append(list).packet(PacketType::INV_UNWATCH);
packet.sendTo(connection.getPeer(), PacketChannel::INVENTORY);
}
void ClientNetworkInterpreter::primaryInteract(const std::string &inv, const std::string &list, unsigned short ind) {
Serializer().append<unsigned short>(0).append(inv).append(list).append<unsigned short>(ind)
.packet(PacketType::INV_INTERACT).sendTo(connection.getPeer(), PacketChannel::INVENTORY);
auto packet = Serializer().append<unsigned short>(0).append(inv).append(list).append<unsigned short>(ind).packet(PacketType::INV_INTERACT);
packet.sendTo(connection.getPeer(), PacketChannel::INVENTORY);
}
void ClientNetworkInterpreter::secondaryInteract(const std::string &inv, const std::string &list, unsigned short ind) {
Serializer().append<unsigned short>(1).append(inv).append(list).append<unsigned short>(ind)
.packet(PacketType::INV_INTERACT).sendTo(connection.getPeer(), PacketChannel::INVENTORY);
auto packet = Serializer().append<unsigned short>(1).append(inv).append(list).append<unsigned short>(ind).packet(PacketType::INV_INTERACT);
packet.sendTo(connection.getPeer(), PacketChannel::INVENTORY);
}

View File

@ -73,9 +73,7 @@ std::vector<ChunkMeshDetails*> MeshGenStream::update() {
}
MeshGenStream::Thread::Thread(ClientGame &defs, std::array<NoiseSample, 3>& offsetSamplers) :
game(defs), offsetSamplers(offsetSamplers), thread(std::bind(&MeshGenStream::Thread::exec, this)) {
thread.detach();
}
game(defs), offsetSamplers(offsetSamplers), thread(std::bind(&MeshGenStream::Thread::exec, this)) {}
void MeshGenStream::Thread::exec() {
while (keepAlive) {

View File

@ -51,11 +51,10 @@ public:
ClientGame &game;
std::array<NoiseSample, 3>& offsetSamplers;
std::thread thread {};
bool keepAlive = true;
std::vector<Unit> tasks = std::vector<Unit>(THREAD_QUEUE_SIZE);
bool keepAlive = true;
std::thread thread {};
};
std::vector<Thread> threads;

View File

@ -72,9 +72,7 @@ std::unique_ptr<std::vector<std::shared_ptr<BlockChunk>>> WorldInterpolationStre
}
WorldInterpolationStream::Thread::Thread(MapGen *gen) : gen(gen),
thread(std::bind(&WorldInterpolationStream::Thread::exec, this)) {
thread.detach();
}
thread(std::bind(&WorldInterpolationStream::Thread::exec, this)) {}
void WorldInterpolationStream::Thread::exec() {
while (!kill) {

View File

@ -53,11 +53,11 @@ private:
explicit Thread(MapGen* gen);
void exec();
std::thread thread;
bool kill = false;
MapGen* gen;
std::vector<Job> tasks = std::vector<Job>(THREAD_QUEUE_SIZE);
bool kill = false;
std::thread thread;
};
std::shared_ptr<MapGenProps> props;

View File

@ -171,10 +171,8 @@ void Server::handlePlayerPacket(ServerClient& client, PacketView& p) {
break;
}
case PacketType::INV_UNWATCH: {
Deserializer d(p.d.data);
std::string source = d.read<std::string>();
std::string list = d.read<std::string>();
std::string source = p.d.read<std::string>();
std::string list = p.d.read<std::string>();
// TODO: When inventory saving / loading is implemented there will need to be a cross-save identifier.
if (source == "current_player") source = "player:" + std::to_string(client.cid);
@ -189,13 +187,12 @@ void Server::handlePlayerPacket(ServerClient& client, PacketView& p) {
break;
}
case PacketType::INV_INTERACT: {
Deserializer d(p.d.data);
unsigned short type = d.read<unsigned short>();
unsigned short type = p.d.read<unsigned short>();
std::string source = d.read<std::string>();
std::string list = d.read<std::string>();
unsigned short ind = d.read<unsigned short>();
std::string source = p.d.read<std::string>();
std::string list = p.d.read<std::string>();
unsigned short ind = p.d.read<unsigned short>();
// TODO: When inventory saving / loading is implemented there will need to be a cross-save identifier.
if (source == "current_player") source = "player:" + std::to_string(client.cid);

View File

@ -52,9 +52,7 @@ std::unique_ptr<std::vector<std::shared_ptr<BlockChunk>>> ServerGenStream::updat
}
ServerGenStream::Thread::Thread(MapGen *gen) : gen(gen),
thread(std::bind(&ServerGenStream::Thread::exec, this)) {
thread.detach();
}
thread(std::bind(&ServerGenStream::Thread::exec, this)) {}
void ServerGenStream::Thread::exec() {
while (!kill) {

View File

@ -39,11 +39,11 @@ private:
explicit Thread(MapGen* gen);
void exec();
std::thread thread;
bool kill = false;
MapGen* gen;
std::vector<Job> tasks = std::vector<Job>(THREAD_QUEUE_SIZE);
bool kill = false;
std::thread thread;
};
std::shared_ptr<MapGenProps> props;

View File

@ -14,8 +14,8 @@
class ServerWorld : public World {
public:
const static int MB_GEN_H = 6;
const static int MB_GEN_V = 4;
const static int MB_GEN_H = 2;
const static int MB_GEN_V = 2;
explicit ServerWorld(unsigned int seed, ServerGame& defs, ClientList& clients);

View File

@ -17,13 +17,13 @@ root(function(e)
size = { 68, 68 },
background = "@aurailus:hot_wheel:hot_wheel_circle",
Gui.InventoryList {
position = { 1, 1 },
slot_spacing = { 2, 2 },
length = 1,
source = "current_player",
list = "main",
}
-- Gui.InventoryList {
-- position = { 7, 1 },
-- slot_spacing = { 2, 2 },
-- length = 1,
-- source = "current_player",
-- list = "main",
-- }
})
e:append(Gui.Rect {
@ -32,10 +32,4 @@ root(function(e)
background = "@aurailus:hot_wheel:hot_wheel_line",
})
-- e:append(Gui.Rect {
-- size = { 88, 28 },
-- position = { 52, 12 },
-- background = "@aurailus:hot_wheel:slots_expanded"
-- })
end)