1 /* $Id: network_content.cpp 26056 2013-11-22 21:50:43Z rubidium $ */
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/>.
10 /** @file network_content.cpp Content sending/receiving part of the network protocol. */
12 #if defined(ENABLE_NETWORK)
14 #include "../stdafx.h"
16 #include "../ai/ai.hpp"
17 #include "../game/game.hpp"
18 #include "../window_func.h"
20 #include "../base_media_base.h"
21 #include "../settings_type.h"
22 #include "network_content.h"
24 #include "table/strings.h"
26 #if defined(WITH_ZLIB)
30 #include "../safeguards.h"
32 extern bool HasScenario(const ContentInfo
*ci
, bool md5sum
);
34 /** The client we use to connect to the server. */
35 ClientNetworkContentSocketHandler _network_content_client
;
37 /** Wrapper function for the HasProc */
38 static bool HasGRFConfig(const ContentInfo
*ci
, bool md5sum
)
40 return FindGRFConfig(BSWAP32(ci
->unique_id
), md5sum
? FGCM_EXACT
: FGCM_ANY
, md5sum
? ci
->md5sum
: nullptr) != nullptr;
44 * Check whether a function piece of content is locally known.
45 * Matches on the unique ID and possibly the MD5 checksum.
46 * @param ci the content info to search for
47 * @param md5sum also match the MD5 checksum?
48 * @return true iff it's known
50 typedef bool (*HasProc
)(const ContentInfo
*ci
, bool md5sum
);
52 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet
*p
)
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();
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
);
64 ci
->unique_id
= p
->Recv_uint32();
65 for (uint j
= 0; j
< sizeof(ci
->md5sum
); j
++) {
66 ci
->md5sum
[j
] = p
->Recv_uint8();
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();
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
));
83 /* Find the appropriate check function */
84 HasProc proc
= nullptr;
86 case CONTENT_TYPE_NEWGRF
:
90 case CONTENT_TYPE_BASE_GRAPHICS
:
91 proc
= BaseGraphics::HasSet
;
94 case CONTENT_TYPE_BASE_MUSIC
:
95 proc
= BaseMusic::HasSet
;
98 case CONTENT_TYPE_BASE_SOUNDS
:
99 proc
= BaseSounds::HasSet
;
102 case CONTENT_TYPE_AI
:
103 proc
= AI::HasAI
; break;
106 case CONTENT_TYPE_AI_LIBRARY
:
107 proc
= AI::HasAILibrary
; break;
110 case CONTENT_TYPE_GAME
:
111 proc
= Game::HasGame
; break;
114 case CONTENT_TYPE_GAME_LIBRARY
:
115 proc
= Game::HasGameLibrary
; break;
118 case CONTENT_TYPE_SCENARIO
:
119 case CONTENT_TYPE_HEIGHTMAP
:
127 if (proc
!= nullptr) {
128 if (proc(ci
, true)) {
129 ci
->state
= ContentInfo::ALREADY_HERE
;
131 ci
->state
= ContentInfo::UNSELECTED
;
132 if (proc(ci
, false)) ci
->upgrade
= true;
135 ci
->state
= ContentInfo::UNSELECTED
;
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
;
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
;
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.
156 ici
->TransferFrom(ci
);
159 this->OnReceiveContentInfo(ici
);
164 /* Missing content info? Don't list it */
165 if (ci
->filesize
== 0) {
170 *this->infos
.Append() = ci
;
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
);
177 this->OnReceiveContentInfo(ci
);
183 * Request the content list for the given type.
184 * @param type The content type to request the list for.
186 void ClientNetworkContentSocketHandler::RequestContentList(ContentType type
)
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
);
204 Packet
*p
= new Packet(PACKET_CONTENT_CLIENT_INFO_LIST
);
205 p
->Send_uint8 ((byte
)type
);
206 p
->Send_uint32(_openttd_newgrf_version
);
212 * Request the content list for a given number of content IDs.
213 * @param count The number of IDs to request.
214 * @param content_ids The unique identifiers of the content to request information about.
216 void ClientNetworkContentSocketHandler::RequestContentList(uint count
, const ContentID
*content_ids
)
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
));
227 Packet
*p
= new Packet(PACKET_CONTENT_CLIENT_INFO_ID
);
228 p
->Send_uint16(p_count
);
230 for (uint i
= 0; i
< p_count
; i
++) {
231 p
->Send_uint32(content_ids
[i
]);
236 content_ids
+= p_count
;
241 * Request the content list for a list of content.
242 * @param cv List with unique IDs and MD5 checksums.
243 * @param send_md5sum Whether we want a MD5 checksum matched set of files or not.
245 void ClientNetworkContentSocketHandler::RequestContentList(ContentVector
*cv
, bool send_md5sum
)
247 if (cv
== nullptr) return;
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)));
255 Packet
*p
= new Packet(send_md5sum
? PACKET_CONTENT_CLIENT_INFO_EXTID_MD5
: PACKET_CONTENT_CLIENT_INFO_EXTID
);
256 p
->Send_uint8(cv
->Length());
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;
264 for (uint j
= 0; j
< sizeof(ci
->md5sum
); j
++) {
265 p
->Send_uint8(ci
->md5sum
[j
]);
271 for (ContentIterator iter
= cv
->Begin(); iter
!= cv
->End(); iter
++) {
272 ContentInfo
*ci
= *iter
;
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)) {
283 *this->infos
.Append() = ci
;
291 * Actually begin downloading the content we selected.
292 * @param[out] files The number of files we are going to download.
293 * @param[out] bytes The number of bytes we are going to download.
294 * @param fallback Whether to use the fallback or not.
296 void ClientNetworkContentSocketHandler::DownloadSelectedContent(uint
&files
, uint
&bytes
, bool fallback
)
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;
305 *content
.Append() = ci
->id
;
306 bytes
+= ci
->filesize
;
309 files
= content
.Length();
311 /* If there's nothing to download, do nothing. */
312 if (files
== 0) return;
314 if (_settings_client
.network
.no_http_content_downloads
|| fallback
) {
315 this->DownloadSelectedContentFallback(content
);
317 this->DownloadSelectedContentHTTP(content
);
322 * Initiate downloading the content over HTTP.
323 * @param content The content to download.
325 void ClientNetworkContentSocketHandler::DownloadSelectedContentHTTP(const ContentIDList
&content
)
327 uint count
= content
.Length();
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;
337 char *p
= content_request
;
338 for (const ContentID
*id
= content
.Begin(); id
!= content
.End(); id
++) {
339 p
+= seprintf(p
, lastof
, "%d\n", *id
);
342 this->http_response_index
= -1;
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! */
350 * Initiate downloading the content over the fallback protocol.
351 * @param content The content to download.
353 void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const ContentIDList
&content
)
355 uint count
= content
.Length();
356 const ContentID
*content_ids
= content
.Begin();
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
));
366 Packet
*p
= new Packet(PACKET_CONTENT_CLIENT_CONTENT
);
367 p
->Send_uint16(p_count
);
369 for (uint i
= 0; i
< p_count
; i
++) {
370 p
->Send_uint32(content_ids
[i
]);
375 content_ids
+= p_count
;
380 * Determine the full filename of a piece of content information
381 * @param ci the information to get the filename from
382 * @param compressed should the filename end with .gz?
383 * @return a statically allocated buffer with the filename or
384 * nullptr when no filename could be made.
386 static char *GetFullFilename(const ContentInfo
*ci
, bool compressed
)
388 Subdirectory dir
= GetContentInfoSubDir(ci
->type
);
389 if (dir
== NO_DIRECTORY
) return nullptr;
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
));
399 * Gunzip a given file and remove the .gz if successful.
400 * @param ci container with filename
401 * @return true if the gunzip completed
403 static bool GunzipFile(const ContentInfo
*ci
)
405 #if defined(WITH_ZLIB)
407 FILE *ftmp
= fopen(GetFullFilename(ci
, true), "rb");
408 if (ftmp
== nullptr) return false;
410 gzFile fin
= gzdopen(fileno(ftmp
), "rb");
411 FILE *fout
= fopen(GetFullFilename(ci
, false), "wb");
413 if (fin
== nullptr || fout
== nullptr) {
418 int read
= gzread(fin
, buff
, sizeof(buff
));
420 /* If gzread() returns 0, either the end-of-file has been
421 * reached or an underlying read error has occurred.
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
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 */
433 gzerror(fin
, &errnum
);
434 if (errnum
!= 0 && errnum
!= Z_STREAM_END
) ret
= false;
437 if (read
< 0 || (size_t)read
!= fwrite(buff
, 1, read
, fout
)) {
438 /* If gzread() returns -1, there was an error in archive */
442 /* DO NOT DO THIS! It will fail to detect broken archive with 1.2.3.3!
443 * if (read < sizeof(buff)) break; */
447 if (fin
!= nullptr) {
448 /* Closes ftmp too! */
450 } else if (ftmp
!= nullptr) {
451 /* In case the gz stream was opened correctly this will
452 * be closed by gzclose. */
455 if (fout
!= nullptr) fclose(fout
);
460 #endif /* defined(WITH_ZLIB) */
463 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet
*p
)
465 if (this->curFile
== nullptr) {
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
));
474 if (!this->BeforeDownload()) {
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
);
485 fclose(this->curFile
);
486 this->curFile
= nullptr;
491 this->OnDownloadProgress(this->curInfo
, (int)toRead
);
493 if (toRead
== 0) this->AfterDownload();
500 * Handle the opening of the file before downloading.
501 * @return false on any error.
503 bool ClientNetworkContentSocketHandler::BeforeDownload()
505 if (!this->curInfo
->IsValid()) {
506 delete this->curInfo
;
507 this->curInfo
= nullptr;
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
== nullptr || (this->curFile
= fopen(filename
, "wb")) == nullptr) {
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
);
525 * Handle the closing and extracting of a file after
526 * downloading it has been done.
528 void ClientNetworkContentSocketHandler::AfterDownload()
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
= nullptr;
535 if (GunzipFile(this->curInfo
)) {
536 unlink(GetFullFilename(this->curInfo
, true));
538 Subdirectory sd
= GetContentInfoSubDir(this->curInfo
->type
);
539 if (sd
== NO_DIRECTORY
) NOT_REACHED();
542 ts
.AddFile(sd
, GetFullFilename(this->curInfo
, false));
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));
550 this->OnDownloadComplete(this->curInfo
->id
);
552 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT
, INVALID_STRING_ID
, WL_ERROR
);
556 /* Also called to just clean up the mess. */
557 void ClientNetworkContentSocketHandler::OnFailure()
559 /* If we fail, download the rest via the 'old' system. */
561 this->DownloadSelectedContent(files
, bytes
, true);
563 this->http_response
.Reset();
564 this->http_response_index
= -2;
566 if (this->curFile
!= nullptr) {
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
);
571 fclose(this->curFile
);
572 this->curFile
= nullptr;
576 void ClientNetworkContentSocketHandler::OnReceiveData(const char *data
, size_t length
)
578 assert(data
== nullptr || length
!= 0);
580 /* Ignore any latent data coming from a connection we closed. */
581 if (this->http_response_index
== -2) return;
583 if (this->http_response_index
== -1) {
584 if (data
!= nullptr) {
585 /* Append the rest of the response. */
586 memcpy(this->http_response
.Append((uint
)length
), data
, length
);
589 /* Make sure the response is properly terminated. */
590 *this->http_response
.Append() = '\0';
592 /* And prepare for receiving the rest of the data. */
593 this->http_response_index
= 0;
597 if (data
!= nullptr) {
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. */
603 /* Just received the data. */
604 this->OnDownloadProgress(this->curInfo
, (int)length
);
606 /* Nothing more to do now. */
610 if (this->curFile
!= nullptr) {
611 /* We've finished downloading a file. */
612 this->AfterDownload();
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. */
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
;
627 /** Check p for not being null and return calling OnFailure if that's not the case. */
628 #define check_not_null(p) { if ((p) == nullptr) { this->OnFailure(); return; } }
629 /** Check p for not being null and then terminate, or return calling OnFailure. */
630 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
633 char *str
= this->http_response
.Begin() + this->http_response_index
;
634 char *p
= strchr(str
, '\n');
635 check_and_terminate(p
);
637 /* Update the index for the next one */
638 this->http_response_index
+= (int)strlen(str
) + 1;
641 p
= strchr(str
, ',');
642 check_and_terminate(p
);
643 this->curInfo
->id
= (ContentID
)atoi(str
);
647 p
= strchr(str
, ',');
648 check_and_terminate(p
);
649 this->curInfo
->type
= (ContentType
)atoi(str
);
651 /* Read the file size */
653 p
= strchr(str
, ',');
654 check_and_terminate(p
);
655 this->curInfo
->filesize
= atoi(str
);
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? */
669 p
= strrchr(str
, '/');
671 p
++; // Start after the '/'
674 if (strecpy(tmp
, p
, lastof(tmp
)) == lastof(tmp
)) {
678 /* Remove the extension from the string. */
679 for (uint i
= 0; i
< 2; i
++) {
680 p
= strrchr(tmp
, '.');
681 check_and_terminate(p
);
684 /* Copy the string, without extension, to the filename. */
685 strecpy(this->curInfo
->filename
, tmp
, lastof(this->curInfo
->filename
));
687 /* Request the next file. */
688 if (!this->BeforeDownload()) {
693 NetworkHTTPSocketHandler::Connect(str
, this);
698 #undef check_and_terminate
702 * Create a socket handler to handle the connection.
704 ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
705 NetworkContentSocketHandler(),
706 http_response_index(-2),
710 lastActivity(_realtime_tick
)
714 /** Clear up the mess ;) */
715 ClientNetworkContentSocketHandler::~ClientNetworkContentSocketHandler()
717 delete this->curInfo
;
718 if (this->curFile
!= nullptr) fclose(this->curFile
);
720 for (ContentIterator iter
= this->infos
.Begin(); iter
!= this->infos
.End(); iter
++) delete *iter
;
723 /** Connect to the content server. */
724 class NetworkContentConnecter
: TCPConnecter
{
727 * Initiate the connecting.
728 * @param address The address of the server.
730 NetworkContentConnecter(const NetworkAddress
&address
) : TCPConnecter(address
) {}
732 virtual void OnFailure()
734 _network_content_client
.isConnecting
= false;
735 _network_content_client
.OnConnect(false);
738 virtual void OnConnect(SOCKET s
)
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);
749 * Connect with the content server.
751 void ClientNetworkContentSocketHandler::Connect()
753 this->lastActivity
= _realtime_tick
;
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
));
761 * Disconnect from the content server.
763 void ClientNetworkContentSocketHandler::Close()
765 if (this->sock
== INVALID_SOCKET
) return;
766 NetworkContentSocketHandler::Close();
768 this->OnDisconnect();
772 * Check whether we received/can send some data from/to the content server and
773 * when that's the case handle it appropriately
775 void ClientNetworkContentSocketHandler::SendReceive()
777 if (this->sock
== INVALID_SOCKET
|| this->isConnecting
) return;
779 if (this->lastActivity
+ IDLE_TIMEOUT
< _realtime_tick
) {
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
;
795 * Download information of a given Content ID if not already tried
796 * @param cid the ID to try
798 void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid
)
800 /* When we tried to download it already, don't try again */
801 if (this->requested
.Contains(cid
)) return;
803 *this->requested
.Append() = cid
;
804 assert(this->requested
.Contains(cid
));
805 this->RequestContentList(1, &cid
);
809 * Get the content info based on a ContentID
810 * @param cid the ContentID to search for
811 * @return the ContentInfo or nullptr if not found
813 ContentInfo
*ClientNetworkContentSocketHandler::GetContent(ContentID cid
)
815 for (ContentIterator iter
= this->infos
.Begin(); iter
!= this->infos
.End(); iter
++) {
816 ContentInfo
*ci
= *iter
;
817 if (ci
->id
== cid
) return ci
;
824 * Select a specific content id.
825 * @param cid the content ID to select
827 void ClientNetworkContentSocketHandler::Select(ContentID cid
)
829 ContentInfo
*ci
= this->GetContent(cid
);
830 if (ci
== nullptr || ci
->state
!= ContentInfo::UNSELECTED
) return;
832 ci
->state
= ContentInfo::SELECTED
;
833 this->CheckDependencyState(ci
);
837 * Unselect a specific content id.
838 * @param cid the content ID to deselect
840 void ClientNetworkContentSocketHandler::Unselect(ContentID cid
)
842 ContentInfo
*ci
= this->GetContent(cid
);
843 if (ci
== nullptr || !ci
->IsSelected()) return;
845 ci
->state
= ContentInfo::UNSELECTED
;
846 this->CheckDependencyState(ci
);
849 /** Select everything we can select */
850 void ClientNetworkContentSocketHandler::SelectAll()
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
);
861 /** Select everything that's an update for something we've got */
862 void ClientNetworkContentSocketHandler::SelectUpgrade()
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
);
873 /** Unselect everything that we've not downloaded so far. */
874 void ClientNetworkContentSocketHandler::UnselectAll()
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
;
882 /** Toggle the state of a content info and check its dependencies */
883 void ClientNetworkContentSocketHandler::ToggleSelectedState(const ContentInfo
*ci
)
886 case ContentInfo::SELECTED
:
887 case ContentInfo::AUTOSELECTED
:
888 this->Unselect(ci
->id
);
891 case ContentInfo::UNSELECTED
:
892 this->Select(ci
->id
);
901 * Reverse lookup the dependencies of (direct) parents over a given child.
902 * @param parents list to store all parents in (is not cleared)
903 * @param child the child to search the parents' dependencies for
905 void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVector
&parents
, const ContentInfo
*child
) const
907 for (ConstContentIterator iter
= this->infos
.Begin(); iter
!= this->infos
.End(); iter
++) {
908 const ContentInfo
*ci
= *iter
;
909 if (ci
== child
) continue;
911 for (uint i
= 0; i
< ci
->dependency_count
; i
++) {
912 if (ci
->dependencies
[i
] == child
->id
) {
913 *parents
.Append() = ci
;
921 * Reverse lookup the dependencies of all parents over a given child.
922 * @param tree list to store all parents in (is not cleared)
923 * @param child the child to search the parents' dependencies for
925 void ClientNetworkContentSocketHandler::ReverseLookupTreeDependency(ConstContentVector
&tree
, const ContentInfo
*child
) const
927 *tree
.Append() = child
;
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
]);
937 for (ConstContentIterator piter
= parents
.Begin(); piter
!= parents
.End(); piter
++) {
938 tree
.Include(*piter
);
944 * Check the dependencies (recursively) of this content info
945 * @param ci the content info to check the dependencies of
947 void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo
*ci
)
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
]);
956 this->DownloadContentInfo(ci
->dependencies
[i
]);
957 } else if (c
->state
== ContentInfo::UNSELECTED
) {
958 c
->state
= ContentInfo::AUTOSELECTED
;
959 this->CheckDependencyState(c
);
965 if (ci
->state
!= ContentInfo::UNSELECTED
) return;
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;
977 this->Unselect(c
->id
);
980 for (uint i
= 0; i
< ci
->dependency_count
; i
++) {
981 const ContentInfo
*c
= this->GetContent(ci
->dependencies
[i
]);
983 DownloadContentInfo(ci
->dependencies
[i
]);
986 if (c
->state
!= ContentInfo::AUTOSELECTED
) continue;
988 /* Only unselect when WE are the only parent. */
990 this->ReverseLookupDependency(parents
, c
);
992 /* First check whether anything depends on us */
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;
999 if (sel_count
== 0) {
1000 /* Nothing depends on us */
1001 this->Unselect(c
->id
);
1004 /* Something manually selected depends directly on us */
1005 if (force_selection
) continue;
1007 /* "Flood" search to find all items in the dependency graph*/
1009 this->ReverseLookupTreeDependency(parents
, c
);
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;
1015 force_selection
= true;
1019 /* So something depended directly on us */
1020 if (force_selection
) continue;
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
);
1030 for (ConstContentIterator iter
= parents
.Begin(); iter
!= parents
.End(); iter
++) {
1031 this->CheckDependencyState(this->GetContent((*iter
)->id
));
1036 /** Clear all downloaded content information. */
1037 void ClientNetworkContentSocketHandler::Clear()
1039 for (ContentIterator iter
= this->infos
.Begin(); iter
!= this->infos
.End(); iter
++) delete *iter
;
1041 this->infos
.Clear();
1042 this->requested
.Clear();
1047 void ClientNetworkContentSocketHandler::OnConnect(bool success
)
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
++;
1056 void ClientNetworkContentSocketHandler::OnDisconnect()
1058 for (ContentCallback
**iter
= this->callbacks
.Begin(); iter
!= this->callbacks
.End(); /* nothing */) {
1059 ContentCallback
*cb
= *iter
;
1061 if (iter
!= this->callbacks
.End() && *iter
== cb
) iter
++;
1065 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(const ContentInfo
*ci
)
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
++;
1074 void ClientNetworkContentSocketHandler::OnDownloadProgress(const ContentInfo
*ci
, int bytes
)
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
++;
1083 void ClientNetworkContentSocketHandler::OnDownloadComplete(ContentID cid
)
1085 ContentInfo
*ci
= this->GetContent(cid
);
1086 if (ci
!= nullptr) {
1087 ci
->state
= ContentInfo::ALREADY_HERE
;
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
++;
1097 #endif /* ENABLE_NETWORK */