diff options
author | Luiz Augusto von Dentz <luiz.dentz-von@nokia.com> | 2010-07-15 11:00:08 +0300 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@nokia.com> | 2010-07-15 10:30:41 -0300 |
commit | a49d09f21a9b775e3c40455c118bd2c01095facb (patch) | |
tree | 4f0040e970f1ac05d455496efeb2817704a7d45b /client | |
parent | 8ceed4f3dc6020911840b1831cb4cc441f2283ac (diff) | |
download | obexd-a49d09f21a9b775e3c40455c118bd2c01095facb.tar.gz |
client: reuse code when reading buffered data
This should also improve speed since now file transfer also tries to read
all buffered data before continue, so each progress will probably be
around the MTU size rather than buffer size.
Diffstat (limited to 'client')
-rw-r--r-- | client/transfer.c | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/client/transfer.c b/client/transfer.c index e37c30b..58ac265 100644 --- a/client/transfer.c +++ b/client/transfer.c @@ -228,35 +228,58 @@ void transfer_unregister(struct transfer_data *transfer) transfer_free(transfer); } -static void get_xfer_listing_progress(GwObexXfer *xfer, - gpointer user_data) +static gboolean transfer_read(struct transfer_data *transfer, GwObexXfer *xfer) { - struct transfer_data *transfer = user_data; - struct transfer_callback *callback = transfer->callback; gint bsize, bread; + /* check if object size is available */ + if (transfer->size == 0) + transfer->size = gw_obex_xfer_object_size(xfer); + + /* read all buffered data */ do { bsize = transfer->buffer_len - transfer->filled; if (bsize < DEFAULT_BUFFER_SIZE) { transfer->buffer_len += DEFAULT_BUFFER_SIZE; - transfer->buffer = g_realloc(transfer->buffer, transfer->buffer_len); + transfer->buffer = g_realloc(transfer->buffer, + transfer->buffer_len); bsize += DEFAULT_BUFFER_SIZE; } - if (gw_obex_xfer_read(xfer, transfer->buffer + transfer->filled, - bsize, &bread, &transfer->err) == FALSE) { + if (gw_obex_xfer_read(xfer, transfer->buffer + + transfer->filled, bsize, &bread, + &transfer->err) == FALSE) { if (transfer->err == GW_OBEX_ERROR_NO_DATA) { transfer->err = 0; - break; + return TRUE; } else - goto fail; + return FALSE; } transfer->filled += bread; + transfer->transferred += bread; } while (bread != 0); + /* set size to transferred if object is done and size is unknown */ + if (gw_obex_xfer_object_done(xfer) == TRUE && + transfer->size == GW_OBEX_UNKNOWN_LENGTH) + transfer->size = transfer->transferred; + + return TRUE; +} + +static void get_buf_xfer_progress(GwObexXfer *xfer, + gpointer user_data) +{ + struct transfer_data *transfer = user_data; + struct transfer_callback *callback = transfer->callback; + + if (transfer_read(transfer, xfer) == FALSE) + goto fail; + if (gw_obex_xfer_object_done(xfer)) { + int bsize; if (transfer->filled > 0 && transfer->buffer[transfer->filled - 1] == '\0') goto done; @@ -286,43 +309,22 @@ static void get_xfer_progress(GwObexXfer *xfer, gpointer user_data) { struct transfer_data *transfer = user_data; struct transfer_callback *callback = transfer->callback; - gint bsize, bread; - gboolean ret; - - if (transfer->buffer_len == 0) { - transfer->buffer_len = DEFAULT_BUFFER_SIZE; - transfer->buffer = g_new0(char, DEFAULT_BUFFER_SIZE); - } - - bsize = transfer->buffer_len - transfer->filled; - ret = gw_obex_xfer_read(xfer, transfer->buffer + transfer->filled, - bsize, &bread, &transfer->err); - if (ret == FALSE) + if (transfer_read(transfer, xfer) == FALSE) goto done; - transfer->filled += bread; - transfer->transferred += bread; - if (transfer->size == 0) - transfer->size = gw_obex_xfer_object_size(xfer); - if (transfer->fd > 0) { gint w; - w = write(transfer->fd, transfer->buffer, bread); + w = write(transfer->fd, transfer->buffer, transfer->filled); if (w < 0) { transfer->err = -errno; goto done; } - transfer->filled = 0; + transfer->filled -= w; } - if (transfer->transferred == transfer->size) - goto done; - - gw_obex_xfer_flush(xfer, NULL); - done: if (callback) callback->func(transfer, transfer->transferred, transfer->err, @@ -424,7 +426,7 @@ int transfer_get(struct transfer_data *transfer, transfer_callback_t func, if (transfer->type != NULL && (strncmp(transfer->type, "x-obex/", 7) == 0 || strncmp(transfer->type, "x-bt/", 5) == 0)) - cb = get_xfer_listing_progress; + cb = get_buf_xfer_progress; else { int fd = open(transfer->name ? : transfer->filename, O_WRONLY | O_CREAT, 0600); |