BIG REFACTOR
This commit is contained in:
@@ -8,18 +8,17 @@
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
unsigned long inflate(const std::string& source, std::string& dest) {
|
||||
unsigned long size = dest.size();
|
||||
uncompress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length());
|
||||
std::uint64_t inflate(const std::string& source, std::string& dest) {
|
||||
std::size_t size;
|
||||
uncompress(reinterpret_cast<Bytef*>(dest.data()), &size, reinterpret_cast<const Bytef*>(source.c_str()), source.length());
|
||||
return size;
|
||||
}
|
||||
|
||||
unsigned long deflate(const std::string& source, std::string& dest) {
|
||||
unsigned long size = source.length();
|
||||
dest.resize(size);
|
||||
|
||||
compress((Bytef*)&dest[0], &size, (const Bytef*)source.c_str(), source.length());
|
||||
dest.resize(size);
|
||||
std::uint64_t deflate(const std::string& source, std::string& dest) {
|
||||
std::size_t size;
|
||||
dest.resize(source.size()); // Resize for the compressed data to fit into
|
||||
compress(reinterpret_cast<Bytef*>(dest.data()), &size, reinterpret_cast<const Bytef*>(source.c_str()), source.length());
|
||||
dest.resize(size); // Resize to cut useless data
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
|
||||
namespace td {
|
||||
|
||||
DataBuffer::DataBuffer() { }
|
||||
DataBuffer::DataBuffer() : m_ReadOffset(0) { }
|
||||
DataBuffer::DataBuffer(const DataBuffer& other) : m_Buffer(other.m_Buffer), m_ReadOffset(other.m_ReadOffset) { }
|
||||
DataBuffer::DataBuffer(DataBuffer&& other) : m_Buffer(std::move(other.m_Buffer)), m_ReadOffset(std::move(other.m_ReadOffset)) { }
|
||||
DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()) { }
|
||||
DataBuffer::DataBuffer(const DataBuffer& other, std::size_t offset) {
|
||||
m_Buffer.reserve(other.GetSize() - offset);
|
||||
DataBuffer::DataBuffer(const std::string& str) : m_Buffer(str.begin(), str.end()), m_ReadOffset(0) { }
|
||||
|
||||
DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) : m_ReadOffset(0) {
|
||||
m_Buffer.reserve(other.GetSize() - static_cast<std::size_t>(offset));
|
||||
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ DataBuffer::const_iterator DataBuffer::end() const {
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) {
|
||||
for (unsigned char u : buffer)
|
||||
os << std::hex << std::setfill('0') << std::setw(2) << (int)u << " ";
|
||||
os << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(u) << " ";
|
||||
os << std::dec;
|
||||
return os;
|
||||
}
|
||||
@@ -81,7 +82,7 @@ bool DataBuffer::ReadFile(const std::string& fileName) {
|
||||
bool DataBuffer::WriteFile(const std::string& fileName) {
|
||||
try {
|
||||
std::ofstream file(fileName, std::ostream::binary);
|
||||
file.write((const char*)m_Buffer.data(), m_Buffer.size());
|
||||
file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
|
||||
file.flush();
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Failed to write file \"" << fileName << "\" reason : " << e.what() << std::endl;
|
||||
|
||||
@@ -33,7 +33,8 @@ NetworkInitializer initializer;
|
||||
|
||||
IPAddresses Dns::Resolve(const std::string& host) {
|
||||
IPAddresses list;
|
||||
addrinfo hints = { 0 }, * addresses;
|
||||
addrinfo hints{};
|
||||
addrinfo* addresses = nullptr;
|
||||
|
||||
//hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_family = AF_INET;
|
||||
@@ -54,7 +55,7 @@ IPAddresses Dns::Resolve(const std::string& host) {
|
||||
#else
|
||||
char straddr[512];
|
||||
|
||||
inet_ntop(p->ai_family, &((sockaddr_in*)p->ai_addr)->sin_addr, straddr, sizeof(straddr));
|
||||
inet_ntop(p->ai_family, &(reinterpret_cast<sockaddr_in*>(p->ai_addr))->sin_addr, straddr, sizeof(straddr));
|
||||
#endif
|
||||
|
||||
list.push_back(IPAddress(straddr));
|
||||
|
||||
@@ -16,7 +16,7 @@ Socket::Socket(Type type)
|
||||
: m_Blocking(false),
|
||||
m_Type(type),
|
||||
m_Status(Disconnected),
|
||||
m_Handle((SocketHandle)INVALID_SOCKET)
|
||||
m_Handle(static_cast<SocketHandle>(INVALID_SOCKET))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
namespace td {
|
||||
namespace network {
|
||||
|
||||
TCPSocket::TCPSocket()
|
||||
: Socket(Socket::TCP), m_Port(0)
|
||||
{
|
||||
m_Handle = (SocketHandle)INVALID_SOCKET;
|
||||
TCPSocket::TCPSocket() : Socket(Socket::TCP), m_Port(0) {
|
||||
m_Handle = static_cast<SocketHandle>(INVALID_SOCKET);
|
||||
}
|
||||
|
||||
bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
|
||||
@@ -32,7 +30,7 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
if (::getaddrinfo(address.ToString().c_str(), std::to_string(port).c_str(), &hints, &result) != 0) {
|
||||
if (::getaddrinfo(address.ToString().c_str(), std::to_string(static_cast<int>(port)).c_str(), &hints, &result) != 0) {
|
||||
std::cerr << "Failed to get address info !\n";
|
||||
return false;
|
||||
}
|
||||
@@ -44,8 +42,8 @@ bool TCPSocket::Connect(const IPAddress& address, unsigned short port) {
|
||||
|
||||
struct addrinfo* ptr = nullptr;
|
||||
for (ptr = result; ptr != nullptr; ptr = ptr->ai_next) {
|
||||
struct sockaddr_in* sockaddr = (struct sockaddr_in*)ptr->ai_addr;
|
||||
if (::connect(m_Handle, (struct sockaddr*)sockaddr, sizeof(struct sockaddr_in)) != 0) {
|
||||
struct sockaddr* sockaddr = ptr->ai_addr;
|
||||
if (::connect(m_Handle, sockaddr, sizeof(sockaddr_in)) != 0) {
|
||||
std::cerr << "Failed to connect with this address !\n";
|
||||
continue;
|
||||
}
|
||||
@@ -78,7 +76,7 @@ size_t TCPSocket::Send(const unsigned char* data, size_t size) {
|
||||
Disconnect();
|
||||
return 0;
|
||||
}
|
||||
sent += cur;
|
||||
sent += static_cast<std::size_t>(cur);
|
||||
}
|
||||
|
||||
return sent;
|
||||
@@ -88,7 +86,7 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
|
||||
buffer.Resize(amount);
|
||||
buffer.SetReadOffset(0);
|
||||
|
||||
int recvAmount = ::recv(m_Handle, (char*)&buffer[0], amount, 0);
|
||||
int recvAmount = ::recv(m_Handle, reinterpret_cast<char*>(buffer.data()), amount, 0);
|
||||
if (recvAmount <= 0) {
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
int err = WSAGetLastError();
|
||||
@@ -104,8 +102,8 @@ std::size_t TCPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
|
||||
buffer.Clear();
|
||||
return 0;
|
||||
}
|
||||
buffer.Resize(recvAmount);
|
||||
return recvAmount;
|
||||
buffer.Resize(static_cast<std::size_t>(recvAmount));
|
||||
return static_cast<std::size_t>(recvAmount);
|
||||
}
|
||||
|
||||
DataBuffer TCPSocket::Receive(std::size_t amount) {
|
||||
@@ -126,7 +124,7 @@ DataBuffer TCPSocket::Receive(std::size_t amount) {
|
||||
return DataBuffer();
|
||||
}
|
||||
|
||||
return DataBuffer(std::string(buf.get(), received));
|
||||
return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
|
||||
}
|
||||
|
||||
TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) {
|
||||
@@ -136,12 +134,12 @@ TCPSocket::TCPSocket(TCPSocket&& other) : Socket(TCP) {
|
||||
m_RemoteIP = other.m_RemoteIP;
|
||||
SetStatus(other.GetStatus());
|
||||
SetBlocking(other.IsBlocking());
|
||||
other.m_Handle = (SocketHandle)INVALID_SOCKET;
|
||||
other.m_Handle = static_cast<SocketHandle>(INVALID_SOCKET);
|
||||
}
|
||||
|
||||
void SendPacket(const DataBuffer& data, network::TCPSocket& socket) {
|
||||
DataBuffer compressed = utils::Compress(data);
|
||||
socket.Send((const std::uint8_t*)compressed.ToString().data(), compressed.GetSize());
|
||||
socket.Send(reinterpret_cast<const std::uint8_t*>(compressed.ToString().data()), compressed.GetSize());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace network {
|
||||
UDPSocket::UDPSocket()
|
||||
: Socket(Socket::UDP), m_Port(0)
|
||||
{
|
||||
m_Handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
m_Handle = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
}
|
||||
|
||||
bool UDPSocket::Connect(const IPAddress& address, unsigned short port) {
|
||||
@@ -31,20 +31,20 @@ bool UDPSocket::Connect(const IPAddress& address, unsigned short port) {
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t UDPSocket::Send(const unsigned char* data, size_t size) {
|
||||
size_t sent = 0;
|
||||
std::size_t UDPSocket::Send(const unsigned char* data, std::size_t size) {
|
||||
std::size_t sent = 0;
|
||||
|
||||
if (this->GetStatus() != Connected)
|
||||
return 0;
|
||||
|
||||
while (sent < size) {
|
||||
int cur = sendto(m_Handle, reinterpret_cast<const char*>(data + sent), size - sent, 0,
|
||||
int cur = ::sendto(m_Handle, reinterpret_cast<const char*>(data + sent), size - sent, 0,
|
||||
reinterpret_cast<sockaddr*>(&m_RemoteAddr), sizeof(sockaddr_in));
|
||||
if (cur <= 0) {
|
||||
Disconnect();
|
||||
return 0;
|
||||
}
|
||||
sent += cur;
|
||||
sent += static_cast<std::size_t>(cur);
|
||||
}
|
||||
|
||||
return sent;
|
||||
@@ -54,7 +54,7 @@ DataBuffer UDPSocket::Receive(std::size_t amount) {
|
||||
std::unique_ptr<char[]> buf(new char[amount]);
|
||||
socklen_t slen = sizeof(sockaddr_in);
|
||||
|
||||
int received = recvfrom(m_Handle, buf.get(), amount, 0,
|
||||
int received = ::recvfrom(m_Handle, buf.get(), amount, 0,
|
||||
reinterpret_cast<sockaddr*>(&m_RemoteAddr), &slen);
|
||||
|
||||
if (received <= 0) {
|
||||
@@ -64,13 +64,40 @@ DataBuffer UDPSocket::Receive(std::size_t amount) {
|
||||
int err = errno;
|
||||
#endif
|
||||
if (err == WOULDBLOCK)
|
||||
return DataBuffer(std::string(buf.get(), received));
|
||||
return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
|
||||
|
||||
Disconnect();
|
||||
return DataBuffer();
|
||||
}
|
||||
|
||||
return DataBuffer(std::string(buf.get(), received));
|
||||
return DataBuffer(std::string(buf.get(), static_cast<std::size_t>(received)));
|
||||
}
|
||||
|
||||
std::size_t UDPSocket::Receive(DataBuffer& buffer, std::size_t amount) {
|
||||
buffer.Resize(amount);
|
||||
buffer.SetReadOffset(0);
|
||||
|
||||
socklen_t slen = sizeof(sockaddr_in);
|
||||
|
||||
int recvAmount = ::recvfrom(m_Handle, reinterpret_cast<char*>(buffer.data()), amount, 0,
|
||||
reinterpret_cast<sockaddr*>(&m_RemoteAddr), &slen);
|
||||
if (recvAmount <= 0) {
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
int err = WSAGetLastError();
|
||||
#else
|
||||
int err = errno;
|
||||
#endif
|
||||
if (err == WOULDBLOCK) {
|
||||
buffer.Clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Disconnect();
|
||||
buffer.Clear();
|
||||
return 0;
|
||||
}
|
||||
buffer.Resize(static_cast<std::size_t>(recvAmount));
|
||||
return static_cast<std::size_t>(recvAmount);
|
||||
}
|
||||
|
||||
} // ns network
|
||||
|
||||
@@ -192,7 +192,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const {
|
||||
game::ChunkCoord coords = pair.first;
|
||||
game::ChunkPtr chunk = pair.second;
|
||||
|
||||
data << coords.first << coords.second << (std::uint64_t)chunk->palette.size();
|
||||
data << coords.x << coords.y << (std::uint64_t)chunk->palette.size();
|
||||
|
||||
std::size_t bufferSize = data.GetSize();
|
||||
data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type));
|
||||
@@ -200,7 +200,7 @@ DataBuffer WorldDataPacket::SerializeCustom() const {
|
||||
|
||||
std::uint8_t bitsPerTile = countBits(chunk->palette.size());
|
||||
|
||||
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
|
||||
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
|
||||
|
||||
@@ -235,7 +235,7 @@ DataBuffer WorldDataPacket::Serialize() const {
|
||||
game::ChunkCoord coords = pair.first;
|
||||
game::ChunkPtr chunk = pair.second;
|
||||
|
||||
data << coords.first << coords.second << (std::uint64_t)chunk->palette.size();
|
||||
data << coords.x << coords.y << (std::uint64_t)chunk->palette.size();
|
||||
|
||||
std::size_t bufferSize = data.GetSize();
|
||||
data.Resize(data.GetSize() + chunk->palette.size() * sizeof(game::ChunkPalette::value_type));
|
||||
@@ -243,7 +243,7 @@ DataBuffer WorldDataPacket::Serialize() const {
|
||||
|
||||
std::uint8_t bitsPerTile = countBits(chunk->palette.size());
|
||||
|
||||
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
|
||||
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
|
||||
|
||||
@@ -278,7 +278,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
|
||||
for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) {
|
||||
game::ChunkPtr chunk = std::make_shared<game::Chunk>();
|
||||
|
||||
game::ChunkCoord::first_type chunkX, chunkY;
|
||||
decltype(game::ChunkCoord::x) chunkX, chunkY;
|
||||
data >> chunkX >> chunkY;
|
||||
|
||||
std::uint64_t chunkPaletteSize;
|
||||
@@ -294,7 +294,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
|
||||
std::uint8_t bitsPerTile = countBits(chunkPaletteSize);
|
||||
|
||||
// A bitmask that contains bitsPerTile set bits
|
||||
game::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||
|
||||
ChunkPackedData chunkData(game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
|
||||
|
||||
@@ -306,7 +306,7 @@ void WorldDataPacket::Deserialize(DataBuffer& data) {
|
||||
int startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG;
|
||||
int endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG;
|
||||
|
||||
game::ChunkData::value_type value;
|
||||
game::Chunk::ChunkData::value_type value;
|
||||
if (startLong == endLong) {
|
||||
value = (chunkData[startLong] >> startOffset);
|
||||
} else {
|
||||
|
||||
@@ -56,8 +56,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
|
||||
const td::game::ChunkCoord& coords = chunkInfo.first;
|
||||
td::game::ChunkPtr chunk = chunkInfo.second;
|
||||
|
||||
std::int32_t chunkX = coords.first * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.second * td::game::Chunk::ChunkHeight;
|
||||
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
|
||||
std::int32_t chunkY = coords.y * td::game::Chunk::ChunkHeight;
|
||||
|
||||
for (int tileY = 0; tileY < td::game::Chunk::ChunkHeight; tileY++) {
|
||||
for (int tileX = 0; tileX < td::game::Chunk::ChunkWidth; tileX++) {
|
||||
@@ -97,8 +97,8 @@ GL::VertexArray loadWorldModel(const td::game::World* world) {
|
||||
|
||||
for (int spawnColor = 0; spawnColor < 2; spawnColor++) {
|
||||
const game::Spawn& spawn = world->getTeam(game::TeamColor(spawnColor)).getSpawn();
|
||||
float fromX = spawn.getTopLeft().getX() , toX = spawn.getBottomRight().getX();
|
||||
float fromY = spawn.getTopLeft().getY() , toY = spawn.getBottomRight().getY();
|
||||
float fromX = spawn.getTopLeft().getX(), toX = spawn.getBottomRight().getX();
|
||||
float fromY = spawn.getTopLeft().getY(), toY = spawn.getBottomRight().getY();
|
||||
|
||||
positions.insert(positions.end(), {
|
||||
fromX, fromY,
|
||||
|
||||
Reference in New Issue
Block a user