Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

<< MV Smart Downloader >>

Torna indietro



Versione: 3.02.01 Win32





  1. #define VERSIONE_PROGRAMMA "MV Smart Downloader v 3.02.01 by mamo139"
  2.  
  3. //#define console_mode
  4.  
  5. #include <gtk/gtk.h>
  6.  
  7. #include <stdio.h>
  8. #include <winsock2.h>
  9. #include <windows.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <math.h>
  14.  
  15. const int processi_num = 20;
  16. const int download_buffer_size = 1024;
  17.  
  18. const int socket_timeout = 10000; //timeout per la conessione, in millisecondi direi
  19. const int backup_time = 180000; //ogni quanto effettuare il backup in millisecondi
  20. const int speed_info_time = 5; //ogni quanto viene comunicata la velocità
  21. const int possibilit_speed = 2; //moltiplicato per speed_info_time da il numero di secondi prima della disconnessione per lentezza
  22.  
  23. const int limite_speed = 1; //disconnette proxy piu lenti, velocità in kb/s
  24.  
  25. long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset);
  26. bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec);
  27.  
  28. long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset);
  29. int mv_decode(char *string, long key1, long key2);
  30. long mv_video_lunghezza(char *video_id, char *proxy_ip);
  31.  
  32. int proxylist_counter(char *file);
  33. int proxylist_loader(char **proxylist,char * file);
  34.  
  35. int funzione(LPVOID param);
  36. int video_backup(LPVOID param);
  37. #ifdef console_mode
  38. int tell_me(LPVOID param);
  39. #endif
  40.  
  41. int main_thread();
  42. int main_grafica_uploader();
  43.  
  44. char * read_clipboard();
  45.  
  46. struct connection_data {
  47. int processo_id;
  48. char processo_status;
  49. char ** proxylist;
  50. float proxy_speed;
  51. long proxy_num;
  52. long proxy_point;
  53. char * video_id;
  54. char * video_buffer;
  55. long video_point;
  56. long video_starting_point;
  57. long video_current_point;
  58. long video_lunghezza;
  59. bool reset;
  60. float percentage;
  61. };
  62.  
  63. //***************** gtk widget
  64. GtkWidget *finestra1;
  65. GtkWidget *tabella1;
  66.  
  67. GtkWidget *label_mvid, *label_processi_scaricanti, *label_processi_terminati;
  68. GtkWidget *label_time, *label_global_speed;
  69.  
  70. GtkWidget *progressbar_main;
  71. GtkWidget **progressbar_parziale, **label_download_parziale, *tabella_download_parziale, *scrolledwindow_download_parziale;
  72.  
  73. GtkWidget *textview_console, *scrolledwindow_console;
  74. GtkTextBuffer *textview_console_buffer;
  75.  
  76. int console_book_size = 100;
  77. char *console_booking, **console_book;
  78. int book_manager();
  79. void textview_printf(char *string);
  80.  
  81. //variabili globali
  82. char main_grafica_uploader_start = 0;
  83.  
  84. double global_speed = 0;
  85. double global_percentage = 0;
  86. long global_processi_terminati = 0;
  87. long global_processi_scaricanti = 0;
  88. clock_t global_time_start, global_time_end;
  89. struct connection_data *global_dati;
  90.  
  91. //*********************************************//
  92. //******************* MAIN ********************//
  93. //*********************************************//
  94. int main(int argc, char *argv[]){
  95.  
  96. long x;
  97. GError ** thread_error;
  98.  
  99. g_type_init();
  100.  
  101. /* Secure glib */
  102. if( ! g_thread_supported() )
  103. g_thread_init( NULL );
  104. /* Secure gtk */
  105. gdk_threads_init();
  106. /* Obtain gtk's global lock */
  107. gdk_threads_enter();
  108.  
  109. gtk_init (&argc, &argv);//inizializzo l'ambiente gtk
  110.  
  111. //********** FINESTRA1 *************//
  112. finestra1 = gtk_window_new(GTK_WINDOW_TOPLEVEL); //creo la finestra principale
  113. gtk_window_set_title(GTK_WINDOW(finestra1), VERSIONE_PROGRAMMA); //setto il titolo della finestra principale
  114. gtk_container_set_border_width(GTK_CONTAINER(finestra1), 3); //setto il bordo della finestra principale
  115. gtk_window_set_resizable(GTK_WINDOW(finestra1), FALSE); //rendo non resizable la finestra principale
  116. gtk_window_set_position(GTK_WINDOW(finestra1), GTK_WIN_POS_CENTER); //posizione iniziale finestra
  117. g_signal_connect(G_OBJECT(finestra1), "delete_event", G_CALLBACK(gtk_main_quit), NULL); //evento chiusura finestra princ
  118.  
  119. //********** TABELLA1 *************//
  120. tabella1 = gtk_table_new(25, 10, TRUE); //creo la tabella principale
  121. gtk_container_add(GTK_CONTAINER(finestra1), tabella1); //inserisco la tabella princ. nella finestra princ.
  122.  
  123. //label_mvid, label_processi_scaricanti, label_processi_terminati
  124. label_mvid = gtk_label_new("Waiting for MegaVideo ID...");
  125. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_mvid, 0, 2, 0, 1);
  126. label_processi_scaricanti = gtk_label_new("...");
  127. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_scaricanti, 6, 8, 3, 4);
  128. label_processi_terminati = gtk_label_new("...");
  129. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_terminati, 8, 10, 3, 4);
  130. //label_time
  131. global_time_start = clock();
  132. label_time = gtk_label_new("time...");
  133. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_time, 8, 10, 0, 1);
  134. //label_global_speed
  135. label_global_speed = gtk_label_new("speed...");
  136. gtk_table_attach_defaults(GTK_TABLE(tabella1), label_global_speed, 4, 6, 3, 4);
  137.  
  138. //progressbar_main
  139. progressbar_main = gtk_progress_bar_new();
  140. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main), "Initializing...");
  141. gtk_table_attach_defaults(GTK_TABLE(tabella1), progressbar_main, 0, 10, 1, 3);
  142.  
  143. //textview_console
  144. textview_console = gtk_text_view_new();
  145. gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview_console), GTK_WRAP_WORD_CHAR);
  146. gtk_text_view_set_editable(GTK_TEXT_VIEW(textview_console),FALSE);
  147.  
  148. textview_console_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_console));
  149. gtk_text_buffer_set_text(textview_console_buffer, "textview_console ready...\n", -1);
  150.  
  151. scrolledwindow_console = gtk_scrolled_window_new(NULL,NULL);
  152. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_console), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  153. gtk_container_add(GTK_CONTAINER(scrolledwindow_console), textview_console);
  154. gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_console, 0, 10, 18, 25);
  155.  
  156. //console booking system
  157. console_booking = (char *) malloc(console_book_size * sizeof(char));
  158. console_book = (char **) malloc(console_book_size * sizeof(char *));
  159. for(x=0;x<console_book_size;x++){
  160. console_book[x] = (char *) malloc(1000 * sizeof(char));
  161. console_booking[x] = 1;
  162. }
  163. g_timeout_add(50, (GSourceFunc) book_manager, NULL);
  164.  
  165. //scrolledwindow download parziali
  166. tabella_download_parziale = gtk_table_new(processi_num*2, 10, TRUE);
  167. scrolledwindow_download_parziale = gtk_scrolled_window_new(NULL,NULL);
  168. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  169. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), tabella_download_parziale);
  170. gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_download_parziale, 0, 4, 3, 18);
  171.  
  172. progressbar_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
  173. for(x=0;x<processi_num;x++){
  174. progressbar_parziale[x] = gtk_progress_bar_new();
  175. gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), progressbar_parziale[x], 0, 10, x*2, x*2+1);
  176. }
  177.  
  178. label_download_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
  179. for(x=0;x<processi_num;x++){
  180. label_download_parziale[x] = gtk_label_new("label...");
  181. gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), label_download_parziale[x], 0, 10, x*2+1, x*2+2);
  182. }
  183.  
  184. gtk_widget_show_all(finestra1);
  185.  
  186. //********** lancio main_thread() *************//
  187. g_thread_create((GThreadFunc) main_thread, NULL, 0, thread_error);
  188.  
  189. // main_grafica_uploader() si occupa di aggiornare la grafica durante il download
  190. //g_idle_add((GSourceFunc) main_grafica_uploader, NULL);
  191. g_timeout_add(100, (GSourceFunc) main_grafica_uploader, NULL);
  192.  
  193. gtk_main();
  194.  
  195. /* Release gtk's global lock */
  196. gdk_threads_leave();
  197.  
  198. return 0;
  199. }
  200.  
  201. int main_grafica_uploader(){
  202. if(main_grafica_uploader_start == 0)
  203. return 1;
  204.  
  205. long x;
  206. long time;
  207. char *console_string;
  208.  
  209. if(global_percentage < 1)
  210. global_time_end = clock();
  211.  
  212. console_string = (char *) malloc(1000*sizeof(char));
  213. time = (global_time_end-global_time_start)/CLOCKS_PER_SEC;
  214.  
  215. gdk_threads_enter();
  216.  
  217. //titolo finestra
  218. sprintf(console_string,"%.2f%% %s", global_percentage*100, VERSIONE_PROGRAMMA);
  219. gtk_window_set_title(GTK_WINDOW(finestra1), console_string);
  220.  
  221. //labels
  222. sprintf(console_string,"%d su %d scaricanti", global_processi_scaricanti, (processi_num-global_processi_terminati));
  223. gtk_label_set_text(GTK_LABEL(label_processi_scaricanti), console_string);
  224.  
  225. sprintf(console_string,"%d su %d terminati", global_processi_terminati, processi_num);
  226. gtk_label_set_text(GTK_LABEL(label_processi_terminati), console_string);
  227.  
  228. sprintf(console_string,"%02d:%02d:%02d", (time/60/60),(time/60)%60, time%60);
  229. gtk_label_set_text(GTK_LABEL(label_time), console_string);
  230.  
  231. sprintf(console_string,"%.2f KB/sec", global_speed);
  232. gtk_label_set_text(GTK_LABEL(label_global_speed), console_string);
  233.  
  234. //progressbar_main
  235. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)global_percentage));
  236. sprintf(console_string,"%.2f%% (%.2f KB/sec)",global_percentage*100,global_speed);
  237. gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main),console_string);
  238.  
  239. for(x=0;x<processi_num;x++){
  240. if(global_dati[x].percentage>1) global_dati[x].percentage=1;
  241.  
  242. gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)global_dati[x].percentage));
  243.  
  244. sprintf(console_string,"%d: proxy %d (%.2f KB/sec)",global_dati[x].processo_id,global_dati[x].proxy_point, global_dati[x].proxy_speed);
  245. gtk_label_set_text(GTK_LABEL(label_download_parziale[x]), console_string);
  246.  
  247. //sprintf(console_string,"%.2f%%",global_dati[x].percentage*100);
  248. //gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_parziale[x]),console_string);
  249. }
  250.  
  251. while(gtk_events_pending())
  252. gtk_main_iteration();
  253.  
  254. gdk_threads_leave();
  255.  
  256. free(console_string);
  257.  
  258. return 1;
  259. }
  260.  
  261. int main_thread(){
  262. FILE *stream;
  263. long x, y, proxy_num, video_bytes=0;
  264. char **proxylist;
  265. char *video_buffer;
  266. bool wait=1;
  267. int processi_terminati;
  268. float overall_speed = 0;
  269. float download_percentage = 0;
  270. float tot_download_percentage = 0;
  271. int download_attivi = 0;
  272. struct connection_data dati_b;
  273. char *console_string;
  274.  
  275. console_string = (char *) malloc(1000 * sizeof(char));
  276.  
  277. DWORD ThreadId;
  278.  
  279. char *video_id;
  280.  
  281. //********* VARIABILI *********//
  282. video_id = read_clipboard();
  283.  
  284. if(video_id == 0 || strlen(video_id) != 8){
  285. #ifdef console_mode
  286. printf("La stringa che hai in memoria non è un ID megavideo valido!\n");
  287. #endif
  288. gtk_label_set_text(GTK_LABEL(label_mvid), "ID megavideo non valido...");
  289. textview_printf("ID megavideo non valido...\n");
  290.  
  291. return 0;
  292. }
  293. else{
  294. #ifdef console_mode
  295. printf("ID video letto dalla clipboard: %s\n",video_id);
  296. #endif
  297. sprintf(console_string, "MegaVideo ID: %s",video_id);
  298. gtk_label_set_text(GTK_LABEL(label_mvid), console_string);
  299. sprintf(console_string, "MegaVideo ID: %s\n",video_id);
  300. textview_printf(console_string);
  301. }
  302.  
  303.  
  304. //******************************//
  305. //*** CARICAMENTO PROXY LIST ***//
  306. //******************************//
  307. #ifdef console_mode
  308. printf("PROXYLIST: caricamento proxylist...\n");
  309. #endif
  310. textview_printf("PROXYLIST: caricamento proxylist...\n");
  311.  
  312. proxy_num = proxylist_counter("proxylist.txt");
  313.  
  314. #ifdef console_mode
  315. printf("PROXYLIST: numero dei proxy: %d\n",proxy_num);
  316. #endif
  317. sprintf(console_string, "PROXYLIST: numero dei proxy: %d\n",proxy_num);
  318. textview_printf(console_string);
  319.  
  320. proxylist = (char **) malloc(proxy_num * sizeof(char *));
  321. for(x=0;x<proxy_num;x++)
  322. proxylist[x] = (char *) malloc(25 * sizeof(char));
  323. proxylist_loader(proxylist,"proxylist.txt");
  324. //for(x=0;x<proxy_num;x++)
  325. // printf("%d: %s\n",x,proxylist[x]);
  326. #ifdef console_mode
  327. printf("PROXYLIST: caricamento proxylist completato!\n\n");
  328. #endif
  329. textview_printf("PROXYLIST: caricamento proxylist completato!\n\n");
  330.  
  331. //*********************************//
  332. //*** CONTROLLO LUNGHEZZA VIDEO ***//
  333. //*********************************//
  334. #ifdef console_mode
  335. printf("VIDEO: id: %s\n",video_id);
  336. #endif
  337. sprintf(console_string, "VIDEO: id: %s\n",video_id);
  338. textview_printf(console_string);
  339. #ifdef console_mode
  340. printf("VIDEO: connessione per trovare lunghezza video...\n");
  341. #endif
  342. textview_printf("VIDEO: connessione per trovare lunghezza video...\n");
  343. x=0;
  344. while(video_bytes > 1000000000 || video_bytes < 100){
  345. video_bytes = 0;
  346. video_bytes = mv_video_lunghezza(video_id, proxylist[x]);
  347. x++;
  348. }
  349. #ifdef console_mode
  350. printf("\n");
  351. #endif
  352. textview_printf("\n");
  353.  
  354. //******************************//
  355. //*** CREAZIONE BUFFER VIDEO ***//
  356. //******************************//
  357. video_buffer = (char *) calloc (video_bytes, sizeof(char));
  358. for(x=0;x<video_bytes;x++)
  359. video_buffer[x] = '\0';
  360. #ifdef console_mode
  361. printf("BUFFER: buffer caricato!\n\n");
  362. #endif
  363. textview_printf("BUFFER: buffer caricato!\n\n");
  364.  
  365. //**********************************//
  366. //*** CARICAMENTO BACKUP MANAGER ***//
  367. //**********************************//
  368. #ifdef console_mode
  369. printf("BACKUP: attivazione...\n\n");
  370. #endif
  371. textview_printf("BACKUP: attivazione...\n\n");
  372. dati_b.video_buffer = video_buffer;
  373. dati_b.video_lunghezza = video_bytes;
  374. dati_b.processo_status = 1;
  375. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) video_backup, &dati_b, 0, &ThreadId);
  376.  
  377. //***********************************************//
  378. //*** CARICAMENTO PROXY E DOWNLOAD SIMULTANEI ***//
  379. //***********************************************//
  380. #ifdef console_mode
  381. printf("CONNECTION MANAGER: attivazione in corso...\n");
  382. printf("CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num);
  383. #endif
  384.  
  385. textview_printf("CONNECTION MANAGER: attivazione in corso...\n");
  386. sprintf(console_string, "CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num);
  387. textview_printf(console_string);
  388. struct connection_data *dati;//strumento di trasmissione informazioni alle funzioni asincrone
  389. dati = (struct connection_data *) malloc(processi_num * sizeof(struct connection_data));
  390. for(x=0;x<processi_num;x++){//attivazione processi
  391. dati[x].processo_id = x;
  392. dati[x].processo_status = 1;
  393. dati[x].proxylist = proxylist;
  394. dati[x].proxy_speed = 0;
  395. dati[x].proxy_num = proxy_num;
  396. dati[x].proxy_point = (unsigned int) 0;//x*proxy_num/processi_num;
  397. dati[x].video_id = video_id;
  398. dati[x].video_buffer = video_buffer;
  399. dati[x].video_point = x*((unsigned long)video_bytes/processi_num);
  400. dati[x].video_starting_point = dati[x].video_point;
  401. dati[x].video_current_point = dati[x].video_point;
  402. dati[x].video_lunghezza = video_bytes;
  403. dati[x].reset = 0;
  404. dati[x].percentage = 0;
  405. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funzione, &dati[x], 0, &ThreadId);
  406. #ifdef console_mode
  407. printf("CONNECTION MANAGER: attivazione ID: %d\n",dati[x].processo_id);
  408. #endif
  409. sprintf(console_string, "CONNECTION MANAGER: attivazione ID: %d\n",dati[x].processo_id);
  410. textview_printf(console_string);
  411. }
  412. #ifdef console_mode
  413. printf("CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati[x].processo_id);
  414. #endif
  415. sprintf(console_string, "CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati[x].processo_id);
  416. textview_printf(console_string);
  417.  
  418. #ifdef console_mode
  419. //attivazione tell_me
  420. CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) tell_me, dati, 0, &ThreadId);
  421. #endif
  422.  
  423. global_dati = dati;
  424. main_grafica_uploader_start = 1; //attivo main_grafica_uploader()
  425.  
  426. while(wait){ //attività di controllo funzioni asincrone
  427. Sleep(1000);
  428. //system("cls");
  429.  
  430. processi_terminati=0;
  431. for(x=0;x<processi_num;x++)
  432. if(dati[x].processo_status == 0) processi_terminati++;
  433. if(processi_terminati == processi_num){
  434. global_speed = 0;
  435. global_percentage = 1;
  436. break;
  437. }
  438.  
  439. #ifdef console_mode
  440. printf("\nCONNECTION MANAGER: %d su %d segmenti terminati.\n\n",processi_terminati,processi_num);
  441. #endif
  442.  
  443. overall_speed = 0; download_attivi=0;
  444. tot_download_percentage = 0;
  445. for(x=0;x<processi_num;x++){
  446. overall_speed = overall_speed + dati[x].proxy_speed;
  447. if(dati[x].proxy_speed >0) download_attivi++;
  448.  
  449. //****************************//
  450. //***** DOWNLOAD GRAFICA *****//
  451. //****************************//
  452. #ifdef console_mode
  453. if(dati[x].processo_id < 10) printf(" ");
  454. printf("%d: ",dati[x].processo_id);
  455. #endif
  456.  
  457. download_percentage = (float)100*(dati[x].video_current_point-dati[x].video_starting_point)/dati[1].video_starting_point;
  458. if(dati[x].processo_status == 0) download_percentage = 100;
  459. tot_download_percentage = tot_download_percentage+download_percentage/processi_num;
  460.  
  461. dati[x].percentage = (float) download_percentage/100;
  462.  
  463. #ifdef console_mode
  464. printf("|");
  465. for(y=0;y<33;y++)
  466. if (download_percentage == 100) printf("X");
  467. else if(y*100/33 < download_percentage) printf("x");
  468. else printf("-");
  469. printf("| %.2f%%\x09%.3f\x09proxy %d\n", download_percentage, dati[x].proxy_speed,dati[x].proxy_point);
  470. #endif
  471.  
  472. }
  473. #ifdef console_mode
  474. printf("\nCONNECTION MANAGER: %.2f%% SPEED: %.3f kbs (%d su %d scaricanti)\n\n",tot_download_percentage, overall_speed,download_attivi,(processi_num-processi_terminati));
  475. #endif
  476.  
  477. //salvo i dati nelle variabili globali
  478. global_speed = overall_speed;
  479. global_percentage = ((double)tot_download_percentage/100);
  480. global_processi_scaricanti = download_attivi;
  481. global_processi_terminati = processi_terminati;
  482.  
  483.  
  484. #ifdef console_mode
  485. printf("\ntell me:\n");
  486. #endif
  487. }
  488. #ifdef console_mode
  489. printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n");
  490. #endif
  491. textview_printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n");
  492. dati_b.processo_status = 0;//termino il sistema di backup
  493.  
  494. //********************************//
  495. //*** SCRITTURA FILE SCARICATO ***//
  496. //********************************//
  497. #ifdef console_mode
  498. printf("BUFFER: scrittura buffer su file in corso...\n");
  499. #endif
  500. textview_printf("BUFFER: scrittura buffer su file in corso...\n");
  501.  
  502. sprintf(console_string, "%s.flv", video_id);
  503. stream = fopen(console_string,"wb");
  504. fwrite(video_buffer,1,video_bytes,stream);
  505. fclose(stream);
  506. #ifdef console_mode
  507. printf("BUFFER: buffer scritto!\n\n");
  508. #endif
  509. textview_printf("BUFFER: buffer scritto!\n\n");
  510.  
  511. return 1;
  512. }
  513.  
  514. //********************************************************************//
  515. #ifdef console_mode
  516. int tell_me(LPVOID param){
  517. struct connection_data *dati = (struct connection_data *) param;
  518. int proc, proxy;
  519. char comando;
  520. while(1){
  521. printf(">");
  522. scanf("%c",&comando);
  523. if(comando == 'r'){ //chiude connessione del processo e la riapre con il proxy selezionato
  524. printf("RESET ");
  525. printf("processo:");
  526. scanf("%d",&proc);
  527. printf("proxy:",proc);
  528. scanf("%d",&proxy);
  529. printf("\nRESET OK: processo %d proxy %d\n",proc,proxy);
  530. if(proxy>0)proxy=proxy-1;
  531.  
  532. dati[proc].reset = 1;
  533. dati[proc].proxy_point = proxy;
  534. }
  535. }
  536. return 1;
  537. }
  538. #endif
  539.  
  540. //********************************************************************//
  541. int funzione(LPVOID param){
  542. long video_point_now;
  543. struct connection_data *dati = (struct connection_data *) param;
  544. char *console_string;
  545.  
  546. console_string = (char *) malloc(1000 * sizeof(char));
  547.  
  548. video_point_now = dati->video_point;
  549.  
  550. #ifdef console_mode
  551. printf("PROCESSO %d: inizializzazione in corso...\n", dati->processo_id);
  552. printf("PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now);
  553. printf("PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point);
  554. #endif
  555.  
  556. sprintf(console_string, "PROCESSO %d: inizializzazione in corso...\n", dati->processo_id);
  557. textview_printf(console_string);
  558. sprintf(console_string, "PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now);
  559. textview_printf(console_string);
  560. sprintf(console_string, "PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point);
  561. textview_printf(console_string);
  562.  
  563. while(1){
  564. if(video_point_now < 0) break;
  565. if(video_point_now >= dati->video_lunghezza) break;
  566. //printf("PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point);
  567. sprintf(console_string, "PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point);
  568. textview_printf(console_string);
  569.  
  570. video_point_now = scarica_mv_video(dati->video_id, video_point_now, dati->video_lunghezza, dati->proxylist[dati->proxy_point], dati->video_buffer, &dati->proxy_speed, &dati->video_current_point, &dati->reset);
  571.  
  572. if(video_point_now >= 0)dati->proxy_point++;
  573. if(dati->proxy_point >= dati->proxy_num) dati->proxy_point = 0;//riavvolgi lista proxy
  574. }
  575. dati->processo_status = 0; //segnalo chiusura processo
  576. //printf("PROCESSO %d: terminato!\n", dati->processo_id);
  577. return 1;
  578. }
  579.  
  580. //********************************************************************//
  581. int video_backup(LPVOID param){
  582. struct connection_data *dati = (struct connection_data *) param;
  583. #ifdef console_mode
  584. printf("BACKUP: attivato...\n");
  585. #endif
  586. textview_printf("BACKUP: attivato...\n");
  587.  
  588. while(1){
  589. Sleep(backup_time);
  590. if(dati->processo_status == 0){
  591. #ifdef console_mode
  592. printf("BACKUP: TERMINATO!\n");
  593. #endif
  594. textview_printf("BACKUP: TERMINATO!\n");
  595. break;
  596. }
  597.  
  598. FILE *stream;
  599. //printf("\nBACKUP: scrittura buffer su file in corso...\n");
  600. stream = fopen("video_temp.flv","wb");
  601. fwrite(dati->video_buffer,1,dati->video_lunghezza,stream);
  602. fclose(stream);
  603. //printf("BACKUP: buffer scritto!\n\n");
  604. }
  605.  
  606. #ifdef console_mode
  607. printf("BACKUP: terminato!\n");
  608. #endif
  609. textview_printf("BACKUP: terminato!\n");
  610. return 1;
  611. }
  612.  
  613. //********************************************************************//
  614. int proxylist_counter(char *file){
  615. FILE *in;
  616. long in_size;
  617. in = fopen(file,"rb");
  618. fseek(in,0,SEEK_END);
  619. in_size = ftell(in);
  620. rewind(in);
  621. char *buffer;
  622. buffer = (char *) malloc(in_size * sizeof(char));
  623. fread(buffer,1,in_size,in);
  624.  
  625. int numero_proxy=0,x,y=0,z=0,c1=0;
  626. for(x=0;x<in_size;x++)
  627. if(buffer[x]== '\x0a') numero_proxy++;
  628.  
  629. return numero_proxy;
  630. }
  631.  
  632. //********************************************************************//
  633. int proxylist_loader(char **proxylist, char *file){
  634. FILE *in;
  635. long in_size;
  636. in = fopen(file,"rb");
  637. fseek(in,0,SEEK_END);
  638. in_size = ftell(in);
  639. rewind(in);
  640. char *buffer;
  641. buffer = (char *) malloc(in_size * sizeof(char));
  642. fread(buffer,1,in_size,in);
  643.  
  644. int numero_proxy=0,x,y=0,z=0,c1=0;
  645. for(x=0;x<in_size;x++)
  646. if(buffer[x]== '\x0a') numero_proxy++;
  647.  
  648. for(x=0;x<in_size;x++){
  649. if(z>26)printf("ERRORE! Sintassi proxylist non corretta...\n");
  650. if(y>=numero_proxy)break;
  651. if(buffer[x]=='\x0a'){y++;c1=0;z=0;}
  652. else if( buffer[x] > 58 || buffer[x]< 46 )
  653. c1=1;
  654. else if(c1==0){
  655. proxylist[y][z] = buffer[x];
  656. proxylist[y][z+1] = '\0';
  657. z++;
  658. }
  659. }
  660.  
  661.  
  662. return numero_proxy;
  663. }
  664.  
  665. //********************************************************************//
  666. long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset){
  667.  
  668. char *mv_xml_file;
  669. mv_xml_file = (char *) malloc(10000 * sizeof(char));
  670.  
  671. char ckey1[10];
  672. char ckey2[10];
  673. char hash[40];
  674. char serverid[10];
  675.  
  676. char svideo_time[20];
  677. if(video_time>0)
  678. itoa(video_time,svideo_time,10);
  679. else
  680. svideo_time[0]='\0';
  681.  
  682. time_t itempo;
  683. char stempo[20];
  684. itempo = time(NULL);
  685. itoa(itempo,stempo,10);
  686. //printf("time: %s\n",stempo);
  687.  
  688. char url[1000]="http://www.megavideo.com/xml/videolink.php?v=";
  689. char url2[]="&width=1278&id=";
  690. char url3[]="&u=";
  691. strcat(url,video_id);
  692. strcat(url,url2);
  693. strcat(url,stempo);
  694. strcat(url,url3);
  695. //printf("%s\n",url);
  696.  
  697. proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo
  698.  
  699. //printf("%s\n\n",mv_xml_file);
  700. //printf("xml strlen: %d\n",strlen(mv_xml_file));
  701. if(strlen(mv_xml_file) < 100) return video_time;
  702. //printf("inizio analisi xml file...\n");
  703.  
  704. int x,ap_counter=0,i=0, key1,key2;
  705. for(x=0;x<strlen(mv_xml_file);x++){
  706. if(mv_xml_file[x] == 34) ap_counter++;
  707.  
  708. if(ap_counter == 17){ //caricamento key1
  709. if(i>0)ckey1[i-1]=mv_xml_file[x];
  710. i++;
  711. }
  712. if(ap_counter == 18){
  713. ckey1[i-1] = '\0';
  714. i=0;
  715. key1=atoi(ckey1);
  716. }
  717.  
  718. if(ap_counter == 19){ //caricamento key2
  719. if(i>0)ckey2[i-1]=mv_xml_file[x];
  720. i++;
  721. }
  722. if(ap_counter == 20){
  723. ckey2[i-1] = '\0';
  724. i=0;
  725. key2=atoi(ckey2);
  726. }
  727.  
  728. if(ap_counter == 21){ //caricamento hash
  729. if(i>0)hash[i-1]=mv_xml_file[x];
  730. i++;
  731. }
  732. if(ap_counter == 22){
  733. hash[i-1] = '\0';
  734. i=0;
  735. }
  736.  
  737. if(ap_counter == 23){ //caricamento serverid
  738. if(i>0)serverid[i-1]=mv_xml_file[x];
  739. i++;
  740. }
  741. if(ap_counter == 24){
  742. serverid[i-1] = '\0';
  743. i=0;
  744. }
  745. }
  746.  
  747. //printf("key1: %d key2: %d serverid: %s\nhash: %s %d\n",key1,key2,serverid,hash,strlen(hash));
  748. if(strlen(hash)!=32) return video_time;
  749.  
  750. mv_decode(hash, key1, key2);
  751. //printf("decoding ok: %s\n\n",hash);
  752.  
  753. char furl[1000]="http://www";
  754. char furl2[]=".megavideo.com/files/";
  755. char furl3[]="/";
  756. strcat(furl,serverid);
  757. strcat(furl,furl2);
  758. strcat(furl,hash);
  759. strcat(furl,furl3);
  760. strcat(furl,svideo_time);
  761.  
  762.  
  763. char fhost[1000]="www";
  764. char fhost2[]=".megavideo.com";
  765. strcat(fhost,serverid);
  766. strcat(fhost,fhost2);
  767.  
  768. //printf("FINAL DOWNLOAD URL: %s\n",furl);
  769.  
  770. video_time = proxy_scarica(furl, fhost,video_buffer, proxy_ip, 15, video_time, video_lunghezza, proxy_speed, video_current_point, reset);
  771.  
  772. free(mv_xml_file);
  773. return video_time;
  774. }
  775.  
  776. //********************************************************************//
  777. long mv_video_lunghezza(char *video_id, char *proxy_ip){
  778.  
  779. char *mv_xml_file;
  780. mv_xml_file = (char *) malloc(10000 * sizeof(char));
  781. mv_xml_file[0] = '\0';
  782.  
  783. char *console_string;
  784. console_string = (char *) malloc(1000 * sizeof(char));
  785.  
  786. char ckey1[10];
  787. char ckey2[10];
  788. char hash[40];
  789. char serverid[10];
  790.  
  791.  
  792.  
  793. time_t itempo;
  794. char stempo[20];
  795. itempo = time(NULL);
  796. itoa(itempo,stempo,10);
  797. //printf("time: %s\n",stempo);
  798.  
  799. char url[1000]="http://www.megavideo.com/xml/videolink.php?v=";
  800. char url2[]="&width=1278&id=";
  801. char url3[]="&u=";
  802. strcat(url,video_id);
  803. strcat(url,url2);
  804. strcat(url,stempo);
  805. strcat(url,url3);
  806. #ifdef console_mode
  807. printf("VIDEO: %s\n",url);
  808. #endif
  809. sprintf(console_string, "VIDEO: %s\n",url);
  810. textview_printf(console_string);
  811.  
  812. proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo
  813.  
  814. //printf("%s\n\n",mv_xml_file);
  815. //printf("VIDEO: xml strlen: %d\n",strlen(mv_xml_file));
  816.  
  817. if(strlen(mv_xml_file) < 10)
  818. return 0;
  819.  
  820. int x,ap_counter=0,i=0, key1,key2;
  821. for(x=0;x<strlen(mv_xml_file);x++){
  822. if(mv_xml_file[x] == 34) ap_counter++;
  823.  
  824. if(ap_counter == 11){ //caricamento key1
  825. if(i>0)ckey1[i-1]=mv_xml_file[x];
  826. i++;
  827. }
  828. if(ap_counter == 12){
  829. ckey1[i-1] = '\0';
  830. i=0;
  831. key1=atoi(ckey1);
  832. }
  833. }
  834. #ifdef console_mode
  835. printf("VIDEO: bytes: %d\n",key1);
  836. #endif
  837. sprintf(console_string, "VIDEO: bytes: %d\n",key1);
  838. textview_printf(console_string);
  839.  
  840. free(console_string);
  841.  
  842. return key1;
  843. }
  844.  
  845. //********************************************************************//
  846. bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec){
  847.  
  848.  
  849. struct sockaddr_in sock; //la struttura per il socket
  850. struct hostent ip; //la struttura per l'indirizzo
  851. int s;
  852.  
  853. unsigned long cmd;
  854. fd_set fds;
  855. struct timeval timeout;
  856.  
  857.  
  858. bool header = true; //variabili per header
  859. bool invio = false;
  860. char ultimo_carattere = '\0';
  861.  
  862. char msg_response[] = "\0";
  863. char modo[4];
  864.  
  865. //proxy ip
  866. char ipn[10];
  867. int k=0,i=0,nip=0;
  868. int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0;
  869. for(k=0;k<(strlen(proxy_ip)+1);k++){
  870. if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){
  871. ipn[i]=proxy_ip[k];
  872. ipn[i+1]='\0';
  873. i++;
  874. }
  875. else{
  876. if(nip==0)ip1=atoi(ipn);
  877. if(nip==1)ip2=atoi(ipn);
  878. if(nip==2)ip3=atoi(ipn);
  879. if(nip==3)ip4=atoi(ipn);
  880. if(nip==4)ipport=atoi(ipn);
  881. nip++;
  882. i=0;
  883. }
  884. }
  885. //printf("%d %d %d %d %d",ip1,ip2,ip3,ip4,ipport);
  886.  
  887. //********* CONNESSIONE *********//
  888. WORD wVersion;
  889. WSADATA wsaData;
  890. wVersion = MAKEWORD(2, 2);
  891. WSAStartup(wVersion, &wsaData); //winsock startup
  892.  
  893. s = socket(AF_INET, SOCK_STREAM, 0);
  894. cmd=1; // imposto modalità socket non boccante (andrà gestita con select())
  895. ioctlsocket( s, FIONBIO, &cmd);
  896. // dati per Select
  897. timeout.tv_sec = timeout_sec; // numero di secondi per il timeout
  898. timeout.tv_usec = 0; // numero di microsecondi per il timeout
  899. FD_ZERO(&fds);
  900. FD_SET(s, &fds); // aggiungo sd all'elenco dei socket gestiti da select
  901.  
  902.  
  903. sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP
  904. sock.sin_addr.S_un.S_un_b.s_b2=ip2;
  905. sock.sin_addr.S_un.S_un_b.s_b3=ip3;
  906. sock.sin_addr.S_un.S_un_b.s_b4=ip4;
  907. sock.sin_port = htons(ipport); //porta
  908. //ip = *(gethostbyname(host)); // in dico l'indirizzo
  909. //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket
  910. sock.sin_family = AF_INET;
  911.  
  912. connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo
  913. if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione
  914.  
  915. //printf("Connessione al server riuscita...\n");
  916. cmd=0;//ripristino socket bloccante
  917. ioctlsocket( s, FIONBIO, &cmd);
  918.  
  919. struct timeval tv;
  920.  
  921. tv.tv_sec = socket_timeout;
  922. tv.tv_usec = 0;
  923. setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  924.  
  925.  
  926. //********* MESSAGGIO *********//
  927.  
  928.  
  929. //messaggio per il server...
  930. char msg_enter[600] = "GET ";
  931. char protocollo[] = " HTTP/1.1\r\n";
  932. char protocollo2[] = "Host: ";
  933. char protocollo3[] = "\r\n";
  934. //char dettagli1[] = "Range: bytes=0-20\r\n";
  935.  
  936. strcat(msg_enter,fileremoto);
  937. strcat(msg_enter,protocollo);
  938. strcat(msg_enter,protocollo2);
  939. strcat(msg_enter,host);
  940. strcat(msg_enter,protocollo3);
  941. //strcat(msg_enter,dettagli1); //header
  942. strcat(msg_enter,"\r\n");
  943.  
  944.  
  945.  
  946. //printf("HTTP PROTOCOL: %s\n", msg_enter);
  947. send(s, msg_enter, strlen(msg_enter), 0); //invia dati
  948. //printf("Download in corso...\n\n");
  949.  
  950. k = 0;
  951. stringa[0] = '\0';
  952. while(recv(s, msg_response, 1, 0) > 0) {
  953.  
  954. if (!header){ stringa[k] = msg_response[0]; k++; }
  955. //else printf("%c",msg_response[0]);
  956.  
  957. //controllo per non scrivere header nel file.
  958. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false;
  959. ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false;
  960. ultimo_carattere = msg_response[0];
  961.  
  962. //fprintf(fo,"%c",msg_response[0]);
  963. }
  964. //printf("recv fine!\n");
  965.  
  966. stringa[k] = '\0';
  967.  
  968. //printf("File Ricevuto\n");
  969.  
  970.  
  971. }//fine if(select())
  972. else{ //timeout
  973. //printf("Connessione timeout!\n");
  974. return false;
  975. }
  976.  
  977. //operazioni di chiusura!
  978. shutdown(s, 2);
  979. closesocket(s);
  980. WSACleanup();
  981.  
  982. return true;
  983. }
  984.  
  985. //********************************************************************//
  986. long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset){
  987.  
  988. long vid_byte_count = video_time;
  989.  
  990. struct sockaddr_in sock; //la struttura per il socket
  991. struct hostent ip; //la struttura per l'indirizzo
  992. int s;
  993.  
  994. unsigned long cmd;
  995. fd_set fds;
  996. struct timeval timeout;
  997.  
  998. long x;
  999.  
  1000. bool header = true; //variabili per header
  1001. bool invio = false;
  1002. char ultimo_carattere = '\0';
  1003.  
  1004. char msg_response[] = "\0";
  1005. char modo[4];
  1006.  
  1007. //proxy ip
  1008. char ipn[10];
  1009. int k=0,i=0,nip=0;
  1010. int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0;
  1011. for(k=0;k<(strlen(proxy_ip)+1);k++){
  1012. if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){
  1013. ipn[i]=proxy_ip[k];
  1014. ipn[i+1]='\0';
  1015. i++;
  1016. }
  1017. else{
  1018. if(nip==0)ip1=atoi(ipn);
  1019. if(nip==1)ip2=atoi(ipn);
  1020. if(nip==2)ip3=atoi(ipn);
  1021. if(nip==3)ip4=atoi(ipn);
  1022. if(nip==4)ipport=atoi(ipn);
  1023. nip++;
  1024. i=0;
  1025. }
  1026. }
  1027. //printf("%d %d %d %d %d",ip1,ip2,ip3,ip4,ipport);
  1028.  
  1029. //********* CONNESSIONE *********//
  1030. WORD wVersion;
  1031. WSADATA wsaData;
  1032. wVersion = MAKEWORD(2, 2);
  1033. WSAStartup(wVersion, &wsaData); //winsock startup
  1034.  
  1035. s = socket(AF_INET, SOCK_STREAM, 0);
  1036. cmd=1; // imposto modalità socket non boccante (andrà gestita con select())
  1037. ioctlsocket( s, FIONBIO, &cmd);
  1038. // dati per Select
  1039. timeout.tv_sec = timeout_sec; // numero di secondi per il timeout
  1040. timeout.tv_usec = 0; // numero di microsecondi per il timeout
  1041. FD_ZERO(&fds);
  1042. FD_SET(s, &fds); // aggiungo s all'elenco dei socket gestiti da select
  1043.  
  1044.  
  1045. sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP
  1046. sock.sin_addr.S_un.S_un_b.s_b2=ip2;
  1047. sock.sin_addr.S_un.S_un_b.s_b3=ip3;
  1048. sock.sin_addr.S_un.S_un_b.s_b4=ip4;
  1049. sock.sin_port = htons(ipport); //porta
  1050. //ip = *(gethostbyname(host)); // in dico l'indirizzo
  1051. //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket
  1052. sock.sin_family = AF_INET;
  1053.  
  1054. connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo
  1055. if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione
  1056.  
  1057. //printf("Connessione al server riuscita...\n");
  1058. cmd=0;//ripristino socket bloccante
  1059. ioctlsocket( s, FIONBIO, &cmd);
  1060.  
  1061. struct timeval tv;
  1062.  
  1063. tv.tv_sec = socket_timeout;
  1064. tv.tv_usec = 0;
  1065. setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv) );
  1066.  
  1067. //********* MESSAGGIO *********//
  1068.  
  1069. //messaggio per il server...
  1070. char msg_enter[600] = "GET ";
  1071. char protocollo[] = " HTTP/1.1\r\n";
  1072. char protocollo2[] = "Host: ";
  1073. char protocollo3[] = "\r\n";
  1074. //char dettagli1[] = "Range: bytes=0-20\r\n";
  1075.  
  1076. strcat(msg_enter,fileremoto);
  1077. strcat(msg_enter,protocollo);
  1078. strcat(msg_enter,protocollo2);
  1079. strcat(msg_enter,host);
  1080. strcat(msg_enter,protocollo3);
  1081. //strcat(msg_enter,dettagli1); //header
  1082. strcat(msg_enter,"\r\n");
  1083.  
  1084.  
  1085.  
  1086. //printf("Inviato al server:\n\n%s\n", msg_enter);
  1087. send(s, msg_enter, strlen(msg_enter), 0); //invia dati
  1088. //printf("Download in corso...\n\n");
  1089.  
  1090. //orologio
  1091. clock_t start,end;
  1092. double tempo_trascorso = 0;
  1093. long cicli=0;
  1094. long cicli2 = 0;
  1095. int possib = 0;
  1096. bool lentezza = 0;
  1097.  
  1098. long downloaded_bytes = 0;
  1099. char *download_buffer;
  1100. download_buffer = (char *) malloc(download_buffer_size * sizeof(char));
  1101.  
  1102. while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){
  1103.  
  1104. //controllo per non scrivere header nel file.
  1105. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false;
  1106. ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false;
  1107. ultimo_carattere = msg_response[0];
  1108.  
  1109. if(!header) break; //ricevuto header esci dal ciclo
  1110.  
  1111. }
  1112.  
  1113. if(downloaded_bytes > 0)
  1114. while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){
  1115. if(cicli == 0 && msg_response[0] != '\x46'){
  1116. break;
  1117. }//se non ricevo header flv chiudo download
  1118. if( cicli>=9 || video_time == 0 ){
  1119. video_buffer[vid_byte_count] = msg_response[0];
  1120. vid_byte_count++;
  1121. break;
  1122. }
  1123. cicli++;
  1124. }
  1125.  
  1126. start = clock();
  1127. if(downloaded_bytes > 0){
  1128. if(cicli>=9 || video_time == 0) {
  1129. while(1){
  1130. downloaded_bytes = recv(s, download_buffer, download_buffer_size, 0x0008 );
  1131. if(downloaded_bytes <= 0) break;
  1132.  
  1133. if(video_buffer[vid_byte_count] != '\0'){
  1134. vid_byte_count = -1; //disattivo processo
  1135. break; //se i dati sono gia stati scritti mi fermo
  1136. }
  1137.  
  1138. for(x=0;x<downloaded_bytes;x++){
  1139. video_buffer[vid_byte_count] = download_buffer[x]; //scrivo dati nel video_buffer
  1140. vid_byte_count++;
  1141. }
  1142.  
  1143. cicli= cicli + downloaded_bytes; //byte del file scaricati in questa connessione
  1144.  
  1145. end = clock();
  1146. tempo_trascorso = ((double)(end-start))/CLOCKS_PER_SEC;
  1147. if( tempo_trascorso >= speed_info_time ){ //sistema di misurazione velocità
  1148. //printf("SPEED:%d kbs\n",((cicli-cicli2)/(speed_info_time*1024)));
  1149. *proxy_speed = ((double)(cicli-cicli2)/(tempo_trascorso*1024));
  1150.  
  1151. *video_current_point = vid_byte_count;
  1152.  
  1153. //limitatore di lentezza
  1154. //if(((cicli-cicli2)/(speed_info_time*1024)) < limite_speed) printf("SPEED: troppo lento %d avvertimento n %d\n",((cicli-cicli2)/(speed_info_time*1024)),possib);
  1155. if(*proxy_speed < limite_speed && possib >= possibilit_speed){ lentezza=1; break; } //printf("SPEED: disconnesso per lentezza!!\n");
  1156. if(*proxy_speed < limite_speed && possib < possibilit_speed) possib++;
  1157. if(*proxy_speed >= limite_speed) possib=0;
  1158.  
  1159. //reset control
  1160. if(*reset == 1){
  1161. *reset = 0;
  1162. break;
  1163. }
  1164.  
  1165. start = clock();
  1166. cicli2 = cicli;
  1167. }
  1168. }
  1169. }
  1170. }
  1171.  
  1172. //printf("recv fine!\n");
  1173. *proxy_speed = 0; //azzero la velocità
  1174.  
  1175. //cancello ultimi 200 byte
  1176. long j,bersaglio;
  1177. if(video_lunghezza > vid_byte_count && vid_byte_count>=0){
  1178. if(vid_byte_count-200 < video_time)
  1179. bersaglio = video_time;
  1180. else
  1181. bersaglio = vid_byte_count-200;
  1182.  
  1183. for(j=vid_byte_count;j>=bersaglio;j--)
  1184. video_buffer[j] = '\0';
  1185.  
  1186. vid_byte_count=bersaglio;
  1187. }
  1188.  
  1189.  
  1190. }//fine if(select())
  1191. else{ //timeout
  1192. //printf("Connessione timeout!\n");
  1193. return vid_byte_count;
  1194. }
  1195.  
  1196. //operazioni di chiusura!
  1197. shutdown(s, 2);
  1198. closesocket(s);
  1199. WSACleanup();
  1200. return vid_byte_count;
  1201. }
  1202.  
  1203. //********************************************************************//
  1204. int mv_decode(char *string, long key1, long key2){
  1205.  
  1206.  
  1207. ...
  1208.  
  1209. return 1;
  1210.  
  1211. }
  1212.  
  1213. //********************************************************************//
  1214. char * read_clipboard(){
  1215.  
  1216. HGLOBAL clip;
  1217. char *str;
  1218.  
  1219. if (!IsClipboardFormatAvailable(CF_TEXT))
  1220. return 0;
  1221.  
  1222. OpenClipboard(NULL);
  1223.  
  1224. clip = GetClipboardData(CF_TEXT);
  1225. str = (char *) GlobalLock(clip);
  1226.  
  1227. CloseClipboard();
  1228.  
  1229. return str;
  1230. }
  1231.  
  1232.  
  1233.  
  1234. int book_manager(){
  1235. long x;
  1236. for(x=0;x<console_book_size;x++){
  1237. if(console_booking[x]==0){
  1238.  
  1239. GtkTextMark *mark;
  1240. GtkTextIter console_end;
  1241.  
  1242. gtk_text_buffer_get_end_iter(textview_console_buffer, &console_end);
  1243. gtk_text_buffer_insert(textview_console_buffer, &console_end, console_book[x], -1);
  1244.  
  1245. gtk_text_buffer_place_cursor(textview_console_buffer, &console_end);
  1246.  
  1247. mark = gtk_text_buffer_get_insert(textview_console_buffer);
  1248. gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(textview_console), mark, 0.0, TRUE, 0.0, 1.0);
  1249.  
  1250. console_booking[x] = 1;
  1251.  
  1252. }
  1253. }
  1254. return 1;
  1255.  
  1256. }
  1257. void textview_printf(char *string){
  1258.  
  1259.  
  1260. long x;
  1261. for(x=0;x<console_book_size;x++){
  1262. if(console_booking[x] == 1){
  1263. console_booking[x] = 0;
  1264. sprintf(console_book[x],"%s",string);
  1265. break;
  1266. }
  1267. if(x == console_book_size-1)
  1268. x=0;
  1269. }
  1270.  
  1271.  
  1272. }
  1273.  
  1274.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".