`
暴风雪
  • 浏览: 389072 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

基于tevent的socket多路复用demo

    博客分类:
  • C艹
 
阅读更多
#include <stdio.h>
#include <unistd.h>
#include <talloc.h>
#include <tevent.h>
#include <sys/time.h>
#include <fcntl.h>
#include <tevent_internal.h>
#include<arpa/inet.h>

#define MAXLEN 1024
#define SERV_PORT 4380
#define MAX_OPEN_FD 1024

struct state {
     TALLOC_CTX *ctx;
     int my_fd,fd2[10];
     int conn_num;
     struct sockaddr_in cliaddr,servaddr;
     struct tevent_fd* fde[10];
     struct tevent_context *ev;
};

static void ctdb_tevent_trace(enum tevent_trace_point tp,
			      void *private_data)
{
	int now,diff;

        time_t t = time(NULL);
	now = time(&t);

	switch (tp) {
	case TEVENT_TRACE_BEFORE_WAIT:
		//diff = now-tevent_after_wait_ts;
		//printf("\tHandling event took %ld seconds!\n",
		//       (long)diff);
		//tevent_before_wait_ts = now;
		break;

	case TEVENT_TRACE_AFTER_WAIT:
		//diff = now-tevent_before_wait_ts;
	        //printf("\tNo event for %ld seconds!\n",
		//       (long)diff);
		//tevent_after_wait_ts = now;
		break;

	default:
                printf("\n")
		/* Do nothing for future tevent trace points */ ;
	}
}

static void handler2(struct tevent_context *ev,
                           struct tevent_fd *fde,
                           uint16_t flags,
                           void *private_data)
{
    struct state *data = talloc_get_type_abort(private_data, struct state);
    int j;
    if (flags & TEVENT_FD_READ) {
        char buf[10000];
        int bytes = read(fde->fd,buf,MAXLEN);
        if(bytes == 0){
            close(fde->fd);
            return;
        }
        for (j = 0; j < bytes; ++j)
        {
            buf[j] = toupper(buf[j]);
        }
        printf("recieve_%s\n",buf);
        write(fde->fd,buf,bytes);
    }

}

static void handler(struct tevent_context *ev,
                           struct tevent_fd *fde,
                           uint16_t flags,
                           void *private_data)
{
    struct state *data = talloc_get_type_abort(private_data, struct state);
    struct tevent_fd* fd_event = NULL;
    struct sockaddr_in cliaddr;

    socklen_t clilen = sizeof(cliaddr);
    data->fd2[data->conn_num] = accept(data->my_fd,(struct sockaddr*)&cliaddr,&clilen);
    if (data->fd2[data->conn_num]== -1) {
        return;
    }
    printf("get_accepted\n");
    data->fde[data->conn_num] = tevent_add_fd(data->ev,
                            data,
                            data->fd2[data->conn_num],
                            TEVENT_FD_READ,
                            handler2,
                            data);
    tevent_fd_set_auto_close(data->fde[data->conn_num]);
    data->conn_num++;
}

int main(void)  {
    struct tevent_context *event_ctx;
    TALLOC_CTX *mem_ctx = talloc_new(NULL); // parent
    event_ctx = tevent_context_init(mem_ctx);
    struct state *data = talloc(mem_ctx, struct state);
    data->ctx = mem_ctx;
    data->conn_num = 0;
    struct tevent_fd* fd_event = NULL;


    int  connfd,efd,ret;
    char buf[MAXLEN];
    struct sockaddr_in cliaddr,servaddr;
    socklen_t clilen = sizeof(cliaddr);

    data->my_fd = socket(AF_INET,SOCK_STREAM,0);

    data->ev= event_ctx;
    data->servaddr.sin_family = AF_INET;
    data->servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    data->servaddr.sin_port = htons(SERV_PORT);
    bind(data->my_fd,(struct sockaddr*)&(data->servaddr),sizeof(data->servaddr));
    listen(data->my_fd,30);

    fd_event = tevent_add_fd(event_ctx,
                                     data,
                                     data->my_fd,
                                     TEVENT_FD_READ,
                                     handler,
                                     data);

    printf("flags\t%u\n",fd_event->flags);
    printf("TEVENT_FD_READ\t%u\n",TEVENT_FD_READ);
    tevent_loop_wait(event_ctx);
    talloc_free(mem_ctx);
    return EXIT_SUCCESS;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics