/* There is a case that system time is changed. */ void pal_time_tzcurrent (struct pal_timeval *tv, struct pal_tzval *tz) { system_uptime (tv, tz); return; }
/*! ** Return current time. ** ** Parameters ** OUT pal_time_t *tp : A pointer to pal_time_t ** ** Results ** -1 for error or pal_time_t. */ pal_time_t pal_time_current (pal_time_t *tp) { struct pal_timeval tv; int ret; /* Get current time i.e. time since reboot. */ ret = system_uptime (&tv, NULL); if (ret != RESULT_OK) return -1; /* When argument is specified copy value. */ if (tp) *tp = (pal_time_t) tv.tv_sec; return tv.tv_sec; }
static void* GB_Server(void *pParam) { pthread_detach(pthread_self()); PRM_GB_SIPD_CFG gb_cfg; struct pollfd poll_table[MAX_GB_MSG_NUM+MAX_GB_CONNECTION_NUM+1]; struct pollfd *poll_entry = NULL; int i, ret; int rlen = 0; GB_CONNECT_STATE *gb_cons = NULL; char localip[16] = {0}; char sipserver_ip[16] = {0}; char localmsg_buf[GB_MAX_PLAYLOAD_BUF]; gb_cons = SN_MALLOC(MAX_GB_CONNECTION_NUM*sizeof(GB_CONNECT_STATE)); if(gb_cons == NULL) { printf("SN_MALLOC gb_cons Err!\n"); return NULL; } SN_MEMSET(gb_cons, 0, MAX_GB_CONNECTION_NUM*sizeof(GB_CONNECT_STATE)); SN_MEMSET(&gb_cfg,0,sizeof(gb_cfg)); for(i=0; i<MAX_GB_CONNECTION_NUM; i++) { GB_ResetConState(&gb_cons[i]); } Log_pid(__FUNCTION__); while(1) { SN_MEMSET(poll_table, 0, sizeof(poll_table)); poll_entry = poll_table; if(localmsg_readSock > 0) { poll_entry->fd = localmsg_readSock; poll_entry->events = POLLIN; poll_entry++; } if(gbmsg_readSock > 0) { poll_entry->fd = gbmsg_readSock; poll_entry->events = POLLIN; poll_entry++; } if(gb_ipchange == 1) { if(IsTimeOfArrival(gb_ipchange_time,30)) { gb_ipchange = 0; } } for(i=0; i<MAX_GB_CONNECTION_NUM; i++) { gb_cons[i].poll_act = NULL; if(gb_cons[i].cur_state > GB_STATE_CONNECTING) { poll_entry->fd = gb_cons[i].connfd; poll_entry->events = POLLIN; gb_cons[i].poll_act = poll_entry; poll_entry++; } else if(gb_cons[i].cur_state == GB_STATE_IDEL) { if(gb_ipchange == 1) { TRACE(SCI_TRACE_NORMAL,MOD_GB, "IP Changed! Please Wait a Moment!"); continue; } GB_Get_GBCfg(&gb_cfg); if(gb_cfg.enable == 1) // 启用 { if(SN_STRLEN((char *)gb_cfg.deviceID) <= 0 || SN_STRLEN((char *)gb_cfg.sipserver_ID) <= 0) { TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s line=%d deviceID=%s sipserver_ID=%s\n",__FUNCTION__,__LINE__,gb_cfg.deviceID,gb_cfg.sipserver_ID); if(gb_cons[i].connfd > 0) close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); GB_Refresh_GBCfg(); continue; } // 进入国标模式 if(gb_cfg.transfer_protocol == GB_TRANSFER_UDP) // UDP { gb_cons[i].connfd = GB_CreateSocket(SOCK_DGRAM, gb_cfg.local_port); } else // TCP { gb_cons[i].connfd = GB_CreateSocket(SOCK_STREAM, gb_cfg.local_port); } if(gb_cons[i].connfd <= 0) { TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s line=%d GB_CreateSocket Err\n",__FUNCTION__,__LINE__); continue; } SN_MEMSET(sipserver_ip,0,sizeof(sipserver_ip)); SN_SPRINTF(sipserver_ip,sizeof(sipserver_ip),"%d.%d.%d.%d", gb_cfg.sipserver_ip[0],gb_cfg.sipserver_ip[1],gb_cfg.sipserver_ip[2],gb_cfg.sipserver_ip[3]); gb_cons[i].transfer_protocol = gb_cfg.transfer_protocol; SN_MEMSET(&gb_cons[i].remoteAddr,0,sizeof(gb_cons[i].remoteAddr)); gb_cons[i].remoteAddr.sin_family = AF_INET; gb_cons[i].remoteAddr.sin_addr.s_addr =inet_addr(sipserver_ip); gb_cons[i].remoteAddr.sin_port = htons(gb_cfg.sipserver_port); gb_cons[i].beginconect_time = system_uptime(); gb_cons[i].cur_state = GB_STATE_CONNECTING; ret = connect(gb_cons[i].connfd, (struct sockaddr *) &(gb_cons[i].remoteAddr), sizeof(gb_cons[i].remoteAddr)); if ( ret == 0 || errno == EISCONN) { gb_cons[i].cur_state = GB_STATE_REGISTER; poll_entry->fd = gb_cons[i].connfd; poll_entry->events = POLLIN; gb_cons[i].poll_act = poll_entry; poll_entry++; GB_GetLocalIPaddrFromSock(gb_cons[i].connfd,localip,sizeof(localip)); GB_Set_LocalIP(localip); GB_sipd_register(&gb_cons[i], 0); // 不带认证的注册请求 gb_cons[i].last_sendtime = get_cur_time()/1000; } } else { GB_Set_gGBConnStatus(0); usleep(100); } } else if(gb_cons[i].cur_state == GB_STATE_CONNECTING) { ret = connect(gb_cons[i].connfd, (struct sockaddr *) &(gb_cons[i].remoteAddr), sizeof(gb_cons[i].remoteAddr)); if ( ret == 0 || errno == EISCONN) { gb_cons[i].cur_state = GB_STATE_REGISTER; poll_entry->fd = gb_cons[i].connfd; poll_entry->events = POLLIN; gb_cons[i].poll_act = poll_entry; poll_entry++; GB_GetLocalIPaddrFromSock(gb_cons[i].connfd,localip,sizeof(localip)); GB_Set_LocalIP(localip); GB_sipd_register(&gb_cons[i], 0); // 不带认证的注册请求 gb_cons[i].last_sendtime = get_cur_time()/1000; } else { if (system_uptime() - gb_cons[i].beginconect_time > 10) { TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s line=%d connect timeout\n",__FUNCTION__,__LINE__); close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); } } } if(gb_cons[i].cur_state == GB_STATE_REGISTER) // 未成功注册上 { if(IsTimeOfArrival(gb_cons[i].last_sendtime, 60)) // 间隔60s 后重新注册 { TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s line=%d REGISTER Fail ! try again!\n",__FUNCTION__,__LINE__); close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); GB_Refresh_GBCfg(); } } else if(gb_cons[i].cur_state == GB_STATE_RUNNING && gb_cons[i].bUnRegister == 1) // 注销检查 { if(IsTimeOfArrival(gb_cons[i].last_sendtime, 5)) // 超过5s 则认为成功注销 { TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s line=%d UNREGISTER Success!\n",__FUNCTION__,__LINE__); close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); GB_Set_gGBConnStatus(0); GB_Refresh_GBCfg(); } } else if(gb_cons[i].cur_state == GB_STATE_RUNNING && gb_cons[i].bUnRegister != 1) { if(IsTimeOfArrival(gb_cons[i].last_registertime, gb_cfg.register_period-60)) // 刷新注册, 提前60s { gb_cons[i].cur_state = GB_STATE_REGISTER; GB_sipd_register(&gb_cons[i], 0); // 不带认证的注册请求 gb_cons[i].last_sendtime = get_cur_time()/1000; } if(gb_cons[i].keepalive_timeout_cnt > gb_cfg.keepalive_timeout_cnt) // 对方离线 { close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); GB_Set_gGBConnStatus(2); } else if(IsTimeOfArrival(gb_cons[i].last_keepalivetime, gb_cfg.keepalive_interval-1)) // 心跳, 减1 是为了去除poll 的超时时间的影响 { gb_cons[i].keepalive_timeout_cnt++; GB_Send_KeepAlive(&gb_cons[i]); gb_cons[i].last_keepalivetime = get_cur_time()/1000; } } } ret = poll(poll_table, poll_entry-poll_table, 1000); if(ret<0) { perror("GB_Server:poll error"); poll(NULL, 0, 1000); continue; } poll_entry = poll_table; if(localmsg_readSock > 0) { if(poll_entry->revents & POLLIN) { // 处理本地消息 SN_MEMSET(localmsg_buf, 0, sizeof(localmsg_buf)); rlen = recvfrom(localmsg_readSock, localmsg_buf, sizeof(localmsg_buf), 0, NULL, 0); if (rlen > 0 && rlen <= sizeof(localmsg_buf)) { SN_MSG * pMsg; SYS_MSG *sys_msg = (SYS_MSG *)localmsg_buf; pMsg = sys_msg->pmsg; GB_MsgHandle(pMsg, &(gb_cons[0])); } } poll_entry++; } if(gbmsg_readSock > 0) { if(poll_entry->revents & POLLIN) { // 处理媒体流模块发来的消息 SN_MEMSET(localmsg_buf, 0, sizeof(localmsg_buf)); rlen = recvfrom(gbmsg_readSock, localmsg_buf, sizeof(localmsg_buf), 0, NULL, 0); if (rlen > 0 && rlen <= sizeof(localmsg_buf)) { } } poll_entry++; } for(i=0; i<MAX_GB_CONNECTION_NUM; i++) { if (gb_cons[i].poll_act != NULL) { if(gb_cons[i].poll_act->revents & POLLIN) { rlen = recv(gb_cons[i].connfd, gb_cons[i].buffer_ptr, gb_cons[i].buffer_end - gb_cons[i].buffer_ptr, 0); if(rlen > 0) { gb_cons[i].buffer_ptr += rlen; gb_cons[i].datasize += rlen; ret = is_recv_whole_messages(&gb_cons[i]); if(ret > 0) // 接收完全部数据 { //处理接收到的数据 GB_handle_messages(&gb_cons[i]); GB_reset_recv_buffer(&gb_cons[i], ret); } } } else if (gb_cons[i].poll_act->revents & POLLERR || gb_cons[i].poll_act->revents & POLLHUP || gb_cons[i].poll_act->revents & POLLNVAL) { if(gb_cons[i].cur_state == GB_STATE_RUNNING && gb_cons[i].keepalive_timeout_cnt > gb_cfg.keepalive_timeout_cnt) { close(gb_cons[i].connfd); GB_ResetConState(&gb_cons[i]); GB_Set_gGBConnStatus(2); GB_Refresh_GBCfg(); } } } } } return NULL; }
int process_time(pid_t pid, time64_t* createTime, time64_t* kernelTime, time64_t* userTime) { #if defined(_WIN32) HANDLE handle; FILETIME ftCreateTime, ftExitTime, ftKernelTime, ftUserTime; SYSTEMTIME stime; FILETIME ftime; ULONGLONG rt; ULARGE_INTEGER kt, ut; memset(&ftUserTime, 0xCC, sizeof(ftUserTime)); handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); if(!handle) return (int)GetLastError(); if(!GetProcessTimes(handle, &ftCreateTime, &ftExitTime, &ftKernelTime, &ftUserTime)) { CloseHandle(handle); return (int)GetLastError(); } CloseHandle(handle); GetSystemTime(&stime); SystemTimeToFileTime(&stime, &ftime); kt = FILETIME2UINT64(&ftime); ut = FILETIME2UINT64(&ftCreateTime); rt = kt.QuadPart > ut.QuadPart ? kt.QuadPart-ut.QuadPart : 0; // for resolution problem kt = FILETIME2UINT64(&ftKernelTime); ut = FILETIME2UINT64(&ftUserTime); *createTime = rt/10000; // nanosecond -> millisecond *userTime = ut.QuadPart/10000; *kernelTime = kt.QuadPart/10000; return 0; #else char content[2*1024] = {0}; int r; unsigned long int utime, stime; unsigned long long starttime; float uptime = 0.0f; sprintf(content, "/proc/%d/stat", pid); r = tools_cat(content, content, sizeof(content)); if(r < 0) return r; // linux: man proc // cat proc/self/stat // (1-pid-%d, 2-filename-%s, 3-state-%c, 4-ppid-%d, 5-pgrp-%d, // 6-session-%d, 7-tty_nr-%d, 8-tpgid-%d, 9-flags-%u, 10-minflt-%lu, // 11-cminflt-%lu, 12-majflt-%lu, 13-cmajflt-%lu, 14-utime-%lu, 15-stime-%lu, // 16-cutime-%ld, 17-cstime-%ld, 18-priority-%ld, 19-nice-%ld, 20-num_threads-%ld, // 21-itrealvalue-%ld, 22-starttime-%llu, 23-vsize-%lu, 24-rss-%ld, 25-rsslim-%lu, // 26-startcode-%lu, 27-endcode-%lu, 28-startstack-%lu, 29-kstkesp-%lu, 30-kstkeip-%lu, // 31-signal-%lu, 32-blocked-%lu, 33-sigignore-%lu, 34-sigcatch-%lu, 35-wchan-%lu, // 36-nswap-%lu, 37-cnswap-%lu, 38-exit_signal-%d, 39-processor-%d, 40-rt_priority-%u, // 41-policy-%u, 42-delayacct_blkio_ticks-%llu, 43-guest_time-%lu, 44-cguest_time-%ld) if(3 != sscanf(content, // warning: use of assignment suppression and length modifier together in gnu_scanf format [-Wformat] //"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu %lu %*ld %*ld %*ld %*ld %*ld %*ld %llu", "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu %*d %*d %*d %*d %*d %*d %llu", &utime, &stime, &starttime)) return -(int)EINVAL; assert(sysconf(_SC_CLK_TCK) == HZ); system_uptime(&uptime); *createTime = (time64_t)uptime*1000 - starttime*1000/HZ; // jiffies -> millisecond *userTime = utime * 1000 / HZ; *kernelTime = stime * 1000 / HZ; return 0; #endif }