}
else
lprintf(lfd, INFO, "TCP socket creat susscess!\n");
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (void *) listen_opt,(int) sizeof(listen_opt));// 设置端口多重邦定
setnonblocking(listener);
bzero(my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listener, (struct sockaddr *) my_addr, sizeof(struct sockaddr)) == -1)
{
lprintf(lfd, FATAL, "TCP bind error!\n");
exit(1);
}
else
lprintf(lfd, INFO, "TCP bind susscess!\n");
if (listen(listener, lisnum) == -1)
{
lprintf(lfd, FATAL, "TCP listen error!\n");
exit(1);
}
else
lprintf(lfd, INFO, "TCP listen susscess!\n");
kdpfd = epoll_create(MAXEPOLLSIZE);// 创建 epoll句柄 , 把监听socket加入到epoll集合里
ev.events = EPOLLIN | EPOLLET;// 注册epoll 事件
ev.data.fd = listener;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, ev)0)
lprintf(lfd, FATAL, "EPOLL_CTL_ADD error!\n");
while (1)
{
sem_wait(sem_tcp);// 等待 sem_TCP
sem_wait(sem_tp);// 将tp值减一
nfds = epoll_wait(kdpfd, events, MAXEPOLLSIZE, 1); // 等待有事件发生
if (nfds == -1)
lprintf(lfd, FATAL,"EPOLL_WAIT error!\n");
for (n = 0; nnfds; ++n) // 处理epoll所有事件
{
if (events[n].data.fd == listener) // 如果是连接事件
{
if ((efd = accept(listener, (struct sockaddr *) their_addr,len))0)
{
lprintf(lfd, FATAL, "accept error!\n");
continue;
}
else
lprintf(lfd, INFO, "Client from :%s\tSocket ID:%d\n", inet_ntoa(their_addr.sin_addr) ,efd);
setnonblocking(efd);// 设置新连接为非阻塞模式
ev.events = EPOLLIN | EPOLLET; // 注册新连接
ev.data.fd = efd;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, efd, ev)0) // 将新连接加入EPOLL的监听队列
lprintf(lfd, FATAL, "EPOLL_CTL_ADD error!\n");
else
{
gettimeofday(ntime, NULL);
cntime[efd] = ntime.tv_sec;
sfd[efd] = efd;
}
}
else if (events[n].eventsEPOLLIN)
tpool_add_work(pool, TcpProcess, (void*)events[n].data.fd);// 读取分析TCP信息
else
{
close(events[n].data.fd);
epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd, ev);
}
}
sem_post(sem_cm);
sem_post(sem_udp);
}
close(listener);
}
int DataProcess(LOVENIX *info)// 处理GPS数据
{
if(sflag == 0(CacheRec != TotalRec)) // 缓存1可用且没有满
{
gps_cache[CacheRec].lat = info-lat;
gps_cache[CacheRec].mileage = info-mileage;
gps_cache[CacheRec].lon = info-lon;
gps_cache[CacheRec].speed = atod(info-speed, strlen(info-speed))*0.514444444*3.6;
gps_cache[CacheRec].udate = atoi(info-udate);
gps_cache[CacheRec].utime = atoi(info-utime);
gps_cache[CacheRec].dir = atoi(info-dir);
sprintf(gps_cache[CacheRec].tid ,"%s",info-tid);
CacheRec++;
// printf("CacheRec %d\tTotalRec %d \t sflag:%d\n",CacheRec,TotalRec,sflag);
if(CacheRec == TotalRec)
{
sflag = 1;
pthread_attr_init(attr);// 初始化属性值,均设为默认值
pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); //设置线程为分离属性
if (pthread_create(thread, attr,(void*) OracleProcess,(void*)gps_cache)) // 创建数据处理线程
lprintf(lfd, FATAL, "oracle pthread_creat error!\n");
CacheRec = 0;
}
}
else if(sflag == 1(Cache1Rec != TotalRec)) // 缓存2可用且没有满
{
gps_cache1[Cache1Rec].mileage = info-mileage;
gps_cache1[Cache1Rec].lat = info-lat;
gps_cache1[Cache1Rec].lon = info-lon;
gps_cache1[Cache1Rec].speed = atod(info-speed, strlen(info-speed))*0.514444444*3.6;
推荐阅读
- 多功能电视怎么设置摄像头,电视摄像机怎么用
- jquery滑动对比,jquery滑动事件
- 类似与jquery,类似与凤行的仙侠文
- 小时候网页格斗小游戏大全,小时候玩的格斗电子游戏
- vb.net键盘扫码 vbs键盘代码
- oracle表关联自己更新,oracle多表关联
- redis查看列表数据,redis 查看list内容
- 1688直播伴侣,1688直播伴侣性能指标显示脱机状态怎么办
- go语言切片扩容技巧 go 切片添加切片