Skip to content

Commit

Permalink
Fix network
Browse files Browse the repository at this point in the history
  • Loading branch information
uis246 committed Dec 24, 2020
1 parent cec4498 commit 66c841f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void GameState::Update(double deltaTime) {
}

void GameState::UpdatePacket(std::shared_ptr<PacketCB> ptr) {
switch ((PacketNamePlayCB)ptr->GetPacketId()) {
switch (ptr->GetPacketId()) {
case SpawnObject: {
auto packet = std::static_pointer_cast<PacketSpawnObject>(ptr);
Entity entity = CreateObject(static_cast<ObjectType>(packet->Type));
Expand Down
4 changes: 3 additions & 1 deletion src/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PacketKeepAliveCB>(packet);
auto packetKeepAliveSB = std::make_shared<PacketKeepAliveSB>(packetKeepAlive->KeepAliveId);
Expand Down
15 changes: 8 additions & 7 deletions src/platform/Linux/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/Linux/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
6 changes: 3 additions & 3 deletions src/platform/SDL/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions src/platform/SDL/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 66c841f

Please sign in to comment.