webserver<2>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <errno.h>
#include "common.h"
#include "serversignal.h"
#include "server_epoll.h" static int open_socket(struct sockaddr_in* paddr);
static int accept_client(int sockfd, struct sockaddr_in* paddr); static int process_request(int connfd);
static volatile sig_atomic_t graceful=; #define HTTP_PORT 18080
#define BACK_LOG 50
#define MAX_FDS 100
#define SOCKLEN sizeof(struct sockaddr_in) #define err_log_exit()\
do{\
perror("server failed");\
fprintf(stderr, "file %s line %d\n", __FILE__, __LINE__);\
exit(EXIT_FAILURE);\
}while()\ #define err_msg_exit(msg)\
do{\
perror(msg);\
fprintf(stderr, "file %s line %d\n", __FILE__, __LINE__);\
exit(EXIT_FAILURE);\
}while()\ static int setnonblocking(int sockfd)
{
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, )|O_NONBLOCK) == -)
{
return -;
} return ;
} int main(int argc, char *argv[])
{
signal_init();
int max_worker = ;
int child = ;
int epollfd = ;
struct sockaddr_in saddr;
int sockfd = ;
int nfds = ;
int index = ;
int fd = ;
int acceptfd = ;
struct epoll_event *events; memset(&saddr, , sizeof(struct sockaddr_in)); sockfd=open_socket(&saddr); if(sockfd == -)
err_log_exit(); while(!graceful&&!child){
if(max_worker>){
switch(fork()){
case -:
err_log_exit();
break;
case :
child =;
break;
default:
printf("child creat\n");
max_worker--;
break;
}
}else{
int status =;
if( - != wait(&status)){
//max_worker++;
fprintf(stderr, "child quit\n");
}
}
} if(!child){
fprintf(stderr, "before quit, kill all child\n");
kill(, SIGINT);
sleep();
return ;
} //child
epollfd = server_epoll_create(MAX_FDS+);
if(epollfd == -)
err_log_exit(); if(server_epoll_event_add(epollfd, sockfd) == -)
err_log_exit(); events = (struct epoll_event*)malloc(MAX_FDS*sizeof(struct epoll_event));
memset(events, , MAX_FDS*sizeof(struct epoll_event)); /* close stdin and stdout, as they are not needed */
/* move stdin to /dev/null */
if (- != (fd = open("/dev/null", O_RDONLY))) {
close(STDIN_FILENO);
dup2(fd, STDIN_FILENO);
close(fd);
} /* move stdout to /dev/null */
if (- != (fd = open("/dev/null", O_WRONLY))) {
close(STDOUT_FILENO);
dup2(fd, STDOUT_FILENO);
close(fd);
} while(child&&!graceful){
nfds = epoll_wait(epollfd, events, MAX_FDS, );
index = ; while(index < nfds){ if(events[index].data.fd == sockfd){
acceptfd = accept_client(sockfd, &saddr);
//waking herd
if(acceptfd == -){
perror("accept failed\n");
}else{
//accept ok
if(server_epoll_event_add(epollfd, acceptfd) == -)
err_log_exit();
}
}else if(events[index].data.fd == acceptfd){
// receive data from client
// if client close, need avoid TIME_WAIT status
if(process_request(acceptfd) == ){
fprintf(stderr, "client close, close connection and quit listen connect fd\n");
if(server_epoll_event_del(epollfd, acceptfd) == -)
err_log_exit();
close(acceptfd);
}
}else{ }
index++;
}; if(nfds == -){
if (errno == EINTR)
continue;
else{
err_log_exit();
}
} }; return ;
} void server_graceful_set(int g)
{
if(g>){
g=;
}else{
g=;
}
graceful=g;
} int server_graceful_get()
{
return graceful;
} static int open_socket(struct sockaddr_in* paddr)
{
int sockfd = ;
struct sockaddr_in sockaddress; bzero(&sockaddress, sizeof(sockaddress)); if((sockfd = socket(AF_INET, SOCK_STREAM, )) == -)
err_log_exit(); sockaddress.sin_family = AF_INET;
sockaddress.sin_port = htons(HTTP_PORT); setnonblocking(sockfd); inet_pton(AF_INET, "10.174.8.163", &(sockaddress.sin_addr)); if(bind(sockfd, (struct sockaddr*)(&sockaddress), sizeof(sockaddress)) == -)
err_log_exit(); if(listen(sockfd, BACK_LOG) == -)
err_log_exit(); *paddr = sockaddress;
return sockfd;
} static int accept_client(int sockfd, struct sockaddr_in* paddr)
{
socklen_t len = SOCKLEN;
int connfd = ; if(paddr != NULL)
{
connfd = accept(sockfd, (struct sockaddr*)(paddr), &len);
}else
{
connfd = -;
}
return connfd;
} static int process_request(int connfd)
{
char request[];
int len = ;
bzero(request, sizeof(request));
len = recv(connfd, request, sizeof(request), );
if(len >)
fprintf(stderr, "%s\n", request); return len;
}
#include <unistd.h>
#include <string.h>
#include "server_epoll.h" int server_epoll_event_add(int epollfd, int sockfd)
{
struct epoll_event ep;
memset(&ep, , sizeof(ep));
ep.events = ;
ep.events |= EPOLLIN;
ep.events |= EPOLLOUT;
ep.data.fd = sockfd; return epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ep);
} int server_epoll_event_del(int epollfd, int sockfd)
{
struct epoll_event ep;
memset(&ep, , sizeof(ep));
ep.events = ;
ep.events |= EPOLLIN;
ep.events |= EPOLLOUT;
ep.data.fd = sockfd; return epoll_ctl(epollfd, EPOLL_CTL_DEL, sockfd, &ep);
} int server_epoll_create(int size)
{
int fd = epoll_create(size);
return fd;
} int server_epoll_close(int fd)
{
return close(fd);
}
上一篇:pytest进阶之xunit fixture


下一篇:PagedList.MVC 应用