Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. int DownloadFile(char* ip, const char* fileName) {
  2.     int fd = -1;
  3.     CellFsErrno check_status;
  4.     check_status = cellFsOpen(fileName, CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_APPEND, &fd, NULL, 0);
  5.     if (check_status != CELL_OK) {
  6.         printf("[DownloadFile] failed with cellFsOpen() 0x%x\n", check_status);
  7.         return -1;
  8.     }
  9.     bool connected = false;
  10.     int sock;
  11.     int offset = 0;
  12.     int total = 0;
  13.     struct hostent *host;
  14.     struct sockaddr_in sockaddr;
  15.     char responseBuffer[RESPONSE_BUFFER_SIZE];
  16.     sockaddr.sin_family = AF_INET;
  17.     sockaddr.sin_port = htons(6969); //Choose any port you want (needs to be opened on your server)
  18.     sockaddr.sin_addr.s_addr = inet_addr(ip);
  19.     sock = socket(AF_INET, SOCK_STREAM, 0);
  20.     if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
  21.         printf("[DownloadFile] failed connecting with socket\n");
  22.         return -1;
  23.     }
  24.  
  25.     char output[100];
  26.     int r, size = 1054096; //size needs to be hardcoded OR you need to request the filesize from the socket server beforehand
  27.     cellMsgDialogOpen2(
  28.         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,
  29.         "Downloading Update...",
  30.         NULL,
  31.         NULL,
  32.         NULL
  33.     );
  34.     int lastPercent = 0;
  35.     int progress = 0;
  36.     int delta;
  37.    
  38.     while ((r = recv(sock, responseBuffer, sizeof(responseBuffer), 0)) > 0) {
  39.         total += r;
  40.         delta = total * 100 / size - progress;
  41.         progress += delta;
  42.         cellMsgDialogProgressBarInc(CELL_MSGDIALOG_PROGRESSBAR_INDEX_SINGLE, delta);
  43.         cellFsWrite(fd, responseBuffer, r, NULL);
  44.         offset++;
  45.     }
  46.  
  47.     cellMsgDialogClose(0);
  48.  
  49.     if (total == 0) {
  50.         printf("[DownloadFile] No data was received\n");
  51.         return -1;
  52.     }
  53.  
  54.     printf("[DownloadFile] Done reading %d bytes\n", total);
  55.     return 0;
  56. }