mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-19 22:02:57 +01:00
network send map refactor
This commit is contained in:
@@ -429,7 +429,7 @@ void Network::UpdateClient()
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NETWORK_STATUS_CONNECTED:
|
case SOCKET_STATUS_CONNECTED:
|
||||||
{
|
{
|
||||||
status = NETWORK_STATUS_CONNECTED;
|
status = NETWORK_STATUS_CONNECTED;
|
||||||
server_connection.ResetLastPacketTime();
|
server_connection.ResetLastPacketTime();
|
||||||
@@ -886,56 +886,22 @@ void Network::Server_Send_AUTH(NetworkConnection& connection)
|
|||||||
|
|
||||||
void Network::Server_Send_MAP(NetworkConnection* connection)
|
void Network::Server_Send_MAP(NetworkConnection* connection)
|
||||||
{
|
{
|
||||||
bool RLEState = gUseRLE;
|
|
||||||
gUseRLE = false;
|
|
||||||
FILE* temp = tmpfile();
|
FILE* temp = tmpfile();
|
||||||
if (!temp) {
|
if (!temp) {
|
||||||
log_warning("Failed to create temporary file to save map.");
|
log_warning("Failed to create temporary file to save map.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SDL_RWops* rw = SDL_RWFromFP(temp, SDL_TRUE);
|
SDL_RWops* rw = SDL_RWFromFP(temp, SDL_TRUE);
|
||||||
scenario_save_network(rw);
|
size_t out_size;
|
||||||
gUseRLE = RLEState;
|
unsigned char *header;
|
||||||
int size = (int)SDL_RWtell(rw);
|
header = save_for_network(rw, out_size);
|
||||||
std::vector<uint8> buffer(size);
|
SDL_RWclose(rw);
|
||||||
SDL_RWseek(rw, 0, RW_SEEK_SET);
|
if (header == nullptr) {
|
||||||
if (SDL_RWread(rw, &buffer[0], size, 1) == 0) {
|
connection->SetLastDisconnectReason(STR_MULTIPLAYER_CONNECTION_CLOSED);
|
||||||
log_warning("Failed to read temporary map file into memory.");
|
connection->Socket->Disconnect();
|
||||||
SDL_RWclose(rw);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size_t chunksize = 65000;
|
size_t chunksize = 65000;
|
||||||
size_t out_size = size;
|
|
||||||
unsigned char *compressed = util_zlib_deflate(&buffer[0], size, &out_size);
|
|
||||||
unsigned char *header;
|
|
||||||
if (compressed != NULL)
|
|
||||||
{
|
|
||||||
header = (unsigned char *)_strdup("open2_sv6_zlib");
|
|
||||||
size_t header_len = strlen((char *)header) + 1; // account for null terminator
|
|
||||||
header = (unsigned char *)realloc(header, header_len + out_size);
|
|
||||||
if (header == nullptr) {
|
|
||||||
log_error("Failed to allocate %u bytes.", header_len + out_size);
|
|
||||||
connection->SetLastDisconnectReason(STR_MULTIPLAYER_CONNECTION_CLOSED);
|
|
||||||
connection->Socket->Disconnect();
|
|
||||||
free(compressed);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
memcpy(&header[header_len], compressed, out_size);
|
|
||||||
out_size += header_len;
|
|
||||||
free(compressed);
|
|
||||||
log_verbose("Sending map of size %u bytes, compressed to %u bytes", size, out_size);
|
|
||||||
} else {
|
|
||||||
log_warning("Failed to compress the data, falling back to non-compressed sv6.");
|
|
||||||
header = (unsigned char *)malloc(size);
|
|
||||||
if (header == nullptr) {
|
|
||||||
log_error("Failed to allocate %u bytes.", size);
|
|
||||||
connection->SetLastDisconnectReason(STR_MULTIPLAYER_CONNECTION_CLOSED);
|
|
||||||
connection->Socket->Disconnect();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
out_size = size;
|
|
||||||
memcpy(header, &buffer[0], size);
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < out_size; i += chunksize) {
|
for (size_t i = 0; i < out_size; i += chunksize) {
|
||||||
size_t datasize = Math::Min(chunksize, out_size - i);
|
size_t datasize = Math::Min(chunksize, out_size - i);
|
||||||
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
||||||
@@ -948,7 +914,48 @@ void Network::Server_Send_MAP(NetworkConnection* connection)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(header);
|
free(header);
|
||||||
SDL_RWclose(rw);
|
}
|
||||||
|
|
||||||
|
unsigned char * Network::save_for_network(SDL_RWops *rw_buffer, size_t &out_size) const
|
||||||
|
{
|
||||||
|
unsigned char * header = nullptr;
|
||||||
|
out_size = 0;
|
||||||
|
bool RLEState = gUseRLE;
|
||||||
|
gUseRLE = false;
|
||||||
|
scenario_save_network(rw_buffer);
|
||||||
|
gUseRLE = RLEState;
|
||||||
|
int size = (int)SDL_RWtell(rw_buffer);
|
||||||
|
std::vector<uint8> buffer(size);
|
||||||
|
SDL_RWseek(rw_buffer, 0, RW_SEEK_SET);
|
||||||
|
if (SDL_RWread(rw_buffer, &buffer[0], size, 1) == 0) {
|
||||||
|
log_warning("Failed to read temporary map file into memory.");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
unsigned char *compressed = util_zlib_deflate(&buffer[0], size, &out_size);
|
||||||
|
if (compressed != NULL)
|
||||||
|
{
|
||||||
|
header = (unsigned char *)_strdup("open2_sv6_zlib");
|
||||||
|
size_t header_len = strlen((char *)header) + 1; // account for null terminator
|
||||||
|
header = (unsigned char *)realloc(header, header_len + out_size);
|
||||||
|
if (header == nullptr) {
|
||||||
|
log_error("Failed to allocate %u bytes.", header_len + out_size);
|
||||||
|
} else {
|
||||||
|
memcpy(&header[header_len], compressed, out_size);
|
||||||
|
out_size += header_len;
|
||||||
|
log_verbose("Sending map of size %u bytes, compressed to %u bytes", size, out_size);
|
||||||
|
}
|
||||||
|
free(compressed);
|
||||||
|
} else {
|
||||||
|
log_warning("Failed to compress the data, falling back to non-compressed sv6.");
|
||||||
|
header = (unsigned char *)malloc(size);
|
||||||
|
if (header == nullptr) {
|
||||||
|
log_error("Failed to allocate %u bytes.", size);
|
||||||
|
} else {
|
||||||
|
out_size = size;
|
||||||
|
memcpy(header, &buffer[0], size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return header;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::Client_Send_CHAT(const char* text)
|
void Network::Client_Send_CHAT(const char* text)
|
||||||
|
|||||||
@@ -236,6 +236,8 @@ private:
|
|||||||
void Client_Handle_EVENT(NetworkConnection& connection, NetworkPacket& packet);
|
void Client_Handle_EVENT(NetworkConnection& connection, NetworkPacket& packet);
|
||||||
void Client_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
void Client_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
||||||
void Server_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
void Server_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
||||||
|
|
||||||
|
unsigned char * save_for_network(SDL_RWops *buffer, size_t &out_size) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace Convert
|
namespace Convert
|
||||||
|
|||||||
Reference in New Issue
Block a user