2 qq 30069409 qq_30069409 于 2017.09.13 17:56 提问

vector,libevent,c++

#include "dem01.h"

class tcpServer;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int m_threads = 0;
typedef struct multival
{
    int pipefd[2];
    pthread_t tid;
    tcpServer *server;
    struct event_base *base;

} multival_t;

class tcpServer
{
    public:
        tcpServer(unsigned int port, int threads);


    public:
        void start();   
        void initializepthred();


    private:
        void acceptHandler();


    public:
        vector<multival_t> multivector;

    private:
        int m_threads;
        unsigned int m_port;

};


void *worker_pthread(void * ptr);
void read_callback(int fd, const short event, void *arg);
void master_callback(struct evconnlistener *listener, int connfd, struct sockaddr, int len, void *ptr);


tcpServer::tcpServer(unsigned int port, int threads = 5)
{
    m_port = port;
    this->m_threads = threads;
    ::m_threads = threads;
    multivector(threads);
}


void read_callback(int fd, const short event, void *arg)
{
    char buffer[10];

    int res = read(fd, buffer, sizeof(buffer) - 1);
    if(res < 0)
    {
            cout << "read_callback read failure" << endl;
            return;
    }   
    buffer[res] = '\0';

    cout << buffer << endl;
}

void *worker_pthread(void *ptr)
{
    pthread_mutex_lock(&mutex);
    multival_t *data = static_cast<multival_t *>(ptr);
    pthread_mutex_unlock(&mutex);

    data->base = event_base_new();

    struct event *listener = event_new(data->base, data->pipefd[0], 
                                                                            EV_READ | EV_PERSIST, read_callback, ptr);
    event_add(listener, NULL);

    event_base_dispatch(data->base);
    event_base_free(data->base);

    pthread_exit(NULL);
}


void master_callback(struct evconnlistener *listener, int connfd, struct sockaddr *peeraddr, int len, void *ptr)
{
    tcpServer *server = static_cast<tcpServer *>(ptr);          
    assert(server);

    int position = connfd % m_threads;

    int fd = server->multivector[position].pipefd[1];

    string buffer;
    stringstream stream;
    stream.str("");
    stream << connfd;
    stream >> buffer;
    stream.str("");

    int res = write(fd, buffer.c_str(), buffer.length());
    if(res < 0)
    {
        cout << "acceptHandler write failure" << endl;
        return; 
    }   
}


void tcpServer::start()
{
    struct event_base *m_base = event_base_new();
    struct sockaddr_in srvaddr;
    memset(&srvaddr, 0, sizeof(srvaddr));

    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(m_port);
    srvaddr.sin_addr.s_addr = INADDR_ANY;

    struct evconnlistener *listener = evconnlistener_new_bind(m_base, master_callback, this, 
                                                                                                                         LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, -1, 
                                                                                                                         (struct sockaddr *)&srvaddr, sizeof(srvaddr));
    initializepthred();

    event_base_dispatch(m_base);
    cout << "loop the main exit" << endl;

    evconnlistener_free(listener);
    event_base_free(m_base);
}

void tcpServer::initializepthred()
{
    for(int i = 0; i < m_threads; i ++)
    {
        multivector[i].base = NULL;
        multivector[i].server = this;

        if(pipe(multivector[i].pipefd) < 0)
        {
            cout << "create pipe failure" << endl;  
        }

        pthread_create(&multivector[i].tid, NULL, worker_pthread, &multivector[i]); 
    }
}


int main ()
{
    tcpServer *server = new tcpServer(8001, 5);
    assert(server);

    server->start();
    return 0;       
}


在构造函数出出现关于vector的问题,不能正常编译,来个好心人帮忙看看。

1个回答

oyljerry
oyljerry   Ds   Rxr 2017.09.13 21:47

multivector(threads);
你不能这么用,需要push_back插入数据

qq_30069409
qq_30069409 为什么在可以写成vector<int> ivec(10)这种形式,而在class中的声明出却不能这么写呢?
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片