コード例 #1
0
ファイル: centernode.c プロジェクト: RinCelery/hdyt.com
//CMD_STATUS Message Handler
int centernode_cmd_load (centernode_t *cn, cnaccess_t *ca, cdnmsg_t *req)
{
    fprintf (stderr, "cmd_load\n");
    pthread_mutex_lock(&cn->objlock);
    fprintf (stderr, "len:%d, cmd:0x%04x, ver:0x%04x, seq:0x%04x, nodeid:0x%04x, passwd:%s\n",
            ntohl(req->pdu.len), ntohs(req->pdu.cmd), ntohs(req->pdu.ver),
            ntohs(req->pdu.seq), ntohs(req->pdu.body.login.nodeid), (char*)req->pdu.body.login.passwd);



    cdnmsg_t *res = (cdnmsg_t *) calloc(sizeof(cdnmsg_t),sizeof(char));
    res->pdu.len = htonl(16);
    res->pdu.cmd = htons(CMD_LOGIN_RES);
    res->pdu.seq = req->pdu.seq;
    res->pdu.ver = req->pdu.ver;

    res->pdu.loginreq.nodeid = req->pdu.body.login.nodeid;
    memcpy (res->pdu.loginreq.passwd, req->pdu.body.login.passwd, 4);
    pthread_mutex_unlock(&cn->objlock);

    pthread_mutex_lock(&ca->lock);
    gdsl_list_insert_tail(ca->send_queue, res);
    pthread_mutex_unlock(&ca->lock);

    res = (cdnmsg_t *) calloc(sizeof(cdnmsg_t), sizeof(char));
    res->pdu.len = htonl(23);
    res->pdu.cmd = htons(CMD_NTP);
    res->pdu.seq = htons(ntohs(req->pdu.seq)+1);
    res->pdu.ver = htons(0x01);

    res->pdu.ntpreq.nodeid = req->pdu.body.login.nodeid;
    memcpy (res->pdu.ntpreq.passwd, req->pdu.body.login.passwd, 4);
    time_t t1 = time(NULL);
    struct tm *tblock;
    tblock = localtime(&t1);
    res->pdu.ntpreq.time[0] = (tblock->tm_year+1900)%100;
    res->pdu.ntpreq.time[1] = tblock->tm_mon+1;
    res->pdu.ntpreq.time[2] = tblock->tm_mday;
    res->pdu.ntpreq.time[3] = tblock->tm_hour;
    res->pdu.ntpreq.time[4] = tblock->tm_min;
    res->pdu.ntpreq.time[5] = tblock->tm_sec;
    res->pdu.ntpreq.time[6] = 0;

    pthread_mutex_lock(&ca->lock);
    gdsl_list_insert_tail(ca->send_queue, res);
    pthread_mutex_unlock(&ca->lock);



    return 0;
}
コード例 #2
0
ファイル: voole_cdn.c プロジェクト: RinCelery/sshfs-redevelop
void tcp_session_read(int fd,void* arg)
{
	session_t* se = (session_t*)arg;
	int r = 0;
	//printf("my event is %d\n",ev);
	cdnmsg_t *msg = NULL;
	if (se->recv_incomplete == 0) {
		r = recv(se->fd, ((uint8_t *)&se->magic) +se->received, 4 -se->received, 0);
		if (r == 0 || (r < 0 && errno != EAGAIN && errno != EINTR)) {
			//return !cberror(ev, se);
			printf("read error,%d\n",errno);
			abort();
			//close(fd);
			return ;
		}
		if (r <  0 || (se->received +=r) < 4) /* 'Total-Length' field */
		{
			printf("read 2 error,%d\n",errno);
			return ;
		}
		if (ntohl(se->magic) > MAXMSGSIZE || ntohl(se->magic) <= 12)
		{
			printf("i cann't read\n");
			return ;
		}
		se->recv_incomplete = (uint8_t*)mpcalloc(ntohl(se->magic));
		msg =(cdnmsg_t*) se->recv_incomplete;
		msg->head.len = se->magic;
		printf("we need to recv %d bytes - %d \n",ntohl(se->magic),se->received);
		return ;
	}
	msg =(cdnmsg_t*) se->recv_incomplete;
	r = recv(se->fd, ((uint8_t *)msg) +se->received, ntohl(msg->head.len) -se->received, 0);
//	printf("i have recv %d-%d from %d\n",se->received,r,se->fd);
	if (r == 0 || (r < 0 && errno != EAGAIN && errno != EINTR)) {
		//return !cberror(ev, se);
		printf(" read error\n");
	}
	if (r > 0  && (se->received +=r) == ntohl(msg->head.len))
	{ /* complete message */
		se->recv_incomplete = 0;
		se->received = 0;
		printf("there are the cdnmsg_t context: \n head.len=%d, head.cmd=0x%08x, head.seq=%d \n body.status=%d,body.len=%d\n",
				ntohl(msg->head.len),
				 ntohl(msg->head.cmd),msg->head.seq,
				 msg->body.downloadres.status,
				 ntohl(msg->body.downloadres.length));
		pthread_mutex_lock(&se->recv_lock);
		gdsl_list_insert_tail(se->recv_queue,msg);
		pthread_mutex_unlock(&se->recv_lock);
		threadpool_invoke_add_task(voolefs.tpool, (void(*)(void *))voole_dispatch, (void*)se);
	}
	return ;
}
コード例 #3
0
ファイル: voole_cdn.c プロジェクト: RinCelery/sshfs-redevelop
/* size is n kb */
int voole_read (const char *path, char *rbuf, size_t size, off_t offset,
		struct fuse_file_info *finfo)
{
	finfo_t *fi = (finfo_t*)finfo->fh;
	session_t* psession = gdsl_list_get_head(fi->list_session);

	if (psession == NULL) return -ENOSYS;
	psession->fi = fi;
	int i = 0;
	printf("size:%zd,offset:%zd\n",size,offset);
	int index = offset/(16*1024);
	result_t *pr = NULL;
	int ret = 0;
	do{
		i++;
		ret = tcp_cmd_download(i+index,0,psession,&pr);
		gdsl_list_insert_tail(fi->list_result,pr);
	}while(i*1024*16<size );

	i=0;
	int sum = 0;
	do{
		pr = gdsl_list_get_head(fi->list_result);
		printf("i am waiting sem 0x%x,seq %d\n",pr->psem,pr->seq);
		int ret = sem_wait(pr->psem);

		if (ret!=0) {
			perror("sem_wait error:\n");
			sleep(10);
			continue;
		}
		if(pr->size <17 && pr->size!=-1) {
			perror("sem_wait size error:");
			continue;
		}
		pthread_mutex_lock(&fi->flock);
		result_t* presult = gdsl_list_remove_head(fi->list_result);
		pthread_mutex_unlock(&fi->flock);
		tcp_sem_payback(pr->psem);

		printf("i have get a sem ,index %d,seq %d\n",i+1,presult->seq);
		if (presult->size <17) printf("get err\n");
		sum += presult->size;
		if (pr->size != -1) 
			memcpy(rbuf+i*1024*16,presult->pdata+17,(sum>size)?(size-sum+presult->size):presult->size);
		printf("this package size is %d\n",presult->size);
		mpfree(presult->pdata);
		mpfree(presult->preq);
		mpfree(presult);	
		i++;
	}while(i*1024*16<size );
	printf("i will go out of recv,%d bytes\n",(sum>size)?size:sum);
	return size; //(sum>size)?size:sum;
}
コード例 #4
0
ファイル: centernode.c プロジェクト: RinCelery/hdyt.com
int cmd_alarm_res (centernode_t *cn, cnaccess_t* ca, cdnmsg_t *req)
{
    cdnmsg_t *msg = (cdnmsg_t*) calloc(sizeof(cdnmsg_t), sizeof(char));
    msg->pdu.len = htonl(16);
    msg->pdu.cmd = htons(ntohs(req->pdu.cmd)|0x00f0);
    msg->pdu.ver = htons(0x01);
    msg->pdu.seq = req->pdu.seq;

    msg->pdu.telemetryres.nodeid = req->pdu.telemetryreq.nodeid;
    msg->pdu.telemetryres.passwd = req->pdu.telemetryreq.passwd;

    pthread_mutex_lock(&ca->lock);
    gdsl_list_insert_tail(ca->send_queue, msg);
    pthread_mutex_unlock(&ca->lock);

    return 0;
}
コード例 #5
0
ファイル: centernode.c プロジェクト: RinCelery/hdyt.com
void centernode_cbtimer(centernode_t *cn)
{
    pthread_mutex_lock(&cn->objlock);
    redis_get_udpinfo(cn, (char **)&cn->udp_addr, &cn->udp_port);

    int i = 0;
    if ((i = redis_get_reportid(cn)))
        cn->udp_report = 1;
    else
        cn->udp_report = 0;
#ifdef REDISBAK
    if (cn->rdbInterval > 60) {
        if ((cn->rdb_conn = redis_connect_tcp(cn->rdb_bakhost, cn->rdb_bakport, cn->rdb_bakpswd)))
            cn->rdbStatus = 1;
    }
#endif
    pthread_mutex_unlock(&cn->objlock);

#ifdef MOBILE
    cnaccess_t *ca = centernode_connect (cn, cn->report_ip, cn->report_port, 0, 64 << 16, 1);
    if (NULL == ca)
        return;
    char nodeInfo[10240] = {"\0"}, xml[10240] = {"\0"}, http[40960] = {"\0"};

    app_map_node(cn, &nodeInfo);

    LOG("INFO", "node %s\n", nodeInfo);
    center_reportXML(xml, sizeof(xml), "LoadStatusReportReq", "0000123123", nodeInfo);
    center_postHttpHeader(http, sizeof(http), cn->report_ip, cn->report_port, xml, strlen(xml));

    char *msg = calloc(strlen(http) + 1, sizeof(char));    
    memset(msg, 0, strlen(http)+1);
    memcpy(msg, http, strlen(http));

    LOG("INFO", "%s\n", http);

    pthread_mutex_lock(&ca->lock);
    gdsl_list_insert_tail(ca->send_queue, msg);
    pthread_mutex_unlock(&ca->lock);
#endif
}
コード例 #6
0
ファイル: cdn_csn.c プロジェクト: RinCelery/sshfs-redevelop
int main()
{
	struct sockaddr_in server_addr;
	bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_port = htons(8000);

	int server_socket = socket(PF_INET,SOCK_STREAM,0);
    if( server_socket < 0)
    {
        printf("Create Socket Failed!");
        exit(1);
    }
	int opt =1;
	setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

	if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
    {
        perror("Server Bind Port 53 Failed: "); 
        exit(1);
    }
	
	if ( listen(server_socket, 5) )
    {
        printf("Server Listen Failed!"); 
        exit(1);
    }
	pserver_context pcontext = (pserver_context) calloc(1,sizeof(server_context));
	pcontext->client_context_lt = gdsl_list_alloc("client socket",NULL,NULL);
	
	pcontext->kdpfd = epoll_create(5);
	pthread_attr_t attr;
    pthread_t threadId;
	pthread_attr_init(&attr); 
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); 
    /* 设置线程为分离属性*/ 
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if(pthread_create(&threadId,&attr,message_handler,(void*)pcontext))
    { 
		perror("pthread_creat error!"); 
        exit(-1); 
	}
	
	while(1)
	{
		struct sockaddr_in client_addr;
        socklen_t length = sizeof(client_addr);
		printf("waiting for connection...\n");
		int new_client_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
        if ( new_client_socket < 0)
        {
            printf("Server Accept Failed!\n");
            /*break;*/
			continue;
        }
		setnonblocking(new_client_socket);
		pconnect_context pconnection = calloc(1,sizeof(connect_context));
		if( pconnection == NULL )
		{
			perror("calloc event fail: ");
			close(new_client_socket);
			continue;
		}
		pconnection->ev.events = EPOLLIN;
		pconnection->session.socket = new_client_socket;
		pconnection->socket = new_client_socket;
		pconnection->ev.data.ptr = (void*)pconnection;/*(void*)&(pconnection->session);*/

		if(epoll_ctl(pcontext->kdpfd, EPOLL_CTL_ADD, new_client_socket,&(pconnection->ev))<0)
		{
			perror("epoll_ctl fail: ");
			close(new_client_socket);
			continue;	
		}
		gdsl_list_insert_tail( pcontext->client_context_lt,(int*)(size_t)new_client_socket);
	}
	return 0;
}
コード例 #7
0
ファイル: main_list.c プロジェクト: Fitor/GDSL
int main (void)
{
    int choix = 0;

    gdsl_list_t l = gdsl_list_alloc ("MY LIST", alloc_string, free_string);

    do
	{
	    printf ("\t\tMENU - LIST\n\n");
	    printf ("\t 1> Create a cell\n");
	    printf ("\t 2> Remove the first cell\n");
	    printf ("\t 3> Remove the last cell\n");
	    printf ("\t 4> Remove a cell\n");
	    printf ("\t 5> Display list in forward order\n");
	    printf ("\t 6> Display list in backward order\n");
	    printf ("\t 7> Flush list\n");
	    printf ("\t 8> Size of list\n");
	    printf ("\t 9> Dump list\n");
	    printf ("\t10> XML dump of list\n");
	    printf ("\t11> Search for a place\n");
	    printf ("\t12> Search for an element\n");
	    printf ("\t13> Sort of list\n");
	    printf ("\t14> Greatest element of list\n");
	    printf ("\t 0> Quit\n\n");
	    printf ("\t\tYour choice: ");
	    scanf ("%d", &choix);

	    switch (choix)
		{
		case 1:
		    {
			char nom[100];
			int done = 0;

			printf ("Nom: ");
			scanf ("%s", nom);

			do
			    {
				int choix;

				printf ("\t\tMENU - CELL INSERTION\n\n");
				printf ("\t1> Insert cell at the beginning of the list\n");
				printf ("\t2> Insert cell at end of list\n");
				printf ("\t3> Insert cell after another cell\n");
				printf ("\t4> Insert cell before another cell\n");
				printf ("\t5> Display the list\n");
				printf ("\t0> RETURN TO MAIN MENU\n\n");
				printf ("\t\tYour choice: ");
				scanf ("%d", &choix );

				switch (choix)
				    {
				    case 1:
					{
					    gdsl_list_insert_head (l, nom);
					    done = 1;
					}
					break;

				    case 2:
					{
					    gdsl_list_insert_tail (l, nom);
					    done = 1;
					}
					break;

				    case 3:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						char Nom[100];
						gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

						printf ("Name of cell after which you want to insert: ");
						scanf ("%s", Nom);
			
						if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom))
						    {
							printf ("The cell '%s' doesn't exist\n", Nom);
						    }
						else
						    {
							gdsl_list_cursor_insert_after (c, nom);
							done = 1;
						    }
						gdsl_list_cursor_free (c);
					    }
					break;

				    case 4:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						char Nom[100];
						gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

						printf ("Name of cell before which you want to insert: ");
						scanf ("%s", Nom);
			
						if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom))
						    {
							printf ("The cell '%s' doesn't exist\n", Nom);
						    }
						else
						    {
							gdsl_list_cursor_insert_before (c, nom);
							done = 1;
						    }
						gdsl_list_cursor_free (c);
					    }
					break;

				    case 5:
					if (gdsl_list_is_empty (l))
					    {
						printf ("The list is empty.\n");
					    }
					else
					    {
						affiche_liste_chaines_fwd (l);
					    }
					break;

				    case 0:
					done = 1;
					break;
				    }
			    } 
			while (!done);
		    }
		    break;

		case 2:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_delete_head (l);
			}
		    break;

		case 3:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_delete_tail (l);
			}
		    break;

		case 4:
		    {
			char nom[100];

			if (gdsl_list_is_empty (l))
			    {
				printf ("The list is empty.\n");
			    }
			else
			    {
				printf ("Name of cell to remove: ");
				scanf ("%s", nom);

				if (!gdsl_list_delete (l, compare_strings, nom))
				    {
					printf ("The cell '%s' doesn't exist\n", nom);
				    }
				else
				    {
					printf ("The cell '%s' is removed from list\n", nom);
				    }
			    }
		    }
		    break;

		case 5:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    affiche_liste_chaines_fwd (l);
			}
		    break;

		case 6:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    affiche_liste_chaines_bwd (l);
			}
		    break;
	  
		case 7:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_flush (l);
			}
		    break;

		case 8:
		    printf ("Card( %s ) = %ld\n", gdsl_list_get_name (l), gdsl_list_get_size (l));
		    break;

		case 9:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_dump (l, print_string, stdout, NULL);
			}
		    break;

		case 10:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    gdsl_list_write_xml (l, print_string, stdout, NULL);
			}
		    break;

		case 11:
		    {
			int pos;
			gdsl_element_t e;

			printf ("Enter the position of the place to search for: ");
			scanf ("%d", & pos);

			e = gdsl_list_search_by_position (l, (ulong) pos);
			if (e != NULL)
			    {
				print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL);
			    }
		    }
		    break;
	  
		case 12:
		    {
			char nom [100];
			gdsl_element_t e;

			printf ("Name of cell to search for: ");
			scanf ("%s", nom);
	    
			e = gdsl_list_search (l, compare_strings, nom);
			if (e == NULL)
			    {
				printf ("The cell '%s' doesn't exist\n", nom);
			    }
			else
			    {
				printf ("The cell '%s' was found: ", nom);
				print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL);
				printf ("\n");
			    }
		    }
		    break;

		case 13:
		    gdsl_list_sort (l, compare_strings);
		    break;
	  
		case 14:
		    if (gdsl_list_is_empty (l))
			{
			    printf ("The list is empty.\n");
			}
		    else
			{
			    printf ("Max Element: %s\n", (char*) gdsl_list_search_max (l, compare_strings));
			}
		    break;

		case 15: /* case for my own tests... */
		    {
			int i;
			gdsl_perm_t p = gdsl_perm_alloc ("p", PERMUTATION_NB);
			gdsl_list_t g = gdsl_list_alloc ("MY LIST 2", alloc_string, free_string);

			gdsl_perm_randomize (p);

			for (i = 0; i < PERMUTATION_NB; i++)
			    {
				char c[2];
				c[0] = 65 + gdsl_perm_get_element (p, i);
				c[1] = '\0';
				gdsl_list_insert_tail (g, c);
			    }

			gdsl_perm_free (p);
			affiche_liste_chaines_fwd (g);
			affiche_liste_chaines_bwd (g);
			printf ("SORT\n");
			gdsl_list_sort (g, compare_strings);
			affiche_liste_chaines_fwd (g);
			affiche_liste_chaines_bwd (g);
			gdsl_list_free (g);
		    }

		    {
			int i = 0;
			gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l);

			for (gdsl_list_cursor_move_to_head (c); gdsl_list_cursor_get_content (c); gdsl_list_cursor_step_forward (c))
			    {
				char toto[50];
				sprintf (toto, "%d", i++);

				gdsl_list_cursor_insert_before (c, toto);

				gdsl_list_cursor_step_backward (c);
				gdsl_list_cursor_delete_after (c);
			    }
			
			gdsl_list_cursor_free (c);
		    }
		    break;
		}
	} 
    while (choix != 0);

    gdsl_list_free (l);

    exit (EXIT_SUCCESS);
}
コード例 #8
0
ファイル: voole_cdn.c プロジェクト: RinCelery/sshfs-redevelop
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;
}
コード例 #9
0
ファイル: voole_cdn.c プロジェクト: RinCelery/sshfs-redevelop
int tcp_cmd_download(off_t index, size_t size, session_t* psession,result_t** ppres)
{
	// fid baa9cdbdc23cdc595c6f7c66d72a0fd5
	printf("i need %zd bytes from index %zd\n",size,index);
	cdnmsg_t* req = mpcalloc(sizeof(cdnmsg_t));
	req->head.len = htonl(sizeof(struct _cdn_msg_download)+sizeof(cdnmsghead_t));
	req->head.cmd = htonl(CMD_DOWNLOAD_REQ);
	req->head.seq = tcp_get_seq(psession);

	strncpy(req->body.downloadreq.fid,psession->fid,sizeof(req->body.downloadreq.fid));//"baa9cdbdc23cdc595c6f7c66d72a0fd5");
	strncpy(req->body.downloadreq.nodeid,"230001",6);
	req->body.downloadreq.offset = htonl(0);
	req->body.downloadreq.index  = htonl(index);
	req->body.downloadreq.length = htonl(size);
	//int ret = send(psession->fd,req,sizeof(cdnmsg_t),0);

	
	pthread_mutex_lock(&psession->send_lock);
	uint32_t len = ntohl(req->head.len);
	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;
				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);

				break;
			}
			else if( ret == -1) {
				pthread_mutex_unlock(&psession->send_lock);
				printf("tcp_cmd_req send error \n");
				tcp_session_close(psession);
				mpfree(req);
				*ppres = NULL;
				return -1;
				//do clean connection.
			}
			sum += ret;
			printf("i have send %d from fd:%d,seq:%d\n",sum,psession->fd,req->head.seq);
			/*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);
		printf("i have put it in cbsend from fd:%d\n",psession->fd);
	}
	
	result_t* presult = mpcalloc(sizeof(result_t));
	presult->signal = 0;
	presult->seq = req->head.seq;
	presult->pdata = NULL;
	presult->size = 0;
	presult->preq = req;
	tcp_sem_get(&presult->psem);
	*ppres = presult;
	return 0;
}