首页 > 代码库 > std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义

std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义

std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义这个容器保存了所有客户端连接的channelChannel2* LibEvtServer::CreateChannel(bufferevent* be){    auto c2 = new Channel2;    c2->ser = this;    auto c =  new Channel(be);//这个be比较重要,be是基于socket的bufferevent,包含socket信息以及发送和接收缓冲区    c2->channel = c;    c->m_event = m_event;//Libevent事件回调指针    int id = -1;    {        //存在多个libevent thread同时访问m_ids、m_allChannels,得加锁#ifdef MUL_LIBEVENT_THREAD        std::lock_guard<std::mutex> lock(m_lts_mtx);#endif        id = m_ids->getId();                m_allChannels[id] = c2;//记录所有channel信息,一个客户端对应一个channel,id通过其中保存的bufferevent就能确定是哪个客户端    }    c->m_id = id;    if(id < 0)        MessageBox(NULL, L"LibEvtServer::CreateChannel异常", L"重大错误", MB_OK);    m_event->on_connect(id);    return c2;}//channelid在发送数据时,通过channelid查找容器m_allChannels,然后就能确定是哪个buffereventif(0 != bufferevent_write(c->m_bev, m_send_buffer, len+4))//第一个参数就是bufferevent,其中能确定socket,通过socket就能确定是哪个客户端有一次同事吃饭的认为是一个客户端一个线程,其实不是这样的,只能这样说一个客户端对应一个bufferevent,在这些bufferevent又是通过base进行轮转的在内部是select模式进行循环检查的,哪个socket可读,可写,有错误,都是知道的然后调用不同的函数,auto bev = bufferevent_socket_new(plt->thread_base, item.fd, BEV_OPT_THREADSAFE);Channel2* c2 = CreateChannel(bev);//设置接收、状态改变 回调函数bufferevent_setcb(bev, conn_readcb, NULL, conn_eventcb, c2);//此时已经将c2传递进去了,然后就能知道id,上层也是通过id进行辨别哪个客户端在发送数据时也通过channelid,这边也知道哪个客户端,其实开线程不现实,在服务器上开6000个线程有点扯啊

 

std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义