Exemplo n.º 1
0
/*
 *函数名称:tcp_init
 *函数功能:完成tcp模块的初始化工作
 *输入参数:none
 *输出参数:none
 *返回值:  on success, return 0(YT_SUCCESSFUL). on error, -1(YT_FAILED) is returned.
*/
int tcp_init()
{
	INFO_PRINT("Net->Tcp模块初始化\n");

	atomic_set(&gsession_counter,0);
	atomic_set(&gpsh_counter,0);
	atomic_set(&gdrop_counter,0);

	return tcp_session_init();
}
Exemplo n.º 2
0
int tcp_cin_init(session_t** se)
{
	int rt=0;
	session_t* pcon = NULL;
	pcon =(session_t*) mpcalloc(sizeof(session_t));
	if (!pcon) goto calloc_fail;
	pcon->host = voolefs.phost[0].host;
	pcon->port = voolefs.phost[0].port;
	rt = tcp_session_open(pcon);
	if (rt) goto open_fail;
	rt = tcp_session_init(pcon);
	if (rt) goto init_fail;
	*se = pcon;
	return rt;


init_fail:
	close(pcon->fd);	
open_fail:
	mpfree(pcon);
calloc_fail:
	*se = NULL;
	return rt;
}
Exemplo n.º 3
0
int tcp_cmd_download_resp_handle (session_t* pse,result_t* presult,cdnmsg_t* msg)
{
	uint32_t  host;
	uint8_t   isnewse;
	struct in_addr in;
	session_t *psession;
	cdnmsg_t  *req;
	switch (msg->body.downloadres.status) {
		case 0:
			presult->signal = 1;
			presult->pdata  = (uint8_t*)msg;
			presult->size = ntohl(msg->head.len)-17;
			//	sem_post(presult->psem);
			printf("i will post a sem,seq %d,sem 0x%x, ret %d\n",msg->head.seq, presult->psem,sem_post(presult->psem));
			
			break;
		case 1:
			break;
		case 2:
			break;
		case 3:
			if (presult->trytimes>5){
				presult->signal = 1;
				presult->pdata  = msg;
				presult->size = -1;
				//  sem_post(presult->psem);
				printf("i will post a sem,seq %d,sem 0x%x, ret %d, but i haven't down anything!\n",
						msg->head.seq, presult->psem,sem_post(presult->psem));
				break;
			}
			psession = gdsl_list_get_head(pse->fi->list_session);
			pthread_mutex_lock(&psession->send_lock);
			gdsl_list_insert_tail(psession->send_queue,presult->preq);
			if (!psession->wtev) {
				psession->wtev = mpcalloc(sizeof(struct event));
				event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
				event_add(psession->wtev,&psession->tv);
			}
			pthread_mutex_unlock(&psession->send_lock);				
			
			//presult->trytimes ++;
			break;
		case 4:
			host = ntohl(*(uint32_t*)((uint8_t*)msg+17));
			pthread_mutex_lock(&pse->fi->flock);
			psession = gdsl_hash_search(pse->fi->hash_session,host);
			if (!psession) {
				psession = mpcalloc(sizeof(struct _cdn_msg_session));
				if (!psession) {
					printf("i can't malloc psession\n");
					abort();
					return;// -ENOSYS;
				}
					//finfo->fh = (uint64_t)psession;
				psession->fi = pse->fi;
				psession->host = host;
				psession->port = *(uint16_t*)((uint8_t*)msg+21);
				in.s_addr= psession->host;
				printf("redirect host: %s,port:%d\n",inet_ntoa(in),ntohs(psession->port));
				/*psession->fd = */tcp_session_open(psession);
				tcp_session_init(psession);
				if (!psession->pdev){
					psession->pdev = mpcalloc(sizeof(struct event));
					event_set(psession->pdev,psession->fd,EV_READ|EV_PERSIST,tcp_session_cback,psession);
					event_add(psession->pdev,&psession->tv);
				}
				gdsl_hash_insert(pse->fi->hash_session,&host);
				gdsl_list_insert_tail(pse->fi->list_session,psession);
				//gdsl_list_insert_tail(voolefs.access,psession);
			}
			else {
				psession = gdsl_list_search(pse->fi->list_session,gdsl_session_host_cmp,&host);
			}
			pthread_mutex_unlock(&pse->fi->flock);
			req = (cdnmsg_t*)presult->preq;	
			pthread_mutex_lock(&psession->send_lock);
			uint32_t len = ntohl(req->head.len);
			presult->trytimes ++;
			if (!psession->eagain){
				int ret = 0,sum=0;
				do{
					ret = write(psession->fd,(void*)req,len);
					if( ret == -1 && (errno == EAGAIN || errno == EINTR)) {
						psession->eagain = 1;
						psession->besent = len-sum;
						psession->send_incomplete = req;
						printf("i met eagain\n");
						//continue;
						gdsl_list_insert_tail(psession->send_queue,req);
						if (!psession->wtev) {
							psession->wtev = mpcalloc(sizeof(struct event));
							event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
							event_add(psession->wtev,&psession->tv);                                                                        
						}
						pthread_mutex_unlock(&psession->send_lock);
					/*	pthread_mutex_lock(&voolefs.reglock),
							gdsl_queue_insert(voolefs.registration, INT2PTR(psession->fd)),
						pthread_mutex_unlock(&voolefs.reglock);
						if (write(voolefs.notify_send_fd, " ", 1) != 1)
							perror("Error writing to notify pipe");
					*/	break;
					}
					else if( ret == -1) {
						pthread_mutex_unlock(&psession->send_lock);
						tcp_session_close(psession);
						mpfree(req);
						//*ppres = NULL;
						abort();
						return -1;
						//do clean connection.
					}
					sum += ret;
					printf("i have send %d from fd:%d\n",sum,psession->fd);
					printf("my pdev is 0x%x, event_base is 0x%x, event is 0x%x\n",psession->pdev,
							(psession->pdev)?((struct event*)psession->pdev)->ev_base:NULL,
							(psession->pdev)?((struct event*)psession->pdev)->ev_events:NULL);
				}while(sum<len);
				
				pthread_mutex_unlock(&psession->send_lock);
			} else{
				gdsl_list_insert_tail(psession->send_queue,req);
				if (!psession->wtev) {
					psession->wtev = mpcalloc(sizeof(struct event));
					event_set(psession->wtev,psession->fd,EV_WRITE|EV_PERSIST,tcp_session_cback,psession);
					event_add(psession->wtev,&psession->tv);                                                                            
				}
				pthread_mutex_unlock(&psession->send_lock);
			/*	pthread_mutex_lock(&voolefs.reglock),
					gdsl_queue_insert(voolefs.registration, INT2PTR(psession->fd)),
				pthread_mutex_unlock(&voolefs.reglock);
				if (write(voolefs.notify_send_fd, " ", 1) != 1)
					perror("Error writing to notify pipe");
			*/
			}
			break;
		default:
			break;
	}
	return 0;
}