#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <semaphore.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <pthread.h>
#include <sys/time.h>


#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/event-config.h>      
#include <stdarg.h>      
#include <event2/util.h>
#include <event2/buffer.h>
#include <queue>

using namespace std ;

#define LISTEN_PORT 4444
#define LISTEN_BACKLOG 32

void do_accept(evutil_socket_t listener, short event, void *arg);
void read_cb(struct bufferevent *bev, void *arg);
void error_cb(struct bufferevent *bev, short event, void *arg);
void write_cb(struct bufferevent *bev, void *arg);
void DoStkidVec(int,char *) ;


static int n_calls = 0;
void cb_timerfunc(evutil_socket_t fd, short what, void *arg)
{
    struct event *me = (struct event *) arg;

    printf("cb_func called %d times so far.\n", ++n_calls);

    if (n_calls > 100)
       event_del(me);
}

void *funcqueue(void *arg)
{
    pthread_detach(pthread_self());

    struct event_base *base = event_base_new();
    struct timeval one_sec = { 1, 0 };
    struct event *ev;

    ev = event_new(base, -1, EV_PERSIST, cb_timerfunc, event_self_cbarg());
    event_add(ev, &one_sec);
    event_base_dispatch(base);

} //funcqueue


int main(int argc, char *argv[])
{
    int ret;
    evutil_socket_t listener;
    listener = socket(AF_INET, SOCK_STREAM, 0);
    assert(listener > 0);
    evutil_make_listen_socket_reuseable(listener);

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(LISTEN_PORT);

    int idx,idy,idz ;

    pthread_t tqueue ;
    int iCPU = 0 ;
    pthread_create(&tqueue,NULL,funcqueue,(void *)(long)iCPU );

    if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return 1;
    }

    if (listen(listener, LISTEN_BACKLOG) < 0) {
        perror("listen");
        return 1;
    }

    printf ("Listening...\n");

    evutil_make_socket_nonblocking(listener);

    struct event_base *base = event_base_new();
    assert(base != NULL);
    struct event *listen_event;
    listen_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    event_add(listen_event, NULL);
    event_base_dispatch(base);

    printf("The End.");
    return 0;
}

void do_accept(evutil_socket_t listener, short event, void *arg)
{
    struct event_base *base = (struct event_base *)arg;
    evutil_socket_t fd;
    struct sockaddr_in sin;
    socklen_t slen;
    fd = accept(listener, (struct sockaddr *)&sin, &slen);
    if (fd < 0) {
        perror("accept");
        return;
    }
    if (fd > FD_SETSIZE) {
        perror("fd > FD_SETSIZE\n");
        return;
    }

    printf("ACCEPT: fd = %u\n", fd);

    int idx,idy,idz  ;

    struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, read_cb, NULL, error_cb, arg);
    bufferevent_enable(bev, EV_READ|EV_PERSIST);
    //bufferevent_enable(bev, EV_READ|EV_WRITE);
}

void read_cb(struct bufferevent *bev, void *arg)
{

    evutil_socket_t fd = bufferevent_getfd(bev);
    struct evbuffer *input = bufferevent_get_input(bev);

    while( 1 ) {
        int datalen = 0 ;
        char buflen[5]={0},record[8192]={0} ;

        size_t len1 = evbuffer_get_length(input);
        if(len1 < 4)
            return ;
        evbuffer_copyout(input, buflen, 4) ;

        try{
            string s(buflen) ;
            datalen = stoi(s) ;
        }catch(std::exception const &e){
            std::cout << "error:" << e.what() << std::endl ;
            return ;
        }

        size_t len2 = evbuffer_get_length(input);
        if(len2 < (4+datalen) )
            return ;
        evbuffer_drain(input, 4);
        evbuffer_remove(input,record,datalen) ;
        record[datalen] = 0x00 ;
        printf("(%d)receive(%s)\n",fd,record) ;
        send(fd,record,strlen(record),MSG_NOSIGNAL) ;
    } //while

    return ;
}

void write_cb(struct bufferevent *bev, void *arg) {}

void error_cb(struct bufferevent *bev, short event, void *arg)
{
    evutil_socket_t fd = bufferevent_getfd(bev);
    printf("fd = %u, ", fd);
    if (event & BEV_EVENT_TIMEOUT) {
        printf("Timed out\n"); //if bufferevent_set_timeouts() called
    }
    else if (event & BEV_EVENT_EOF) {
        printf("connection closed\n");
    }
    else if (event & BEV_EVENT_ERROR) {
        printf("some other error\n");
    }
    bufferevent_free(bev);
}

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()