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);
225 uint p_count =
min(count, (
SEND_MTU -
sizeof(
PacketSize) -
sizeof(byte) -
sizeof(uint16)) /
sizeof(uint32));
230 for (uint i = 0; i < p_count; i++) {
236 content_ids += p_count;
247 if (cv == NULL)
return;
251 assert(cv->
Length() < 255);
253 (
sizeof(uint8) +
sizeof(uint32) + (send_md5sum ? 16 : 0)));
256 p->Send_uint8(cv->
Length());
260 p->Send_uint8((byte)ci->
type);
262 if (!send_md5sum)
continue;
264 for (uint j = 0; j <
sizeof(ci->
md5sum); j++) {
265 p->Send_uint8(ci->
md5sum[j]);
312 if (files == 0)
return;
327 uint count = content.
Length();
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;
339 p +=
seprintf(p, lastof,
"%d\n", *
id);
355 uint count = content.
Length();
364 uint p_count =
min(count, (
SEND_MTU -
sizeof(
PacketSize) -
sizeof(byte) -
sizeof(uint16)) /
sizeof(uint32));
369 for (uint i = 0; i < p_count; i++) {
375 content_ids += p_count;
391 static char buf[MAX_PATH];
405 #if defined(WITH_ZLIB)
408 if (ftmp == NULL)
return false;
410 gzFile fin = gzdopen(fileno(ftmp),
"rb");
413 if (fin == NULL || fout == NULL) {
418 int read = gzread(fin, buff,
sizeof(buff));
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)) {
450 }
else if (ftmp != NULL) {
455 if (fout != NULL) fclose(fout);
463 bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(
Packet *p)
480 size_t toRead = (size_t)(p->
size - p->
pos);
481 if (fwrite(p->
buffer + p->
pos, 1, toRead, this->curFile) != toRead) {
483 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE,
WL_ERROR);
491 this->OnDownloadProgress(this->
curInfo, (
int)toRead);
514 if (filename == NULL || (this->
curFile = fopen(filename,
"wb")) == NULL) {
517 ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE,
WL_ERROR);
550 this->OnDownloadComplete(this->
curInfo->
id);
557 void ClientNetworkContentSocketHandler::OnFailure()
568 long size = ftell(this->
curFile);
569 if (size > 0) this->OnDownloadProgress(this->
curInfo, (
int)-size);
578 assert(data == NULL || length != 0);
599 if (fwrite(data, 1, length, this->
curFile) != length) {
604 this->OnDownloadProgress(this->
curInfo, (
int)length);
628 #define check_not_null(p) { if ((p) == NULL) { this->OnFailure(); return; } }
630 #define check_and_terminate(p) { check_not_null(p); *(p) = '\0'; }
634 char *p = strchr(str,
'\n');
635 check_and_terminate(p);
641 p = strchr(str,
',');
642 check_and_terminate(p);
647 p = strchr(str,
',');
648 check_and_terminate(p);
653 p = strchr(str,
',');
654 check_and_terminate(p);
660 if (strncmp(str,
"ottd", 4) == 0) {
669 p = strrchr(str,
'/');
679 for (uint i = 0; i < 2; i++) {
680 p = strrchr(tmp,
'.');
681 check_and_terminate(p);
698 #undef check_and_terminate
706 http_response_index(-2),
732 virtual void OnFailure()
735 _network_content_client.OnConnect(
false);
738 virtual void OnConnect(SOCKET s)
740 assert(_network_content_client.
sock == INVALID_SOCKET);
742 _network_content_client.
sock = s;
743 _network_content_client.
Reopen();
744 _network_content_client.OnConnect(
true);
765 if (this->
sock == INVALID_SOCKET)
return;
768 this->OnDisconnect();
817 if (ci->
id == cid)
return ci;
909 if (ci == child)
continue;
933 for (uint i = 0; i < tree.
Length(); i++) {
994 bool force_selection =
false;
996 if ((*iter)->IsSelected()) sel_count++;
999 if (sel_count == 0) {
1005 if (force_selection)
continue;
1015 force_selection =
true;
1020 if (force_selection)
continue;
1047 void ClientNetworkContentSocketHandler::OnConnect(
bool success)
1052 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1056 void ClientNetworkContentSocketHandler::OnDisconnect()
1061 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1065 void ClientNetworkContentSocketHandler::OnReceiveContentInfo(
const ContentInfo *ci)
1070 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1074 void ClientNetworkContentSocketHandler::OnDownloadProgress(
const ContentInfo *ci,
int bytes)
1079 if (iter != this->
callbacks.
End() && *iter == cb) iter++;
1083 void ClientNetworkContentSocketHandler::OnDownloadComplete(
ContentID cid)
1093 if (iter != this->
callbacks.
End() && *iter == cb) iter++;