diff --git a/src/GameState.cpp b/src/GameState.cpp index 87ccef23..ec70307d 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -84,7 +84,7 @@ void GameState::Update(double deltaTime) { } void GameState::UpdatePacket(std::shared_ptr ptr) { - switch ((PacketNamePlayCB)ptr->GetPacketId()) { + switch (ptr->GetPacketId()) { case SpawnObject: { auto packet = std::static_pointer_cast(ptr); Entity entity = CreateObject(static_cast(packet->Type)); diff --git a/src/Network.cpp b/src/Network.cpp index 89b67f17..a5304037 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -118,16 +118,18 @@ void Network::SendPacket(PacketSB &packet, int compressionThreshold, bool more) //Send compressed compressed < uncompressed if (compressed_len && compressed_len + header_size < len) {//This is wrong, I know. Let's try to avoid server decompression overhead. + //Write header compressed.WriteVarInt(compressed_len + VarIntLen(len)); compressed.WriteVarInt(len); socket->SendData(compressed.buffer + 10 - header_size, header_size + compressed_len, more); return; } - //Send uncompressed otherwise } + //Send uncompressed if compressed not sent socket->SendData(buffer.buffer, buffer.size, more); } else { + //Compression disabled. Just send StreamWOBuffer buffer(len+VarIntLen(len)); buffer.WriteVarInt(len); buffer.WriteVarInt(packet.GetPacketId()); diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 8789c46b..2f6cb4c4 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -94,7 +94,7 @@ void NetworkClient::ExecNs() { if (packet->GetPacketId() != PacketNamePlayCB::KeepAliveCB) { PUSH_EVENT("ReceivedPacket", packet); } - else { + else {//Send keepalive back timeOfLastKeepAlivePacket = std::chrono::steady_clock::now(); auto packetKeepAlive = std::static_pointer_cast(packet); auto packetKeepAliveSB = std::make_shared(packetKeepAlive->KeepAliveId); diff --git a/src/platform/Linux/Socket.cpp b/src/platform/Linux/Socket.cpp index 2fc2dcb3..2662dd4d 100644 --- a/src/platform/Linux/Socket.cpp +++ b/src/platform/Linux/Socket.cpp @@ -57,7 +57,7 @@ Socket::Socket(std::string &address, uint16_t port) { setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &ka_timeout, sizeof(ka_timeout)); } -void Socket::Connect(unsigned char *buffPtr, size_t buffLen) { +void Socket::Connect(uint8_t *buffPtr, size_t buffLen) { int result; result = sendto(sock, buffPtr, buffLen, MSG_FASTOPEN | MSG_NOSIGNAL, ai->ai_addr, ai->ai_addrlen); @@ -89,22 +89,23 @@ Socket::~Socket() noexcept { } } -void Socket::ReadData(unsigned char *buffPtr, size_t buffLen) { +void Socket::ReadData(uint8_t *buffPtr, size_t buffLen) { int result; size_t totalReceived = 0; - do { - result = recv(sock, buffPtr, buffLen, MSG_WAITALL | MSG_NOSIGNAL); + while (totalReceived < buffLen) { + result = recv(sock, buffPtr + totalReceived, buffLen - totalReceived, MSG_WAITALL | MSG_NOSIGNAL); if (result == -1) { if(errno == EINTR) continue; else throw std::runtime_error("Data receiving failed: " + std::string(std::strerror(errno))); - } + } else if (result == 0) + throw std::runtime_error("Connection closed by server"); totalReceived += result; - } while (totalReceived < buffLen); + } } -void Socket::SendData(unsigned char *buffPtr, size_t buffLen, bool more) { +void Socket::SendData(uint8_t *buffPtr, size_t buffLen, bool more) { int result; result = send(sock, buffPtr, buffLen, MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : 0)); if (result == -1) { diff --git a/src/platform/Linux/Socket.hpp b/src/platform/Linux/Socket.hpp index a2b31794..ea10a762 100644 --- a/src/platform/Linux/Socket.hpp +++ b/src/platform/Linux/Socket.hpp @@ -12,8 +12,8 @@ class Socket final { Socket(std::string &addr, uint16_t port); ~Socket() noexcept; - void ReadData(unsigned char *buffPtr, size_t buffLen); - void SendData(unsigned char *buffPtr, size_t buffLen, bool more = false); + void ReadData(uint8_t *buffPtr, size_t buffLen); + void SendData(uint8_t *buffPtr, size_t buffLen, bool more = false); - void Connect(unsigned char *buffPtr, size_t buffLen); + void Connect(uint8_t *buffPtr, size_t buffLen); }; diff --git a/src/platform/SDL/Socket.cpp b/src/platform/SDL/Socket.cpp index 60497d84..414a6ce2 100644 --- a/src/platform/SDL/Socket.cpp +++ b/src/platform/SDL/Socket.cpp @@ -16,7 +16,7 @@ Socket::~Socket() noexcept { SDLNet_Quit(); } -void Socket::ReadData(unsigned char *buffPtr, size_t buffLen) { +void Socket::ReadData(uint8_t *buffPtr, size_t buffLen) { size_t totalReceived = 0; while (buffLen > totalReceived) { size_t received = SDLNet_TCP_Recv(socket, buffPtr + totalReceived, buffLen - totalReceived); @@ -26,7 +26,7 @@ void Socket::ReadData(unsigned char *buffPtr, size_t buffLen) { } } -void Socket::SendData(unsigned char *buffPtr, size_t buffLen, bool more) { +void Socket::SendData(uint8_t *buffPtr, size_t buffLen, bool more) { if (more || !buffer.empty()) { std::copy(buffPtr, buffPtr + buffLen, std::back_inserter(buffer)); if (!more) @@ -37,7 +37,7 @@ void Socket::SendData(unsigned char *buffPtr, size_t buffLen, bool more) { } } -void Socket::Connect(unsigned char *buffPtr, size_t buffLen) { +void Socket::Connect(uint8_t *buffPtr, size_t buffLen) { socket = SDLNet_TCP_Open(&server); if (!socket) LOG(WARNING) << "Connection failed: " << std::string(SDLNet_GetError()); diff --git a/src/platform/SDL/Socket.hpp b/src/platform/SDL/Socket.hpp index f7f40b2d..96537ff7 100644 --- a/src/platform/SDL/Socket.hpp +++ b/src/platform/SDL/Socket.hpp @@ -17,9 +17,9 @@ class Socket final { Socket(std::string &addr, uint16_t port); ~Socket() noexcept; - void ReadData(unsigned char *buffPtr, size_t buffLen); - void SendData(unsigned char *buffPtr, size_t buffLen, bool more = false); + void ReadData(uint8_t *buffPtr, size_t buffLen); + void SendData(uint8_t *buffPtr, size_t buffLen, bool more = false); - void Connect(unsigned char *buffPtr, size_t buffLen = 0); + void Connect(uint8_t *buffPtr, size_t buffLen); };