IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Reactor模式的实际使用 -> 正文阅读

[网络协议]Reactor模式的实际使用

文章目录


前言

前面两篇文章讲述了reactor高并发模型,以及如何实现百万并发,这篇文章我们会将Reactor百万并发的模型上实现http服务器以及websocket服务器。


其实说到底,就是如何在recv_cb中读协议的数据和send_cb中如何组协议的数据

废话不多说,直接上代码

1、对于http协议

//   GET / HTTP/1.1
//	Host: 192.168.31.131:8888
//	User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0
//	Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
//	Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
//	Accept-Encoding: gzip, deflate
//	Connection: keep-alive
//	Upgrade-Insecure-Requests: 1

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) {

	//GET POST
	char linebuf[1024] = {0};
	int idx = readline(ev->buffer, 0, linebuf);

	if(strstr(linebuf, "GET")) {
		ev->method = HTTP_METHOD_GET;

		//uri
		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) {
		//return 404

		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);
		//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;
}

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);

		}

		// if (len == ev->length) close(fd);
		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; //6 payload length < 126   sizeof(ws_ophdr) + sizeof(mask_key)
		if(hdr->mask) {  //mask set 1
			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上实现对协议的解读和组装来实现。所以没什么困难的。

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-12-26 22:36:04  更:2021-12-26 22:36:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/6 10:54:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码