#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/epoll.h>
#include <pthread.h>
#define MAXLNE 4096
#define POLL_SIZE 1024
#define BUFFER_LENGTH 1024
#define MAX_EPOLL_EVENT 1024
typedef int (*NCALLBACK)(int fd, int event, void*arg);
#define READ_CB 0
#define WRITE_CB 1
#define ACCEPT_CB 2
#define NOSET_CB 3
int init_reactor(struct reactor *r);
int nreactor_set_event(int fd, NCALLBACK cb, int event, void* arg);
int read_callback(int fd, int event, void* arg);
int write_callback(int fd, int event, void* arg);
int accept_callback(int fd, int event, void* arg);
//单例模式(线程不安全)
struct reactor* instance = NULL;
//每一个io对应一个item
struct nitem {//每一个fd对应一个item
int fd;
int status;
void* arg;
int events;//如果只写一个回调函数,可以根据具体的events调用不同的回调函数
//当前的回调函数,当前这个fd需要执行的事件,
//epoll返回的时候可能出现即可读又可写,可以写一个或多个回调函数,发送把发送
//内容放到发送缓冲区中,接受把接收内容放接收缓冲区中
#if 0
int (*callback)(int fd, int event, void* arg);
#else
//int (*readcb)(int fd, int event, void* arg);//接收
//int (*writecb)(int fd,int event, void* arg);//发送
//int (*acceptcb)(int fd,int event, void* arg);//连接
NCALLBACK readcb;//EPOLLIN
NCALLBACK writecb;//EPOLLOUT
NCALLBACK acceptcb;//EPOLLIN
#endif
//buffer满了不会阻塞,一般对于char设为unsigned因为存储的是字符
unsigned char sbuffer[BUFFER_LENGTH];
int slength;//发送长度
unsigned char rbuffer[BUFFER_LENGTH];
int rlength;//接收长度
};
//存放epoll,listenfd是对外提供的,只有accept处理listenfd
struct reactor {
int epfd;//管理所有的io
//struct nitem* items//所有的item, 这些item用红黑树或链表来存储
struct itemblock*
Reactor网络框架笔记
于 2022-03-14 11:35:28 首次发布
本文详细探讨了Reactor网络框架的原理与应用,涵盖了它如何处理并发事件、响应式编程模型及其在现代高并发系统中的核心优势。通过实例解析,深入理解Reactor在网络编程中的关键角色。
摘要由CSDN通过智能技术生成