void obc_session_unref(struct obc_session *session) { int refs; refs = __sync_sub_and_fetch(&session->refcount, 1); DBG("%p: ref=%d", session, refs); if (refs > 0) return; sessions = g_slist_remove(sessions, session); if (!session->obex) goto disconnect; /* Wait OBEX Disconnect to complete if command succeed otherwise * proceed with transport disconnection since there is nothing else to * be done */ if (g_obex_disconnect(session->obex, disconnect_complete, session, NULL)) return; disconnect: /* Disconnect transport */ if (session->id > 0 && session->transport != NULL) { session->transport->disconnect(session->id); session->id = 0; } session_free(session); }
void obc_session_shutdown(struct obc_session *session) { struct pending_request *p; GError *err; DBG("%p", session); if (session->disconnecting == TRUE) { DBG("%p already disconnecting", session); return; } session->disconnecting = TRUE; obc_session_ref(session); /* Unregister any pending transfer */ err = g_error_new(OBEX_IO_ERROR, OBEX_IO_DISCONNECTED, "Session closed by user"); if (session->p != NULL && session->p->id != 0) { p = session->p; session->p = NULL; if (p->func) p->func(session, p->transfer, err, p->data); pending_request_free(p); } while ((p = g_queue_pop_head(session->queue))) { if (p->func) p->func(session, p->transfer, err, p->data); pending_request_free(p); } g_error_free(err); /* Unregister interfaces */ if (session->path) session_unregistered(session); DBG("Checking the need for disconnect request"); /* Send a disconnect request and wait for reply */ if (session->id > 0 && session->transport != NULL && session->obex != NULL) { DBG("Generating disconnect request. "); err = NULL; p = pending_request_new(session, NULL, NULL, NULL); p->req_id = g_obex_disconnect(session->obex, disconnect_cb, p, &err); if (err != NULL) { DBG("Generating disconnect request failed. "); disconnect_cb(session->obex, NULL, NULL, p); } else { /* Finalize when reply arrives */ DBG("Generating disconnect request succeeded. "); } } else { DBG("Unreferring without disconnect request."); obc_session_unref(session); } }