int DownloadFile(char* ip, const char* fileName) {
int fd = -1;
CellFsErrno check_status;
check_status = cellFsOpen(fileName, CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_APPEND, &fd, NULL, 0);
if (check_status != CELL_OK) {
printf("[DownloadFile] failed with cellFsOpen() 0x%x\n", check_status);
return -1;
}
bool connected = false;
int sock;
int offset = 0;
int total = 0;
struct hostent *host;
struct sockaddr_in sockaddr;
char responseBuffer[RESPONSE_BUFFER_SIZE];
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(6969); //Choose any port you want (needs to be opened on your server)
sockaddr.sin_addr.s_addr = inet_addr(ip);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
printf("[DownloadFile] failed connecting with socket\n");
return -1;
}
char output[100];
int r, size = 1054096; //size needs to be hardcoded OR you need to request the filesize from the socket server beforehand
cellMsgDialogOpen2(
CELL_MSGDIALOG_TYPE_SE_TYPE_NORMAL | CELL_MSGDIALOG_TYPE_BUTTON_TYPE_NONE | CELL_MSGDIALOG_TYPE_DISABLE_CANCEL_ON | CELL_MSGDIALOG_TYPE_DEFAULT_CURSOR_NONE | CELL_MSGDIALOG_TYPE_PROGRESSBAR_SINGLE,
"Downloading Update...",
NULL,
NULL,
NULL
);
int lastPercent = 0;
int progress = 0;
int delta;
while ((r = recv(sock, responseBuffer, sizeof(responseBuffer), 0)) > 0) {
total += r;
delta = total * 100 / size - progress;
progress += delta;
cellMsgDialogProgressBarInc(CELL_MSGDIALOG_PROGRESSBAR_INDEX_SINGLE, delta);
cellFsWrite(fd, responseBuffer, r, NULL);
offset++;
}
cellMsgDialogClose(0);
if (total == 0) {
printf("[DownloadFile] No data was received\n");
return -1;
}
printf("[DownloadFile] Done reading %d bytes\n", total);
return 0;
}