Skip to content

Commit 9464a01

Browse files
committed
fix: chunk packet loop
1 parent e77f66e commit 9464a01

File tree

8 files changed

+454
-424
lines changed

8 files changed

+454
-424
lines changed

include/buffer.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ class Buffer {
3939
void writeUUID(const UUID& uuid);
4040

4141
void writeBool(bool value);
42-
void writeNBT(const std::string& nbtData);
43-
void writePosition(int32_t x, int32_t y, int32_t z);
44-
void writeFloat(float value);
45-
void writeDouble(double value);
46-
void writeIdentifier(const std::string& id);
47-
void writeVarLong(int64_t value);
42+
void writeNBT(const std::string& nbtData);
43+
void writePosition(int32_t x, int32_t y, int32_t z);
44+
void writeFloat(float value);
45+
void writeDouble(double value);
46+
void writeIdentifier(const std::string& id);
47+
void writeVarLong(int64_t value);
48+
void writeShort(int16_t value);
4849
};
4950

5051
#endif

src/buffer.cpp

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ Buffer::Buffer() : _pos(0) {}
1010
Buffer::Buffer(const std::vector<uint8_t>& data) : _data(data), _pos(0) {}
1111

1212
uint8_t Buffer::readByte() {
13-
if (_pos >= _data.size())
14-
throw std::runtime_error("Buffer underflow on byte");
13+
if (_pos >= _data.size()) throw std::runtime_error("Buffer underflow on byte");
1514
return _data[_pos++];
1615
}
1716

18-
void Buffer::writeByte(uint8_t byte) {
19-
_data.push_back(byte);
20-
}
17+
void Buffer::writeByte(uint8_t byte) { _data.push_back(byte); }
2118

2219
void Buffer::writeBytes(const std::string& data) {
2320
_data.insert(_data.end(), data.begin(), data.end());
@@ -27,6 +24,11 @@ void Buffer::writeBytes(const std::vector<uint8_t>& data) {
2724
_data.insert(_data.end(), data.begin(), data.end());
2825
}
2926

27+
void Buffer::writeShort(int16_t value) {
28+
writeByte(static_cast<uint8_t>((value >> 8) & 0xFF));
29+
writeByte(static_cast<uint8_t>(value & 0xFF));
30+
}
31+
3032
void Buffer::writeUUID(const UUID& uuid) {
3133
uint64_t msb = uuid.getMostSigBits();
3234
uint64_t lsb = uuid.getLeastSigBits();
@@ -44,8 +46,7 @@ int Buffer::readVarInt() {
4446
currentByte = readByte();
4547
value |= (currentByte & 0x7F) << position;
4648
position += 7;
47-
if (position >= 32)
48-
throw std::runtime_error("VarInt too big");
49+
if (position >= 32) throw std::runtime_error("VarInt too big");
4950
} while (currentByte & 0x80);
5051

5152
return value;
@@ -105,13 +106,9 @@ void Buffer::writeString(const std::string& str) {
105106
_data.insert(_data.end(), str.begin(), str.end());
106107
}
107108

108-
std::vector<uint8_t>& Buffer::getData() {
109-
return _data;
110-
}
109+
std::vector<uint8_t>& Buffer::getData() { return _data; }
111110

112-
size_t Buffer::remaining() const {
113-
return _data.size() - _pos;
114-
}
111+
size_t Buffer::remaining() const { return _data.size() - _pos; }
115112

116113
uint16_t Buffer::readUShort() {
117114
uint16_t val = (readByte() << 8) | readByte();
@@ -140,43 +137,44 @@ void Buffer::writeLong(long value) {
140137
}
141138
}
142139

143-
void Buffer::writeBool(bool value) {
144-
writeByte(value ? 0x01 : 0x00);
145-
}
140+
void Buffer::writeBool(bool value) { writeByte(value ? 0x01 : 0x00); }
146141

147-
void Buffer::writeNBT(const std::string& nbtData) {
148-
writeBytes(nbtData);
149-
}
142+
void Buffer::writeNBT(const std::string& nbtData) { writeBytes(nbtData); }
150143

151144
void Buffer::writePosition(int32_t x, int32_t y, int32_t z) {
152-
int64_t packed = ((int64_t)(x & 0x3FFFFFF) << 38) | ((int64_t)(y & 0xFFF) << 26) | (int64_t)(z & 0x3FFFFFF);
153-
writeLong(packed);
145+
int64_t packed = ((int64_t)(x & 0x3FFFFFF) << 38) | ((int64_t)(y & 0xFFF) << 26) |
146+
(int64_t)(z & 0x3FFFFFF);
147+
writeLong(packed);
154148
}
155149

156150
void Buffer::writeFloat(float value) {
157-
union { float f; uint32_t i; } u;
158-
u.f = value;
159-
writeUInt(u.i);
151+
union {
152+
float f;
153+
uint32_t i;
154+
} u;
155+
u.f = value;
156+
writeUInt(u.i);
160157
}
161158

162159
void Buffer::writeDouble(double value) {
163-
union { double d; uint64_t i; } u;
164-
u.d = value;
165-
writeLong(u.i);
160+
union {
161+
double d;
162+
uint64_t i;
163+
} u;
164+
u.d = value;
165+
writeLong(u.i);
166166
}
167167

168168
void Buffer::writeVarLong(int64_t value) {
169-
while (true) {
170-
if ((value & ~0x7FL) == 0) {
171-
writeByte((uint8_t)value);
172-
return;
173-
} else {
174-
writeByte((uint8_t)((value & 0x7F) | 0x80));
175-
value >>= 7;
176-
}
177-
}
169+
while (true) {
170+
if ((value & ~0x7FL) == 0) {
171+
writeByte((uint8_t)value);
172+
return;
173+
} else {
174+
writeByte((uint8_t)((value & 0x7F) | 0x80));
175+
value >>= 7;
176+
}
177+
}
178178
}
179179

180-
void Buffer::writeIdentifier(const std::string& id) {
181-
writeString(id);
182-
}
180+
void Buffer::writeIdentifier(const std::string& id) { writeString(id); }

src/networking/networkQueuer.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
#include "logger.hpp"
12
#include "networking.hpp"
23
#include "packet.hpp"
34
#include "player.hpp"
45
#include "server.hpp"
5-
#include "logger.hpp"
66

77
#include <arpa/inet.h>
88
#include <cstdint>
99
#include <errno.h>
1010
#include <exception>
1111
#include <iostream>
1212
#include <netinet/in.h>
13+
#include <string>
1314
#include <sys/epoll.h>
1415
#include <sys/socket.h>
1516
#include <unistd.h>
16-
#include <string>
1717

1818
void NetworkManager::receiverThreadLoop() {
1919
const int MaxEvent = 256;
@@ -23,8 +23,7 @@ void NetworkManager::receiverThreadLoop() {
2323
int eventCount = epoll_wait(_epollFd, events, MaxEvent, 50);
2424

2525
if (eventCount == -1) {
26-
if (errno == EINTR)
27-
continue;
26+
if (errno == EINTR) continue;
2827
break;
2928
}
3029

@@ -37,7 +36,8 @@ void NetworkManager::receiverThreadLoop() {
3736
socklen_t addr_len = sizeof(client_addr);
3837
int client_fd = accept(_serverSocket, (sockaddr*)&client_addr, &addr_len);
3938
if (client_fd != -1) {
40-
// g_logger->logNetwork(INFO, "New connection accepted on socket " + std::to_string(client_fd), "Network Manager");
39+
// g_logger->logNetwork(INFO, "New connection accepted on socket " +
40+
// std::to_string(client_fd), "Network Manager");
4141
epoll_event event;
4242
event.events = EPOLLIN;
4343
event.data.fd = client_fd;
@@ -56,13 +56,11 @@ void NetworkManager::receiverThreadLoop() {
5656
p = it->second;
5757
} else {
5858
auto temp_it = getServer().getTempPlayerLst().find(fd);
59-
if (temp_it != getServer().getTempPlayerLst().end())
60-
p = temp_it->second;
59+
if (temp_it != getServer().getTempPlayerLst().end()) p = temp_it->second;
6160
}
6261

6362
if (eventFlags & EPOLLERR || eventFlags & EPOLLHUP) {
64-
if (p)
65-
getServer().removePlayerFromAnyList(p);
63+
if (p) getServer().removePlayerFromAnyList(p);
6664
epoll_ctl(_epollFd, EPOLL_CTL_DEL, fd, nullptr);
6765
close(fd);
6866
continue;
@@ -75,8 +73,7 @@ void NetworkManager::receiverThreadLoop() {
7573
else
7674
handleIncomingData(fd);
7775
} catch (const std::exception& e) {
78-
if (p)
79-
getServer().removePlayerFromAnyList(p);
76+
if (p) getServer().removePlayerFromAnyList(p);
8077
epoll_ctl(_epollFd, EPOLL_CTL_DEL, fd, nullptr);
8178
close(fd);
8279
}
@@ -90,14 +87,17 @@ void NetworkManager::senderThreadLoop() {
9087
Packet* p = nullptr;
9188

9289
while (_outgoingPackets.tryPop(p)) {
93-
if (p == nullptr)
94-
break;
90+
if (p == nullptr) break;
9591

9692
try {
9793
// g_logger->logNetwork(INFO, "Sending packet to player", "Network Manager");
94+
std::cout << "Sending packet 0x" << std::hex << p->getId() << " (" << std::dec
95+
<< p->getData().getData().size() << " bytes)" << std::endl;
96+
9897
send(p->getSocket(), p->getData().getData().data(), p->getSize(), MSG_NOSIGNAL);
9998
if (p->getPlayer() && p->getPlayer()->getPlayerState() == PlayerState::None) {
100-
// g_logger->logNetwork(INFO, "Closing status connection after response", "Network Manager");
99+
// g_logger->logNetwork(INFO, "Closing status connection after response",
100+
// "Network Manager");
101101
getServer().removePlayerFromAnyList(p->getPlayer());
102102
epoll_ctl(_epollFd, EPOLL_CTL_DEL, p->getSocket(), nullptr);
103103
close(p->getSocket());
@@ -113,9 +113,7 @@ void NetworkManager::senderThreadLoop() {
113113
}
114114
}
115115

116-
void NetworkManager::enqueueOutgoingPacket(Packet* p) {
117-
_outgoingPackets.push(p);
118-
}
116+
void NetworkManager::enqueueOutgoingPacket(Packet* p) { _outgoingPackets.push(p); }
119117

120118
void NetworkManager::handleIncomingData(Player* connection) {
121119
Packet* p;
@@ -131,7 +129,8 @@ void NetworkManager::handleIncomingData(Player* connection) {
131129

132130
void NetworkManager::handleIncomingData(int socket) {
133131
Packet* p;
134-
// g_logger->logNetwork(INFO, "Handling incoming data for socket " + std::to_string(socket), "Network Manager");
132+
// g_logger->logNetwork(INFO, "Handling incoming data for socket " + std::to_string(socket),
133+
// "Network Manager");
135134
try {
136135
p = new Packet(socket, getServer());
137136
_incomingPackets.push(p);

0 commit comments

Comments
 (0)