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"
24 #include "table/strings.h"
26 #if defined(WITH_ZLIB)
30 #include "../safeguards.h"
52 bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(
Packet *p)
65 for (uint j = 0; j <
sizeof(ci->
md5sum); j++) {
107 proc = AI::HasAILibrary;
break;
115 proc = Game::HasGameLibrary;
break;
128 if (proc(ci,
true)) {
132 if (proc(ci,
false)) ci->
upgrade =
true;
159 this->OnReceiveContentInfo(ici);
177 this->OnReceiveContentInfo(ci);
226 uint p_count =
min(count, (
SEND_MTU -
sizeof(
PacketSize) -
sizeof(byte) -
sizeof(byte) -
sizeof(uint16)) /
sizeof(uint32));
231 for (uint i = 0; i < p_count; i++) {
237 content_ids += p_count;
248 if (cv == NULL)
return;
253 assert(cv->
Length() < 255);
254 assert(cv->
Length() < (
SEND_MTU -
sizeof(
PacketSize) -
sizeof(byte) -
sizeof(uint8)) / (send_md5sum ? 20 :
sizeof(uint32)));
257 p->Send_uint8(cv->
Length());
261 p->Send_uint8((byte)ci->
type);
263 if (!send_md5sum)
continue;
265 for (uint j = 0; j <
sizeof(ci->
md5sum); j++) {
266 p->Send_uint8(ci->
md5sum[j]);
313 if (files == 0)
return;
328 uint count = content.
Length();
334 uint bytes = (10 + 1) * count + 1;
335 char *content_request = MallocT<char>(bytes);
336 const char *
lastof = content_request + bytes - 1;
338 char *p = content_request;
340 p +=
seprintf(p, lastof,
"%d\n", *
id);
356 uint count = content.
Length();
365 uint p_count =
min(count, (
SEND_MTU -
sizeof(
PacketSize) -
sizeof(byte) -
sizeof(uint16)) /
sizeof(uint32));
370 for (uint i = 0; i < p_count; i++) {
376 content_ids += p_count;
392 static char buf[MAX_PATH];
406 #if defined(WITH_ZLIB)
409 if (ftmp == NULL)
return false;
411 gzFile fin = gzdopen(fileno(ftmp),
"rb");
414 if (fin == NULL || fout == NULL) {
419 int read = gzread(fin, buff,
sizeof(buff));
434 gzerror(fin, &errnum);
435 if (errnum != 0 && errnum != Z_STREAM_END) ret =
false;
438 if (read < 0 || (
size_t)read != fwrite(buff, 1, read, fout)) {
451 }
else if (ftmp != NULL) {
456 if (fout != NULL) fclose(fout);
464 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(
Packet *p)
481 size_t toRead = (size_t)(p->
size - p->
pos);
482 if (fwrite(p->
buffer + p->
pos, 1, toRead, this->curFile) != toRead) {
484 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE,
WL_ERROR);
492 this->OnDownloadProgress(this->
curInfo, (
int)toRead);
515 if (filename == NULL || (this->
curFile = fopen(filename,
"wb")) == NULL) {
518 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE,
WL_ERROR);
551 this->OnDownloadComplete(this->
curInfo->
id);
558 void ClientNetworkContentSocketHandler::OnFailure()
569 long size = ftell(this->
curFile);
570 if (size > 0) this->OnDownloadProgress(this->
curInfo, (
int)-size);
579 assert(data == NULL || length != 0);
600 if (fwrite(data, 1, length, this->
curFile) != length) {
605 this->OnDownloadProgress(this->
curInfo, (
int)length);
629 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
631 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
635 char *p = strchr(str,
'\n');
636 check_and_terminate(p);
642 p = strchr(str,
',');
643 check_and_terminate(p);
648 p = strchr(str,
',');
649 check_and_terminate(p);
654 p = strchr(str,
',');
655 check_and_terminate(p);
661 if (strncmp(str,
"ottd", 4) == 0) {
670 p = strrchr(str,
'/');
680 for (uint i = 0; i < 2; i++) {
681 p = strrchr(tmp,
'.');
682 check_and_terminate(p);
699 #undef check_and_terminate
707 http_response_index(-2),
733 virtual void OnFailure()
736 _network_content_client.OnConnect(
false);
739 virtual void OnConnect(SOCKET s)
741 assert(_network_content_client.
sock == INVALID_SOCKET);
743 _network_content_client.
sock = s;
744 _network_content_client.
Reopen();
745 _network_content_client.OnConnect(
true);
766 if (this->
sock == INVALID_SOCKET)
return;
769 this->OnDisconnect();
818 if (ci->
id == cid)
return ci;
910 if (ci == child)
continue;
934 for (uint i = 0; i < tree.
Length(); i++) {
995 bool force_selection =
false;
997 if ((*iter)->IsSelected()) sel_count++;
1000 if (sel_count == 0) {
1006 if (force_selection)
continue;
1016 force_selection =
true;
1021 if (force_selection)
continue;
1048 void ClientNetworkContentSocketHandler::OnConnect(
bool success)
1053 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1057 void ClientNetworkContentSocketHandler::OnDisconnect()
1062 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1066 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(
const ContentInfo *ci)
1071 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1075 void ClientNetworkContentSocketHandler::OnDownloadProgress(
const ContentInfo *ci,
int bytes)
1080 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1084 void ClientNetworkContentSocketHandler::OnDownloadComplete(
ContentID cid)
1094 if (iter != this->
callbacks.
End() && *iter == cb) iter++;