Merge branch 'master' into jgrpp

# Conflicts:
#	.github/workflows/release-linux.yml
#	.github/workflows/release-macos.yml
#	src/industry_cmd.cpp
#	src/industry_cmd.h
#	src/network/core/http_curl.cpp
#	src/network/core/tcp_http.cpp
#	src/network/core/tcp_http.h
#	src/network/network_content.h
#	src/script/api/script_goal.cpp
#	src/script/api/script_industry.cpp
#	src/script/api/script_league.cpp
#	src/script/api/script_story_page.cpp
#	src/script/api/script_town.cpp
#	src/train.h
#	src/train_cmd.cpp
This commit is contained in:
Jonathan G Rennison
2023-03-04 01:11:51 +00:00
77 changed files with 1197 additions and 761 deletions

View File

@@ -317,13 +317,6 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uin
{
bytes = 0;
#ifdef __EMSCRIPTEN__
/* Emscripten is loaded via an HTTPS connection. As such, it is very
* difficult to make HTTP connections. So always use the TCP method of
* downloading content. */
fallback = true;
#endif
ContentIDList content;
for (const ContentInfo *ci : this->infos) {
if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
@@ -337,6 +330,8 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uin
/* If there's nothing to download, do nothing. */
if (files == 0) return;
this->isCancelled = false;
if (_settings_client.network.no_http_content_downloads || fallback) {
this->DownloadSelectedContentFallback(content);
} else {
@@ -350,25 +345,14 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uin
*/
void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
{
uint count = (uint)content.size();
/* Allocate memory for the whole request.
* Requests are "id\nid\n..." (as strings), so assume the maximum ID,
* which is uint32 so 10 characters long. Then the newlines and
* multiply that all with the count and then add the '\0'. */
uint bytes = (10 + 1) * count + 1;
char *content_request = MallocT<char>(bytes);
const char *lastof = content_request + bytes - 1;
char *p = content_request;
std::string content_request;
for (const ContentID &id : content) {
p += seprintf(p, lastof, "%d\n", id);
content_request += std::to_string(id) + "\n";
}
this->http_response_index = -1;
new NetworkHTTPContentConnecter(NetworkContentMirrorConnectionString(), this, NETWORK_CONTENT_MIRROR_URL, content_request);
/* NetworkHTTPContentConnecter takes over freeing of content_request! */
NetworkHTTPSocketHandler::Connect(NetworkContentMirrorUriString(), this, content_request);
}
/**
@@ -598,25 +582,31 @@ void ClientNetworkContentSocketHandler::AfterDownload()
}
}
bool ClientNetworkContentSocketHandler::IsCancelled() const
{
return this->isCancelled;
}
/* Also called to just clean up the mess. */
void ClientNetworkContentSocketHandler::OnFailure()
{
/* If we fail, download the rest via the 'old' system. */
uint files, bytes;
this->DownloadSelectedContent(files, bytes, true);
this->http_response.clear();
this->http_response.shrink_to_fit();
this->http_response_index = -2;
if (this->curFile != nullptr) {
/* Revert the download progress when we are going for the old system. */
long size = ftell(this->curFile);
if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
this->OnDownloadProgress(this->curInfo, -1);
fclose(this->curFile);
this->curFile = nullptr;
}
/* If we fail, download the rest via the 'old' system. */
if (!this->isCancelled) {
uint files, bytes;
this->DownloadSelectedContent(files, bytes, true);
}
}
void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
@@ -752,7 +742,8 @@ ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
http_response_index(-2),
curFile(nullptr),
curInfo(nullptr),
isConnecting(false)
isConnecting(false),
isCancelled(false)
{
this->lastActivity = std::chrono::steady_clock::now();
}
@@ -798,7 +789,10 @@ public:
void ClientNetworkContentSocketHandler::Connect()
{
if (this->sock != INVALID_SOCKET || this->isConnecting) return;
this->isCancelled = false;
this->isConnecting = true;
new NetworkContentConnecter(NetworkContentServerConnectionString());
}
@@ -807,6 +801,7 @@ void ClientNetworkContentSocketHandler::Connect()
*/
NetworkRecvStatus ClientNetworkContentSocketHandler::CloseConnection(bool error)
{
this->isCancelled = true;
NetworkContentSocketHandler::CloseConnection();
if (this->sock == INVALID_SOCKET) return NETWORK_RECV_STATUS_OKAY;