1、Reactor模式的定义
Reactor反应堆模式,也叫做分发者模式,也叫做通知者模式。它是一种设计模式,将就绪事件派发给对应服务器处理程序。按事件分类处理。
2、Reactor模式中的主要角色
在Reactor模式当中有几个关键的参与者,下面我们来看看他们分别是什么?
- Handle(句柄):由操作系统提供用来标识每个事件如Socket描述符、文件描述符等通常是用整数来标识。
- EventHandler(事件处理器):由一个或者多个回调函数组成,这些回调方法用来处理对应的事件
- Dispatcher(派发器):当事件就绪时派发器分配任务给对应的事件处理器
- ConcreteEvent:事件处理器中的各个回调方法的具体实现
- SychronousEvent Demultiplexer(同步事件分离器):本质是系统调用,在Linux当中是指多路IO复用比如说:select、poll、epoll等。
3、代码实现
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
using namespace std;
#define BUFFER_LEN 1024
#define CLIENT_MAX_COUNT 2048
typedef int (*EPOLL_CALLBACK)(int fd);
struct CONNECT_ITEM {
char rBuffer[BUFFER_LEN] = {0};
int rLen = 0;
char wBuffer[BUFFER_LEN] = {0};
int wLen = 0;
union {
EPOLL_CALLBACK accept_callback = nullptr;
EPOLL_CALLBACK recv_callback;
}recv_t;
EPOLL_CALLBACK send_callback = nullptr;
}connect_item[CLIENT_MAX_COUNT];
int epfd = 0;
enum _EPOLL_CTRL{
ADD,
MOD
};
void setEvent(int fd, EPOLL_EVENTS events, _EPOLL_CTRL ctrl) {
epoll_event ev;
ev.events = events; //默认水平触发(LT),有(数据)事件就会一直触发,知道全部处理完
/*
EPOLLET为边沿触发(ET),当有事件发生时只触发一次,
比如来数据了,如果一次没有读完,不会再触发了,所以必须全部读完,在进行下一次epoll_wait
*/
//ev.events = EPOLLIN | EPOLLET;
ev.data.fd = fd;
epoll_ctl(epfd, ctrl == ADD ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, fd, &ev);
}
int recv_cb(int fd) {
char* buffer = connect_item[fd].rBuffer;
int index = connect_item[fd].rLen;
int count = recv(fd, buffer + index, BUFFER_LEN - index, 0);
if (count == 0) {
printf("disconnect: %d\n", fd);
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
return count;
}else if (count < 0) {
printf("error\n");
return count;
}
connect_item[fd].rLen += count; //这里的rLen可能会超出buffer的大小,这里就不做处理了
printf("RECV===>>> clientfd: %d, count: %d, buffer: %s\n", fd, count, buffer);
//改变该文件描述符的事件类型为EPOLLOUT
setEvent(fd, EPOLLOUT, MOD);
//发送buffer赋值
memcpy(connect_item[fd].wBuffer, connect_item[fd].rBuffer, connect_item[fd].rLen);
connect_item[fd].wLen = connect_item[fd].rLen;
return count;
}
int send_cb(int fd) {
char* buffer = connect_item[fd].wBuffer;
int index = connect_item[fd].wLen;
int count = send(fd, buffer, connect_item[fd].wLen, 0);
//改变该文件描述符的事件类型为EPOLLIN
setEvent(fd, EPOLLIN, MOD);
return count;
}
int accept_cb(int fd) {
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);
setEvent(clientfd, EPOLLIN, ADD);
memset(connect_item[clientfd].rBuffer, 0, sizeof(connect_item[clientfd].rBuffer));
connect_item[clientfd].rLen = 0;
memset(connect_item[clientfd].wBuffer, 0, sizeof(connect_item[clientfd].wBuffer));
connect_item[clientfd].wLen = 0;
connect_item[clientfd].recv_t.recv_callback = recv_cb;
connect_item[clientfd].send_callback = send_cb;
printf("ACCEPT===>>> clientfd:%d\n", clientfd);
return clientfd;
}
int main() {
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(2048);
bind(listenfd, (sockaddr*)&serverAddr, sizeof(serverAddr));
listen(listenfd, 10);
epfd = epoll_create(1); // int size
connect_item[listenfd].recv_t.accept_callback = accept_cb;
setEvent(listenfd, EPOLLIN, ADD);
struct epoll_event events[1024] = {0};
while (1) {
int nready = epoll_wait(epfd, events, 1024, -1);
for (int i = 0; i < nready; i++) {
int connfd = events[i].data.fd;
if (events[i].events & EPOLLIN) {
int count = connect_item[connfd].recv_t.recv_callback(connfd);
}else if (events[i].events & EPOLLOUT) {
connect_item[connfd].send_callback(connfd);
}
}
}
close(listenfd);
return 0;
}