Beispiel #1
0
int dpdk_handle_event(struct epoll_event ev)
{
    char recv_buf[BUFFER_SIZE];
    int len;
    int send_len = 0;
    char send_buf[BUFFER_SIZE];

    if (ev.events&EPOLLIN)
    {

        while(1)
        {
            len = anssock_recvfrom(ev.data.fd, recv_buf, BUFFER_SIZE, 0, NULL, NULL);
            if(len > 0)
            {
                sprintf(send_buf, "I have received your message.");

                send_len = anssock_send(ev.data.fd, send_buf, 2500, 0);
                if(send_len < 0)
                {
                    printf("send data failed, send_len %d \n", send_len);
                }

                printf("receive from client(%d) , data len:%d \n", ev.data.fd, len);
            }
            else if(len < 0)
            {
                if (errno == ANS_EAGAIN)
                {
                    break;
                }
                else
                {
                    printf("remote close the socket, errno %d \n", errno);
                    anssock_close(ev.data.fd);
                    break;
                }
            }
            else
            {
                printf("remote close the socket, len %d \n", len);
                anssock_close(ev.data.fd);
                break;
            }

        }
    }

    if (ev.events&EPOLLERR || ev.events&EPOLLHUP)
    {
        printf("remote close the socket, event %x \n", ev.events);
        anssock_close(ev.data.fd);
    }

    return 0;
}
Beispiel #2
0
static int
HandleReadEvent(int epoll_fd, struct epoll_event ev)
{
	int rd;
	int i;
	int len;
	int sent;
	char recv_buf[BUFFER_SIZE];
	int sockid = ev.data.fd;
	/* HTTP request handling */
	rd = anssock_recvfrom(sockid, recv_buf, BUFFER_SIZE, 0, NULL, NULL);

	if (rd <= 0) {
		return rd;
	}
	/* just response http 200*/
	len = strlen(http_200);
	sent = anssock_send(ev.data.fd, http_200, len, 0);

	ev.events = EPOLLIN | EPOLLOUT;
	anssock_epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockid, NULL);
//	printf("read and close sockid:%d\n", sockid); //test purpose
	return rd;
}
Beispiel #3
0
int main(void)
{
    int ret;
    int i = 0 ;
    int fd, recvfd = 0;
    int epfd;
    int data_num =0;
    char send_data[2048];
    struct timeval start, end;
    struct sockaddr_in addr_in;  
    struct sockaddr_in remote_addr;  
    struct epoll_event event;
    char recv_buf[2038];
    int recv_len; 

    ret = anssock_init(NULL);
    if(ret != 0)
        printf("init sock ring failed \n");


    /* create epoll socket */
    epfd = anssock_epoll_create(0);
    if(epfd < 0)
    {
        printf("create epoll socket failed \n");
        return -1;
    }

    fd = anssock_socket(AF_INET, SOCK_DGRAM, 0);	
    if(fd < 0)
    {
        printf("create socket failed \n");
        anssock_close(epfd);
        return -1;
    }

    memset(&addr_in, 0, sizeof(addr_in));      
    addr_in.sin_family = AF_INET;  
    addr_in.sin_port   = htons(8888);  
    addr_in.sin_addr.s_addr = htonl(0x02020202); 

    ret =  anssock_bind(fd, (struct sockaddr *)&addr_in, sizeof(addr_in) );
    if(ret != 0)
    {
        printf("bind socket failed \n");
        anssock_close(fd);
        anssock_close(epfd);
        return -1;
    }

    memset(&remote_addr, 0, sizeof(remote_addr));      
    remote_addr.sin_family = AF_INET;  
    remote_addr.sin_port   = htons(9999);  
    remote_addr.sin_addr.s_addr = htonl(0x02020205); 


    event.data.fd = fd;  
    event.events = EPOLLIN | EPOLLET;  

    ret = anssock_epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
    if(ret != 0)
    {
        printf("epoll ctl failed \n");
        anssock_close(fd);
        anssock_close(epfd);
        return -1;
    }

    printf("start dpdk udp application \n");

    int event_num = 0;
    memset(send_data, 0, sizeof(send_data));
    
    while(1)
    {
        event_num = anssock_epoll_wait (epfd, events, 20, -1);
        if(event_num <= 0)
        {
            printf("epoll_wait failed \n");
            continue;
        }
            
        for(i = 0; i < event_num; i++)
        {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))  
            {  
                printf("dpdk socket(%d) error\n", events[i].data.fd);
                anssock_close (events[i].data.fd);  
                continue;  
            } 
            
            if (events[i].events & EPOLLIN)
            {
                while(1)
                {
                    recv_len = anssock_recvfrom(events[i].data.fd, recv_buf, 2048, 0, NULL, NULL);

                    if(recv_len > 0)  
                    {  
                        printf("Recv: %s \n", recv_buf);

                        data_num++;
                        sprintf(send_data, "Hello, linux_udp, num:%d !", data_num);

                        anssock_sendto(events[i].data.fd, send_data, strlen(send_data) + 1, 0, (struct sockaddr *)&remote_addr,  sizeof(remote_addr));
                    } 
                    else if(recv_len < 0)
                    {
                        if (errno == ANS_EAGAIN)   
                        {
                            break;
                        }
                        else
                        {
                            printf("remote close the socket, errno %d \n", errno);
                            anssock_close(events[i].data.fd);
                            break;
                        }
                    }
                    else
                    {
                        printf("remote close the socket, len %d \n", recv_len);
                        anssock_close(events[i].data.fd);
                        break;
                    }

                }
            
            }
            else
            {
                printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd);
            }

            
        }
    
    }



    anssock_close(fd);
    anssock_close(epfd);

    return 0;
}