Skip to content

Commit

Permalink
Fixed a bunch of issues with file transfers.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Mar 23, 2015
1 parent 7cef970 commit 1b87d84
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
50 changes: 37 additions & 13 deletions file_transfers.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,34 @@ FILE_TRANSFER *get_file_transfer(uint32_t friend_number, uint32_t file_number)

/* The following are internal file status helper functions */
static void utox_update_user_file(FILE_TRANSFER *file){
MSG_FILE *msg = file->ui_data;
if(!msg){
FILE_TRANSFER *file_copy = malloc(sizeof(FILE_TRANSFER));

memcpy(file_copy, file, sizeof(FILE_TRANSFER));
postmessage(FRIEND_FILE_UPDATE, 0, 0, file_copy);
}

static void calculate_speed(FILE_TRANSFER *file)
{
if ((file->speed) > file->num_packets * 20 * 1371) {
++file->num_packets;
return;
}

file->num_packets = 0;

uint64_t time = get_time();
if (!file->last_check_time) {
file->last_check_time = time;
return;
}
msg->status = file->status;
msg->progress = file->size_transferred;
msg->speed = 0;
msg->path = file->path;
redraw();

if(time - file->last_check_time >= 1000 * 1000 * 100) {
file->speed = (((double)(file->size_transferred - file->last_check_transferred) * 1000.0 * 1000.0 * 1000.0) / (double)(time - file->last_check_time)) + 0.5;
file->last_check_time = time;
file->last_check_transferred = file->size_transferred;
}

utox_update_user_file(file);
}

static void utox_run_file(FILE_TRANSFER *file, uint8_t us){
Expand Down Expand Up @@ -276,14 +295,13 @@ static void incoming_file_callback_request(Tox *tox, uint32_t friend_number, uin
file_transfer_local_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME);
// postmessage(FRIEND_FILE_IN_NEW_INLINE, friend_number, file_number, NULL);
} else {
file_handle->ui_data = message_add_type_file(file_handle);
// postmessage(FRIEND_FILE_IN_NEW, friend_number, file_number, NULL);
postmessage(FRIEND_FILE_NEW, 0, 0, file_handle);
}
}

static void incoming_file_callback_chunk(Tox *UNUSED(tox), uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, void *UNUSED(user_data)){
debug("FileTransfer:\tIncoming chunk for friend (%u), and file (%u). Start (%u), End (%u).\r",
friend_number, file_number, position, length);
//debug("FileTransfer:\tIncoming chunk for friend (%u), and file (%u). Start (%u), End (%u).\r",
// friend_number, file_number, position, length);

TOX_ERR_FILE_SEND_CHUNK error;
FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
Expand Down Expand Up @@ -314,6 +332,8 @@ static void incoming_file_callback_chunk(Tox *UNUSED(tox), uint32_t friend_numbe
}
file_handle->size_transferred += length;

calculate_speed(file_handle);

/* TODO re-implement file transfer speed and time remaining...
if(time(NULL) - file_handle->last_chunk_time >= 10) {
debug("FileTransfer:\ttime update running\n");
Expand Down Expand Up @@ -378,7 +398,7 @@ void outgoing_file_send_new(Tox *tox, uint32_t friend_number, uint8_t *path, con
++friend[friend_number].transfer_count;
debug("Sending file %d of %d(max) to friend(%d).\n", friend[friend_number].transfer_count, MAX_FILE_TRANSFERS, friend_number);
// Create a new msg for the UI and save it's pointer
file_handle->ui_data = message_add_type_file(file_handle);
postmessage(FRIEND_FILE_NEW, 0, 0, file_handle);
} else {
debug("tox_file_send() failed\n");
}
Expand Down Expand Up @@ -507,7 +527,7 @@ int outgoing_file_send_avatar(Tox *tox, uint32_t friend_number, uint8_t *avatar,

static void outgoing_file_callback_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, void *UNUSED(user_data)){

debug("FileTransfer:\tChunk requested for friend_id (%u), and file_id (%u). Start (%u), End (%u).\r", friend_number, file_number, position, length);
//debug("FileTransfer:\tChunk requested for friend_id (%u), and file_id (%u). Start (%u), End (%u).\r", friend_number, file_number, position, length);
//send a chunk of data size of length with

FILE_TRANSFER *file_handle = get_file_transfer(friend_number, file_number);
Expand Down Expand Up @@ -564,6 +584,10 @@ static void outgoing_file_callback_chunk(Tox *tox, uint32_t friend_number, uint3
chunk = buffer;

tox_file_send_chunk(tox, friend_number, file_number, position, chunk, length, &error);
file_handle->size_transferred += length;

calculate_speed(file_handle);

free(buffer);
}

Expand Down
16 changes: 8 additions & 8 deletions file_transfers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ enum UTOX_FILE_TRANSFER_STATUS{
typedef struct FILE_TRANSFER {
uint32_t friend_number, file_number;
uint8_t status;
enum TOX_FILE_KIND kind;
uint32_t kind;
_Bool incoming, in_memory, is_avatar;
uint8_t *path, *name, *file_id;
size_t path_length, name_length, size, size_transferred;
size_t path_length, name_length;
uint64_t size, size_transferred;
uint8_t *memory, *avatar;

/* speed + progress calculations. */
uint32_t speed, num_packets;
uint64_t last_check_time, last_check_transferred;

FILE *file;
time_t request_time, start_time, last_chunk_time, finish_time, pause_time;
MSG_FILE *ui_data;
} FILE_TRANSFER;

typedef struct {
uint64_t size_transferred;
uint32_t speed;
} FILE_PROGRESS;

/** local callback for file transfers
*
* Called with a friend & file number, and will update it with control.
Expand Down
20 changes: 10 additions & 10 deletions messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void draw_message_image(UTOX_NATIVE_IMAGE *image, int x, int y, uint32_t
}

/* Called by new file transfer to add a new message to the msg list */
MSG_FILE* message_add_type_file(FILE_TRANSFER *file){
MSG_FILE* message_add_type_file(FILE_TRANSFER *file){//TODO shove on ui thread
MSG_FILE *msg = malloc(sizeof(MSG_FILE));
msg->author = file->incoming ? 0 : 1;
msg->msg_type = MSG_TYPE_FILE;
Expand Down Expand Up @@ -229,17 +229,17 @@ void messages_draw(MESSAGES *m, int x, int y, int width, int height)
if(progress > file->size) {
progress = file->size;
}
char_t text[16];
STRING_IDX len;
len = sprint_bytes(text, sizeof(text), file->speed);
text[len++] = '/';
text[len++] = 's';
char_t size_text[16];
STRING_IDX size_text_len = sprint_bytes(size_text, sizeof(size_text), file->speed);
size_text[size_text_len++] = '/';
size_text[size_text_len++] = 's';
uint64_t etasec = 0;
if(file->speed) {
etasec = (file->size - progress) / file->speed;
}
STRING_IDX len2 = len;
len2 = snprintf((char*)text, sizeof(text), "%us", (uint32_t)etasec);

char_t text2[16];
STRING_IDX len2 = snprintf((char*)text2, sizeof(text2), "%us", (uint32_t)etasec);

// progress rectangle
uint32_t w = (file->size == 0) ? 0 : (progress * (uint64_t)106 * SCALE) / file->size;
Expand Down Expand Up @@ -298,8 +298,8 @@ void messages_draw(MESSAGES *m, int x, int y, int width, int height)
drawalpha(BM_FTB2, btn_bg_x, bbtn_bg_y, btn_bg_w, bbtn_bg_h, (mouse_bbtn ? COLOR_BUTTON_SUCCESS_HOVER_BACKGROUND : COLOR_BUTTON_SUCCESS_BACKGROUND));
drawalpha(BM_PAUSE, btnx, bbtn_y, btnw, btnh, (mouse_bbtn ? COLOR_BUTTON_SUCCESS_HOVER_TEXT : COLOR_BUTTON_SUCCESS_TEXT));

drawtext(x + dx + 5 * SCALE + 53 * SCALE - textwidth(text, len) / 2, y + 10 * SCALE, text, len);
drawtext(x + dx + 5 * SCALE + 106 * SCALE - textwidth(text, len2), y + 10 * SCALE, text, len2);
drawtext(x + dx + 5 * SCALE + 53 * SCALE - textwidth(size_text, size_text_len) / 2, y + 10 * SCALE, size_text, size_text_len);
drawtext(x + dx + 5 * SCALE + 106 * SCALE - textwidth(text2, len2), y + 10 * SCALE, text2, len2);

framerect((x + dx) + 5 * SCALE, y + 17 * SCALE, (x + dx) + 111 * SCALE, y + 24 * SCALE, COLOR_BUTTON_INPROGRESS_TEXT);
drawrectw((x + dx) + 5 * SCALE, y + 17 * SCALE, w, 7 * SCALE, COLOR_BUTTON_INPROGRESS_TEXT);
Expand Down
30 changes: 19 additions & 11 deletions tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,17 +1535,25 @@ void tox_message(uint8_t tox_message_id, uint16_t param1, uint16_t param2, void
break;
}

case FRIEND_FILE_IN_NEW:
case FRIEND_FILE_IN_NEW_INLINE:
case FRIEND_FILE_OUT_NEW:
case FRIEND_FILE_OUT_NEW_INLINE:
case FRIEND_FILE_IN_STATUS:
case FRIEND_FILE_OUT_STATUS:
case FRIEND_FILE_IN_DONE:
case FRIEND_FILE_IN_DONE_INLINE:
case FRIEND_FILE_OUT_DONE:
case FRIEND_FILE_IN_PROGRESS:
case FRIEND_FILE_OUT_PROGRESS:{
case FRIEND_FILE_NEW: {
FILE_TRANSFER *file_handle = data;
file_handle->ui_data = message_add_type_file(file_handle);
updatefriend(&friend[file_handle->friend_number]);
break;
}

case FRIEND_FILE_UPDATE:{
FILE_TRANSFER *file = data;
MSG_FILE *msg = file->ui_data;
if(!msg){//TODO shove on ui thread
return;
}
msg->status = file->status;
msg->progress = file->size_transferred;
msg->speed = file->speed;
msg->path = file->path;
updatefriend(&friend[file->friend_number]);
free(file);
break;
}

Expand Down
13 changes: 2 additions & 11 deletions tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,8 @@ enum {
PREVIEW_FRAME_NEW,

/* friend file */
FRIEND_FILE_IN_NEW,
FRIEND_FILE_IN_NEW_INLINE,
FRIEND_FILE_OUT_NEW,
FRIEND_FILE_OUT_NEW_INLINE,
FRIEND_FILE_IN_STATUS,
FRIEND_FILE_OUT_STATUS,
FRIEND_FILE_IN_DONE,
FRIEND_FILE_IN_DONE_INLINE,
FRIEND_FILE_OUT_DONE,
FRIEND_FILE_IN_PROGRESS,
FRIEND_FILE_OUT_PROGRESS,
FRIEND_FILE_NEW,
FRIEND_FILE_UPDATE,

/* group */
GROUP_ADD,
Expand Down

0 comments on commit 1b87d84

Please sign in to comment.