/** * function invoked when a file transfer is received. **/ static void file_transfer_received(LinphoneChatMessage *message, const LinphoneContent* content, const LinphoneBuffer *buffer){ FILE* file=NULL; if (!linphone_chat_message_get_user_data(message)) { /*first chunk, creating file*/ file = fopen("receive_file.dump","wb"); linphone_chat_message_set_user_data(message,(void*)file); /*store fd for next chunks*/ } file = (FILE*)linphone_chat_message_get_user_data(message); if (linphone_buffer_is_empty(buffer)) { printf("File transfert completed\n"); linphone_chat_room_destroy(linphone_chat_message_get_chat_room(message)); linphone_chat_message_destroy(message); fclose(file); running=FALSE; } else { /* store content on a file*/ if (fwrite(linphone_buffer_get_content(buffer),linphone_buffer_get_size(buffer),1,file)==-1){ ms_warning("file_transfer_received() write failed: %s",strerror(errno)); } } }
/* * function called when the file transfer is initiated. file content should be feed into object LinphoneContent * */ static void file_transfer_send(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, char* buff, size_t* size){ int offset=-1; if (!linphone_chat_message_get_user_data(message)) { /*first chunk*/ offset=0; } else { /*subsequent chunk*/ offset = (int)((long)(linphone_chat_message_get_user_data(message))&0x00000000FFFFFFFF); } *size = MIN(*size,sizeof(big_file)-offset); /*updating content->size with minimun between remaining data and requested size*/ if (*size==0) { /*end of file*/ return; } memcpy(buff,big_file+offset,*size); /*store offset for next chunk*/ linphone_chat_message_set_user_data(message,(void*)(offset+*size)); }
/** * function invoked when a file transfer is received. * */ void file_transfer_received(LinphoneChatMessage *message, const LinphoneContent* content, const LinphoneBuffer *buffer){ FILE* file=NULL; char receive_file[256]; LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(message); LinphoneCore *lc = linphone_chat_room_get_core(cr); snprintf(receive_file,sizeof(receive_file), "%s/receive_file.dump", liblinphone_tester_writable_dir_prefix); if (!linphone_chat_message_get_user_data(message)) { /*first chunk, creating file*/ file = fopen(receive_file,"wb"); linphone_chat_message_set_user_data(message,(void*)file); /*store fd for next chunks*/ } file = (FILE*)linphone_chat_message_get_user_data(message); if (linphone_buffer_is_empty(buffer)) { /* tranfer complete */ stats* counters = get_stats(lc); counters->number_of_LinphoneMessageExtBodyReceived++; fclose(file); } else { /* store content on a file*/ if (fwrite(linphone_buffer_get_content(buffer),linphone_buffer_get_size(buffer),1,file)==-1){ ms_error("file_transfer_received(): write() failed: %s",strerror(errno)); } } }
/* * function called when the file transfer is initiated. file content should be feed into object LinphoneContent * */ LinphoneBuffer * file_transfer_send(LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t size){ LinphoneBuffer *lb; size_t file_size; size_t size_to_send; FILE *file_to_send; uint8_t *buf; if (size == 0) return linphone_buffer_new(); /*end of file*/ file_to_send = linphone_chat_message_get_user_data(message); fseek(file_to_send, 0, SEEK_END); file_size = ftell(file_to_send); fseek(file_to_send, offset, SEEK_SET); size_to_send = MIN(size, file_size - offset); buf = ms_malloc(size_to_send); if (fread(buf, size_to_send, 1, file_to_send)!=size_to_send){ ms_error("fread error"); } lb = linphone_buffer_new_from_data(buf, size_to_send); ms_free(buf); return lb; }