修改网络监听退出流程
This commit is contained in:
@ -145,11 +145,11 @@ int exce(const int command,ctx *all_ctx)
|
||||
return 1;
|
||||
|
||||
case RUN:
|
||||
printf("runing\n");
|
||||
printf("SYS:runing\n");
|
||||
return 1;
|
||||
|
||||
case QUIT:
|
||||
printf("shuting down\n");
|
||||
printf("SYS:shuting down\n");
|
||||
all_ctx->statue = -1;
|
||||
write(all_ctx->fifofd[1],"q",1);
|
||||
return 1;
|
||||
|
@ -206,21 +206,23 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
ev.data.fd = fifo_fd;
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||
char iss_buf[256];
|
||||
int http_fd = init_network(port);
|
||||
self->http_fd = init_network(port);
|
||||
|
||||
ev.data.fd = http_fd;
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, http_fd, &ev);
|
||||
ev.data.fd = self->http_fd;
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
|
||||
struct epoll_event events;
|
||||
self->epoll_fd = epfd;
|
||||
for(;;)
|
||||
{
|
||||
/*工作循环-----------------------------*/
|
||||
int nf = epoll_wait(epfd,&events,1,-1);
|
||||
if (nf == -1) {
|
||||
perror("epoll_wait");
|
||||
break;
|
||||
}
|
||||
if(events.data.fd ==http_fd)
|
||||
if(events.data.fd ==self->http_fd)
|
||||
{
|
||||
int nt_fd = accept4(http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC);
|
||||
int nt_fd = accept4(self->http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC);
|
||||
if(nt_fd == -1)
|
||||
continue;
|
||||
sprintf(iss_buf,"s/%d/e",nt_fd);
|
||||
@ -234,6 +236,7 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
switch(command){
|
||||
case 'q':
|
||||
//退出逻辑
|
||||
quit_server(self);
|
||||
return 1;
|
||||
break;
|
||||
case 'u':
|
||||
@ -243,6 +246,7 @@ int server_run(int port,int fifo_fd,netm *self)
|
||||
}
|
||||
}
|
||||
}
|
||||
/*工作循环----------------------------*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,8 @@ typedef struct network_manager
|
||||
log_manager *logmanager;
|
||||
int last_alc;
|
||||
int port;
|
||||
int epoll_fd;
|
||||
int http_fd;
|
||||
}netm;
|
||||
|
||||
typedef struct rebot_message
|
||||
|
@ -98,9 +98,9 @@ void log_manager_stop(log_manager *self)
|
||||
pthread_mutex_lock(&self->mtx);
|
||||
self->stop = 1; /* 置退出标志 */
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
printf("stopping loger\n");
|
||||
printf("SYS:stopping loger\n");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
printf("done\n");
|
||||
printf("SYS:done\n");
|
||||
}
|
||||
|
||||
//定期清理函数
|
||||
|
@ -1,9 +1,40 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include<unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/epoll.h>
|
||||
#include "quit.h"
|
||||
#include "tem/ctl.h"
|
||||
|
||||
int quit_server(netm *self)
|
||||
{
|
||||
if(self ==NULL)
|
||||
return -1;
|
||||
if(self->epoll_fd != -1)
|
||||
{
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->http_fd,NULL);
|
||||
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->fifo_fd[0],NULL);
|
||||
self->epoll_fd = -1;
|
||||
}
|
||||
//关闭epoll监听
|
||||
if(self->http_fd != -1)
|
||||
{
|
||||
close(self->http_fd);
|
||||
self->http_fd =-1;
|
||||
}
|
||||
//关闭socket监听
|
||||
if(self->fifo_fd[0] != -1)
|
||||
{
|
||||
close(self->fifo_fd[0]);
|
||||
self->fifo_fd[0] = -1;
|
||||
}
|
||||
//关闭管道监听
|
||||
}
|
||||
|
||||
void quit_all(int status,void *self_p)
|
||||
{
|
||||
alres *resouce =(alres*)self_p;
|
||||
@ -13,6 +44,7 @@ void quit_all(int status,void *self_p)
|
||||
logs *netlog = (logs*)malloc(sizeof(logs));
|
||||
netlog->next = NULL;
|
||||
memcpy(netlog->log,"shuting down networkserver",27);
|
||||
quit_server(resouce->network);
|
||||
resouce->loger->in_log(netlog,resouce->loger);
|
||||
free(resouce->network);
|
||||
//释放网络资源
|
||||
|
@ -14,6 +14,7 @@ typedef struct all_resources
|
||||
|
||||
|
||||
void quit_all(int status,void *self_p);
|
||||
int quit_server(netm *self);
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user