异步请求池实现

问题

解决 请求与接收异步问题

1.发送请求, 1个io还是多个io=》多个io
2。 接收结果的线程,如何拿到fd
fd通过epoll_ctl添加到epoll中
四元组
1.commit 发送请求
2.thread_callback 单独接收结果(可以理解为epoll_wait的作用)
3.init 创建
4.destroy 销毁

1.init

a epoll_create
b pthread_create

2.commit

a.socket	//准备socket
b.connect server	//连接服务器
c.encode-->readis/mysql/dns 准备协议
d.send //发送数据到服务器
f.fd -->epoll //将fd加入到epoll中-》epoll_ctl

3.callback

while(1){
	epoll_wait();//判读有无可读
		recv();	// 有的话,读取数据
			parser();//解析数据
		fd-->epoll delete;//解析完了以后删除fd
}

4.destory

close(epfd);
pthread_cancel(thid);

代码




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <errno.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <sys/epoll.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <pthread.h>


#define DNS_SVR				"114.114.114.114"


#define DNS_HOST			0x01
#define DNS_CNAME			0x05



struct dns_header {
	unsigned short id;
	unsigned short flags;
	unsigned short qdcount;
	unsigned short ancount;
	unsigned short nscount;
	unsigned short arcount;
};

struct dns_question {
	int length;
	unsigned short qtype;
	unsigned short qclass;
	char *qname;
};

struct dns_item {
	char *domain;
	char *ip;
};

int dns_create_header(struct dns_header *header) {

	if (header == NULL) return -1;
	memset(header, 0, sizeof(struct dns_header));

	srandom(time(NULL));

	header->id = random();
	header->flags |= htons(0x0100);
	header->qdcount = htons(1);

	return 0;
}

int dns_create_question(struct dns_question *question, const char *hostname) {

	if (question == NULL) return -1;
	memset(question, 0, sizeof(struct dns_question));

	question->qname = (char*)malloc(strlen(hostname) + 2);
	if (question->qname == NULL) return -2;

	question->length = strlen(hostname) + 2;

	question->qtype = htons(1);
	question->qclass = htons(1);

	const char delim[2] = ".";

	char *hostname_dup = strdup(hostname);
	char *token = strtok(hostname_dup, delim);

	char *qname_p = question->qname;

	while (token != NULL) {

		size_t len = strlen(token);

		*qname_p = len;
		qname_p ++;

		strncpy(qname_p, token, len+1);
		qname_p += len;

		token = strtok(NULL, delim);
	}

	free(hostname_dup);

	return 0;
	
}

int dns_build_request(struct dns_header *header, struct dns_question *question, char *request) {

	int header_s = sizeof(struct dns_header);
	int question_s = question->length + sizeof(question->qtype) + sizeof(question->qclass);

	int length = question_s + header_s;

	int offset = 0;
	memcpy(request+offset, header, sizeof(struct dns_header));
	offset += sizeof(struct dns_header);

	memcpy(request+offset, question->qname, question->length);
	offset += question->length;

	memcpy(request+offset, &question->qtype, sizeof(question->qtype));
	offset += sizeof(question->qtype);

	memcpy(request+offset, &question->qclass, sizeof(question->qclass));

	return length;
	
}

static int is_pointer(int in) {
	return ((in & 0xC0) == 0xC0);
}


static void dns_parse_name(unsigned char *chunk, unsigned char *ptr, char *out, int *len) {

	int flag = 0, n = 0, alen = 0;
	char *pos = out + (*len);

	while (1) {

		flag = (int)ptr[0];
		if (flag == 0) break;

		if (is_pointer(flag)) {
			
			n = (int)ptr[1];
			ptr = chunk + n;
			dns_parse_name(chunk, ptr, out, len);
			break;
			
		} else {

			ptr ++;
			memcpy(pos, ptr, flag);
			pos += flag;
			ptr += flag;

			*len += flag;
			if ((int)ptr[0] != 0) {
				memcpy(pos, ".", 1);
				pos += 1;
				(*len) += 1;
			}
		}
	
	}
	
}


static int dns_parse_response(char *buffer, struct dns_item **domains) {

	int i = 0;
	unsigned char *ptr = buffer;

	ptr += 4;
	int querys = ntohs(*(unsigned short*)ptr);

	ptr += 2;
	int answers = ntohs(*(unsigned short*)ptr);

	ptr += 6;
	for (i = 0;i < querys;i ++) {
		while (1) {
			int flag = (int)ptr[0];
			ptr += (flag + 1);

			if (flag == 0) break;
		}
		ptr += 4;
	}

	char cname[128], aname[128], ip[20], netip[4];
	int len, type, ttl, datalen;

	int cnt = 0;
	struct dns_item *list = (struct dns_item*)calloc(answers, sizeof(struct dns_item));
	if (list == NULL) {
		return -1;
	}

	for (i = 0;i < answers;i ++) {
		
		bzero(aname, sizeof(aname));
		len = 0;

		dns_parse_name(buffer, ptr, aname, &len);
		ptr += 2;

		type = htons(*(unsigned short*)ptr);
		ptr += 4;

		ttl = htons(*(unsigned short*)ptr);
		ptr += 4;

		datalen = ntohs(*(unsigned short*)ptr);
		ptr += 2;

		if (type == DNS_CNAME) {

			bzero(cname, sizeof(cname));
			len = 0;
			dns_parse_name(buffer, ptr, cname, &len);
			ptr += datalen;
			
		} else if (type == DNS_HOST) {

			bzero(ip, sizeof(ip));

			if (datalen == 4) {
				memcpy(netip, ptr, datalen);
				inet_ntop(AF_INET , netip , ip , sizeof(struct sockaddr));

				printf("%s has address %s\n" , aname, ip);
				printf("\tTime to live: %d minutes , %d seconds\n", ttl / 60, ttl % 60);

				list[cnt].domain = (char *)calloc(strlen(aname) + 1, 1);
				memcpy(list[cnt].domain, aname, strlen(aname));
				
				list[cnt].ip = (char *)calloc(strlen(ip) + 1, 1);
				memcpy(list[cnt].ip, ip, strlen(ip));
				
				cnt ++;
			}
			
			ptr += datalen;
		}
	}

	*domains = list;
	ptr += 2;

	return cnt;
	
}


int dns_client_commit(const char *domain) {

	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		perror("create socket failed\n");
		exit(-1);
	}

	printf("url:%s\n", domain);

	struct sockaddr_in dest;
	bzero(&dest, sizeof(dest));
	dest.sin_family = AF_INET;
	dest.sin_port = htons(53);
	dest.sin_addr.s_addr = inet_addr(DNS_SVR);
	
	int ret = connect(sockfd, (struct sockaddr*)&dest, sizeof(dest));
	printf("connect :%d\n", ret);

	struct dns_header header = {0};
	dns_create_header(&header);

	struct dns_question question = {0};
	dns_create_question(&question, domain);

	char request[1024] = {0};
	int req_len = dns_build_request(&header, &question, request);
	int slen = sendto(sockfd, request, req_len, 0, (struct sockaddr*)&dest, sizeof(struct sockaddr));

	char buffer[1024] = {0};
	struct sockaddr_in addr;
	size_t addr_len = sizeof(struct sockaddr_in);
		
	int n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, (socklen_t*)&addr_len);
		
	printf("recvfrom n : %d\n", n);
	struct dns_item *domains = NULL;
	dns_parse_response(buffer, &domains);

	return 0;
}

char *domain[] = {
//	"www.ntytcp.com",
	"bojing.wang",
	"www.baidu.com",
	"tieba.baidu.com",
	"news.baidu.com",
	"zhidao.baidu.com",
	"music.baidu.com",
	"image.baidu.com",
	"v.baidu.com",
	"map.baidu.com",
	"baijiahao.baidu.com",
	"xueshu.baidu.com",
	"cloud.baidu.com",
	"www.163.com",
	"open.163.com",
	"auto.163.com",
	"gov.163.com",
	"money.163.com",
	"sports.163.com",
	"tech.163.com",
	"edu.163.com",
	"www.taobao.com",
	"q.taobao.com",
	"sf.taobao.com",
	"yun.taobao.com",
	"baoxian.taobao.com",
	"www.tmall.com",
	"suning.tmall.com",
	"www.tencent.com",
	"www.qq.com",
	"www.aliyun.com",
	"www.ctrip.com",
	"hotels.ctrip.com",
	"hotels.ctrip.com",
	"vacations.ctrip.com",
	"flights.ctrip.com",
	"trains.ctrip.com",
	"bus.ctrip.com",
	"car.ctrip.com",
	"piao.ctrip.com",
	"tuan.ctrip.com",
	"you.ctrip.com",
	"g.ctrip.com",
	"lipin.ctrip.com",
	"ct.ctrip.com"
};

struct async_context{
	
	int epfd;
	pthread_t thid;

};

typedef void (*async_result_cb)(void *arg,int count);
#define ASYNC_CLIENT_NUM	1024
struct epoll_arg
{
	async_result_cb cb;
	int fd;
};

void dns_async_free_domain(struct dns_item *domains,int count){
	for(int i=0;i<count;i++){
		free(domains[i].domain);
		free(domains[i].ip);
	}
	free(domains);
}

void *dns_async_callback(void *arg){

	struct async_context *ctx = (struct async_context*)arg;
	while(1){
		struct epoll_event events[ASYNC_CLIENT_NUM] ={0};

		int nready = epoll_wait(ctx->epfd,events,ASYNC_CLIENT_NUM,-1);
		if(nready<0) continue;
		for(int i=0;i<nready;i++){
			struct epoll_arg *data = events[i].data.ptr;
			int sockfd = data->fd;

			char buffer[1024] = {0};
			struct sockaddr_in addr;
			size_t addr_len = sizeof(struct sockaddr_in);
				
			int n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, (socklen_t*)&addr_len);
				
			printf("recvfrom n : %d\n", n);
			struct dns_item *domains = NULL;
			int count = dns_parse_response(buffer, &domains);

			data->cb(domains,count);

			epoll_ctl(ctx->epfd,EPOLL_CTL_DEL,sockfd,NULL);
			close(sockfd);

			dns_async_free_domain(domains,count);
			free(data);
		}
	}
}

int dns_async_context_init(struct async_context* ctx){
	
	if(ctx==NULL) return -1;	
	//1 epoll create
	int epfd = epoll_create(1);
	if(epfd<0) return -1;
	ctx->epfd = epfd;

	//2 pthread_create

	int ret = pthread_create(&ctx->thid,NULL,dns_async_callback,ctx);
	if(ret){
		close(epfd);
		return -1;

	}
	return 0;

}

int dns_async_context_destory(struct async_context *ctx){

	close(ctx->epfd);
	pthread_cancel(ctx->thid);

}

int protocol_encode(void *arg){

}
int dns_async_client_commit(struct async_context *ctx,async_result_cb cb){

	//socket
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		perror("create socket failed\n");
		exit(-1);
	}

	printf("url:%s\n", domain);

	struct sockaddr_in dest;
	bzero(&dest, sizeof(dest));
	dest.sin_family = AF_INET;
	dest.sin_port = htons(53);
	dest.sin_addr.s_addr = inet_addr(DNS_SVR);
	//connect server
	int ret = connect(sockfd, (struct sockaddr*)&dest, sizeof(dest));
	printf("connect :%d\n", ret);

	// encode_protocol
	struct dns_header header = {0};
	dns_create_header(&header);

	struct dns_question question = {0};
	dns_create_question(&question, domain);

	char request[1024] = {0};
	int req_len = dns_build_request(&header, &question, request);
	//send
	int slen = sendto(sockfd, request, req_len, 0, (struct sockaddr*)&dest, sizeof(struct sockaddr));
	// protocol_encode();
#if 0
	char buffer[1024] = {0};
	struct sockaddr_in addr;
	size_t addr_len = sizeof(struct sockaddr_in);
		
	int n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, (socklen_t*)&addr_len);
		
	printf("recvfrom n : %d\n", n);
	struct dns_item *domains = NULL;
	dns_parse_response(buffer, &domains);
#else
	//1-->fd-->epoll
	struct epoll_arg *eparg = (struct epoll_arg *)calloc(1,sizeof(struct epoll_arg));
	if(eparg ==NULL){
		return -1;
	}
	eparg->fd = sockfd;
	eparg->cb = cb;


	struct epoll_event ev;
	ev.data.ptr=eparg;
	ev.events = EPOLLIN;
	epoll_ctl(ctx->epfd,EPOLL_CTL_ADD,sockfd,&ev);


#endif
	return 0;
}

int main(int argc, char *argv[]) {

	int count = sizeof(domain) / sizeof(domain[0]);
	struct async_context *ctx = calloc(1,ASYNC_CLIENT_NUM);
	dns_async_context_init(ctx);
#if 0
	for (int i = 0;i < count;i ++) {
		dns_client_commit(domain[i]);
	}
#else
	for (int i = 0;i < count;i ++) {
		dns_async_client_commit(ctx,domain[i]);
	}
#endif

	dns_async_context_destory(ctx);
	free(ctx);
	getchar();
	
}








异步请求的基本实现方法如下: 1. 基于事件驱动模型(Event-Driven Model)开发异步请求,在此模型下应用程序的各个模块可以根据需求注册监听相应的事件,当事件发生时就会触发对应的回调函数。 2. 设定异步请求的工作流程:先将待处理的请求放入请求队列中,然后从队列中取出一个异步请求并处理,同时保证其他请求还能进入到队列里面等待处理。 3. 建立一个线程或使用协程(Coroutine)来处理异步任务,可以并发地读取和处理请求队列中的请求,提高后台处理请求的效率。 4. 利用回调函数机制,将异步请求处理结果通知给应用程序,类似于Signal/Slot机制,这样在请求处理结束后,回调函数会通知对应的应用程序。 5. 为了防止请求过多,需要对请求队列长度和工作线程数等参数进行合理的调整和控制。 以下是一个基本的异步请求实现的代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAXQUEUE 1000 #define MAXTHREADS 50 typedef struct { void (*function)(void *arg); void *argument; } task; task queue[MAXQUEUE]; int queueSize = 0; int queueStart = 0; int queueEnd = -1; pthread_mutex_t queueMutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t queueNotEmpty = PTHREAD_COND_INITIALIZER; pthread_cond_t queueNotFull = PTHREAD_COND_INITIALIZER; void enqueue(task t) { pthread_mutex_lock(&queueMutex); while (queueSize >= MAXQUEUE) { pthread_cond_wait(&queueNotFull, &queueMutex); } queueEnd = (queueEnd + 1) % MAXQUEUE; queue[queueEnd] = t; queueSize++; pthread_cond_signal(&queueNotEmpty); pthread_mutex_unlock(&queueMutex); } task dequeue() { task t; pthread_mutex_lock(&queueMutex); while (queueSize <= 0) { pthread_cond_wait(&queueNotEmpty, &queueMutex); } t = queue[queueStart]; queueStart = (queueStart + 1) % MAXQUEUE; queueSize--; pthread_cond_signal(&queueNotFull); pthread_mutex_unlock(&queueMutex); return t; } void *worker(void *arg) { while (1) { task t = dequeue(); t.function(t.argument); } } void createThreadPool(int numThreads) { pthread_t thread[numThreads]; for (int i = 0; i < numThreads; i++) { pthread_create(&thread[i], NULL, &worker, NULL); } } void asyncFunction(void *arg) { int num = *(int *) arg; printf("Async function %d\n", num); } int main() { int numTasks = 100; int threads = 10; createThreadPool(threads); for (int i = 0; i < numTasks; i++) { int *n = (int *) malloc(sizeof(int)); *n = i; task t = {asyncFunction, n}; enqueue(t); } pthread_exit(NULL); } ``` 在此代码中,函数enqueue()和dequeue()实现异步请求的基本工作流程,createThreadPool()创建了线程实现异步处理请求,而在main()中,任务被创建并放入异步请求中等待处理。同时,在异步函数asyncFunction()中模拟了异步处理请求的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老了希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值