//
#include <fcntl.h>
#include <cerrno>
#include <cstdlib>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <algorithm>
#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 1024
#define MAX_NODE 2 << 9
#define IP_PORT "%s:%d say: %s"
struct node_data {
int sockfd;
char *writebuf;
char recvbuf[BUFFER_SIZE];
};
struct node{
struct node_data data;
char* ip;
int port;
};
int on = 1;
// 将文件描述符设置成非阻塞
int setNonBlocking(int fd) {
int old_opt = fcntl(fd, F_GETFD);
int new_opt = old_opt | O_NONBLOCK;
fcntl(fd, F_SETFD, new_opt);
return old_opt;
}
char *getIp(sockaddr_in *remote) {
return inet_ntoa(remote->sin_addr);
}
int getPort(sockaddr_in *remote) {
return ntohs(remote->sin_port);
}
/**
将文件描述符fd上的EPOLLIN注册到epollfd指示的epoll内核事件中,参数oneshot指定是否注册fd上的EPOLLONESHOT事件
*/
void addfd(int epollfd, int fd, bool oneshot) {
auto nd = new node_data;
epoll_event event;
nd->sockfd = fd;
event.data.ptr = (void *) nd;
event.events = EPOLLIN | EPOLLET;
if (oneshot) {
event.events |= EPOLLONESHOT;
}
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setNonBlocking(fd);
}
void set_epollOut(int epollfd, int fd) {
node_data* nd = (node_data*)malloc(sizeof(node_data));
epoll_event* event = (epoll_event*)malloc(sizeof(epoll_event));
nd->sockfd = fd;
event->data.ptr = (void *) nd;
event->events = EPOLLOUT | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, event);
}
void set_epollIn(int epollfd, int fd) {
auto nd = new node_data;
epoll_event event;
nd->sockfd = fd;
event.data.ptr = (void *) nd;
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
}
/**
重置fd上的事件。这样操作之后,尽管fd上的EPOLLONESHOT事件被注册,但是操作仍然会触发fd上的EPOLLIN事件,且只能触发一次
*/
void reset_oneshot(int epoll_fd, int fd) {
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event);
}
void init_sock(sockaddr_in *address, int port, const char *ip) {
bzero(address, sizeof(address));
address->sin_family = AF_INET;
inet_pton(AF_INET, ip, &address->sin_addr);
address->sin_port = htons(port);
}
/**
//工作线程
void* worker(void* arg){
int sockfd = ((fds*)arg)->sockfd;
int epollfd = ((fds*)arg)->epollfd;
printf("start new thread to reveive data on fd : %d\n",sockfd);
char buf[BUFFER_SIZE];
memset(buf, '\0', BUFFER_SIZE);
//循环读取sockfd上的数据,直到遇到EAGAIN错误
while (1) {
int ret = recv(sockfd, buf, BUFFER_SIZE-1, 0);
if(ret == 0){
close(sockfd);
printf("foreiner closed the connection\n");
break;
}else if(ret < 0){
if (errno == EAGAIN) {
reset_oneshot(epollfd, sockfd);
printf("read later\n");
break;
}
}else{
printf("get content : %s\n",buf);
sleep(5);
}
}
printf("end thread receiveing data on fd : %d\n",sockfd);
}
*/
int main(int argc, const char *argv[]) {
if (argc <= 2) {
printf("usage: %s ip_address port_number\n", basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int ret = 0;
struct sockaddr_in address;
init_sock(&address, port, ip);
int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
if (socket_fd == -1) {
perror("create socket error");
exit(1);
}
//设定socket 选项
ret = setsockopt(socket_fd,
SOL_SOCKET,
SO_REUSEADDR,
&on,
sizeof(on));
if(ret == -1){
printf("Failed to set socket options\n");
}
ret = bind(socket_fd, (struct sockaddr *) &address, sizeof(address));
if (ret == -1) {
perror("bind error");
exit(1);
}
ret = listen(socket_fd, 5);
if (ret == -1) {
perror("listen error");
exit(1);
}
epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(5);
if (epollfd == -1) {
perror("epoll create error");
exit(1);
}
/**
监听socket 的fd不能注册EPOLLONESHOT事件的,否则应用程序只能处理一个客户连接!因为后续的客户连接请求将不再触发socket_fd上的EPOLLIN
*/
//addfd(epollfd, socket_fd, false);
/**
* malloc enough node
*/
//struct node* nodes = (node*)malloc(sizeof(node)*MAX_NODE);
struct node nodes[100];
epoll_event ev;
ev.events = EPOLLIN;
node_data nd;
nd.sockfd = socket_fd;
ev.data.ptr = (void *) &nd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, socket_fd, &ev);
setNonBlocking(socket_fd);
printf("server has started! waiting connection....\n");
int nodeCnt = 0;
while (1) {
int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
if (ret < 0) {
printf("epoll failure\n");
break;
}
for (int i = 0; i < ret; i++) {
node_data *tmp_data = (node_data *) events[i].data.ptr;
if (tmp_data->sockfd == socket_fd) {
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(socket_fd, (struct sockaddr *) &client_address, &client_addrlength);
setNonBlocking(connfd);
//对每个非监听文件描述符都注册EPOLLONESHOT事件
//addfd(epollfd, connfd, true);
struct epoll_event event = {0};
event.events = EPOLLIN;
node_data* tmp_nd = (node_data*)malloc(sizeof(node_data));
tmp_nd->sockfd = connfd;
event.data.ptr = (void*)tmp_nd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event);
nodes[nodeCnt].data = *tmp_nd;
nodes[nodeCnt].ip = getIp(&client_address);
nodes[nodeCnt].port = getPort(&client_address);
nodeCnt++;
printf("comes a new node: %d, exists %d node \n",connfd,nodeCnt);
} else if (events[i].events & EPOLLIN) {
memset(tmp_data->recvbuf, '\0', BUFFER_SIZE);
int readlen = recv(tmp_data->sockfd, tmp_data->recvbuf, BUFFER_SIZE - 1, 0);
// set write evvent
if (readlen < 0) {
//error
} else if (readlen == 0) {
} else {
//set print node msg
printf("node %d received :%s\n",tmp_data->sockfd, tmp_data->recvbuf);
// broadcast
for (int j = 0; j < nodeCnt; ++j) {
char msg[BUFFER_SIZE];
memset(msg,'\0',BUFFER_SIZE);
sprintf(msg,IP_PORT, nodes[j].ip,nodes[j].port,tmp_data->recvbuf);
nodes[j].data.writebuf = msg;
send(nodes[j].data.sockfd, nodes[j].data.writebuf, BUFFER_SIZE, 0);
}
}
//新启动一个工作线程为sockfd服务
//pthread_create(&thread, NULL, worker, (void*) &fds_for_new_worker);
} else {
printf("something else happened \n");
}
}
}
close(socket_fd);
return 0;
}