首頁 > 軟體

C++基於reactor的伺服器百萬並行實現與講解

2022-07-01 18:03:26

reactor實現的原理請參考:
https://www.jb51.net/article/253794.htm
本次百萬並行的程式碼實現也是基於上面程式碼進行更改而來

並行量和承載的概念
並行量:一個伺服器能同時承載使用者端的數量
承載:使用者端傳送給伺服器的請求(http或tcp等)在200ms內可以返回正確的結果

一、伺服器的程式碼實現與講解

結構體程式碼主要構建的結構如圖所示
連結串列結構,每個eventblock結點,包括一個ntyevent陣列,陣列中儲存fd

/*結構體定義連結串列陣列*/
struct ntyevent {
	int fd;//要監聽的檔案描述符
	int events;//對應的監聽事件,	EPOLLIN和EPOLLOUT(不同的事件,走不同的回撥函數)
	void *arg;//指向自己結構體指標
	int (*callback)(int fd, int events, void *arg);
	
	int status;//是否在監聽:1->在紅黑樹上(監聽),0->不在(不監聽)
	char buffer[BUFFER_LENGTH];
	int length;
	long last_active;
};

struct eventblock {

	struct eventblock *next;
	struct ntyevent *events;//陣列
	
};

struct ntyreactor {
	//控制程式碼
	int epfd;
	//結點個數
	int blkcnt;
	struct eventblock *evblk; //fd --> 100w
};

初始化fd 上樹、下樹程式碼

//nty_event_set(event, sockfd, acceptor, reactor);
//初始化sockfd
void nty_event_set(struct ntyevent *ev, int fd, NCALLBACK callback, void *arg) {

	ev->fd = fd;
	ev->callback = callback;
	ev->events = 0;
	ev->arg = arg;
	ev->last_active = time(NULL);

	return ;
	
}
//nty_event_add(reactor->epfd, EPOLLIN, event);
//對監聽的epoll紅黑樹上的結點的修改
int nty_event_add(int epfd, int events, struct ntyevent *ev) {

	struct epoll_event ep_ev = {0, {0}};
	ep_ev.data.ptr = ev;
	ep_ev.events = ev->events = events;

	int op;
	if (ev->status == 1) {
		op = EPOLL_CTL_MOD;
	} else {
		op = EPOLL_CTL_ADD;
		ev->status = 1;
	}

	if (epoll_ctl(epfd, op, ev->fd, &ep_ev) < 0) {
		printf("event add failed [fd=%d], events[%d]n", ev->fd, events);
		return -1;
	}

	return 0;
}

int nty_event_del(int epfd, struct ntyevent *ev) {

	struct epoll_event ep_ev = {0, {0}};

	if (ev->status != 1) {
		return -1;
	}
	ep_ev.data.ptr = ev;
	ev->status = 0;
	epoll_ctl(epfd, EPOLL_CTL_DEL, ev->fd, &ep_ev);

	return 0;
}

回撥函數程式碼的書寫
注意看recv_cb的回撥函數中,recv之後,立馬下樹,然後又重新初始化fd,上樹。這樣做的目的是因為程式碼邏輯是recv收到資料後,立即原樣send,所以需要對fd的屬性進行更改,需要重新初始化賦值,然後上樹

int recv_cb(int fd, int events, void *arg) {

	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	struct ntyevent *ev = ntyreactor_idx(reactor, fd);

	int len = recv(fd, ev->buffer, BUFFER_LENGTH , 0); // 
	nty_event_del(reactor->epfd, ev);

	if (len > 0) {
		
		ev->length = len;
		ev->buffer[len] = '';

		printf("C[%d]:%sn", fd, ev->buffer);

		nty_event_set(ev, fd, send_cb, reactor);
		nty_event_add(reactor->epfd, EPOLLOUT, ev);
		
		
	} else if (len == 0) {

		close(ev->fd);
		//printf("[fd=%d] pos[%ld], closedn", fd, ev-reactor->events);
		 
	} else {

		close(ev->fd);
		printf("recv[fd=%d] error[%d]:%sn", fd, errno, strerror(errno));
		
	}

	return len;
}

int send_cb(int fd, int events, void *arg) {

	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	struct ntyevent *ev = ntyreactor_idx(reactor, fd);

	int len = send(fd, ev->buffer, ev->length, 0);
	if (len > 0) {
		printf("send[fd=%d], [%d]%sn", fd, len, ev->buffer);

		nty_event_del(reactor->epfd, ev);
		nty_event_set(ev, fd, recv_cb, reactor);
		nty_event_add(reactor->epfd, EPOLLIN, ev);
		
	} else {

		close(ev->fd);

		nty_event_del(reactor->epfd, ev);
		printf("send[fd=%d] error %sn", fd, strerror(errno));

	}

	return len;
}

int accept_cb(int fd, int events, void *arg) {

	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	if (reactor == NULL) return -1;

	struct sockaddr_in client_addr;
	socklen_t len = sizeof(client_addr);

	int clientfd;

	if ((clientfd = accept(fd, (struct sockaddr*)&client_addr, &len)) == -1) {
		if (errno != EAGAIN && errno != EINTR) {
			
		}
		printf("accept: %sn", strerror(errno));
		return -1;
	}
	int flag = 0;
	if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) {
		printf("%s: fcntl nonblocking failed, %dn", __func__, MAX_EPOLL_EVENTS);
		return -1;
	}
	/*儲存*/
	struct ntyevent *event = ntyreactor_idx(reactor, clientfd);
	
	nty_event_set(event, clientfd, recv_cb, reactor);
	nty_event_add(reactor->epfd, EPOLLIN, event);

	
	printf("new connect [%s:%d], pos[%d]n", 
		inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), clientfd);

	return 0;

}

連結串列的初始化與銷燬

//初始化連結串列
int ntyreactor_init(struct ntyreactor *reactor) {

	if (reactor == NULL) return -1;
	memset(reactor, 0, sizeof(struct ntyreactor));

	reactor->epfd = epoll_create(1);
	if (reactor->epfd <= 0) {
		printf("create epfd in %s err %sn", __func__, strerror(errno));
		return -2;
	}

	struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	if (evs == NULL) {
		printf("ntyreactor_alloc ntyevents failedn");
		return -2;
	}
	memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));

	struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
	if (block == NULL) {
		printf("ntyreactor_alloc eventblock failedn");
		return -2;
	}
	memset(block, 0, sizeof(struct eventblock));

	block->events = evs;
	block->next = NULL;

	reactor->evblk = block;
	reactor->blkcnt = 1;

	return 0;
}

找到fd應在連結串列陣列中儲存的位置並返回

//新增塊數(eventblock結點個數)
//ntyreactor_alloc(reactor);
int ntyreactor_alloc(struct ntyreactor *reactor) {

	if (reactor == NULL) return -1;
	if (reactor->evblk == NULL) return -1;

	struct eventblock *blk = reactor->evblk;
	while (blk->next != NULL) {
		blk = blk->next;
	}

	struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	if (evs == NULL) {
		printf("ntyreactor_alloc ntyevents failedn");
		return -2;
	}
	memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));

	struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
	if (block == NULL) {
		printf("ntyreactor_alloc eventblock failedn");
		return -2;
	}
	memset(block, 0, sizeof(struct eventblock));

	block->events = evs;
	block->next = NULL;

	blk->next = block;
	reactor->blkcnt ++; //

	return 0;
}

//struct ntyevent *event = ntyreactor_idx(reactor, sockfd);
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd) {

	int blkidx = sockfd / MAX_EPOLL_EVENTS;
	//如果塊數(eventblock結點個數)不能滿足新的sockfd的存放
	while (blkidx >= reactor->blkcnt) {
		//新增塊數(eventblock結點個數)
		ntyreactor_alloc(reactor);
	}

	//找到存放sockfd的塊(eventblock對應的結點)
	int i = 0;
	struct eventblock *blk = reactor->evblk;
	while(i ++ < blkidx && blk != NULL) {
		blk = blk->next;
	}

	//返回對應塊(eventblock對應的結點)的存放sockfd陣列的那個具體位置
	return &blk->events[sockfd % MAX_EPOLL_EVENTS];
}

上樹,並初始化連結串列陣列上對應的fd

//ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
//上樹,並初始化連結串列陣列上對應的fd
int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, NCALLBACK *acceptor) {

	if (reactor == NULL) return -1;
	if (reactor->evblk == NULL) return -1;

	//reactor->evblk->events[sockfd];
	//找到sock所在的具體位置
	struct ntyevent *event = ntyreactor_idx(reactor, sockfd);

	初始化sockfd
	nty_event_set(event, sockfd, acceptor, reactor);
	//對監聽的epoll紅黑樹上的結點的修改
	nty_event_add(reactor->epfd, EPOLLIN, event);

	return 0;
}

epollwait

//ntyreactor_run(reactor);
int ntyreactor_run(struct ntyreactor *reactor) {
	if (reactor == NULL) return -1;
	if (reactor->epfd < 0) return -1;
	if (reactor->evblk == NULL) return -1;
	
	struct epoll_event events[MAX_EPOLL_EVENTS+1];
	
	int checkpos = 0, i;

	while (1) {
/*
		long now = time(NULL);
		for (i = 0;i < 100;i ++, checkpos ++) {
			if (checkpos == MAX_EPOLL_EVENTS) {
				checkpos = 0;
			}

			if (reactor->events[checkpos].status != 1) {
				continue;
			}

			long duration = now - reactor->events[checkpos].last_active;

			if (duration >= 60) {
				close(reactor->events[checkpos].fd);
				printf("[fd=%d] timeoutn", reactor->events[checkpos].fd);
				nty_event_del(reactor->epfd, &reactor->events[checkpos]);
			}
		}
*/

		int nready = epoll_wait(reactor->epfd, events, MAX_EPOLL_EVENTS, 1000);
		if (nready < 0) {
			printf("epoll_wait error, exitn");
			continue;
		}

		for (i = 0;i < nready;i ++) {

			struct ntyevent *ev = (struct ntyevent*)events[i].data.ptr;

			//看fd連線是否發生變化
			if ((events[i].events & EPOLLIN) && (ev->events & EPOLLIN)) {
				ev->callback(ev->fd, events[i].events, ev->arg);
			}
			if ((events[i].events & EPOLLOUT) && (ev->events & EPOLLOUT)) {
				ev->callback(ev->fd, events[i].events, ev->arg);
			}
			
		}

	}
}

main函數;此伺服器程式碼開設了100個監聽的埠,目的是因為使用者端測試程式也是執行在虛擬機器器的Ubuntu上,通過開三臺來充當使用者端來進行測試。有因為一臺Ubuntu最多有6w個埠,3臺有18w埠。如果伺服器只開設一個監聽埠,則最多有18w埠。因此要達到100w並行則應多開設埠

// 3, 6w, 1, 100 == 
// <remoteip, remoteport, localip, localport>
int main(int argc, char *argv[]) {

	unsigned short port = SERVER_PORT; // listen 8888
	if (argc == 2) {
		port = atoi(argv[1]);//把引數 str 所指向的字串轉換為一個整數(型別為 int 型)
	}
	struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));
	/*初始化三個結構體,建立連結串列*/
	ntyreactor_init(reactor);

	int i = 0;
	int sockfds[PORT_COUNT] = {0};
	for (i = 0;i < PORT_COUNT;i ++) {
		//埠號的監聽
		sockfds[i] = init_sock(port+i);
		//上樹
		ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
	}

	// epoll_wait
	ntyreactor_run(reactor);
	//
	ntyreactor_destory(reactor);

	for (i = 0;i < PORT_COUNT;i ++) {
		close(sockfds[i]);
	}

	free(reactor);

	return 0;
}

完整伺服器程式碼展示

/*連結串列儲存陣列,把epoll變成對事件的管理,用連結串列陣列的目的就是為了回撥函數*/
/*recv寫法:程式碼邏輯是收到資料後,立即原樣返回所以才那樣寫*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define BUFFER_LENGTH		4096
#define MAX_EPOLL_EVENTS	1024
#define SERVER_PORT			8888
#define PORT_COUNT			100
typedef int NCALLBACK(int ,int, void*);
//struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
struct ntyevent {
	int fd;//要監聽的檔案描述符
	int events;//對應的監聽事件,	EPOLLIN和EPOLLOUT(不同的事件,走不同的回撥函數)
	void *arg;//指向自己結構體指標
	int (*callback)(int fd, int events, void *arg);
	int status;//是否在監聽:1->在紅黑樹上(監聽),0->不在(不監聽)
	char buffer[BUFFER_LENGTH];
	int length;
	long last_active;
};
struct eventblock {
	struct eventblock *next;
	struct ntyevent *events;//陣列
};
struct ntyreactor {
	//控制程式碼
	int epfd;
	//結點個數
	int blkcnt;
	struct eventblock *evblk; //fd --> 100w
};
int recv_cb(int fd, int events, void *arg);
int send_cb(int fd, int events, void *arg);
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd);
//nty_event_set(event, sockfd, acceptor, reactor);
//初始化sockfd
void nty_event_set(struct ntyevent *ev, int fd, NCALLBACK callback, void *arg) {
	ev->fd = fd;
	ev->callback = callback;
	ev->events = 0;
	ev->arg = arg;
	ev->last_active = time(NULL);
	return ;
}
//nty_event_add(reactor->epfd, EPOLLIN, event);
//對監聽的epoll紅黑樹上的結點的修改
int nty_event_add(int epfd, int events, struct ntyevent *ev) {
	struct epoll_event ep_ev = {0, {0}};
	ep_ev.data.ptr = ev;
	ep_ev.events = ev->events = events;
	int op;
	if (ev->status == 1) {
		op = EPOLL_CTL_MOD;
	} else {
		op = EPOLL_CTL_ADD;
		ev->status = 1;
	}
	if (epoll_ctl(epfd, op, ev->fd, &ep_ev) < 0) {
		printf("event add failed [fd=%d], events[%d]n", ev->fd, events);
		return -1;
	}
	return 0;
}
int nty_event_del(int epfd, struct ntyevent *ev) {
	struct epoll_event ep_ev = {0, {0}};
	if (ev->status != 1) {
		return -1;
	}
	ep_ev.data.ptr = ev;
	ev->status = 0;
	epoll_ctl(epfd, EPOLL_CTL_DEL, ev->fd, &ep_ev);
	return 0;
}
int recv_cb(int fd, int events, void *arg) {
	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	struct ntyevent *ev = ntyreactor_idx(reactor, fd);
	int len = recv(fd, ev->buffer, BUFFER_LENGTH , 0); // 
	nty_event_del(reactor->epfd, ev);
	if (len > 0) {
		ev->length = len;
		ev->buffer[len] = '';
		printf("C[%d]:%sn", fd, ev->buffer);
		nty_event_set(ev, fd, send_cb, reactor);
		nty_event_add(reactor->epfd, EPOLLOUT, ev);
	} else if (len == 0) {
		close(ev->fd);
		//printf("[fd=%d] pos[%ld], closedn", fd, ev-reactor->events);
	} else {
		close(ev->fd);
		printf("recv[fd=%d] error[%d]:%sn", fd, errno, strerror(errno));
	}
	return len;
}
int send_cb(int fd, int events, void *arg) {
	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	struct ntyevent *ev = ntyreactor_idx(reactor, fd);
	int len = send(fd, ev->buffer, ev->length, 0);
	if (len > 0) {
		printf("send[fd=%d], [%d]%sn", fd, len, ev->buffer);
		nty_event_del(reactor->epfd, ev);
		nty_event_set(ev, fd, recv_cb, reactor);
		nty_event_add(reactor->epfd, EPOLLIN, ev);
	} else {
		close(ev->fd);
		nty_event_del(reactor->epfd, ev);
		printf("send[fd=%d] error %sn", fd, strerror(errno));
	}
	return len;
}
int accept_cb(int fd, int events, void *arg) {
	struct ntyreactor *reactor = (struct ntyreactor*)arg;
	if (reactor == NULL) return -1;
	struct sockaddr_in client_addr;
	socklen_t len = sizeof(client_addr);
	int clientfd;
	if ((clientfd = accept(fd, (struct sockaddr*)&client_addr, &len)) == -1) {
		if (errno != EAGAIN && errno != EINTR) {
		}
		printf("accept: %sn", strerror(errno));
		return -1;
	}
	int flag = 0;
	if ((flag = fcntl(clientfd, F_SETFL, O_NONBLOCK)) < 0) {
		printf("%s: fcntl nonblocking failed, %dn", __func__, MAX_EPOLL_EVENTS);
		return -1;
	}
	/*儲存*/
	struct ntyevent *event = ntyreactor_idx(reactor, clientfd);
	nty_event_set(event, clientfd, recv_cb, reactor);
	nty_event_add(reactor->epfd, EPOLLIN, event);
	printf("new connect [%s:%d], pos[%d]n", 
		inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), clientfd);
	return 0;
}
int init_sock(short port) {
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	fcntl(fd, F_SETFL, O_NONBLOCK);
	struct sockaddr_in server_addr;
	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(port);
	bind(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
	if (listen(fd, 20) < 0) {
		printf("listen failed : %sn", strerror(errno));
	}
	return fd;
}
//新增塊數(eventblock結點個數)
//ntyreactor_alloc(reactor);
int ntyreactor_alloc(struct ntyreactor *reactor) {
	if (reactor == NULL) return -1;
	if (reactor->evblk == NULL) return -1;
	struct eventblock *blk = reactor->evblk;
	while (blk->next != NULL) {
		blk = blk->next;
	}
	struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	if (evs == NULL) {
		printf("ntyreactor_alloc ntyevents failedn");
		return -2;
	}
	memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
	if (block == NULL) {
		printf("ntyreactor_alloc eventblock failedn");
		return -2;
	}
	memset(block, 0, sizeof(struct eventblock));
	block->events = evs;
	block->next = NULL;
	blk->next = block;
	reactor->blkcnt ++; //
	return 0;
}
//struct ntyevent *event = ntyreactor_idx(reactor, sockfd);
struct ntyevent *ntyreactor_idx(struct ntyreactor *reactor, int sockfd) {
	int blkidx = sockfd / MAX_EPOLL_EVENTS;
	//如果塊數(eventblock結點個數)不能滿足新的sockfd的存放
	while (blkidx >= reactor->blkcnt) {
		//新增塊數(eventblock結點個數)
		ntyreactor_alloc(reactor);
	}
	//找到存放sockfd的塊(eventblock對應的結點)
	int i = 0;
	struct eventblock *blk = reactor->evblk;
	while(i ++ < blkidx && blk != NULL) {
		blk = blk->next;
	}
	//返回對應塊(eventblock對應的結點)的存放sockfd陣列的那個具體位置
	return &blk->events[sockfd % MAX_EPOLL_EVENTS];
}
//初始化連結串列
int ntyreactor_init(struct ntyreactor *reactor) {
	if (reactor == NULL) return -1;
	memset(reactor, 0, sizeof(struct ntyreactor));
	reactor->epfd = epoll_create(1);
	if (reactor->epfd <= 0) {
		printf("create epfd in %s err %sn", __func__, strerror(errno));
		return -2;
	}
	struct ntyevent *evs = (struct ntyevent*)malloc((MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	if (evs == NULL) {
		printf("ntyreactor_alloc ntyevents failedn");
		return -2;
	}
	memset(evs, 0, (MAX_EPOLL_EVENTS) * sizeof(struct ntyevent));
	struct eventblock *block = (struct eventblock *)malloc(sizeof(struct eventblock));
	if (block == NULL) {
		printf("ntyreactor_alloc eventblock failedn");
		return -2;
	}
	memset(block, 0, sizeof(struct eventblock));
	block->events = evs;
	block->next = NULL;
	reactor->evblk = block;
	reactor->blkcnt = 1;
	return 0;
}
int ntyreactor_destory(struct ntyreactor *reactor) {
	close(reactor->epfd);
	//free(reactor->events);
	struct eventblock *blk = reactor->evblk;
	struct eventblock *blk_next = NULL;
	while (blk != NULL) {
		blk_next = blk->next;
		free(blk->events);
		free(blk);
		blk = blk_next;
	}
	return 0;
}
//ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
//上樹,並初始化連結串列陣列上對應的fd
int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, NCALLBACK *acceptor) {
	if (reactor == NULL) return -1;
	if (reactor->evblk == NULL) return -1;
	//reactor->evblk->events[sockfd];
	//找到sock所在的具體位置
	struct ntyevent *event = ntyreactor_idx(reactor, sockfd);
	初始化sockfd
	nty_event_set(event, sockfd, acceptor, reactor);
	//對監聽的epoll紅黑樹上的結點的修改
	nty_event_add(reactor->epfd, EPOLLIN, event);
	return 0;
}
//ntyreactor_run(reactor);
int ntyreactor_run(struct ntyreactor *reactor) {
	if (reactor == NULL) return -1;
	if (reactor->epfd < 0) return -1;
	if (reactor->evblk == NULL) return -1;
	struct epoll_event events[MAX_EPOLL_EVENTS+1];
	int checkpos = 0, i;
	while (1) {
/*
		long now = time(NULL);
		for (i = 0;i < 100;i ++, checkpos ++) {
			if (checkpos == MAX_EPOLL_EVENTS) {
				checkpos = 0;
			}
			if (reactor->events[checkpos].status != 1) {
				continue;
			}
			long duration = now - reactor->events[checkpos].last_active;
			if (duration >= 60) {
				close(reactor->events[checkpos].fd);
				printf("[fd=%d] timeoutn", reactor->events[checkpos].fd);
				nty_event_del(reactor->epfd, &reactor->events[checkpos]);
			}
		}
*/
		int nready = epoll_wait(reactor->epfd, events, MAX_EPOLL_EVENTS, 1000);
		if (nready < 0) {
			printf("epoll_wait error, exitn");
			continue;
		}
		for (i = 0;i < nready;i ++) {
			struct ntyevent *ev = (struct ntyevent*)events[i].data.ptr;
			//看fd連線是否發生變化
			if ((events[i].events & EPOLLIN) && (ev->events & EPOLLIN)) {
				ev->callback(ev->fd, events[i].events, ev->arg);
			}
			if ((events[i].events & EPOLLOUT) && (ev->events & EPOLLOUT)) {
				ev->callback(ev->fd, events[i].events, ev->arg);
			}
		}
	}
}
// 3, 6w, 1, 100 == 
// <remoteip, remoteport, localip, localport>
int main(int argc, char *argv[]) {
	unsigned short port = SERVER_PORT; // listen 8888
	if (argc == 2) {
		port = atoi(argv[1]);//把引數 str 所指向的字串轉換為一個整數(型別為 int 型)
	}
	struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));
	/*初始化三個結構體,建立連結串列*/
	ntyreactor_init(reactor);
	int i = 0;
	int sockfds[PORT_COUNT] = {0};
	for (i = 0;i < PORT_COUNT;i ++) {
		//埠號的監聽
		sockfds[i] = init_sock(port+i);
		//上樹
		ntyreactor_addlistener(reactor, sockfds[i], accept_cb);
	}
	// epoll_wait
	ntyreactor_run(reactor);
	//
	ntyreactor_destory(reactor);
	for (i = 0;i < PORT_COUNT;i ++) {
		close(sockfds[i]);
	}
	free(reactor);
	return 0;
}

reactor的寫法感覺和epoll的普通寫法,感覺差別就是reactor多了個回撥函數,具體沒啥優點?
epoll是針對io的管理。 reactor對針對事件的管理
不同的事件,針對不同的回撥函數
效能上沒啥差異,但提高了程式碼的複用性。具體需要自己慢慢體會體會,嗚嗚嗚嗚還有體會到,程式設計思想不過關。

二、環境設定

限制是fd的限制,系統預設fd最多有1024個,按照一個連線一個fd的做法,那就需要百萬個fd。這裡有兩種修改方法,一是使用ulimit -n命令,這個命令重啟就失效;二是修改/etc/security/limits.conf檔案,這是永久有效的,重啟或sysctl -p生效。

* hard nofile 1048576
* soft nofile 1048576

hard是硬限制,不能超過該值,soft是軟限制,可以超過,超過後就開始回收。
這個檔案裡還有一些其他的引數可以瞭解一下,fs.file_max是fd可取到的最大值,注意與fd最大個數區分。
突破這兩個限制後,還會遇到一個問題,使用者端會報錯:connection timedout。連線超時,即是使用者端未收到伺服器對使用者端connect()的迴應包。這裡有兩種可能,使用者端為收到伺服器的包或是伺服器未收到使用者端的connect包。事實上,是因為系統有個防火牆iotables,這個防火牆是基於網路卡和協定棧之間的過濾機制netfilter實現的。netfilter當連線數到達一定程度時,會不允許再向外傳送connect包。修改也是通過/etc/security/limits.conf檔案

net.nf_conntrack_max=1048576

突破這些限制,就可以實現百萬並行了。
這裡再介紹/etc/security/limits.conf中幾個引數
net.ipv4.tcp_mem=262144 524288 786432是所有TCP協定棧所佔空間的大小,單位是頁(4KB)。介紹一下後面寫的三個值,當所佔空間大小超過第二個值時,系統會進行優化,此時如果佔用空間降到第一個值以下,不再優化,第三個值是上限,不允許分配超過比大小的空間。
net.ipv4.tcp_wmem=2048 2048 4096是每個socket對應的寫緩衝區大小,三個值分別是最小值、預設值、最大值,單位是B。
net.ipv4.tcp_rmem=2048 2048 4096是每個socket對應的讀緩衝區大小,三個值分別是最小值、預設值、最大值,單位是B。
做百萬並行時,如果記憶體不大,可以相應調小。在實際應用中,如果傳輸大檔案,調大;如果傳輸的都是字元,調小,就可以接收更多fd。

到此這篇關於C++基於reactor的伺服器百萬並行實現的文章就介紹到這了,更多相關reactor伺服器百萬並行內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com