I'm sure this question has been asked before, and maybe it's just somewhere miles and miles hidden somewhere in Google results or Stackoverflow. But so far I've setup a nonblocking cache server. That does well with small messages or data. But what if the client attempts to send large amounts of data? I've used a delimiter to tell where the packet ends, and before I was using the blocking thread per client setup. After realizing this route isn't reliable due to hardware limitations. So I'm going the single thread approach if possible.
Basically, I want to know how can I do recv that captures packets of data that appends into a single buffer that I can use when the user is done sending data?
One idea I had was simply do a recv like normal. If the delimiter isn't found, then fork the recv in a thread and add up the data in a buffer somewhere.
I.E
struct tempBuffer{
int socket;
std::string buffer;
bool isDone;
};
std::vector<tempBuffer> temp;
...
if !findDelimiter(recvResponseData)
startThread(collectData, socket);
Here is what my recvAll looks like:
std::string swiss_cache::recvAll(int socket, bool &quit) {
std::string buffer;
size_t sentBuffer = 0;
quit = false;
//now it is time to receive the page
while(true)
{
char buf[CHUNK_SIZE+1];
memset(buf, 0, strlen(buf));
size_t tmpres = recv(socket, buf, CHUNK_SIZE, 0);
if(tmpres == 0){
quit = true;
return buffer;
}
buffer += (const char*)buf;
if(findEscapeKey(buffer)) { /* FOUND ESCAPE KEYWORD TO LEAVE RECV */
return buffer;
}
}
return buffer;
} /* recvAll */
How can I collect packets of data in a single buffer from a client in a no block mode when the client has to send multiple packets of data?