OpenTTD: src/network/network_content.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_content.cpp
Go to the documentation of this file.
1 /* $Id: network_content.cpp 27576 2016-05-22 10:45:46Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #if defined(ENABLE_NETWORK)
13
14 #include "../stdafx.h"
15 #include "../rev.h"
16 #include "../ai/ai.hpp"
17 #include "../game/game.hpp"
18 #include "../window_func.h"
19 #include "../error.h"
20 #include "../base_media_base.h"
21 #include "../settings_type.h"
22 #include "network_content.h"
23
24 #include "table/strings.h"
25
26 #if defined(WITH_ZLIB)
27 #include <zlib.h>
28 #endif
29
30 #include "../safeguards.h"
31
32 extern bool HasScenario(const ContentInfo *ci, bool md5sum);
33
35 ClientNetworkContentSocketHandler _network_content_client;
36
38 static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
39 {
40 return FindGRFConfig(BSWAP32(ci->unique_id), md5sum ? FGCM_EXACT : FGCM_ANY, md5sum ? ci->md5sum : NULL) != NULL;
41 }
42
50 typedef bool (*HasProc)(const ContentInfo *ci, bool md5sum);
51
52 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
53 {
54 ContentInfo *ci = new ContentInfo();
55 ci->type = (ContentType)p->Recv_uint8();
56 ci->id = (ContentID)p->Recv_uint32();
57 ci->filesize = p->Recv_uint32();
58
59 p->Recv_string(ci->name, lengthof(ci->name));
60 p->Recv_string(ci->version, lengthof(ci->version));
61 p->Recv_string(ci->url, lengthof(ci->url));
62 p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
63
64 ci->unique_id = p->Recv_uint32();
65 for (uint j = 0; j < sizeof(ci->md5sum); j++) {
66 ci->md5sum[j] = p->Recv_uint8();
67 }
68
69 ci->dependency_count = p->Recv_uint8();
70 ci->dependencies = MallocT<ContentID>(ci->dependency_count);
71 for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
72
73 ci->tag_count = p->Recv_uint8();
74 ci->tags = MallocT<char[32]>(ci->tag_count);
75 for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
76
77 if (!ci->IsValid()) {
78 delete ci;
79 this->Close();
80 return false;
81 }
82
83 /* Find the appropriate check function */
84 HasProc proc = NULL;
85 switch (ci->type) {
86 case CONTENT_TYPE_NEWGRF:
87 proc = HasGRFConfig;
88 break;
89
90 case CONTENT_TYPE_BASE_GRAPHICS:
91 proc = BaseGraphics::HasSet;
92 break;
93
94 case CONTENT_TYPE_BASE_MUSIC:
95 proc = BaseMusic::HasSet;
96 break;
97
98 case CONTENT_TYPE_BASE_SOUNDS:
99 proc = BaseSounds::HasSet;
100 break;
101
102 case CONTENT_TYPE_AI:
103 proc = AI::HasAI; break;
104 break;
105
106 case CONTENT_TYPE_AI_LIBRARY:
107 proc = AI::HasAILibrary; break;
108 break;
109
110 case CONTENT_TYPE_GAME:
111 proc = Game::HasGame; break;
112 break;
113
114 case CONTENT_TYPE_GAME_LIBRARY:
115 proc = Game::HasGameLibrary; break;
116 break;
117
118 case CONTENT_TYPE_SCENARIO:
119 case CONTENT_TYPE_HEIGHTMAP:
120 proc = HasScenario;
121 break;
122
123 default:
124 break;
125 }
126
127 if (proc != NULL) {
128 if (proc(ci, true)) {
129 ci->state = ContentInfo::ALREADY_HERE;
130 } else {
131 ci->state = ContentInfo::UNSELECTED;
132 if (proc(ci, false)) ci->upgrade = true;
133 }
134 } else {
135 ci->state = ContentInfo::UNSELECTED;
136 }
137
138 /* Something we don't have and has filesize 0 does not exist in te system */
139 if (ci->state == ContentInfo::UNSELECTED && ci->filesize == 0) ci->state = ContentInfo::DOES_NOT_EXIST;
140
141 /* Do we already have a stub for this? */
142 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
143 ContentInfo *ici = *iter;
144 if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
145 memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
146 /* Preserve the name if possible */
147 if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
148 if (ici->IsSelected()) ci->state = ici->state;
149
150 /*
151 * As ici might be selected by the content window we cannot delete that.
152 * However, we want to keep most of the values of ci, except the values
153 * we (just) already preserved.
154 * So transfer data and ownership of allocated memory from ci to ici.
155 */
156 ici->TransferFrom(ci);
157 delete ci;
158
159 this->OnReceiveContentInfo(ici);
160 return true;
161 }
162 }
163
164 /* Missing content info? Don't list it */
165 if (ci->filesize == 0) {
166 delete ci;
167 return true;
168 }
169
170 *this->infos.Append() = ci;
171
172 /* Incoming data means that we might need to reconsider dependencies */
173 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
174 this->CheckDependencyState(*iter);
175 }
176
177 this->OnReceiveContentInfo(ci);
178
179 return true;
180 }
181
186 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type)
187 {
188 if (type == CONTENT_TYPE_END) {
189 this->RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
190 this->RequestContentList(CONTENT_TYPE_BASE_MUSIC);
191 this->RequestContentList(CONTENT_TYPE_BASE_SOUNDS);
192 this->RequestContentList(CONTENT_TYPE_SCENARIO);
193 this->RequestContentList(CONTENT_TYPE_HEIGHTMAP);
194 this->RequestContentList(CONTENT_TYPE_AI);
195 this->RequestContentList(CONTENT_TYPE_AI_LIBRARY);
196 this->RequestContentList(CONTENT_TYPE_GAME);
197 this->RequestContentList(CONTENT_TYPE_GAME_LIBRARY);
198 this->RequestContentList(CONTENT_TYPE_NEWGRF);
199 return;
200 }
201
202 this->Connect();
203
204 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_LIST);
205 p->Send_uint8 ((byte)type);
206 p->Send_uint32(_openttd_newgrf_version);
207
208 this->SendPacket(p);
209 }
210
216 void ClientNetworkContentSocketHandler::RequestContentList(uint count, const ContentID *content_ids)
217 {
218 this->Connect();
219
220 while (count > 0) {
221 /* We can "only" send a limited number of IDs in a single packet.
222 * A packet begins with the packet size and a byte for the type.
223 * Then this packet adds a uint16 for the count in this packet.
224 * The rest of the packet can be used for the IDs. */
225 uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
226
227 Packet *p = new Packet(PACKET_CONTENT_CLIENT_INFO_ID);
228 p->Send_uint16(p_count);
229
230 for (uint i = 0; i < p_count; i++) {
231 p->Send_uint32(content_ids[i]);
232 }
233
234 this->SendPacket(p);
235 count -= p_count;
236 content_ids += p_count;
237 }
238 }
239
245 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector *cv, bool send_md5sum)
246 {
247 if (cv == NULL) return;
248
249 this->Connect();
250
251 assert(cv->Length() < 255);
252 assert(cv->Length() < (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint8)) /
253 (sizeof(uint8) + sizeof(uint32) + (send_md5sum ? /*sizeof(ContentInfo::md5sum)*/16 : 0)));
254
255 Packet *p = new Packet(send_md5sum ? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5 : PACKET_CONTENT_CLIENT_INFO_EXTID);
256 p->Send_uint8(cv->Length());
257
258 for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
259 const ContentInfo *ci = *iter;
260 p->Send_uint8((byte)ci->type);
261 p->Send_uint32(ci->unique_id);
262 if (!send_md5sum) continue;
263
264 for (uint j = 0; j < sizeof(ci->md5sum); j++) {
265 p->Send_uint8(ci->md5sum[j]);
266 }
267 }
268
269 this->SendPacket(p);
270
271 for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) {
272 ContentInfo *ci = *iter;
273 bool found = false;
274 for (ContentIterator iter2 = this->infos.Begin(); iter2 != this->infos.End(); iter2++) {
275 ContentInfo *ci2 = *iter2;
276 if (ci->type == ci2->type && ci->unique_id == ci2->unique_id &&
277 (!send_md5sum || memcmp(ci->md5sum, ci2->md5sum, sizeof(ci->md5sum)) == 0)) {
278 found = true;
279 break;
280 }
281 }
282 if (!found) {
283 *this->infos.Append() = ci;
284 } else {
285 delete ci;
286 }
287 }
288 }
289
296 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint &files, uint &bytes, bool fallback)
297 {
298 bytes = 0;
299
300 ContentIDList content;
301 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
302 const ContentInfo *ci = *iter;
303 if (!ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) continue;
304
305 *content.Append() = ci->id;
306 bytes += ci->filesize;
307 }
308
309 files = content.Length();
310
311 /* If there's nothing to download, do nothing. */
312 if (files == 0) return;
313
314 if (_settings_client.network.no_http_content_downloads || fallback) {
315 this->DownloadSelectedContentFallback(content);
316 } else {
317 this->DownloadSelectedContentHTTP(content);
318 }
319 }
320
325 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList &content)
326 {
327 uint count = content.Length();
328
329 /* Allocate memory for the whole request.
330 * Requests are "id\nid\n..." (as strings), so assume the maximum ID,
331 * which is uint32 so 10 characters long. Then the newlines and
332 * multiply that all with the count and then add the '\0'. */
333 uint bytes = (10 + 1) * count + 1;
334 char *content_request = MallocT<char>(bytes);
335 const char *lastof = content_request + bytes - 1;
336
337 char *p = content_request;
338 for (const ContentID *id = content.Begin(); id != content.End(); id++) {
339 p += seprintf(p, lastof, "%d\n", *id);
340 }
341
342 this->http_response_index = -1;
343
344 NetworkAddress address(NETWORK_CONTENT_MIRROR_HOST, NETWORK_CONTENT_MIRROR_PORT);
345 new NetworkHTTPContentConnecter(address, this, NETWORK_CONTENT_MIRROR_URL, content_request);
346 /* NetworkHTTPContentConnecter takes over freeing of content_request! */
347 }
348
353 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList &content)
354 {
355 uint count = content.Length();
356 const ContentID *content_ids = content.Begin();
357 this->Connect();
358
359 while (count > 0) {
360 /* We can "only" send a limited number of IDs in a single packet.
361 * A packet begins with the packet size and a byte for the type.
362 * Then this packet adds a uint16 for the count in this packet.
363 * The rest of the packet can be used for the IDs. */
364 uint p_count = min(count, (SEND_MTU - sizeof(PacketSize) - sizeof(byte) - sizeof(uint16)) / sizeof(uint32));
365
366 Packet *p = new Packet(PACKET_CONTENT_CLIENT_CONTENT);
367 p->Send_uint16(p_count);
368
369 for (uint i = 0; i < p_count; i++) {
370 p->Send_uint32(content_ids[i]);
371 }
372
373 this->SendPacket(p);
374 count -= p_count;
375 content_ids += p_count;
376 }
377 }
378
386 static char *GetFullFilename(const ContentInfo *ci, bool compressed)
387 {
388 Subdirectory dir = GetContentInfoSubDir(ci->type);
389 if (dir == NO_DIRECTORY) return NULL;
390
391 static char buf[MAX_PATH];
392 FioGetFullPath(buf, lastof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
393 strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
394
395 return buf;
396 }
397
403 static bool GunzipFile(const ContentInfo *ci)
404 {
405 #if defined(WITH_ZLIB)
406 bool ret = true;
407 FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
408 if (ftmp == NULL) return false;
409
410 gzFile fin = gzdopen(fileno(ftmp), "rb");
411 FILE *fout = fopen(GetFullFilename(ci, false), "wb");
412
413 if (fin == NULL || fout == NULL) {
414 ret = false;
415 } else {
416 byte buff[8192];
417 for (;;) {
418 int read = gzread(fin, buff, sizeof(buff));
419 if (read == 0) {
420 /* If gzread() returns 0, either the end-of-file has been
421 * reached or an underlying read error has occurred.
422 *
423 * gzeof() can't be used, because:
424 * 1.2.5 - it is safe, 1 means 'everything was OK'
425 * 1.2.3.5, 1.2.4 - 0 or 1 is returned 'randomly'
426 * 1.2.3.3 - 1 is returned for truncated archive
427 *
428 * So we use gzerror(). When proper end of archive
429 * has been reached, then:
430 * errnum == Z_STREAM_END in 1.2.3.3,
431 * errnum == 0 in 1.2.4 and 1.2.5 */
432 int errnum;
433 gzerror(fin, &errnum);
434 if (errnum != 0 && errnum != Z_STREAM_END) ret = false;
435 break;
436 }
437 if (read < 0 || (size_t)read != fwrite(buff, 1, read, fout)) {
438 /* If gzread() returns -1, there was an error in archive */
439 ret = false;
440 break;
441 }
442 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
443 * if (read < sizeof(buff)) break; */
444 }
445 }
446
447 if (fin != NULL) {
448 /* Closes ftmp too! */
449 gzclose(fin);
450 } else if (ftmp != NULL) {
451 /* In case the gz stream was opened correctly this will
452 * be closed by gzclose. */
453 fclose(ftmp);
454 }
455 if (fout != NULL) fclose(fout);
456
457 return ret;
458 #else
459 NOT_REACHED();
460 #endif /* defined(WITH_ZLIB) */
461 }
462
463 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet *p)
464 {
465 if (this->curFile == NULL) {
466 delete this->curInfo;
467 /* When we haven't opened a file this must be our first packet with metadata. */
468 this->curInfo = new ContentInfo;
469 this->curInfo->type = (ContentType)p->Recv_uint8();
470 this->curInfo->id = (ContentID)p->Recv_uint32();
471 this->curInfo->filesize = p->Recv_uint32();
472 p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
473
474 if (!this->BeforeDownload()) {
475 this->Close();
476 return false;
477 }
478 } else {
479 /* We have a file opened, thus are downloading internal content */
480 size_t toRead = (size_t)(p->size - p->pos);
481 if (fwrite(p->buffer + p->pos, 1, toRead, this->curFile) != toRead) {
482 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
483 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
484 this->Close();
485 fclose(this->curFile);
486 this->curFile = NULL;
487
488 return false;
489 }
490
491 this->OnDownloadProgress(this->curInfo, (int)toRead);
492
493 if (toRead == 0) this->AfterDownload();
494 }
495
496 return true;
497 }
498
503 bool ClientNetworkContentSocketHandler::BeforeDownload()
504 {
505 if (!this->curInfo->IsValid()) {
506 delete this->curInfo;
507 this->curInfo = NULL;
508 return false;
509 }
510
511 if (this->curInfo->filesize != 0) {
512 /* The filesize is > 0, so we are going to download it */
513 const char *filename = GetFullFilename(this->curInfo, true);
514 if (filename == NULL || (this->curFile = fopen(filename, "wb")) == NULL) {
515 /* Unless that fails of course... */
516 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
517 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
518 return false;
519 }
520 }
521 return true;
522 }
523
528 void ClientNetworkContentSocketHandler::AfterDownload()
529 {
530 /* We read nothing; that's our marker for end-of-stream.
531 * Now gunzip the tar and make it known. */
532 fclose(this->curFile);
533 this->curFile = NULL;
534
535 if (GunzipFile(this->curInfo)) {
536 unlink(GetFullFilename(this->curInfo, true));
537
538 Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
539 if (sd == NO_DIRECTORY) NOT_REACHED();
540
541 TarScanner ts;
542 ts.AddFile(sd, GetFullFilename(this->curInfo, false));
543
544 if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
545 /* Music can't be in a tar. So extract the tar! */
546 ExtractTar(GetFullFilename(this->curInfo, false), BASESET_DIR);
547 unlink(GetFullFilename(this->curInfo, false));
548 }
549
550 this->OnDownloadComplete(this->curInfo->id);
551 } else {
552 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR);
553 }
554 }
555
556 /* Also called to just clean up the mess. */
557 void ClientNetworkContentSocketHandler::OnFailure()
558 {
559 /* If we fail, download the rest via the 'old' system. */
560 uint files, bytes;
561 this->DownloadSelectedContent(files, bytes, true);
562
563 this->http_response.Reset();
564 this->http_response_index = -2;
565
566 if (this->curFile != NULL) {
567 /* Revert the download progress when we are going for the old system. */
568 long size = ftell(this->curFile);
569 if (size > 0) this->OnDownloadProgress(this->curInfo, (int)-size);
570
571 fclose(this->curFile);
572 this->curFile = NULL;
573 }
574 }
575
576 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t length)
577 {
578 assert(data == NULL || length != 0);
579
580 /* Ignore any latent data coming from a connection we closed. */
581 if (this->http_response_index == -2) return;
582
583 if (this->http_response_index == -1) {
584 if (data != NULL) {
585 /* Append the rest of the response. */
586 memcpy(this->http_response.Append((uint)length), data, length);
587 return;
588 } else {
589 /* Make sure the response is properly terminated. */
590 *this->http_response.Append() = '\0';
591
592 /* And prepare for receiving the rest of the data. */
593 this->http_response_index = 0;
594 }
595 }
596
597 if (data != NULL) {
598 /* We have data, so write it to the file. */
599 if (fwrite(data, 1, length, this->curFile) != length) {
600 /* Writing failed somehow, let try via the old method. */
601 this->OnFailure();
602 } else {
603 /* Just received the data. */
604 this->OnDownloadProgress(this->curInfo, (int)length);
605 }
606 /* Nothing more to do now. */
607 return;
608 }
609
610 if (this->curFile != NULL) {
611 /* We've finished downloading a file. */
612 this->AfterDownload();
613 }
614
615 if ((uint)this->http_response_index >= this->http_response.Length()) {
616 /* It's not a real failure, but if there's
617 * nothing more to download it helps with
618 * cleaning up the stuff we allocated. */
619 this->OnFailure();
620 return;
621 }
622
623 delete this->curInfo;
624 /* When we haven't opened a file this must be our first packet with metadata. */
625 this->curInfo = new ContentInfo;
626
628 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
629
630 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
631
632 for (;;) {
633 char *str = this->http_response.Begin() + this->http_response_index;
634 char *p = strchr(str, '\n');
635 check_and_terminate(p);
636
637 /* Update the index for the next one */
638 this->http_response_index += (int)strlen(str) + 1;
639
640 /* Read the ID */
641 p = strchr(str, ',');
642 check_and_terminate(p);
643 this->curInfo->id = (ContentID)atoi(str);
644
645 /* Read the type */
646 str = p + 1;
647 p = strchr(str, ',');
648 check_and_terminate(p);
649 this->curInfo->type = (ContentType)atoi(str);
650
651 /* Read the file size */
652 str = p + 1;
653 p = strchr(str, ',');
654 check_and_terminate(p);
655 this->curInfo->filesize = atoi(str);
656
657 /* Read the URL */
658 str = p + 1;
659 /* Is it a fallback URL? If so, just continue with the next one. */
660 if (strncmp(str, "ottd", 4) == 0) {
661 if ((uint)this->http_response_index >= this->http_response.Length()) {
662 /* Have we gone through all lines? */
663 this->OnFailure();
664 return;
665 }
666 continue;
667 }
668
669 p = strrchr(str, '/');
670 check_not_null(p);
671 p++; // Start after the '/'
672
673 char tmp[MAX_PATH];
674 if (strecpy(tmp, p, lastof(tmp)) == lastof(tmp)) {
675 this->OnFailure();
676 return;
677 }
678 /* Remove the extension from the string. */
679 for (uint i = 0; i < 2; i++) {
680 p = strrchr(tmp, '.');
681 check_and_terminate(p);
682 }
683
684 /* Copy the string, without extension, to the filename. */
685 strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
686
687 /* Request the next file. */
688 if (!this->BeforeDownload()) {
689 this->OnFailure();
690 return;
691 }
692
693 NetworkHTTPSocketHandler::Connect(str, this);
694 return;
695 }
696
697 #undef check
698 #undef check_and_terminate
699 }
700
704 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
705 NetworkContentSocketHandler(),
706 http_response_index(-2),
707 curFile(NULL),
708 curInfo(NULL),
709 isConnecting(false),
710 lastActivity(_realtime_tick)
711 {
712 }
713
715 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
716 {
717 delete this->curInfo;
718 if (this->curFile != NULL) fclose(this->curFile);
719
720 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
721 }
722
724 class NetworkContentConnecter : TCPConnecter {
725 public:
730 NetworkContentConnecter(const NetworkAddress &address) : TCPConnecter(address) {}
731
732 virtual void OnFailure()
733 {
734 _network_content_client.isConnecting = false;
735 _network_content_client.OnConnect(false);
736 }
737
738 virtual void OnConnect(SOCKET s)
739 {
740 assert(_network_content_client.sock == INVALID_SOCKET);
741 _network_content_client.isConnecting = false;
742 _network_content_client.sock = s;
743 _network_content_client.Reopen();
744 _network_content_client.OnConnect(true);
745 }
746 };
747
751 void ClientNetworkContentSocketHandler::Connect()
752 {
753 this->lastActivity = _realtime_tick;
754
755 if (this->sock != INVALID_SOCKET || this->isConnecting) return;
756 this->isConnecting = true;
757 new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
758 }
759
763 void ClientNetworkContentSocketHandler::Close()
764 {
765 if (this->sock == INVALID_SOCKET) return;
766 NetworkContentSocketHandler::Close();
767
768 this->OnDisconnect();
769 }
770
775 void ClientNetworkContentSocketHandler::SendReceive()
776 {
777 if (this->sock == INVALID_SOCKET || this->isConnecting) return;
778
779 if (this->lastActivity + IDLE_TIMEOUT < _realtime_tick) {
780 this->Close();
781 return;
782 }
783
784 if (this->CanSendReceive()) {
785 if (this->ReceivePackets()) {
786 /* Only update activity once a packet is received, instead of everytime we try it. */
787 this->lastActivity = _realtime_tick;
788 }
789 }
790
791 this->SendPackets();
792 }
793
798 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
799 {
800 /* When we tried to download it already, don't try again */
801 if (this->requested.Contains(cid)) return;
802
803 *this->requested.Append() = cid;
804 assert(this->requested.Contains(cid));
805 this->RequestContentList(1, &cid);
806 }
807
813 ContentInfo *ClientNetworkContentSocketHandler::GetContent(ContentID cid)
814 {
815 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
816 ContentInfo *ci = *iter;
817 if (ci->id == cid) return ci;
818 }
819 return NULL;
820 }
821
822
827 void ClientNetworkContentSocketHandler::Select(ContentID cid)
828 {
829 ContentInfo *ci = this->GetContent(cid);
830 if (ci == NULL || ci->state != ContentInfo::UNSELECTED) return;
831
832 ci->state = ContentInfo::SELECTED;
833 this->CheckDependencyState(ci);
834 }
835
840 void ClientNetworkContentSocketHandler::Unselect(ContentID cid)
841 {
842 ContentInfo *ci = this->GetContent(cid);
843 if (ci == NULL || !ci->IsSelected()) return;
844
845 ci->state = ContentInfo::UNSELECTED;
846 this->CheckDependencyState(ci);
847 }
848
850 void ClientNetworkContentSocketHandler::SelectAll()
851 {
852 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
853 ContentInfo *ci = *iter;
854 if (ci->state == ContentInfo::UNSELECTED) {
855 ci->state = ContentInfo::SELECTED;
856 this->CheckDependencyState(ci);
857 }
858 }
859 }
860
862 void ClientNetworkContentSocketHandler::SelectUpgrade()
863 {
864 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
865 ContentInfo *ci = *iter;
866 if (ci->state == ContentInfo::UNSELECTED && ci->upgrade) {
867 ci->state = ContentInfo::SELECTED;
868 this->CheckDependencyState(ci);
869 }
870 }
871 }
872
874 void ClientNetworkContentSocketHandler::UnselectAll()
875 {
876 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
877 ContentInfo *ci = *iter;
878 if (ci->IsSelected() && ci->state != ContentInfo::ALREADY_HERE) ci->state = ContentInfo::UNSELECTED;
879 }
880 }
881
883 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo *ci)
884 {
885 switch (ci->state) {
886 case ContentInfo::SELECTED:
887 case ContentInfo::AUTOSELECTED:
888 this->Unselect(ci->id);
889 break;
890
891 case ContentInfo::UNSELECTED:
892 this->Select(ci->id);
893 break;
894
895 default:
896 break;
897 }
898 }
899
905 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector &parents, const ContentInfo *child) const
906 {
907 for (ConstContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) {
908 const ContentInfo *ci = *iter;
909 if (ci == child) continue;
910
911 for (uint i = 0; i < ci->dependency_count; i++) {
912 if (ci->dependencies[i] == child->id) {
913 *parents.Append() = ci;
914 break;
915 }
916 }
917 }
918 }
919
925 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector &tree, const ContentInfo *child) const
926 {
927 *tree.Append() = child;
928
929 /* First find all direct parents. We can't use the "normal" iterator as
930 * we are including stuff into the vector and as such the vector's data
931 * store can be reallocated (and thus move), which means out iterating
932 * pointer gets invalid. So fall back to the indices. */
933 for (uint i = 0; i < tree.Length(); i++) {
934 ConstContentVector parents;
935 this->ReverseLookupDependency(parents, tree[i]);
936
937 for (ConstContentIterator piter = parents.Begin(); piter != parents.End(); piter++) {
938 tree.Include(*piter);
939 }
940 }
941 }
942
947 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
948 {
949 if (ci->IsSelected() || ci->state == ContentInfo::ALREADY_HERE) {
950 /* Selection is easy; just walk all children and set the
951 * autoselected state. That way we can see what we automatically
952 * selected and thus can unselect when a dependency is removed. */
953 for (uint i = 0; i < ci->dependency_count; i++) {
954 ContentInfo *c = this->GetContent(ci->dependencies[i]);
955 if (c == NULL) {
956 this->DownloadContentInfo(ci->dependencies[i]);
957 } else if (c->state == ContentInfo::UNSELECTED) {
958 c->state = ContentInfo::AUTOSELECTED;
959 this->CheckDependencyState(c);
960 }
961 }
962 return;
963 }
964
965 if (ci->state != ContentInfo::UNSELECTED) return;
966
967 /* For unselection we need to find the parents of us. We need to
968 * unselect them. After that we unselect all children that we
969 * depend on and are not used as dependency for us, but only when
970 * we automatically selected them. */
971 ConstContentVector parents;
972 this->ReverseLookupDependency(parents, ci);
973 for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
974 const ContentInfo *c = *iter;
975 if (!c->IsSelected()) continue;
976
977 this->Unselect(c->id);
978 }
979
980 for (uint i = 0; i < ci->dependency_count; i++) {
981 const ContentInfo *c = this->GetContent(ci->dependencies[i]);
982 if (c == NULL) {
983 DownloadContentInfo(ci->dependencies[i]);
984 continue;
985 }
986 if (c->state != ContentInfo::AUTOSELECTED) continue;
987
988 /* Only unselect when WE are the only parent. */
989 parents.Clear();
990 this->ReverseLookupDependency(parents, c);
991
992 /* First check whether anything depends on us */
993 int sel_count = 0;
994 bool force_selection = false;
995 for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
996 if ((*iter)->IsSelected()) sel_count++;
997 if ((*iter)->state == ContentInfo::SELECTED) force_selection = true;
998 }
999 if (sel_count == 0) {
1000 /* Nothing depends on us */
1001 this->Unselect(c->id);
1002 continue;
1003 }
1004 /* Something manually selected depends directly on us */
1005 if (force_selection) continue;
1006
1007 /* "Flood" search to find all items in the dependency graph*/
1008 parents.Clear();
1009 this->ReverseLookupTreeDependency(parents, c);
1010
1011 /* Is there anything that is "force" selected?, if so... we're done. */
1012 for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1013 if ((*iter)->state != ContentInfo::SELECTED) continue;
1014
1015 force_selection = true;
1016 break;
1017 }
1018
1019 /* So something depended directly on us */
1020 if (force_selection) continue;
1021
1022 /* Nothing depends on us, mark the whole graph as unselected.
1023 * After that's done run over them once again to test their children
1024 * to unselect. Don't do it immediately because it'll do exactly what
1025 * we're doing now. */
1026 for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1027 const ContentInfo *c = *iter;
1028 if (c->state == ContentInfo::AUTOSELECTED) this->Unselect(c->id);
1029 }
1030 for (ConstContentIterator iter = parents.Begin(); iter != parents.End(); iter++) {
1031 this->CheckDependencyState(this->GetContent((*iter)->id));
1032 }
1033 }
1034 }
1035
1037 void ClientNetworkContentSocketHandler::Clear()
1038 {
1039 for (ContentIterator iter = this->infos.Begin(); iter != this->infos.End(); iter++) delete *iter;
1040
1041 this->infos.Clear();
1042 this->requested.Clear();
1043 }
1044
1045 /*** CALLBACK ***/
1046
1047 void ClientNetworkContentSocketHandler::OnConnect(bool success)
1048 {
1049 for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1050 ContentCallback *cb = *iter;
1051 cb->OnConnect(success);
1052 if (iter != this->callbacks.End() && *iter == cb) iter++;
1053 }
1054 }
1055
1056 void ClientNetworkContentSocketHandler::OnDisconnect()
1057 {
1058 for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1059 ContentCallback *cb = *iter;
1060 cb->OnDisconnect();
1061 if (iter != this->callbacks.End() && *iter == cb) iter++;
1062 }
1063 }
1064
1065 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo *ci)
1066 {
1067 for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1068 ContentCallback *cb = *iter;
1069 cb->OnReceiveContentInfo(ci);
1070 if (iter != this->callbacks.End() && *iter == cb) iter++;
1071 }
1072 }
1073
1074 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo *ci, int bytes)
1075 {
1076 for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1077 ContentCallback *cb = *iter;
1078 cb->OnDownloadProgress(ci, bytes);
1079 if (iter != this->callbacks.End() && *iter == cb) iter++;
1080 }
1081 }
1082
1083 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid)
1084 {
1085 ContentInfo *ci = this->GetContent(cid);
1086 if (ci != NULL) {
1087 ci->state = ContentInfo::ALREADY_HERE;
1088 }
1089
1090 for (ContentCallback **iter = this->callbacks.Begin(); iter != this->callbacks.End(); /* nothing */) {
1091 ContentCallback *cb = *iter;
1092 cb->OnDownloadComplete(cid);
1093 if (iter != this->callbacks.End() && *iter == cb) iter++;
1094 }
1095 }
1096
1097 #endif /* ENABLE_NETWORK */