int32_t vrs_callback_update(const uint8_t session_id) { static Generic_Cmd *cmd; int i; /* Check if CTX was initialized */ if(vc_ctx == NULL) { v_print_log(VRS_PRINT_ERROR, "Basic callback functions were not set.\n"); return VRS_NO_CB_FUNC; } else { int session_found = 0; pthread_mutex_lock(&vc_ctx->mutex); /* Go through all sessions ... */ for(i = 0; i<vc_ctx->max_sessions; i++) { /* ... and try to find connection with session_id */ if(vc_ctx->vsessions[i] != NULL && vc_ctx->vsessions[i]->session_id == session_id) { /* Pop all incoming commands from queue */ while(v_in_queue_cmd_count(vc_ctx->vsessions[i]->in_queue) > 0) { cmd = v_in_queue_pop(vc_ctx->vsessions[i]->in_queue); vc_call_callback_func(session_id, cmd); v_cmd_destroy(&cmd); } session_found = 1; break; } } pthread_mutex_unlock(&vc_ctx->mutex); if(session_found == 1) { return VRS_SUCCESS; } } v_print_log(VRS_PRINT_ERROR, "Invalid session_id: %d.\n", session_id); return VRS_FAILURE; }
/** * \brief This is function of main data thread. It waits for new data in * incoming queues of session, that are in OPEN/CLOSEREQ states. */ void *vs_data_loop(void *arg) { struct VS_CTX *vs_ctx = (struct VS_CTX*)arg; struct Generic_Cmd *cmd; struct timespec ts; struct timeval tv; int i, ret = 0; gettimeofday(&tv, NULL); ts.tv_sec = tv.tv_sec + 1; ts.tv_nsec = 1000*tv.tv_usec; while(vs_ctx->state != SERVER_STATE_CLOSED) { #ifdef __linux__ ret = sem_timedwait(vs_ctx->data.sem, &ts); #elif __APPLE__ /* Fast fix */ ret = sem_wait(vs_ctx->data.sem); #endif if(ret == 0) { for(i=0; i<vs_ctx->max_sessions; i++) { if(vs_ctx->vsessions[i]->dgram_conn->host_state == UDP_SERVER_STATE_OPEN || vs_ctx->vsessions[i]->stream_conn->host_state == TCP_SERVER_STATE_STREAM_OPEN) { /* Pop all data of incoming messages from queue */ while(v_in_queue_cmd_count(vs_ctx->vsessions[i]->in_queue) > 0) { cmd = v_in_queue_pop(vs_ctx->vsessions[i]->in_queue); vs_handle_node_cmd(vs_ctx, vs_ctx->vsessions[i], cmd); v_cmd_destroy(&cmd); } } } } else { if(errno == ETIMEDOUT) { ts.tv_sec++; } } } v_print_log(VRS_PRINT_DEBUG_MSG, "Exiting data thread\n"); pthread_exit(NULL); return NULL; }