reactor 反應堆模型

2021-10-06 04:12:26 字數 3463 閱讀 2966

1. reactor模型,本質上講管理網路io

使用下面這個結構用來管理我們的io

struct ntyreactor ;
下面這段**比較核心(我們關心的事件與發生的事件發生時,才去呼叫)

2. **實現:

#include #include #include #include #include #include #include #include #include #define buffer_length        4096

#define max_epoll_events    1024

#define server_port            8888

typedef int ncallback(int ,int, void*);

struct ntyevent ;

struct ntyreactor ;

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

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

void nty_event_set(struct ntyevent *ev, int fd, ncallback callback, void *arg)

//nty_event_add(reactor->epfd, epollin, &reactor->events[clientfd]);

int nty_event_add(int epfd, int events, struct ntyevent *ev) };

ep_ev.data.ptr = ev;

ep_ev.events = ev->events = events;

int op;

if (ev->status == 1) else

if (epoll_ctl(epfd, op, ev->fd, &ep_ev) < 0)

return 0;

}int nty_event_del(int epfd, struct ntyevent *ev) };

if (ev->status != 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) else if (len == 0) else

return len;

}int send_cb(int fd, int events, void *arg) else

return len;

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

printf("accept: %s\n", strerror(errno));

return -1;

}int i = 0;

do }

if (i == max_epoll_events)

int flag = 0;

if ((flag = fcntl(clientfd, f_setfl, o_nonblock)) < 0)

nty_event_set(&reactor->events[clientfd], clientfd, recv_cb, reactor);//設定註冊函式

nty_event_add(reactor->epfd, epollin, &reactor->events[clientfd]);

} while (0);

printf("new connect [%s:%d][time:%ld], pos[%d]\n", 

inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), reactor->events[i].last_active, i);

return 0;

}int init_sock(short port)

return fd;

}int ntyreactor_init(struct ntyreactor *reactor)

reactor->events = (struct ntyevent*)malloc((max_epoll_events) * sizeof(struct ntyevent));

if (reactor->events == null)

}int ntyreactor_destory(struct ntyreactor *reactor)

int ntyreactor_addlistener(struct ntyreactor *reactor, int sockfd, ncallback *acceptor)

int ntyreactor_run(struct ntyreactor *reactor)

if (reactor->events[checkpos].status != 1)

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

if (duration >= 60)

}int nready = epoll_wait(reactor->epfd, events, max_epoll_events, 1000);

if (nready < 0)

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

if ((events[i].events & epollout) && (ev->events & epollout)) }}

}int main(int argc, char *ar**)

int sockfd = init_sock(port);

struct ntyreactor *reactor = (struct ntyreactor*)malloc(sizeof(struct ntyreactor));

ntyreactor_init(reactor);//初始化reactor

ntyreactor_addlistener(reactor, sockfd, accept_cb);//新增監聽事件

ntyreactor_run(reactor);

ntyreactor_destory(reactor);

close(sockfd);

return 0;

}

epoll反應堆模型

參考 參考 求職期間,還是得好好學習。看了大概的思路,自己理解著敲一下。和原來 的有些地方不同。include include include include include include include include include define max events 1024 監聽上限 de...

epoll反應堆模型實現

在高併發tcp請求中,為了實現資源的節省,效率的提公升,epoll逐漸替代了之前的select和poll,它在使用者層上規避了忙輪詢這種效率不高的監聽方式,epoll的時間複雜度為o 1 也就意味著,epoll在高併發場景,隨著檔案描述符的增長,有良好的可擴充套件性。關鍵函式有三個 核心資料結構 t...

網路程式設計 反應堆reactor框架設計

tcp 高效能網路框架需要滿足的需求有以下三點 1 採用 reactor 模型,可以靈活使用 poll epoll 作為事件分發實現。2 必須支援多執行緒,從而可以支援單執行緒單 reactor 模式,也可以支援多執行緒主 從 reactor 模式。可以將套接字上的 i o 事件分離到多個執行緒上。...