前言
前面两篇文章讲述了reactor高并发模型,以及如何实现百万并发,这篇文章我们会将Reactor百万并发的模型上实现http服务器以及websocket服务器。
其实说到底,就是如何在recv_cb中读协议的数据和send_cb中如何组协议的数据
废话不多说,直接上代码
1、对于http协议
int readline(char *allbuf, int idx, char *linebuf)
{
int len = strlen(allbuf);
for(; idx < len; idx ++)
{
if(allbuf[idx] == '\r' && allbuf[idx+1] == '\n') {
return idx+2;
}else {
*(linebuf++) = allbuf[idx];
}
}
return -1;
}
int http_request(struct ntyevent *ev) {
char linebuf[1024] = {0};
int idx = readline(ev->buffer, 0, linebuf);
if(strstr(linebuf, "GET")) {
ev->method = HTTP_METHOD_GET;
int i = 0;
while (linebuf[sizeof("GET ") + i] != ' ')
{
i++;
}
linebuf[sizeof("GET ") + i] = '\0';
sprintf(ev->resource, "./%s/%s", HTTP_WEBSERVER_HTML_ROOT, linebuf+sizeof("GET "));
} else if(strstr(linebuf, "POST")) {
}
}
int http_response(struct ntyevent *ev) {
if (ev == NULL) return -1;
memset(ev->buffer, 0, BUFFER_LENGTH);
int filefd = open(ev->resource, O_RDONLY);
if(filefd == -1) {
ev->ret_code = 404;
ev->length = sprintf(ev->buffer,
"HTTP/1.1 404 Not Found\r\n\
Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n\
Content-Type: text/html;charset=ISO-8859-1\r\n\
Content-Length: %d\r\n\r\n<html><head><title>404 Not Found</title></head><body><H1>404</H1></body></html>\r\n\r\n",
83);
} else {
ev->ret_code = 200;
struct stat stat_buf;
fstat(filefd, &stat_buf);
close(filefd);
if(S_ISDIR(stat_buf.st_mode)) {
ev->ret_code = 404;
ev->length = sprintf(ev->buffer,
"HTTP/1.1 404 Not Found\r\n\
Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n\
Content-Type: text/html;charset=ISO-8859-1\r\n\
Content-Length: %d\r\n\r\n<html><head><title>404 Not Found</title></head><body><H1>404</H1></body></html>\r\n\r\n",
83);
}else if (S_ISREG(stat_buf.st_mode)) {
ev->length = sprintf(ev->buffer,
"HTTP/1.1 200 OK\r\n\
Date: Thu, 11 Nov 2021 12:28:52 GMT\r\n\
Content-Type: text/html;charset=ISO-8859-1\r\n\
Content-Length: %ld\r\n\r\n",
stat_buf.st_size);
}
}
return ev->length;
}
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);
if (len > 0) {
ev->length = len;
ev->buffer[len] = '\0';
printf("C[%d]:%s\n", fd, ev->buffer);
http_request(ev);
nty_event_del(reactor->epfd, ev);
nty_event_set(ev, fd, send_cb, reactor);
nty_event_add(reactor->epfd, EPOLLOUT, ev);
} else if (len == 0) {
nty_event_del(reactor->epfd, ev);
close(ev->fd);
} else {
nty_event_del(reactor->epfd, ev);
close(ev->fd);
printf("recv[fd=%d] error[%d]:%s\n", 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);
http_response(ev);
int len = send(fd, ev->buffer, ev->length, 0);
if (len > 0) {
printf("send[fd=%d], [%d]%s\n", fd, len, ev->buffer);
if(ev->ret_code == 200) {
int filefd = open(ev->resource, O_RDONLY);
struct stat stat_buf;
fstat(filefd, &stat_buf);
sendfile(fd, filefd, NULL, stat_buf.st_size);
close(filefd);
}
nty_event_del(reactor->epfd, ev);
nty_event_set(ev, fd, recv_cb, reactor);
nty_event_add(reactor->epfd, EPOLLOUT, ev);
} else {
close(ev->fd);
nty_event_del(reactor->epfd, ev);
printf("send[fd=%d] error %s\n", fd, strerror(errno));
}
return len;
}
2、对于websocket服务器
int transmission(struct ntyevent *ev) {
ws_ophdr *hdr = (ws_ophdr *) ev->buffer;
if(hdr->pl_len < 126){
printf("hdr->pl_len %d \n", hdr->pl_len);
unsigned char *payload = ev->buffer + 6;
if(hdr->mask) {
umask(payload, hdr->pl_len, ev->buffer + 2);
}
printf("payload : %s \n", payload);
}else if(hdr->pl_len == 126) {
ws_head_126 *hdr126 = ev->buffer + sizeof(ws_ophdr);
}else {
ws_head_127 *hdr127 = ev->buffer + sizeof(ws_ophdr);
}
}
int websocket_request(struct ntyevent *ev) {
if(ev->status_machine == WS_HANDSHAKE) {
handshake(ev);
ev->status_machine = WS_TRANSMISSION;
} else if(ev->status_machine == WS_TRANSMISSION) {
transmission(ev);
} else {
}
}
int recv_cb(int fd, int events, void *arg) {
struct ntyreactor *reactor = (struct ntyreactor*)arg;
struct ntyevent *ev = reactor->events+fd;
int len = recv(fd, ev->buffer, BUFFER_LENGTH, 0);
if (len > 0) {
ev->length = len;
ev->buffer[len] = '\0';
printf("C[%d]:%s\n", fd, ev->buffer);
websocket_request(ev);
nty_event_del(reactor->epfd, ev);
nty_event_set(ev, fd, send_cb, reactor);
nty_event_add(reactor->epfd, EPOLLOUT, ev);
} else if (len == 0) {
nty_event_del(reactor->epfd, ev);
close(ev->fd);
printf("[fd=%d] pos[%ld], closed\n", fd, ev-reactor->events);
} else {
nty_event_del(reactor->epfd, ev);
close(ev->fd);
printf("recv[fd=%d] error[%d]:%s\n", fd, errno, strerror(errno));
}
return len;
}
上面的代码只是简单非完整的写了支持http和websocket的服务端代码,旨在介绍如何在Reactor模型下开发具体的业务。无论你要实现什么协议,都可以在recv_cb和send_cb上实现对协议的解读和组装来实现。所以没什么困难的。
|