namespace client {
-ChunkReceiver::ChunkReceiver(ChunkStore &store, const WorldSave &save)
-: store(store)
+ChunkReceiver::ChunkReceiver(Client &client, ChunkStore &store, const WorldSave &save)
+: client(client)
+, store(store)
, save(save)
, transmissions()
, timer(5000) {
for (ChunkTransmission &trans : transmissions) {
if (trans.active && (timer.Elapsed() - trans.last_update) > timer.Interval()) {
cout << "timeout for transmission of chunk " << trans.coords << endl;
- trans.Clear();
+ if (trans.header_received) {
+ client.SendChunkRequest(trans.coords);
+ trans.Reset();
+ trans.last_update = timer.Elapsed();
+ } else {
+ // well shit
+ trans.Clear();
+ }
}
}
if (transmissions.size() > 3) {
Chunk *chunk = store.Allocate(trans.coords);
if (!chunk) {
- // chunk no longer of interes, just drop the data
+ // chunk no longer of interest, just drop the data
// it should probably be cached to disk, but not now :P
trans.Clear();
return;
if (uncompress(dst, &dst_len, src, src_len) != Z_OK) {
// omg, now what?
cout << "got corruped chunk data for " << trans.coords << endl;
+ client.SendChunkRequest(trans.coords);
+ trans.Reset();
+ // chunk data can, and probably will, contain invalid block IDs, so
+ // zero it to be safe
+ memset(dst, 0, dst_len);
+ return;
}
} else {
memcpy(dst, src, min(src_len, dst_len));
}
-void ChunkTransmission::Clear() noexcept {
+void ChunkTransmission::Reset() noexcept {
data_size = 0;
data_received = 0;
last_update = 0;
header_received = false;
+}
+
+void ChunkTransmission::Clear() noexcept {
+ Reset();
active = false;
}
return conn.Send(client_pack, client_sock);
}
+uint16_t Client::SendChunkRequest(
+ const glm::ivec3 &coords
+) {
+ auto pack = Packet::Make<Packet::ChunkBegin>(client_pack);
+ pack.WriteChunkCoords(coords);
+ return conn.Send(client_pack, client_sock);
+}
+
uint16_t Client::SendMessage(
uint8_t type,
uint32_t ref,