I am working on network programming using epoll and I have this code.
int str_len = read(m_events[i].data.fd, buf, BUF_SIZE);
printf("read %d \n", str_len);
if(str_len == 0){
if(!removeClient(m_events[i].data.fd))
break;
close(m_events[i].data.fd);
}
else {
printf("ServerManager::eventAcceptLoop, A message has been received \n");
pushWork(buf);
}
the buf is declared like this
buf[BUF_SIZE];
and I want to pass the data in the buf to the functiion “pushWork”
pushWork(char * pdata){
hand this pdata to the thread pool to parse and handle it
}
I think I need to copy the data in the buf instead of pointing to the buf because it will be overriden whenever a new data comes in. Right now I do something like
char *pdata = buf;
pushWork(pdata)
Should I do memcopy or something else? and flush the buf?
Please let me know how I can handle this. Thanks in advance…
Yes, you can copy the data:
At the
pushWorkend, it has to take care of freeing the data when it’s done:You will also want to consider encapsulating this all into a
std::vectorso you don’t have to worry about exception safety:In this case, pushWork needs to be modified to take a
std::vectorby value, but the upside is that it won’t have to worry about deleting the data when done, and will be fully exception-safe.