void test_rtsp_client_request (void) { rtsp_ret_e ret; char machine_name[NAME_SIZE]; /* Start the RTSP Server */ pthread_create(&server_thread_id, NULL, &server_thread_activation, NULL); sleep(2); /* sa_ignore IGNORE_RETURN */ gethostname(machine_name, NAME_SIZE); ret = rtsp_client_init(machine_name, 8554); CU_ASSERT_EQUAL(ret, RTSP_SUCCESS); ret = rtsp_send_request( "vqe-channels", MEDIA_TYPE_APP_SDP, TIME_ADD_A_R(get_sys_time(), VQEC_UPDATER_TIMEOUT_BACKGROUND)); CU_ASSERT_EQUAL(ret, RTSP_SUCCESS); CU_ASSERT_EQUAL(rtsp_get_response_code(), MSG_200_OK); ret = rtsp_client_close(); CU_ASSERT_EQUAL(ret, RTSP_SUCCESS); system("kill -9 `pidof VCDServer`"); sleep(2); }
int main(int argc, char *argv[]) { struct timeval time_spec; //struct tm *t; int err; if((err = get_sys_time(&time_spec)) != 0) return -1; t = localtime(&time_spec.tv_sec); if(t == NULL) return -2; int err; struct tm t; err = time_char2tm(argv[1], &t); if(err != 0) { printf("[%s] error[%d]\n", argv[1], err); return -3; } printf("%s", asctime(&t)); printf("%s", ctime(&ti)); return 0; }
int main ( int argc, char **argv ) { struct Library * TimerBase; int size=0,i,h=0,m=0,ml=0,s=0; char *exec; struct timeval time1, time2; struct timerequest *tr; if ( argc <= 1 ) { printf ( "timeme 1.0 - Times how long a command takes to execute.\n" ); printf ( "Usage: timeme (command) [params] ...\nContact: [email protected]\n" ); exit ( 0 ); } /* count the arguments */ for ( i=1;i<argc;i++ ) size += strlen ( argv[i] ); size += argc-2; exec = ( char * ) AllocMem ( size+1, MEMF_PUBLIC|MEMF_CLEAR ); /* build the execute line */ sprintf ( exec, "%s", argv[1] ); for ( i=2;i<argc;i++ ) { sprintf ( exec, "%s %s", exec, argv[i] ); } if ( get_sys_time ( &time1 ) ) error_exit ( "Error getting timer.device." ); ( void ) Execute ( exec, 0,0 ); if ( get_sys_time ( &time2 ) ) error_exit ( "Error getting timer.device." ); if ( ! ( tr = create_timer ( UNIT_MICROHZ ) ) ) error_exit ( "Error getting timer.device." ); TimerBase = ( struct Library * ) tr->tr_node.io_Device; SubTime ( &time2,&time1 ); TimerBase = ( struct Library * ) ( -1 ); delete_timer ( tr ); h = time2.tv_secs/3600; m = time2.tv_secs/60; ml = m%60; printf ( "\n-- %d hours, %d minutes, %ld secs, %ld msec --\n", h,ml,time2.tv_secs%60,time2.tv_micro ); FreeMem ( exec, size+1 ); exit ( 0 ); }
CALLSET_ENTRY (idle, init) { idle_ready_time = get_sys_time () + TIME_100MS; idle_second_timer = 0; idle_10second_timer = 0; #ifndef CONFIG_RTC idle_minute_timer = 0; #endif }
/** * Event handler callback for the TLM polling event. * * @param[in] evptr - pointer to libevent structure. * @param[in] fd - not used * @param[in] event - not used * @param[in] arg - ptr to polling interval (in ms) */ void vqec_dp_tlm_event_handler (const vqec_event_t *const evptr, int32_t fd, int16_t event, void *arg) { abs_time_t cur_time; vqec_dp_input_shim_run_service(*(uint16_t *)arg); cur_time = get_sys_time(); vqec_dpchan_poll_ev_handler(cur_time); }
static int pr_sys_time() { struct timeval time_spec; //struct tm *t; int err; if((err = get_sys_time(&time_spec)) != 0) return -1; printf("%s", ctime(&time_spec.tv_sec)); return 0; }
int main (int argc, char *argv[]) { char serverHost[NAME_SIZE]; int serverPort; int loglevel; if (argc < 5) { printf( "\nUsage: %s %s\n", argv[0], applicationUsage); exit(-1); } strncpy(serverHost, argv[1], NAME_SIZE); serverPort = atoi(argv[2]); loglevel = atoi(argv[3]); /* Initialize the logging facility */ syslog_facility_open(LOG_RTSP, LOG_CONS); syslog_facility_filter_set(LOG_RTSP, LOG_INFO); /* Initialize a rtsp client */ if (rtsp_client_init(serverHost, serverPort) != RTSP_SUCCESS) { printf("Error:: rtsp client initialization failed!\n"); exit(-1); } char name[NAME_SIZE]; strncpy(name, argv[4], NAME_SIZE); syslog_print(RTSP_CHANNEL_UPDATE_NOTICE); if (rtsp_send_request( name, MEDIA_TYPE_APP_SDP, TIME_ADD_A_R(get_sys_time(), VQEC_UPDATER_TIMEOUT_BACKGROUND)) != RTSP_SUCCESS) { printf("Failed to send out the request!\n"); exit(-1); } if (rtsp_get_response_code() == MSG_200_OK) { printf("\n\nResponse body = \n%s\n", rtsp_get_response_body()); } if (rtsp_client_close() != RTSP_SUCCESS) { printf("Error:: rtsp client close failed!\n"); exit(-1); } exit(0); }
/* * This callback is executed when the time to send the next * receiver report is reached. */ UT_STATIC void rtcp_receive_report_timeout_cb (const vqec_event_t * const evptr, int32_t fd, int16_t event, void *arg) { rtp_session_t *p_sess = NULL; rtp_member_t *local_member = NULL; uint64_t time; struct timeval tv; if (!arg) { rtcp_tmevt_log_err("%s %s", __FUNCTION__, s_arg_is_null); /* ignore return {nothing to be done if stop fails} */ (void)vqec_event_stop(evptr); return; } p_sess = (rtp_session_t *)arg; local_member = p_sess->rtp_local_source; if (!local_member) { rtcp_tmevt_log_err("%s %s", __FUNCTION__, s_src_is_null); /* ignore return nothing to be done if stop fails */ (void)vqec_event_stop(evptr); return; } /* Update the timestamps */ time = TIME_GET_A(msec, get_sys_time()); local_member->rtp_timestamp = (uint32_t)time; local_member->ntp_timestamp = abs_time_to_ntp(msec_to_abs_time(time)); MCALL(p_sess, rtp_update_stats, TRUE); MCALL(p_sess, rtp_session_timeout_transmit_report); timerclear(&tv); rel_time_t diff = TIME_SUB_A_A(p_sess->next_send_ts, msec_to_abs_time(time)); /* If the rel-time delta is -ive [lagging behind] use 0. */ if (TIME_CMP_R(lt, diff, REL_TIME_0)) { diff = REL_TIME_0; } tv = TIME_GET_R(timeval, diff); /* ignore return no recourse if start fails*/ (void)vqec_event_start(evptr, &tv); }
/*************************************************************************** * int main * Parses command line. Format is: * argv[0] [-v] bin_file * * options: * -v Verbose mode. Prints text output rather than raw output. * *-------------------------------------------------------------------------- * To go into man page: * Name: emu-lejosrun - Emulate lejos RCX code in Unix * * Synosis: emu-lejosrun [-v] bin_file * * Description: Executes a binary file created by the lejos compiler within * Unix rather than in the RCX environment. The Java byte-codes * are executed here, and their actions are listed rather than * executed as they would be on the real RCX device. * * Options: -v Verbose mode. Normally the output is printed in raw * mode. The actual hex values are printed. Using this * option displays more user-friendly output. *-------------------------------------------------------------------------- ***************************************************************************/ int main (int argc, char *argv[]) { int c; char *file = argv[argc - 1]; /* Process any options. */ while(--argc > 0 && (*++argv)[0] == '-') while((c = *++argv[0]) != 0) switch (c) { case 'v': verbose = 1; break; default: printf("%s: unknown option %c\n", argv[0], c); exit(1); } if (argc != 1) { printf ("%s runs a binary dumped by the linker.\n", argv[0]); printf ("Use: %s [-v] binary_file\n", argv[0]); exit (1); } #if DEBUG_STARTUP printf ("Reading binary %s\n", file); #endif readBinary (file); if (gettimeofday(&gStart, NULL)) perror("main: gettimeofday"); timer_handler (SIGALRM); if (setitimer(ITIMER_REAL, &itimer, null) < 0) { printf ("Failed to set interval timer\n"); exit(1); } init_sensors (); last_ad_time = get_sys_time(); run(); itimer.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &itimer, null); signal(SIGALRM, SIG_DFL); exit(0); }
/** * Add a new request to the display queue. */ void deff_queue_add (U8 id, U16 timeout) { struct deff_queue_entry *dq; /* Ensure that no entry is added to the queue twice. If it's already in there, just return. */ dq = deff_queue_find (id); if (dq) return; /* Find and initialize a free entry. If the queue is full, then the effect just won't ever happen. */ dq = deff_queue_find (DEFF_NULL); if (dq) { dq->id = id; dq->timeout = get_sys_time() + timeout; } }
/** Service the switch queue. This function is called * periodically to see if any pending switch transitions have * completed their debounce time. If any switch becomes * unstable before the period expires, it will be removed * from the queue prior to this function being called. So * all that is needed here is to decrement the timers, and if * one reaches zero, consider that switch transitioned. */ void switch_service_queue (void) { pending_switch_t *entry; U8 elapsed_time; /* See how long since the last time we serviced the queue. This is in 16ms ticks. */ elapsed_time = get_elapsed_time (switch_last_service_time); entry = switch_queue; while (unlikely (entry < switch_queue_top)) { entry->timer -= elapsed_time; if (entry->timer <= 0) { /* Debounce interval is complete. The entry can be removed from the queue */ switch_queue_remove (entry); /* See if the switch held its state during the debounce period */ if (bit_test (sw_unstable, entry->id)) { /* Debouncing failed, so don't process the switch. * Restart IRQ-level scanning. */ disable_irq (); bit_off (sw_stable, entry->id); bit_off (sw_unstable, entry->id); enable_irq (); } else { /* Debouncing succeeded, so process the switch */ switch_transitioned (entry->id); } } entry++; } switch_last_service_time = get_sys_time (); }
/** * @brief update_db_host * 更新主机信息表 * @Param: hostinfo * 更新信息: 传入 hostname ,hostip * Returns: */ int update_db_host(HostRegInfo & hostinfo) { ostringstream ostr; string setstr; string wherestr; ostr.str(""); ostr << "HostName='" << hostinfo.hostname << "'"; wherestr = ostr.str(); string tablename=get_table_name(RTU_TYPE_HOST); int count=findHostname(hostinfo.hostname); if(count < 0) { LOG_ERROR("findHostname error..\n"); return -1; } else if(count == 0) { LOG_ERROR("hostname no found..\n"); return -1; } else if(count > 0) { /* 更新主机 */ char lastcomtime[30]; get_sys_time(lastcomtime); ostr.str(""); ostr << "HostIP=" << hostinfo.hostip << ",LastComTime='" <<lastcomtime<< "'"; setstr = ostr.str(); } return db_update_query(setstr,wherestr,tablename); }
/****************************************************************************** * Adjust nll's state / predict a receive time for the given sample. ******************************************************************************/ void vqec_nll_adjust (vqec_nll_t * nll, abs_time_t actual_time, uint32_t pcr32, rel_time_t est_rtp_delta, boolean * disc, abs_time_t * predicted_time) { rel_time_t time_delta = REL_TIME_0, correction = REL_TIME_0, arrival_error; abs_time_t pred_arrival, prev_pred_base; boolean reset_base; if (!nll || !disc || !predicted_time) { VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "adjust() invalid inputs %p/%p/%p\n", nll, disc, predicted_time); if (predicted_time) { *predicted_time = get_sys_time(); } return; } prev_pred_base = nll->pred_base; if (*disc) { nll->num_exp_disc++; VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "nll(%p) explicit discontinuity signaled: " "actual_time %llu, pcr32 %u, est_rtp %llu\n", nll, TIME_GET_A(msec, actual_time), pcr32, TIME_GET_R(usec, est_rtp_delta)); } /* * The NLL operates in two modes: non-tracking and tracking. In * non-tracking mode, the original receive times of the samples are * unknown, and therefore, it just uses the sender RTP timestamps to * determine the send time for packets. In tracking mode, the receive * times of samples are known, except, e.g., when packets are received * of order. */ switch (nll->mode) { case VQEC_NLL_MODE_NONTRACKING: /* non-tracking mode */ if (!nll->got_first) { /* * if this is the 1st sample, set pred_base = actual_time if * actual_time is non-0, otherwise set it to current time. */ nll->got_first = TRUE; *disc = TRUE; if (!IS_ABS_TIME_ZERO(actual_time)) { nll->pred_base = actual_time; } else { nll->pred_base = get_sys_time(); } } else { /* else-of !nll->got_first */ /* * the code below considers the condition delta(rtp) >100 msecs * an "Implicit discontinuity". Whenever there the code detects * that the timing may be discontinuous, or the caller sets the * discontinuity flag, "est_rtp_delta" is added to the previous * value of pred_base. No limits are imposed on "est_rtp_delta". */ if (!*disc) { time_delta = vqec_nll_rtp_delta(nll->pcr32_base, pcr32); } if (*disc || TIME_CMP_R(gt, time_delta, MAX_NLL_DISCONTINUITY_THRESHOLD) || TIME_CMP_R(gt, TIME_NEG_R(time_delta), MAX_NLL_DISCONTINUITY_THRESHOLD)) { nll->pred_base = TIME_ADD_A_R(nll->pred_base, est_rtp_delta); if (!*disc) { nll->num_imp_disc++; /* implicitly discontinuous oper */ *disc = TRUE; VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Implicit discontinuity : " "nll(%p) pcr %u old_pcr %u pred_base %llu\n", nll, pcr32, nll->pcr32_base, TIME_GET_A(msec, nll->pred_base)); } } else { /* else-of (*disc || TIME_CMP_R(... */ nll->pred_base = TIME_ADD_A_R(nll->pred_base, time_delta); } } /* !nll->got_first */ /* * Protect backward fold in predicted time; the previous predicted * time is selected if this condition is true. */ if (TIME_CMP_A(lt, nll->pred_base, prev_pred_base)) { VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Prediction in past: nll(%p)" "new_base = %llu old_base = %llu pcr32 %u\n", nll, TIME_GET_A(msec, nll->pred_base), TIME_GET_A(msec, prev_pred_base), nll->pcr32_base); nll->pred_base = prev_pred_base; nll->predict_in_past++; } nll->pcr32_base = pcr32; *predicted_time = nll->pred_base; if (nll->switch_to_tracking) { /* switch to tracking mode */ /* * The rcc-repair burst and primary streams are received * somewhat "asynchronously" of each other, i.e., the primary * packets may be received at any epoch within the join latency * window. Because of the bandwidth / join skew, in tracking * mode the arrival error should be computed from the first * primary packet's actual receive time. However, the time * skew between the predicted receive time of the 1st primary, * and it's actual receive time must be preserved, and added to * all subsequent predictions in tracking mode. This value is * termed "primary_offset". To switch to tracking mode it is * *necessary* that a packet with non-0 actual time is provided. * The error case is explicitly handled by using current predicted * base as actual time causing primary_offset to be 0. This concept * of time-shift is now an integral part of the nll, as is * computed / cached implicitly when switching to tracking mode. */ abs_time_t act; if (IS_ABS_TIME_ZERO(actual_time)) { act = nll->pred_base; } else { act = actual_time; } nll->primary_offset = TIME_SUB_A_A(nll->pred_base, act); nll->pred_base = nll->last_actual_time = act; nll->switch_to_tracking = FALSE; nll->mode = VQEC_NLL_MODE_TRACKING; VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Switch to tracking : nll(%p) pred_base %llu " "primary offset %lld actual time %llu \n", nll, TIME_GET_A(msec, nll->pred_base), TIME_GET_R(usec, nll->primary_offset), TIME_GET_A(msec, actual_time)); } break; /* done with non-tracking processing */ case VQEC_NLL_MODE_TRACKING: /* tracking mode */ if (!nll->got_first) { /* * if this is the 1st sample, set pred_base to actual_time + * non-tracking-offset if actual time is non-0; otherwise, set it to * current time + non-tracking-offset. */ nll->got_first = TRUE; *disc = TRUE; if (!IS_ABS_TIME_ZERO(actual_time)) { nll->pred_base = actual_time; } else { nll->pred_base = get_sys_time(); } } else { /* else-of !nll->got_first */ reset_base = FALSE; if (!*disc) { time_delta = vqec_nll_rtp_delta(nll->pcr32_base, pcr32); /* * We leave open the possibility of bounding this delta * prior computing arrival error, and instead using the * receive timestamp delta estimate (as for explicit disc) * in an attempt to better predict implicit discontinuities. * (this can also be done if the arrival error exceeded * threshold, but there was no explicit discontinuity). */ } else { /* * In case of explicit discontinuity, approximate rtp delta from * the receive timestamps of the current & previous sample, if * they are both non-0. This approx is used only If the delta is * < DISCONTINUITY_THRESHOLD msecs. Otherwise we'll reset * the nll's pred_base / error average. */ if (!IS_ABS_TIME_ZERO(actual_time) && (!IS_ABS_TIME_ZERO(nll->last_actual_time))) { time_delta = TIME_SUB_A_A(actual_time, nll->last_actual_time); if (TIME_CMP_R(gt, time_delta, MAX_NLL_DISCONTINUITY_THRESHOLD) || TIME_CMP_R(gt, TIME_NEG_R(time_delta), MAX_NLL_DISCONTINUITY_THRESHOLD)) { reset_base = TRUE; } /* (TIME_CMP_R(gt...) */ } /* (!IS_ABS_TIME_ZERO()... */ } /* (!*disc) */ /* * If we have a bounded estimate of the sender's time delta, * compute arrival error, and ensure that it is bounded by * +/- MAX_ARRIVAL_ERROR msecs. If not, we'll reset the nll's * pred_base / error average. If no actual time is provided, * we use, actual_time = pred_base + rtp_delta = pred_arrival. */ if (!reset_base) { abs_time_t act; if (IS_ABS_TIME_ZERO(actual_time)) { act = TIME_ADD_A_R(nll->pred_base, time_delta); } else { act = actual_time; } pred_arrival = TIME_ADD_A_R(nll->pred_base, time_delta); arrival_error = TIME_SUB_A_A(pred_arrival, act); if (TIME_CMP_R(lt, arrival_error, MAX_ARRIVAL_ERROR) && TIME_CMP_R(lt, TIME_NEG_R(arrival_error), MAX_ARRIVAL_ERROR)) { correction = vqec_nll_update_error(nll, arrival_error); nll->pred_base = TIME_ADD_A_R(pred_arrival, correction); } else { reset_base = TRUE; /* arrival error is out-of-bound */ } } if (reset_base) { /* explicit disc or excessive error */ if (!*disc) { nll->num_imp_disc++; /* implicit discontinuous oper */ *disc = TRUE; } /* * We must have an actual time provided for this particular * case. If one is not provided, the last pred_base is used; * a counter is incremented to keep track of such rather * unlikely incidents. */ if (!IS_ABS_TIME_ZERO(actual_time)) { nll->pred_base = actual_time; } else { nll->reset_base_no_act_time++; VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Reset base w/o act time " "nll(%p)", nll); } nll->error_avg = REL_TIME_0; /* reset error average */ VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Tracking mode discontinuity : " "nll(%p) imp_disc %u exp_disc %u pcr %u old_pcr %u " "pred_base %llu\n", nll, nll->num_imp_disc, nll->num_exp_disc, pcr32, nll->pcr32_base, TIME_GET_A(msec, nll->pred_base)); } /* end-of (reset_base) */ } /* end-of (!nll->got_first) */ /* * Protect backward fold in predicted time; the previous predicted * time is selected if this condition is true. */ if (TIME_CMP_A(lt, nll->pred_base, prev_pred_base)) { VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL, "Prediction in past: nll(%p)" "new_base = %llu old_base = %llu pcr32 %u\n", nll, TIME_GET_A(msec, nll->pred_base), TIME_GET_A(msec, prev_pred_base), nll->pcr32_base); nll->pred_base = prev_pred_base; nll->predict_in_past++; } nll->pcr32_base = pcr32; nll->last_actual_time = actual_time; *predicted_time = TIME_ADD_A_R(nll->pred_base, nll->primary_offset); break; /* done with tracking processing */ default: /* not reached */ VQEC_DP_ASSERT_FATAL(0, "nll fatal error"); break; } /* end switch(mode) */ if (VQEC_DP_GET_DEBUG_FLAG(VQEC_DP_DEBUG_COLLECT_STATS)) { nll->num_obs++; } VQEC_DP_DEBUG(VQEC_DP_DEBUG_NLL_ADJUST, "Adjustment: nll(%p), base %llu, " "pcr32 %u, correction %lld, cum err %lld\n", nll, TIME_GET_A(msec, nll->pred_base), nll->pcr32_base, TIME_GET_R(usec, correction), TIME_GET_R(usec, nll->error_avg)); }
U8 get_elapsed_time (U16 then) { return get_sys_time () - then; }
int main(int argc,char **argv) { LONG seconds; struct timerequest *tr; /* IO block for timer commands */ struct timeval oldtimeval; /* timevals to store times */ struct timeval mytimeval; struct timeval currentval; printf("\nTimer test\n"); /* sleep for two seconds */ currentval.tv_secs = 2; currentval.tv_micro = 0; time_delay( ¤tval, UNIT_VBLANK ); printf( "After 2 seconds delay\n" ); /* sleep for four seconds */ currentval.tv_secs = 4; currentval.tv_micro = 0; time_delay( ¤tval, UNIT_VBLANK ); printf( "After 4 seconds delay\n" ); /* sleep for 500,000 micro-seconds = 1/2 second */ currentval.tv_secs = 0; currentval.tv_micro = 500000; time_delay( ¤tval, UNIT_MICROHZ ); printf( "After 1/2 second delay\n" ); printf( "DOS Date command shows: " ); (void) Execute( "date", 0, 0 ); /* save what system thinks is the time....we'll advance it temporarily */ get_sys_time( &oldtimeval ); printf("Original system time is:\n"); show_time(oldtimeval.tv_secs ); printf("Setting a new system time\n"); seconds = 1000 * SECSPERDAY + oldtimeval.tv_secs; set_new_time( seconds ); /* (if user executes the AmigaDOS DATE command now, he will*/ /* see that the time has advanced something over 1000 days */ printf( "DOS Date command now shows: " ); (void) Execute( "date", 0, 0 ); get_sys_time( &mytimeval ); printf( "Current system time is:\n"); show_time(mytimeval.tv_secs); /* Added the microseconds part to show that time keeps */ /* increasing even though you ask many times in a row */ printf("Now do three TR_GETSYSTIMEs in a row (notice how the microseconds increase)\n\n"); get_sys_time( &mytimeval ); printf("First TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro); get_sys_time( &mytimeval ); printf("Second TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro); get_sys_time( &mytimeval ); printf("Third TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro); printf( "\nResetting to former time\n" ); set_new_time( oldtimeval.tv_secs ); get_sys_time( &mytimeval ); printf( "Current system time is:\n"); show_time(mytimeval.tv_secs); /* just shows how to set up for using the timer functions, does not */ /* demonstrate the functions themselves. (TimerBase must have a */ /* legal value before AddTime, SubTime or CmpTime are performed. */ tr = create_timer( UNIT_MICROHZ ); if (tr) TimerBase = (struct Library *)tr->tr_node.io_Device; else printf("Could't create timer with UNIT_MICROHZ\n"); /* and how to clean up afterwards */ TimerBase = (struct Library *)(-1); delete_timer( tr ); return 0; }
/* Function: RecvResponse * Description: Receive the response to the DESCRIBE request * Parameters: abort_time - time after which receive should be aborted * Returns: Success or failed */ rtsp_ret_e RecvResponse (abs_time_t abort_time) { int len; char resp[MAX_BUFFER_LENGTH]; int total_length; int acc_len; int req_len = MAX_BUFFER_LENGTH; int rv; struct timeval timeout; /* * The socket is blocking and consequently the recv syscall will block until * a TCP fragment is received or a timeout is reached. If the timeout * is reached the method will immediately free the response data buffer * and return declaring failure. The timeout value is arbitrarily chosen. * The socket will return with EAGAIN when there is a timeout. * * It is also possible that there are multiple TCP fragments and while no * timeouts are seen, an update continues to be active. Thus the total * time for receiving a complete response has been bounded as well. */ timeout.tv_sec = RTSP_CLIENT_RESPONSE_FRAG_RECV_TIMEOUT_SECS; timeout.tv_usec = 0; rv = setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); if (rv) { VQEC_LOG_ERROR("RecvResponse:: Error in setting socket option \n"); return RTSP_SOCKET_ERR; } bzero(resp, MAX_BUFFER_LENGTH); /* Peek the message first to figure out the content length */ if ((len = recv(client.socket, resp, MAX_BUFFER_LENGTH, MSG_PEEK)) != SOCKET_ERROR) { VQEC_DEBUG(VQEC_DEBUG_UPDATER, "RecvResponse:: Received the first byte ...\n"); total_length = GetResponseLength(resp); req_len += total_length; /* Check the size of the request */ if (req_len > MAX_BUFFER_LENGTH * MAX_CHANNELS) { VQEC_LOG_ERROR("RecvResponse:: Ask too much memory " "in receiving the response\n"); return RTSP_MALLOC_ERR; } /* Allocate the memory for response data */ client.response.data_buffer = malloc(sizeof(char) * req_len); if (client.response.data_buffer == NULL) { VQEC_LOG_ERROR("RecvResponse:: Memory allocation error occured " "in receiving the response\n"); return RTSP_MALLOC_ERR; } /* Reset the memory */ memset(client.response.data_buffer, 0, req_len); bzero(resp, MAX_BUFFER_LENGTH); acc_len = 0; while ((len = recv(client.socket, resp, MAX_BUFFER_LENGTH, 0))) { if (len <= 0) { /* * EAGAIN is nominally returned when there is a timeout or the * socket has been closed - abort reception in that case. */ if (errno == ECONNRESET || errno == EAGAIN) { VQEC_LOG_ERROR("RecvResponse:: Socket error occured " "in receiving the response (ECONNRESET / EAGAIN)\n"); free(client.response.data_buffer); client.response.data_buffer = NULL; return RTSP_SOCKET_ERR; } } else { strncat(client.response.data_buffer, resp, len); acc_len += len; if (acc_len > total_length) { break; } VQEC_DEBUG(VQEC_DEBUG_UPDATER, "Received fragment %d/%d/%d\n", len, acc_len, total_length); bzero(resp, MAX_BUFFER_LENGTH); } if (TIME_CMP_A(gt, get_sys_time(), abort_time)) { VQEC_LOG_ERROR("RecvResponse:: maximum timeout exceeded\n"); free(client.response.data_buffer); client.response.data_buffer = NULL; return RTSP_SOCKET_ERR; } } VQEC_DEBUG(VQEC_DEBUG_UPDATER, "RecvResponse:: Received %d bytes ...\n", acc_len); return RTSP_SUCCESS; } else { VQEC_LOG_ERROR("RecvResponse:: Socket error occured in receiving " "the response\n"); return RTSP_SOCKET_ERR; } }
/* 将BUFFER的内容以十六进制的格式打印出来 */ void fhlog(char *buf, int len) { int ret; int i; int j; char temp1[100]; char temp2[100]; char *p; int offset; int t1; int t2; FILE* fp; /* 如果不打印,退出 */ if (BUF_LEVEL == 0) return; /* 如果文件未打开 */ if ((fp = fopen(BUF_FILE_NAME, "a")) == NULL) return ; /* 初始化 */ i = 0; j = 0; memset(temp1, 0, sizeof(temp1)); memset(temp2, 0, sizeof(temp2)); fprintf(fp, "时间:[%08d] 时间:[%06d]\n", get_sys_date(), get_sys_time()); for (i = 0; i < len; i ++) { if ((i + 1) % 10 == 0 && (i + 1) % 20 != 0) sprintf(temp2, "%02X-", (unsigned char)(*(buf + i))); else sprintf(temp2, "%02X ", (unsigned char)(*(buf + i))); strcat(temp1, temp2); if ((i + 1) % 20 == 0 && i != 0) { fprintf(fp, "%s", temp1); memset(temp1, 0, sizeof(temp1)); zmemcpy(temp1, buf + j, 20); for (t1 = 0; t1 < 20; t1 ++) { if (!isprint(*(temp1 + t1))) *(temp1 + t1) = '.'; fprintf(fp, "%c", *(temp1 + t1)); } fprintf(fp, "\n"); j = i + 1; memset(temp1, 0, sizeof(temp1)); } } if ((i + 1) % 20 != 0) { fprintf(fp, "%-60s", temp1); zmemcpy(temp1, buf + j, len - j); for (t1 = 0; t1 < len - j; t1 ++) { if (!isprint(*(temp1 + t1))) *(temp1 + t1) = '.'; fprintf(fp, "%c", *(temp1 + t1)); } fprintf(fp, "\n"); } fprintf(fp, "\n\n"); fclose(fp); return; }
/* Function: build_suppressed_msg_value * Description: Pack info of a syslog message into a xmlrpc array of structs: * Key: "msg name" * Value: Array of N items: * Index 0 Struct of 1 members: * Index 0 Key: String "para name" * Index 0 Value: Struct "para value" * * Index 1 Struct of 1 members: * Index 1 Key: String "para name" * Index 1 Value: Struct "para value" * * Parameters: env[in]: xmlrpc env struct * p_msg[in]: ptr to a msg struct * Returns: xml formatted data */ static xmlrpc_value *build_suppressed_msg_value (xmlrpc_env *const env, syslog_msg_def_t *p_msg) { xmlrpc_value *retval; xmlrpc_value *msg_value = NULL; msg_value = xmlrpc_array_new(env); if (!msg_value) { goto done; } if ((build_xml_struct(env, msg_value, "Name", p_msg->msg_name, XMLRPC_CHAR) == NULL) || (build_xml_struct(env, msg_value, "Facility", p_msg->facility->fac_name, XMLRPC_CHAR) == NULL) || (build_xml_struct(env, msg_value, "Format", p_msg->output_format, XMLRPC_CHAR) == NULL)) { return NULL; } if (p_msg->limiter) { char ts_buf[BUFFER_SIZE], string_buffer[BUFFER_SIZE]; snprintf(string_buffer, BUFFER_SIZE, "%llu", p_msg->limiter->suppressed); abs_time_t now = get_sys_time(); rel_time_t last_sup_time = TIME_SUB_A_A(now, p_msg->limiter->last_time); if ((build_xml_struct(env, msg_value, "Limit rate", &(p_msg->limiter->rate_limit), XMLRPC_UINT) == NULL) || (build_xml_struct(env, msg_value, "Suppressed last time (sec ago)", (char *)rel_time_to_str( last_sup_time, ts_buf, BUFFER_SIZE), XMLRPC_CHAR) == NULL) || (build_xml_struct(env, msg_value, "Suppressed message count", string_buffer, XMLRPC_CHAR) == NULL)) { return NULL; } } done: retval = xmlrpc_build_value(env, "{s:A}", p_msg->msg_name, msg_value); xmlrpc_DECREF(msg_value); return retval; }