Http第三方接口实现/异步Http请求

1. 同步与异步的区别?

阻塞非阻塞 

 

2. 如何设计?

   思想:  客户端发送n多请求给服务器,然后客户端自己起一个线程,去轮询服务器发送回来的应答,好像这个客户端的线程起了一个服务一样,等待服务器的应答消息,客户端使用了epoll来管理这些IO。

3.部分代码  --  使用epoll来管理IO

struct async_context *http_async_client_init(void) {

	int epfd = epoll_create(1); // 
	if (epfd < 0) return NULL;

	struct async_context *ctx = calloc(1, sizeof(struct async_context));
	if (ctx == NULL) {
		close(epfd);
		return NULL;
	}
	ctx->epfd = epfd;

	int ret = pthread_create(&ctx->thread_id, NULL, http_async_client_callback, ctx);
	if (ret) {
		perror("pthread_create");
		return NULL;
	}
	usleep(1); 

	return ctx;

}

 

static void *http_async_client_callback(void *arg) {

	struct async_context *ctx = (struct async_context*)arg;
	int epfd = ctx->epfd;

	while (1) {

		struct epoll_event events[ASYNC_CLIENT_NUM] = {0};

		int nready = epoll_wait(epfd, events, ASYNC_CLIENT_NUM, -1);
		if (nready < 0) {
			if (errno == EINTR || errno == EAGAIN) {
				continue;
			} else {
				break;
			}
		} else if (nready == 0) {
			continue;
		}

		printf("nready:%d\n", nready);
		int i = 0;
		for (i = 0;i < nready;i ++) {

			struct ep_arg *data = (struct ep_arg*)events[i].data.ptr;
			int sockfd = data->sockfd;
			
			char buffer[BUFFER_SIZE] = {0};
			struct sockaddr_in addr;
			size_t addr_len = sizeof(struct sockaddr_in);
			int n = recv(sockfd, buffer, BUFFER_SIZE, 0);

			data->cb(data->hostname, buffer); //call cb
			
			int ret = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
			//printf("epoll_ctl DEL --> sockfd:%d\n", sockfd);

			close(sockfd); /////

			free(data);

		}
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值