📄 ants.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/time.h>#include <dirent.h>#include <fcntl.h>#include <host.h>#include <ants.h>#include <http.h>#include <config.h> int buffer_size(int size, int *thread) { int i, q, n; if(!thread) return 0; n = *thread; if(n < 1 ) n = 1; else if(n > 20) n = MAX_THREAD; i = size / n; i >>= 4; if(!i) { /*fixup very tiny file size( < thread*16) */ n = 1; i = size; } while(i < MIN_BUF_SIZE && n > 1) { i <<= 1; n >>= 1; } if(i < MIN_BUF_SIZE) i = MIN_BUF_SIZE; else if(i > MAX_BUF_SIZE) i = MAX_BUF_SIZE; if(n != *thread) *thread = n; if(i > PACKSIZE) { if((q = i % PACKSIZE)) i -= q; } return i;} int statistic(struct ants *owner) { int i, left, this_read, total, thread; long long cost; double percent, speed; static struct timeval tv, last; static int show; struct mission *pms = owner->pms; struct ants *ant = pms->first_ant; if(++show >= 10) show = 0; else return -1; total = pms->size; thread = pms->thread; gettimeofday(&tv, NULL); speed = -1; left = 0; if(pms->oneshot) { pms->oneshot = 0; last = tv; for(i = 0; i < thread; i++) left += (ant[i].end - ant[i].position + 1); pms->count = total - left; } else { for(i = 0; i < thread; i++) left += (ant[i].end - ant[i].position + 1); this_read = (total - left) - pms->count; pms->count = total - left; cost = (tv.tv_sec - last.tv_sec) * 1000000 + (tv.tv_usec + last.tv_usec); last = tv; cost /= 1000; percent = pms->count / (total / 100); this_read /= 1024; speed = (this_read * 1000) /cost; printf("thread %d get skip_lock - - -DOWNLOAD: %4.1f %%- - -DOWNLOAD SPEED : %4.1f Kb/s .\n", owner->thread, percent, speed); } return (int)speed;} void fill_ants(struct ants *ant, struct mission *pms, int size) { int i, bytes, thread, start; start = 0; thread = pms->thread; bytes = pms->size / thread; for(i = 0; i < thread; i++) { ant[i].thread = i + 1; ant[i].pms = pms; ant[i].buf_size = size; ant[i].start = start; ant[i].position = start; ant[i].end = ant[i].start + bytes -1; start = ant[i].end + 1; } ant[thread - 1].end = pms->size - 1;} void state_init(struct mission *pms) { int i, fd, thread; int num[MAX_THREAD]; struct ants *ant = pms->first_ant; fd = pms->fd; thread = pms->thread; for(i = 0; i < thread; i++) num[i] = ant[i].end; lseek(fd, 0, 0); write(fd, pms, sizeof(struct mission)); write(fd, num, sizeof(int) * thread);} void state_save(struct mission *pms) { int i, fd, thread, offset; int num[MAX_THREAD]; struct ants *ant = pms->first_ant; fd = pms->fd; thread = pms->thread; offset = sizeof(struct mission) + (pms->thread * sizeof(int)); for(i = 0; i < thread; i++) num[i] = ant[i].position; lseek(fd, offset, 0); write(fd, num, sizeof(int) * thread);} int scan_temp(char *path) { int i, num, fd; char *p; char filename[MAX_HOSTNAME]; struct dirent **namelist; chdir(path); if(( num = scandir(".", &namelist, 0 , alphasort)) < 0) return -1; for(i = 0; i < num; i++) { strcpy(filename, namelist[i]->d_name); p = rindex(filename, '.'); if(p++) { if(strcmp(p, "nat") == 0) { fd = open(filename, O_RDWR, 0644); if(fd) return fd; } } } return -1;} int build_ants(struct mission *pms, char *name, int *thread) { int size, fd; struct ants *ant; char file[MAX_HOSTNAME]; if(!fill_mission(pms, name)) return 0; size = get_http_size(pms); if(size == -1) return 0; sprintf(file, "%s%s.nat",pms->file_path, pms->file); fd = open(file, O_WRONLY|O_CREAT, 0644); if(fd <= 0) { printf("can't open temp file\n"); return 0; } pms->fd = fd; pms ->size = size; size = buffer_size(size, thread); /* and fixup thread number*/ if(!size) return 0; printf("buffer size is %d.\n", size); pms->thread = *thread; pms->oneshot = 1; pms->count = 0; ant = malloc(sizeof(struct ants) * (*thread)); pms->first_ant = ant; pms->skip_lock.lock = 0; fill_ants(ant, pms, size); state_init(pms); return 1;} int rebuild_ants(int fd, struct mission *pms) { int i, n, one, thread; int num[MAX_THREAD * 2]; int *p; p = num; struct ants *ant; n = 0; one = 1; lseek(fd, 0, 0); read(fd, pms, sizeof(struct mission)); thread = pms->thread; read(fd, num, (sizeof(int) * thread * 2)); pms->fd = fd; for(i = 0; i < thread; i++) { int j; j = i; while((i < thread) && (num[i] == num[i + thread])) /*exclude download 100% thread*/ i++; if(i >= thread) break; if(i != j) { p[n] = num[i]; p[n + thread] = num[n + thread]; p++; } n++; } if(!n) return 0; thread = n; ant = malloc(sizeof(struct ants) * thread); pms->first_ant = ant; pms->skip_lock.lock = 0; pms->oneshot = 1; pms->thread = thread; for(i = 0; i < thread; i++) { ant[i].thread = i + 1; ant[i].pms = pms; ant[i].start = num[i + thread]; ant[i].position = num[i + thread]; ant[i].end = num[i]; ant[i].buf_size = buffer_size((ant[i].end - ant[i].position + 1), &one); } return 1;} int del_file(char *file) { /* ugly!!! I don't know how to delete file */ char del[MAX_HOSTNAME + 6]; sprintf(del, "rm -f %s",file); system(del); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -