int cometd_impl_process_sync(const cometd* h, JsonNode* root) { JsonArray* arr = json_node_get_array(root); GList* msgs = json_array_get_elements(arr); GList* item; for (item = msgs; item; item = g_list_next(item)) cometd_process_msg(h, item->data); g_list_free(msgs); // TODO: What happens if cometd_fire_listeners blows up? return COMETD_SUCCESS; }
gpointer cometd_loop_gthread_run(gpointer data) { const cometd* h = (const cometd*) data; cometd_conn* conn = h->conn; cometd_loop* loop = h->loop; JsonNode *connect = NULL, *payload = NULL; long backoff = 0; guint attempt; for (attempt = 1; cometd_should_recv(h); ++attempt) { cometd_loop_wait(loop, backoff); payload = cometd_recv(h); connect = cometd_msg_extract_connect(payload); cometd_inbox_push(h->inbox, payload); cometd_process_msg(h, connect); if (cometd_should_retry_recv(h)) backoff = cometd_get_backoff(h, attempt); else backoff = attempt = 0; json_node_free(payload); json_node_free(connect); // bail out if we should no longer backoff if (backoff == -1) break; } // If we've bailed from the loop, it's because we gave up // on the COMETD_UNCONNECTED state and we are no longer retrying or we // have intentially disconnected. // // If we gave up retrying, then this ensures that our state gets set correctly // and it should be a signal to the inbox queue to stop waiting. cometd_conn_set_state(conn, COMETD_DISCONNECTED); return NULL; }
/** * Reads JsonNodes that are received by the inbox thread. * If a handler wishes to to keep a JsonNode in memory * longer than the lifetime of a cometd_callback, then it * must increase the reference count to the node. */ void cometd_listen(const cometd* h) { JsonNode* msg; // TODO: COMETD_UNINITIALIZED is a useless state const long stop = COMETD_DISCONNECTED | COMETD_DISCONNECTING | COMETD_UNINITIALIZED; // TODO: Add tests that demostrate that cometd_listen actually returns while (!cometd_conn_is_state(h->conn, stop)) { while (msg == cometd_inbox_take(h->inbox)) if (msg != NULL) { cometd_process_msg(h, msg); // We need are responsible for destroying the message // after it has been taken from the queue and processed. json_node_free(msg); } } }