📄 sender.c
字号:
memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line)); position += strlen(tmp_line); } }#endif } memset(tmp_line, 0, MAX_PATH); sprintf(tmp_line, "/>\n"); size += strlen(tmp_line); /* Reallocate memory for fdt_inst_payload */ if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {#ifdef WIN32 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);#else printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);#endif return NULL; } memcpy((fdt_inst_payload + position), tmp_line, strlen(tmp_line)); position += strlen(tmp_line); if(tmp_file->next == NULL) { break; } tmp_file = tmp_file->next; } memset(tmp_line, 0, MAX_PATH); sprintf(tmp_line, "</FDT-Instance>\n"); size += strlen(tmp_line); size++; /* Reallocate memory for fdt_inst_payload */ if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {#ifdef WIN32 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);#else printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);#endif return NULL; } memcpy((fdt_inst_payload + position), tmp_line, strlen(tmp_line)); position += strlen(tmp_line); *(fdt_inst_payload + position) = '\0'; *fdt_inst_len = size - 1; return fdt_inst_payload;}/* * This function sends files defined in FDT file and FDT Instances. * * Params: fdt_t *fdt: Pointer to FDT structure, * char *base_dir: Pointer to buffer containing base directory for file * or directory to be parsed to the fdt, * char *file_path: Pointer to buffer containing path of file/directory to be sent, * int *s_id: Pointer to Session identifier, * int *ch_id: Pointer to Channel identifier (if NULL use queue mode), * unsigned short eslen: Encoding Symbol Length, * unsigned int max_sblen: Maximum-Size Source Block Length, * unsigned char fec_enc_id: FEC Encoding ID, * unsigned short fec_inst_id: FEC Instance ID, * int use_fec_oti_ext_hdr: Use EXT_FTI header [0 = no, 1 = yes], * int encode_content: Encode content (1 = yes, 0 = no), * int verbosity:. * * Return: int: 0, -1, -2 depending how functions end. * */int fdtbasedsend(fdt_t *fdt, int *s_id, int *ch_id, arguments_t *a) { char *fdt_inst_buf = NULL; #ifdef USE_ZLIB char *compr_fdt_inst_buf = NULL; #ifdef WIN32 ULONGLONG compr_fdt_inst_buf_len = 0;#else unsigned long long compr_fdt_inst_buf_len = 0;#endif #endif #ifdef WIN32 int j;#endif file_t *file; uri_t *uri; char path[MAX_PATH]; static int nb_of_instances_in_last_round = 0; unsigned short file_es_len = a->alc_a.es_len; unsigned int file_max_sb_len = a->alc_a.max_sb_len; unsigned char file_fec_enc_id = a->alc_a.fec_enc_id; unsigned short file_fec_inst_id = a->alc_a.fec_inst_id; time_t systime; #ifdef WIN32 ULONGLONG curr_time; ULONGLONG fdt_inst_len; ULONGLONG sent = 0;#else unsigned long long curr_time; unsigned long long fdt_inst_len; unsigned long long sent = 0;#endif bool incomplete_fdt = false; time(&systime); curr_time = systime + 2208988800U; /* set_session_tx_toi(*s_id, 0); */ set_fdt_instance_id(*s_id, (get_fdt_instance_id(*s_id) - nb_of_instances_in_last_round)); nb_of_instances_in_last_round = 0; if(fdt->expires < curr_time) { set_session_state(*s_id, SExiting); return -2; } if(fdt->complete) { /* Send "Complete FDT" with all File definitions at the beginning of the carousel */ file = fdt->file_list; fdt_inst_buf = create_fdt_instance(file, fdt->nb_of_files, fdt, s_id, &fdt_inst_len); if(fdt_inst_buf == NULL) { return -1; } #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { compr_fdt_inst_buf = buffer_zlib_compress(fdt_inst_buf, fdt_inst_len, &compr_fdt_inst_buf_len); if(compr_fdt_inst_buf == NULL) { free(fdt_inst_buf); return -1; } }#endif #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { sent = send_fdt_instance(compr_fdt_inst_buf, compr_fdt_inst_buf_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity); } else { sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity); }#else sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);#endif set_fdt_instance_id(*s_id, (get_fdt_instance_id(*s_id) + 1)); nb_of_instances_in_last_round++; #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { free(compr_fdt_inst_buf); }#endif free(fdt_inst_buf); if(sent == -1) { return -1; } else if(sent == -2) { return -2; } } file = fdt->file_list; while(file != NULL) { /* loop for sending all files in fdt */ if(a->alc_a.use_fec_oti_ext_hdr == 0) { if(file->fec_enc_id == -1) { incomplete_fdt = true; } else if(((file->fec_enc_id > 127) && (file->fec_inst_id == -1))) { incomplete_fdt = true; } else if(file->max_sb_len == 0) { incomplete_fdt = true; } else if(file->es_len == 0) { incomplete_fdt = true; } else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) && (file->max_nb_of_es == 0))) { incomplete_fdt = true; } else if(file->file_len == 0) { incomplete_fdt = true; } if(incomplete_fdt) {#ifdef WIN32 printf("FDT does not contain FEC-OTI information, TOI: %I64u\n", file->toi);#else printf("FDT does not contain FEC-OTI information, TOI: %llu\n", file->toi);#endif fflush(stdout); return -1; } } if(file->toi == 0) { incomplete_fdt = true; } else if(file->location == NULL) { incomplete_fdt = true; } if(file->encoding != NULL) {#ifdef USE_ZLIB if(!strcmp(file->encoding, "gzip") || !strcmp(file->encoding, "pad")) {#else if(!strcmp(file->encoding, "pad")) {#endif if(file->file_len == 0) { incomplete_fdt = true; } else if(file->toi_len == 0) { incomplete_fdt = true; } } } if(incomplete_fdt) {#ifdef WIN32 printf("FDT does not contain enough File information, TOI: %I64u\n", file->toi);#else printf("FDT does not contain enough File information, TOI: %llu\n", file->toi);#endif fflush(stdout); return -1; } if((file->encoding != NULL) && (strcmp(a->file_path, "") == 0)) { printf("Content encoding is not supported with -f option.\n"); fflush(stdout); return -1; } uri = parse_uri(file->location, strlen(file->location)); memset(path, 0, MAX_PATH); if(!(strcmp(a->alc_a.base_dir, "") == 0)) { strcpy(path, a->alc_a.base_dir); strcat(path, "/"); } strcat(path, uri->path);#ifdef USE_ZLIB if(file->encoding != NULL) { if(!strcmp(file->encoding, "gzip")) { strcat(path, GZ_SUFFIX); } }#endif if(ch_id != NULL) { if(((file->es_len != 0) && (file->max_sb_len != 0))) { file_es_len = file->es_len; file_max_sb_len = file->max_sb_len; } } if(file->fec_enc_id != -1) { if(file->fec_enc_id != file_fec_enc_id) { file_fec_enc_id = (unsigned char)file->fec_enc_id; file_fec_inst_id = (unsigned short)file->fec_inst_id; } } #ifdef WIN32 for(j = 0; j < (int)strlen(path); j++) { if(*(path + j) == '/') { *(path + j) = '\\'; } }#endif /* Send FDT Instance with next File definition before the file is sent */ fdt_inst_buf = create_fdt_instance(file, 1, fdt, s_id, &fdt_inst_len); if(fdt_inst_buf == NULL) { free_uri(uri); return -1; } #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { compr_fdt_inst_buf = buffer_zlib_compress(fdt_inst_buf, fdt_inst_len, &compr_fdt_inst_buf_len); if(compr_fdt_inst_buf == NULL) { free_uri(uri); free(fdt_inst_buf); return -1; } }#endif #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { sent = send_fdt_instance(compr_fdt_inst_buf, compr_fdt_inst_buf_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity); } else { sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity); }#else sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, s_id, ch_id, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);#endif if(sent == -1) { #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { free(compr_fdt_inst_buf); }#endif free(fdt_inst_buf); free_uri(uri); return -1; } else if(sent == -2) { #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { free(compr_fdt_inst_buf); }#endif free(fdt_inst_buf); free_uri(uri); return -2; }#ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { sent = send_file(path, s_id, ch_id, file_es_len, file_max_sb_len, file_fec_enc_id, file_fec_inst_id, file, compr_fdt_inst_buf_len, compr_fdt_inst_buf, a->alc_a.verbosity); } else { sent = send_file(path, s_id, ch_id, file_es_len, file_max_sb_len, file_fec_enc_id, file_fec_inst_id, file, fdt_inst_len, fdt_inst_buf, a->alc_a.verbosity); }#else sent = send_file(path, s_id, ch_id, file_es_len, file_max_sb_len, file_fec_enc_id, file_fec_inst_id, file, fdt_inst_len, fdt_inst_buf, a->alc_a.verbosity); #endif set_fdt_instance_id(*s_id, (get_fdt_instance_id(*s_id) + 1)); nb_of_instances_in_last_round++; free_uri(uri); #ifdef USE_ZLIB if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) { free(compr_fdt_inst_buf); }#endif free(fdt_inst_buf); if(sent == -1) { return -1; } else if(sent == -2) { return -2; } file = file->next; } return 0; } #ifdef USE_ZLIB/* * This function removes tempororary ~gz files in sender side. * * Params: char *fdt_file: Pointer to buffer containing fdt file name, * char *base_dir: Pointer to buffer containing base directory for file * or directory to be parsed to the fdt, * char *file_path: Pointer to buffer containing file or directory parsed to the fdt, * * Return: int: 0 in success, -1 otherwise * */int remove_gz_files(char *fdt_file, char *base_dir, char *file_path) { uri_t *uri; char path[MAX_PATH]; file_t *file; fdt_t *fdt; char *buf = NULL; struct stat file_stats; unsigned int fdt_length = 0; FILE *fp; int nbytes;#ifdef WIN32 int j;#endif if(stat(fdt_file, &file_stats) == -1) { printf("Error: %s is not valid file name\n", fdt_file); fflush(stdout); return -1; } fdt_length = file_stats.st_size; /* Allocate memory for buf, to read fdt file to it */ if(!(buf = (char*)calloc((fdt_length+1), sizeof(char)))) { printf("Could not alloc memory for fdt buffer!\n"); fflush(stdout); return -1; } if((fp = fopen(fdt_file, "rb")) == NULL) { printf("Error: unable to open fdt_file %s\n", fdt_file); fflush(stdout); free(buf); return -1; } nbytes = fread(buf, 1, fdt_length, fp); if(nbytes <= 0) { printf("fread error\n"); fflush(stdout); fclose(fp); free(buf); return -1; } fdt = decode_fdt_payload(buf); free(buf); fclose(fp); if(fdt == NULL) { return -1; } file = fdt->file_list; while(file != NULL) { uri = parse_uri(file->location, strlen(file->location)); memset(path, 0, MAX_PATH); if(!(strcmp(base_dir, "") == 0)) { strcpy(path, base_dir); strcat(path, "/"); } strcat(path, uri->path); strcat(path, GZ_SUFFIX); #ifdef WIN32 for(j = 0; j < (int)strlen(path); j++) { if(path[j] == '/') { path[j] = '\\'; } }#endif if(remove(path) == -1) { printf("%s: errno %i\n", path, errno); fflush(stdout); } file = file->next; } return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -