2009-01-17 16:53:32 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/** @file network_content.cpp Content sending/receiving part of the network protocol. */
|
|
|
|
|
|
|
|
#if defined(ENABLE_NETWORK)
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../rev.h"
|
|
|
|
#include "../ai/ai.hpp"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "../gui.h"
|
2009-08-09 16:54:03 +00:00
|
|
|
#include "../base_media_base.h"
|
2010-02-03 18:42:23 +00:00
|
|
|
#include "../settings_type.h"
|
2009-01-17 16:53:32 +00:00
|
|
|
#include "network_content.h"
|
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-06 19:33:45 +00:00
|
|
|
extern bool HasScenario(const ContentInfo *ci, bool md5sum);
|
2009-01-20 16:51:55 +00:00
|
|
|
ClientNetworkContentSocketHandler _network_content_client;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/** Wrapper function for the HasProc */
|
|
|
|
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
|
|
|
|
{
|
2010-10-17 12:12:13 +00:00
|
|
|
return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? ci->md5sum : NULL) != NULL;
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a function piece of content is locally known.
|
|
|
|
* Matches on the unique ID and possibly the MD5 checksum.
|
|
|
|
* @param ci the content info to search for
|
|
|
|
* @param md5sum also match the MD5 checksum?
|
|
|
|
* @return true iff it's known
|
|
|
|
*/
|
|
|
|
typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
|
|
|
|
|
|
|
|
DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_INFO)
|
|
|
|
{
|
|
|
|
ContentInfo *ci = new ContentInfo();
|
|
|
|
ci->type = (ContentType)p->Recv_uint8();
|
|
|
|
ci->id = (ContentID)p->Recv_uint32();
|
|
|
|
ci->filesize = p->Recv_uint32();
|
|
|
|
|
|
|
|
p->Recv_string(ci->name, lengthof(ci->name));
|
|
|
|
p->Recv_string(ci->version, lengthof(ci->name));
|
|
|
|
p->Recv_string(ci->url, lengthof(ci->url));
|
2009-01-18 13:12:57 +00:00
|
|
|
p->Recv_string(ci->description, lengthof(ci->description), true);
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
ci->unique_id = p->Recv_uint32();
|
|
|
|
for (uint j = 0; j < sizeof(ci->md5sum); j++) {
|
|
|
|
ci->md5sum[j] = p->Recv_uint8();
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->dependency_count = p->Recv_uint8();
|
|
|
|
ci->dependencies = MallocT<ContentID>(ci->dependency_count);
|
|
|
|
for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
|
|
|
|
|
|
|
|
ci->tag_count = p->Recv_uint8();
|
|
|
|
ci->tags = MallocT<char[32]>(ci->tag_count);
|
|
|
|
for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
|
|
|
|
|
|
|
|
if (!ci->IsValid()) {
|
|
|
|
delete ci;
|
|
|
|
this->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the appropriate check function */
|
|
|
|
HasProc proc = NULL;
|
|
|
|
switch (ci->type) {
|
|
|
|
case CONTENT_TYPE_NEWGRF:
|
|
|
|
proc = HasGRFConfig;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_TYPE_BASE_GRAPHICS:
|
2009-08-09 16:54:03 +00:00
|
|
|
proc = BaseGraphics::HasSet;
|
2009-01-17 16:53:32 +00:00
|
|
|
break;
|
|
|
|
|
2009-12-26 09:38:41 +00:00
|
|
|
case CONTENT_TYPE_BASE_MUSIC:
|
|
|
|
proc = BaseMusic::HasSet;
|
|
|
|
break;
|
|
|
|
|
2009-08-09 19:50:44 +00:00
|
|
|
case CONTENT_TYPE_BASE_SOUNDS:
|
|
|
|
proc = BaseSounds::HasSet;
|
|
|
|
break;
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
case CONTENT_TYPE_AI:
|
|
|
|
case CONTENT_TYPE_AI_LIBRARY:
|
|
|
|
proc = AI::HasAI; break;
|
|
|
|
break;
|
|
|
|
|
2009-03-06 19:33:45 +00:00
|
|
|
case CONTENT_TYPE_SCENARIO:
|
|
|
|
case CONTENT_TYPE_HEIGHTMAP:
|
|
|
|
proc = HasScenario;
|
|
|
|
break;
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proc != NULL) {
|
|
|
|
if (proc(ci, true)) {
|
|
|
|
ci->state = ContentInfo::ALREADY_HERE;
|
|
|
|
} else {
|
|
|
|
ci->state = ContentInfo::UNSELECTED;
|
2009-01-20 21:05:13 +00:00
|
|
|
if (proc(ci, false)) ci->upgrade = true;
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ci->state = ContentInfo::UNSELECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Something we don't have and has filesize 0 does not exist in te system */
|
|
|
|
if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
/* Do we already have a stub for this? */
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
ContentInfo *ici = *iter;
|
|
|
|
if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
|
|
|
|
memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
|
|
|
|
/* Preserve the name if possible */
|
|
|
|
if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
|
2009-01-20 21:05:13 +00:00
|
|
|
if (ici->IsSelected()) ci->state = ici->state;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
2010-07-05 21:07:31 +00:00
|
|
|
/*
|
|
|
|
* As ici might be selected by the content window we cannot delete that.
|
|
|
|
* However, we want to keep most of the values of ci, except the values
|
2010-07-08 18:38:38 +00:00
|
|
|
* we (just) already preserved.
|
|
|
|
* So transfer data and ownership of allocated memory from ci to ici.
|
2010-07-05 21:07:31 +00:00
|
|
|
*/
|
2010-07-08 18:38:38 +00:00
|
|
|
ici->TransferFrom(ci);
|
2010-07-05 21:07:31 +00:00
|
|
|
delete ci;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
2010-07-08 18:38:38 +00:00
|
|
|
this->OnReceiveContentInfo(ici);
|
2009-01-20 16:51:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Missing content info? Don't list it */
|
|
|
|
if (ci->filesize == 0) {
|
|
|
|
delete ci;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*this->infos.Append() = ci;
|
|
|
|
|
|
|
|
/* Incoming data means that we might need to reconsider dependencies */
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
this->CheckDependencyState(*iter);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
this->OnReceiveContentInfo(ci);
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
|
|
|
|
{
|
2009-01-20 21:05:13 +00:00
|
|
|
if (type == CONTENT_TYPE_END) {
|
|
|
|
this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
|
2009-12-26 09:38:41 +00:00
|
|
|
this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
|
2009-08-09 19:50:44 +00:00
|
|
|
this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
|
2009-03-06 19:33:45 +00:00
|
|
|
this->RequestContentList(CONTENT_TYPE_SCENARIO);
|
|
|
|
this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
|
2010-02-10 16:24:05 +00:00
|
|
|
#ifdef ENABLE_AI
|
2009-01-20 21:05:13 +00:00
|
|
|
this->RequestContentList(CONTENT_TYPE_AI);
|
|
|
|
this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
|
2010-02-10 16:24:05 +00:00
|
|
|
#endif /* ENABLE_AI */
|
|
|
|
this->RequestContentList(CONTENT_TYPE_NEWGRF);
|
2009-01-20 21:05:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
this->Connect();
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
|
|
|
|
p->Send_uint8 ((byte)type);
|
|
|
|
p->Send_uint32(_openttd_newgrf_version);
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
|
|
|
|
{
|
2009-01-20 16:51:55 +00:00
|
|
|
this->Connect();
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
while (count > 0) {
|
|
|
|
/* We can "only" send a limited number of IDs in a single packet.
|
|
|
|
* A packet begins with the packet size and a byte for the type.
|
|
|
|
* Then this packet adds a byte for the content type and a uint16
|
|
|
|
* for the count in this packet. The rest of the packet can be
|
|
|
|
* used for the IDs. */
|
|
|
|
uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
|
|
|
|
|
|
|
|
Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID);
|
|
|
|
p->Send_uint16(p_count);
|
|
|
|
|
|
|
|
for (uint i = 0; i < p_count; i++) {
|
|
|
|
p->Send_uint32(content_ids[i]);
|
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2009-01-17 16:53:32 +00:00
|
|
|
count -= p_count;
|
2009-05-19 21:25:20 +00:00
|
|
|
content_ids += p_count;
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
|
|
|
|
{
|
|
|
|
if (cv == NULL) return;
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
this->Connect();
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/* 20 is sizeof(uint32) + sizeof(md5sum (byte[16])) */
|
|
|
|
assert(cv->Length() < 255);
|
|
|
|
assert(cv->Length() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) / (send_md5sum ? 20 : sizeof(uint32)));
|
|
|
|
|
|
|
|
Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID);
|
|
|
|
p->Send_uint8(cv->Length());
|
|
|
|
|
|
|
|
for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
|
|
|
|
const ContentInfo *ci = *iter;
|
|
|
|
p->Send_uint8((byte)ci->type);
|
|
|
|
p->Send_uint32(ci->unique_id);
|
|
|
|
if (!send_md5sum) continue;
|
|
|
|
|
|
|
|
for (uint j = 0; j < sizeof(ci->md5sum); j++) {
|
|
|
|
p->Send_uint8(ci->md5sum[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
|
|
|
|
ContentInfo *ci = *iter;
|
|
|
|
bool found = false;
|
|
|
|
for (ContentIterator iter2 = this->infos.Begin(); iter2 != this->infos.End(); iter2++) {
|
2009-01-23 10:20:29 +00:00
|
|
|
ContentInfo *ci2 = *iter2;
|
2009-01-20 16:51:55 +00:00
|
|
|
if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
|
|
|
|
(!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
*this->infos.Append() = ci;
|
|
|
|
} else {
|
|
|
|
delete ci;
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2009-01-20 16:51:55 +00:00
|
|
|
bytes = 0;
|
|
|
|
|
2010-02-03 17:12:19 +00:00
|
|
|
ContentIDList content;
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
2009-01-20 16:51:55 +00:00
|
|
|
const ContentInfo *ci = *iter;
|
2009-08-02 13:58:11 +00:00
|
|
|
if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
2010-02-03 17:12:19 +00:00
|
|
|
*content.Append() = ci->id;
|
2009-01-20 16:51:55 +00:00
|
|
|
bytes += ci->filesize;
|
|
|
|
}
|
|
|
|
|
2010-02-03 17:12:19 +00:00
|
|
|
files = content.Length();
|
|
|
|
|
|
|
|
/* If there's nothing to download, do nothing. */
|
|
|
|
if (files == 0) return;
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
if (_settings_client.network.no_http_content_downloads || fallback) {
|
|
|
|
this->DownloadSelectedContentFallback(content);
|
|
|
|
} else {
|
|
|
|
this->DownloadSelectedContentHTTP(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
|
|
|
|
{
|
|
|
|
uint count = content.Length();
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
for (const ContentID *id = content.Begin(); id != content.End(); id++) {
|
|
|
|
p += seprintf(p, lastof, "%d\n", *id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->http_response_index = -1;
|
|
|
|
|
|
|
|
NetworkAddress address(NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT);
|
|
|
|
new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
|
|
|
|
/* NetworkHTTPContentConnecter takes over freeing of content_request! */
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
|
|
|
|
{
|
|
|
|
uint count = content.Length();
|
|
|
|
const ContentID *content_ids = content.Begin();
|
2009-01-20 16:51:55 +00:00
|
|
|
this->Connect();
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
while (count > 0) {
|
|
|
|
/* We can "only" send a limited number of IDs in a single packet.
|
2009-03-14 18:16:29 +00:00
|
|
|
* A packet begins with the packet size and a byte for the type.
|
|
|
|
* Then this packet adds a uint16 for the count in this packet.
|
|
|
|
* The rest of the packet can be used for the IDs. */
|
2009-01-17 16:53:32 +00:00
|
|
|
uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
|
|
|
|
|
|
|
|
Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT);
|
|
|
|
p->Send_uint16(p_count);
|
|
|
|
|
|
|
|
for (uint i = 0; i < p_count; i++) {
|
|
|
|
p->Send_uint32(content_ids[i]);
|
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPacket(p);
|
2009-01-17 16:53:32 +00:00
|
|
|
count -= p_count;
|
2009-05-19 21:25:20 +00:00
|
|
|
content_ids += p_count;
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the full filename of a piece of content information
|
|
|
|
* @param ci the information to get the filename from
|
|
|
|
* @param compressed should the filename end with .gz?
|
|
|
|
* @return a statically allocated buffer with the filename or
|
|
|
|
* NULL when no filename could be made.
|
|
|
|
*/
|
|
|
|
static char *GetFullFilename(const ContentInfo *ci, bool compressed)
|
|
|
|
{
|
|
|
|
Subdirectory dir;
|
|
|
|
switch (ci->type) {
|
|
|
|
default: return NULL;
|
|
|
|
case CONTENT_TYPE_BASE_GRAPHICS: dir = DATA_DIR; break;
|
2009-12-26 09:38:41 +00:00
|
|
|
case CONTENT_TYPE_BASE_MUSIC: dir = GM_DIR; break;
|
2009-08-09 19:50:44 +00:00
|
|
|
case CONTENT_TYPE_BASE_SOUNDS: dir = DATA_DIR; break;
|
2009-01-17 16:53:32 +00:00
|
|
|
case CONTENT_TYPE_NEWGRF: dir = DATA_DIR; break;
|
|
|
|
case CONTENT_TYPE_AI: dir = AI_DIR; break;
|
|
|
|
case CONTENT_TYPE_AI_LIBRARY: dir = AI_LIBRARY_DIR; break;
|
|
|
|
case CONTENT_TYPE_SCENARIO: dir = SCENARIO_DIR; break;
|
|
|
|
case CONTENT_TYPE_HEIGHTMAP: dir = HEIGHTMAP_DIR; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char buf[MAX_PATH];
|
|
|
|
FioGetFullPath(buf, lengthof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
|
|
|
|
strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gunzip a given file and remove the .gz if successful.
|
|
|
|
* @param ci container with filename
|
|
|
|
* @return true if the gunzip completed
|
|
|
|
*/
|
|
|
|
static bool GunzipFile(const ContentInfo *ci)
|
|
|
|
{
|
|
|
|
#if defined(WITH_ZLIB)
|
|
|
|
bool ret = true;
|
2009-02-21 14:00:35 +00:00
|
|
|
FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
|
|
|
|
gzFile fin = gzdopen(fileno(ftmp), "rb");
|
2009-01-17 16:53:32 +00:00
|
|
|
FILE *fout = fopen(GetFullFilename(ci, false), "wb");
|
|
|
|
|
|
|
|
if (fin == NULL || fout == NULL) {
|
|
|
|
ret = false;
|
2010-04-21 11:42:00 +00:00
|
|
|
} else {
|
|
|
|
byte buff[8192];
|
2011-02-25 21:53:43 +00:00
|
|
|
for (;;) {
|
2010-04-21 11:42:00 +00:00
|
|
|
int read = gzread(fin, buff, sizeof(buff));
|
|
|
|
if (read == 0) {
|
|
|
|
/* If gzread() returns 0, either the end-of-file has been
|
|
|
|
* reached or an underlying read error has occurred.
|
|
|
|
*
|
|
|
|
* gzeof() can't be used, because:
|
|
|
|
* 1.2.5 - it is safe, 1 means 'everything was OK'
|
|
|
|
* 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
|
|
|
|
* 1.2.3.3 - 1 is returned for truncated archive
|
|
|
|
*
|
|
|
|
* So we use gzerror(). When proper end of archive
|
|
|
|
* has been reached, then:
|
|
|
|
* errnum == Z_STREAM_END in 1.2.3.3,
|
|
|
|
* errnum == 0 in 1.2.4 and 1.2.5 */
|
|
|
|
int errnum;
|
|
|
|
gzerror(fin, &errnum);
|
|
|
|
if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
|
|
|
|
/* If gzread() returns -1, there was an error in archive */
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
|
|
|
|
* if (read < sizeof(buff)) break; */
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 02:57:15 +00:00
|
|
|
if (fin != NULL) {
|
|
|
|
/* Closes ftmp too! */
|
|
|
|
gzclose(fin);
|
|
|
|
} else if (ftmp != NULL) {
|
|
|
|
/* In case the gz stream was opened correctly this will
|
|
|
|
* be closed by gzclose. */
|
|
|
|
fclose(ftmp);
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
if (fout != NULL) fclose(fout);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
NOT_REACHED();
|
|
|
|
#endif /* defined(WITH_ZLIB) */
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
|
|
|
|
{
|
|
|
|
if (this->curFile == NULL) {
|
2010-02-03 17:15:35 +00:00
|
|
|
delete this->curInfo;
|
2009-01-17 16:53:32 +00:00
|
|
|
/* When we haven't opened a file this must be our first packet with metadata. */
|
|
|
|
this->curInfo = new ContentInfo;
|
|
|
|
this->curInfo->type = (ContentType)p->Recv_uint8();
|
|
|
|
this->curInfo->id = (ContentID)p->Recv_uint32();
|
|
|
|
this->curInfo->filesize = p->Recv_uint32();
|
|
|
|
p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
|
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
if (!this->BeforeDownload()) {
|
2009-01-17 16:53:32 +00:00
|
|
|
this->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We have a file opened, thus are downloading internal content */
|
|
|
|
size_t toRead = (size_t)(p->size - p->pos);
|
|
|
|
if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
2010-11-25 13:41:30 +00:00
|
|
|
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
|
2009-01-17 16:53:32 +00:00
|
|
|
this->Close();
|
|
|
|
fclose(this->curFile);
|
|
|
|
this->curFile = NULL;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-06 10:11:59 +00:00
|
|
|
this->OnDownloadProgress(this->curInfo, (int)toRead);
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
if (toRead == 0) this->AfterDownload();
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the opening of the file before downloading.
|
|
|
|
* @return false on any error.
|
|
|
|
*/
|
|
|
|
bool ClientNetworkContentSocketHandler::BeforeDownload()
|
|
|
|
{
|
|
|
|
if (!this->curInfo->IsValid()) {
|
2009-01-17 16:53:32 +00:00
|
|
|
delete this->curInfo;
|
|
|
|
this->curInfo = NULL;
|
2010-02-03 17:15:35 +00:00
|
|
|
return false;
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
if (this->curInfo->filesize != 0) {
|
|
|
|
/* The filesize is > 0, so we are going to download it */
|
|
|
|
const char *filename = GetFullFilename(this->curInfo, true);
|
2010-11-25 13:43:09 +00:00
|
|
|
if (filename == NULL || (this->curFile = fopen(filename, "wb")) == NULL) {
|
2010-02-03 17:15:35 +00:00
|
|
|
/* Unless that fails ofcourse... */
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
2010-11-25 13:41:30 +00:00
|
|
|
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
|
2010-02-03 17:15:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
/**
|
|
|
|
* Handle the closing and extracting of a file after
|
|
|
|
* downloading it has been done.
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::AfterDownload()
|
|
|
|
{
|
|
|
|
/* We read nothing; that's our marker for end-of-stream.
|
|
|
|
* Now gunzip the tar and make it known. */
|
|
|
|
fclose(this->curFile);
|
|
|
|
this->curFile = NULL;
|
|
|
|
|
|
|
|
if (GunzipFile(this->curInfo)) {
|
|
|
|
unlink(GetFullFilename(this->curInfo, true));
|
|
|
|
|
2010-05-10 09:37:17 +00:00
|
|
|
TarScanner ts;
|
|
|
|
ts.AddFile(GetFullFilename(this->curInfo, false), 0);
|
2010-02-03 17:15:35 +00:00
|
|
|
|
2010-02-26 00:00:55 +00:00
|
|
|
if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
|
|
|
|
/* Music can't be in a tar. So extract the tar! */
|
|
|
|
ExtractTar(GetFullFilename(this->curInfo, false));
|
|
|
|
unlink(GetFullFilename(this->curInfo, false));
|
|
|
|
}
|
|
|
|
|
2010-02-03 17:15:35 +00:00
|
|
|
this->OnDownloadComplete(this->curInfo->id);
|
|
|
|
} else {
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
|
2010-02-03 17:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
/* 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.Reset();
|
|
|
|
this->http_response_index = -2;
|
|
|
|
|
|
|
|
if (this->curFile != NULL) {
|
2011-03-06 10:11:59 +00:00
|
|
|
/* 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);
|
|
|
|
|
2010-02-03 18:42:23 +00:00
|
|
|
fclose(this->curFile);
|
|
|
|
this->curFile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
|
|
|
|
{
|
|
|
|
assert(data == NULL || length != 0);
|
|
|
|
|
|
|
|
/* Ignore any latent data coming from a connection we closed. */
|
|
|
|
if (this->http_response_index == -2) return;
|
|
|
|
|
|
|
|
if (this->http_response_index == -1) {
|
|
|
|
if (data != NULL) {
|
|
|
|
/* Append the rest of the response. */
|
2010-02-03 21:45:48 +00:00
|
|
|
memcpy(this->http_response.Append((uint)length), data, length);
|
2010-02-03 18:42:23 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* Make sure the response is properly terminated. */
|
|
|
|
*this->http_response.Append() = '\0';
|
|
|
|
|
|
|
|
/* And prepare for receiving the rest of the data. */
|
|
|
|
this->http_response_index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
/* We have data, so write it to the file. */
|
|
|
|
if (fwrite(data, 1, length, this->curFile) != length) {
|
|
|
|
/* Writing failed somehow, let try via the old method. */
|
|
|
|
this->OnFailure();
|
|
|
|
} else {
|
|
|
|
/* Just received the data. */
|
2011-03-06 10:11:59 +00:00
|
|
|
this->OnDownloadProgress(this->curInfo, (int)length);
|
2010-02-03 18:42:23 +00:00
|
|
|
}
|
|
|
|
/* Nothing more to do now. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->curFile != NULL) {
|
|
|
|
/* We've finished downloading a file. */
|
|
|
|
this->AfterDownload();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uint)this->http_response_index >= this->http_response.Length()) {
|
|
|
|
/* It's not a real failure, but if there's
|
|
|
|
* nothing more to download it helps with
|
|
|
|
* cleaning up the stuff we allocated. */
|
|
|
|
this->OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this->curInfo;
|
|
|
|
/* When we haven't opened a file this must be our first packet with metadata. */
|
|
|
|
this->curInfo = new ContentInfo;
|
|
|
|
|
|
|
|
/** Check p for not being null and return calling OnFailure if that's not the case. */
|
2010-02-04 13:35:20 +00:00
|
|
|
#define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
|
2010-02-03 18:42:23 +00:00
|
|
|
/** Check p for not being null and then terminate, or return calling OnFailure. */
|
2010-02-04 13:35:20 +00:00
|
|
|
#define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char *str = this->http_response.Begin() + this->http_response_index;
|
|
|
|
char *p = strchr(str, '\n');
|
|
|
|
check_and_terminate(p);
|
|
|
|
|
|
|
|
/* Update the index for the next one */
|
2010-02-03 21:45:48 +00:00
|
|
|
this->http_response_index += (int)strlen(str) + 1;
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
/* Read the ID */
|
|
|
|
p = strchr(str, ',');
|
|
|
|
check_and_terminate(p);
|
|
|
|
this->curInfo->id = (ContentID)atoi(str);
|
|
|
|
|
|
|
|
/* Read the type */
|
|
|
|
str = p + 1;
|
|
|
|
p = strchr(str, ',');
|
|
|
|
check_and_terminate(p);
|
|
|
|
this->curInfo->type = (ContentType)atoi(str);
|
|
|
|
|
|
|
|
/* Read the file size */
|
|
|
|
str = p + 1;
|
|
|
|
p = strchr(str, ',');
|
|
|
|
check_and_terminate(p);
|
|
|
|
this->curInfo->filesize = atoi(str);
|
|
|
|
|
|
|
|
/* Read the URL */
|
|
|
|
str = p + 1;
|
|
|
|
/* Is it a fallback URL? If so, just continue with the next one. */
|
|
|
|
if (strncmp(str, "ottd", 4) == 0) {
|
|
|
|
if ((uint)this->http_response_index >= this->http_response.Length()) {
|
|
|
|
/* Have we gone through all lines? */
|
|
|
|
this->OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strrchr(str, '/');
|
2010-02-04 13:35:20 +00:00
|
|
|
check_not_null(p);
|
2010-04-10 21:34:21 +00:00
|
|
|
p++; // Start after the '/'
|
2010-02-03 18:42:23 +00:00
|
|
|
|
|
|
|
char tmp[MAX_PATH];
|
|
|
|
if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
|
|
|
|
this->OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Remove the extension from the string. */
|
|
|
|
for (uint i = 0; i < 2; i++) {
|
|
|
|
p = strrchr(tmp, '.');
|
|
|
|
check_and_terminate(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the string, without extension, to the filename. */
|
|
|
|
strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
|
|
|
|
|
|
|
|
/* Request the next file. */
|
|
|
|
if (!this->BeforeDownload()) {
|
|
|
|
this->OnFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkHTTPSocketHandler::Connect(str, this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef check
|
|
|
|
#undef check_and_terminate
|
|
|
|
}
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/**
|
|
|
|
* Create a socket handler with the given socket and (server) address.
|
|
|
|
* @param s the socket to communicate over
|
|
|
|
* @param sin the IP/port of the server
|
|
|
|
*/
|
2009-01-20 16:51:55 +00:00
|
|
|
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
|
2009-04-02 18:35:59 +00:00
|
|
|
NetworkContentSocketHandler(),
|
2010-02-03 18:42:23 +00:00
|
|
|
http_response_index(-2),
|
2009-01-17 16:53:32 +00:00
|
|
|
curFile(NULL),
|
2009-01-20 16:51:55 +00:00
|
|
|
curInfo(NULL),
|
|
|
|
isConnecting(false)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear up the mess ;) */
|
|
|
|
ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
|
|
|
|
{
|
|
|
|
delete this->curInfo;
|
|
|
|
if (this->curFile != NULL) fclose(this->curFile);
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
class NetworkContentConnecter : TCPConnecter {
|
|
|
|
public:
|
|
|
|
NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
virtual void OnFailure()
|
|
|
|
{
|
|
|
|
_network_content_client.isConnecting = false;
|
|
|
|
_network_content_client.OnConnect(false);
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
virtual void OnConnect(SOCKET s)
|
|
|
|
{
|
|
|
|
assert(_network_content_client.sock == INVALID_SOCKET);
|
|
|
|
_network_content_client.isConnecting = false;
|
|
|
|
_network_content_client.sock = s;
|
2009-04-07 18:23:14 +00:00
|
|
|
_network_content_client.Reopen();
|
2009-01-20 16:51:55 +00:00
|
|
|
_network_content_client.OnConnect(true);
|
|
|
|
}
|
|
|
|
};
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
/**
|
|
|
|
* Connect with the content server.
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::Connect()
|
|
|
|
{
|
|
|
|
this->lastActivity = _realtime_tick;
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
if (this->sock != INVALID_SOCKET || this->isConnecting) return;
|
|
|
|
this->isConnecting = true;
|
2009-04-03 17:20:57 +00:00
|
|
|
new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-20 16:51:55 +00:00
|
|
|
* Disconnect from the content server.
|
2009-01-17 16:53:32 +00:00
|
|
|
*/
|
2009-01-20 21:05:13 +00:00
|
|
|
void ClientNetworkContentSocketHandler::Close()
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2009-01-20 16:51:55 +00:00
|
|
|
if (this->sock == INVALID_SOCKET) return;
|
2009-01-20 21:05:13 +00:00
|
|
|
NetworkContentSocketHandler::Close();
|
|
|
|
|
|
|
|
this->OnDisconnect();
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether we received/can send some data from/to the content server and
|
|
|
|
* when that's the case handle it appropriately
|
|
|
|
*/
|
2009-01-20 16:51:55 +00:00
|
|
|
void ClientNetworkContentSocketHandler::SendReceive()
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
2009-01-20 16:51:55 +00:00
|
|
|
if (this->sock == INVALID_SOCKET || this->isConnecting) return;
|
|
|
|
|
|
|
|
if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
|
|
|
|
this->Close();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
2010-10-15 19:33:08 +00:00
|
|
|
if (this->CanSendReceive()) {
|
2010-11-30 13:38:46 +00:00
|
|
|
this->ReceivePackets();
|
2009-02-25 17:00:59 +00:00
|
|
|
this->lastActivity = _realtime_tick;
|
|
|
|
}
|
|
|
|
|
2010-11-30 13:38:46 +00:00
|
|
|
this->SendPackets();
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download information of a given Content ID if not already tried
|
|
|
|
* @param cid the ID to try
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
|
|
|
|
{
|
|
|
|
/* When we tried to download it already, don't try again */
|
|
|
|
if (this->requested.Contains(cid)) return;
|
|
|
|
|
|
|
|
*this->requested.Append() = cid;
|
|
|
|
assert(this->requested.Contains(cid));
|
|
|
|
this->RequestContentList(1, &cid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content info based on a ContentID
|
|
|
|
* @param cid the ContentID to search for
|
|
|
|
* @return the ContentInfo or NULL if not found
|
|
|
|
*/
|
|
|
|
ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
|
|
|
|
{
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
ContentInfo *ci = *iter;
|
|
|
|
if (ci->id == cid) return ci;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a specific content id.
|
|
|
|
* @param cid the content ID to select
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::Select(ContentID cid)
|
|
|
|
{
|
|
|
|
ContentInfo *ci = this->GetContent(cid);
|
2009-01-20 21:05:13 +00:00
|
|
|
if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
ci->state = ContentInfo::SELECTED;
|
|
|
|
this->CheckDependencyState(ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unselect a specific content id.
|
|
|
|
* @param cid the content ID to deselect
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
|
|
|
|
{
|
|
|
|
ContentInfo *ci = this->GetContent(cid);
|
2009-01-20 21:05:13 +00:00
|
|
|
if (ci == NULL || !ci->IsSelected()) return;
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
ci->state = ContentInfo::UNSELECTED;
|
|
|
|
this->CheckDependencyState(ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Select everything we can select */
|
|
|
|
void ClientNetworkContentSocketHandler::SelectAll()
|
|
|
|
{
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
ContentInfo *ci = *iter;
|
|
|
|
if (ci->state == ContentInfo::UNSELECTED) {
|
|
|
|
ci->state = ContentInfo::SELECTED;
|
|
|
|
this->CheckDependencyState(ci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Select everything that's an update for something we've got */
|
2009-01-20 21:05:13 +00:00
|
|
|
void ClientNetworkContentSocketHandler::SelectUpgrade()
|
2009-01-20 16:51:55 +00:00
|
|
|
{
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
ContentInfo *ci = *iter;
|
2009-01-20 21:05:13 +00:00
|
|
|
if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
|
2009-01-20 16:51:55 +00:00
|
|
|
ci->state = ContentInfo::SELECTED;
|
|
|
|
this->CheckDependencyState(ci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Unselect everything that we've not downloaded so far. */
|
|
|
|
void ClientNetworkContentSocketHandler::UnselectAll()
|
|
|
|
{
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
ContentInfo *ci = *iter;
|
2009-08-21 08:41:54 +00:00
|
|
|
if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 14:12:47 +00:00
|
|
|
/** Toggle the state of a content info and check its dependencies */
|
2009-01-20 16:51:55 +00:00
|
|
|
void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
|
|
|
|
{
|
|
|
|
switch (ci->state) {
|
|
|
|
case ContentInfo::SELECTED:
|
|
|
|
case ContentInfo::AUTOSELECTED:
|
|
|
|
this->Unselect(ci->id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ContentInfo::UNSELECTED:
|
|
|
|
this->Select(ci->id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse lookup the dependencies of (direct) parents over a given child.
|
|
|
|
* @param parents list to store all parents in (is not cleared)
|
|
|
|
* @param child the child to search the parents' dependencies for
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
|
|
|
|
{
|
|
|
|
for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
|
|
|
|
const ContentInfo *ci = *iter;
|
|
|
|
if (ci == child) continue;
|
|
|
|
|
|
|
|
for (uint i = 0; i < ci->dependency_count; i++) {
|
|
|
|
if (ci->dependencies[i] == child->id) {
|
|
|
|
*parents.Append() = ci;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse lookup the dependencies of all parents over a given child.
|
|
|
|
* @param tree list to store all parents in (is not cleared)
|
|
|
|
* @param child the child to search the parents' dependencies for
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
|
|
|
|
{
|
|
|
|
*tree.Append() = child;
|
|
|
|
|
2011-01-23 11:20:55 +00:00
|
|
|
/* First find all direct parents. We can't use the "normal" iterator as
|
|
|
|
* we are including stuff into the vector and as such the vector's data
|
|
|
|
* store can be reallocated (and thus move), which means out iterating
|
|
|
|
* pointer gets invalid. So fall back to the indices. */
|
|
|
|
for (uint i = 0; i < tree.Length(); i++) {
|
2009-01-20 16:51:55 +00:00
|
|
|
ConstContentVector parents;
|
2011-01-23 11:20:55 +00:00
|
|
|
this->ReverseLookupDependency(parents, tree[i]);
|
2009-01-20 16:51:55 +00:00
|
|
|
|
|
|
|
for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
|
|
|
|
tree.Include(*piter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the dependencies (recursively) of this content info
|
|
|
|
* @param ci the content info to check the dependencies of
|
|
|
|
*/
|
|
|
|
void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
|
|
|
|
{
|
|
|
|
if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
|
|
|
|
/* Selection is easy; just walk all children and set the
|
|
|
|
* autoselected state. That way we can see what we automatically
|
|
|
|
* selected and thus can unselect when a dependency is removed. */
|
|
|
|
for (uint i = 0; i < ci->dependency_count; i++) {
|
|
|
|
ContentInfo *c = this->GetContent(ci->dependencies[i]);
|
|
|
|
if (c == NULL) {
|
|
|
|
this->DownloadContentInfo(ci->dependencies[i]);
|
|
|
|
} else if (c->state == ContentInfo::UNSELECTED) {
|
|
|
|
c->state = ContentInfo::AUTOSELECTED;
|
|
|
|
this->CheckDependencyState(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->state != ContentInfo::UNSELECTED) return;
|
|
|
|
|
|
|
|
/* For unselection we need to find the parents of us. We need to
|
|
|
|
* unselect them. After that we unselect all children that we
|
|
|
|
* depend on and are not used as dependency for us, but only when
|
|
|
|
* we automatically selected them. */
|
|
|
|
ConstContentVector parents;
|
|
|
|
this->ReverseLookupDependency(parents, ci);
|
|
|
|
for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
|
|
|
|
const ContentInfo *c = *iter;
|
|
|
|
if (!c->IsSelected()) continue;
|
|
|
|
|
|
|
|
this->Unselect(c->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < ci->dependency_count; i++) {
|
|
|
|
const ContentInfo *c = this->GetContent(ci->dependencies[i]);
|
|
|
|
if (c == NULL) {
|
|
|
|
DownloadContentInfo(ci->dependencies[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c->state != ContentInfo::AUTOSELECTED) continue;
|
|
|
|
|
|
|
|
/* Only unselect when WE are the only parent. */
|
|
|
|
parents.Clear();
|
|
|
|
this->ReverseLookupDependency(parents, c);
|
|
|
|
|
|
|
|
/* First check whether anything depends on us */
|
|
|
|
int sel_count = 0;
|
|
|
|
bool force_selection = false;
|
|
|
|
for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
|
|
|
|
if ((*iter)->IsSelected()) sel_count++;
|
|
|
|
if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
|
|
|
|
}
|
|
|
|
if (sel_count == 0) {
|
|
|
|
/* Nothing depends on us */
|
|
|
|
this->Unselect(c->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Something manually selected depends directly on us */
|
|
|
|
if (force_selection) continue;
|
|
|
|
|
|
|
|
/* "Flood" search to find all items in the dependency graph*/
|
|
|
|
parents.Clear();
|
|
|
|
this->ReverseLookupTreeDependency(parents, c);
|
|
|
|
|
|
|
|
/* Is there anything that is "force" selected?, if so... we're done. */
|
|
|
|
for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
|
|
|
|
if ((*iter)->state != ContentInfo::SELECTED) continue;
|
|
|
|
|
|
|
|
force_selection = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* So something depended directly on us */
|
|
|
|
if (force_selection) continue;
|
|
|
|
|
|
|
|
/* Nothing depends on us, mark the whole graph as unselected.
|
|
|
|
* After that's done run over them once again to test their children
|
2010-08-15 14:06:43 +00:00
|
|
|
* to unselect. Don't do it immediately because it'll do exactly what
|
2009-01-20 16:51:55 +00:00
|
|
|
* we're doing now. */
|
|
|
|
for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
|
|
|
|
const ContentInfo *c = *iter;
|
|
|
|
if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
|
|
|
|
}
|
|
|
|
for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
|
|
|
|
this->CheckDependencyState(this->GetContent((*iter)->id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/** Clear all downloaded content information. */
|
2009-01-23 10:20:29 +00:00
|
|
|
void ClientNetworkContentSocketHandler::Clear()
|
|
|
|
{
|
|
|
|
for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
|
|
|
|
|
|
|
|
this->infos.Clear();
|
2009-02-24 13:25:35 +00:00
|
|
|
this->requested.Clear();
|
2009-01-23 10:20:29 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 16:51:55 +00:00
|
|
|
/*** CALLBACK ***/
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::OnConnect(bool success)
|
|
|
|
{
|
2009-01-21 12:23:08 +00:00
|
|
|
for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
|
|
|
|
ContentCallback *cb = *iter;
|
|
|
|
cb->OnConnect(success);
|
2009-01-23 09:00:01 +00:00
|
|
|
if (iter != this->callbacks.End() && *iter == cb) iter++;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::OnDisconnect()
|
|
|
|
{
|
2009-01-21 12:23:08 +00:00
|
|
|
for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
|
|
|
|
ContentCallback *cb = *iter;
|
|
|
|
cb->OnDisconnect();
|
2009-01-23 09:00:01 +00:00
|
|
|
if (iter != this->callbacks.End() && *iter == cb) iter++;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
|
|
|
|
{
|
2009-01-21 12:23:08 +00:00
|
|
|
for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
|
|
|
|
ContentCallback *cb = *iter;
|
|
|
|
cb->OnReceiveContentInfo(ci);
|
2009-01-23 09:00:01 +00:00
|
|
|
if (iter != this->callbacks.End() && *iter == cb) iter++;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-06 10:11:59 +00:00
|
|
|
void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
|
2009-01-20 16:51:55 +00:00
|
|
|
{
|
2009-01-21 12:23:08 +00:00
|
|
|
for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
|
|
|
|
ContentCallback *cb = *iter;
|
|
|
|
cb->OnDownloadProgress(ci, bytes);
|
2009-01-23 09:00:01 +00:00
|
|
|
if (iter != this->callbacks.End() && *iter == cb) iter++;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
|
|
|
|
{
|
|
|
|
ContentInfo *ci = this->GetContent(cid);
|
|
|
|
if (ci != NULL) {
|
|
|
|
ci->state = ContentInfo::ALREADY_HERE;
|
|
|
|
}
|
|
|
|
|
2009-01-21 12:23:08 +00:00
|
|
|
for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
|
|
|
|
ContentCallback *cb = *iter;
|
|
|
|
cb->OnDownloadComplete(cid);
|
2009-01-23 09:00:01 +00:00
|
|
|
if (iter != this->callbacks.End() && *iter == cb) iter++;
|
2009-01-20 16:51:55 +00:00
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|