- 论坛徽章:
- 1
|
- void TServer::ReadEvent(Conn *pcon)
- {
- if(pcon->is_down==1||pcon->m_Engine==NULL)return;
- char *p_cmd;
- int res;
- while ((res = pcon->m_Engine->ReadPack(&p_cmd))>0)
- {
- S_CON_INFO s_info;
- s_info.pcon= pcon;
- s_info.run_code = 1;
- s_info.fd=pcon->GetFd();
- //读取句柄消息
- s_info.data_buf = new char[res+1];
- memcpy(s_info.data_buf, p_cmd, res+1);
- s_info.data_buf_len=res;
- int tid = rand()%m_ThreadCount;
- if(write(m_pThreadInfo[tid].m_notifySendFd, &s_info, sizeof(S_CON_INFO))==-1){
- printf("管道写入错误====pid:%d\n",tid);
- }
- }
- }
复制代码 这里s_info.data_buf = new char[res+1]; 不断new内存,管道另一边就得不断delete内存,是不是很浪费?有没有更好的办法
ReadEvent函数是libevent句柄触发的,只要有句柄有消息,就会调用,while ((res = pcon->m_Engine->ReadPack(&p_cmd))>0)主要负责将这个句柄的消息按照分包原则每个包读取出来,然后通过管道,向线程池哪里发送数据,线程池收到管道数据后,先拷贝数据,然后删除发送管道这边的内存,在继续处理数据罗辑部分。 |
|