T* RingBuffer<T>::readOneBuffer() { pthread_mutex_lock(&_lock); while (getReadPos() == getWritePos()) pthread_cond_wait(&_rdcond, &_lock); T *buf = _buffer + getReadPos()*getBufSize(); pthread_mutex_unlock(&_lock); return buf; }
/** * Convenience function. */ size_t read(uint8_t* data, size_t startPos, size_t size) { size_t oldReadPos = getReadPos(); setReadPos(startPos); size_t ammountRead = read(data, size); setReadPos(oldReadPos); return ammountRead; }
void RingBuffer<T>::updateReadPos() { pthread_mutex_lock(&_lock); _readPos = (getReadPos() + 1) % getNumBufers(); pthread_cond_signal(&_wrcond); pthread_mutex_unlock(&_lock); }
T* RingBuffer<T>::writeOneBuffer() { pthread_mutex_lock(&_lock); int futWritePos = (getWritePos() + 1) % getNumBufers(); while (futWritePos == getReadPos()) pthread_cond_wait(&_wrcond, &_lock); T *buf = _buffer + getWritePos()*getBufSize(); pthread_mutex_unlock(&_lock); return buf; }
/** * Get Line * Retrive the entire contents of a line: string from current position until CR or LF, whichever comes first, then increment the read position * until it's past the last CR or LF in the line * * @return Contents of the line in a string (without CR or LF) */ string HTTPMessage::getLine() { string ret = ""; int startPos = getReadPos(); bool newLineReached = false; char c = 0; // Append characters to the return string until we hit the end of the buffer, a CR (13) or LF (10) for(unsigned int i = startPos; i < size(); i++) { // If the next byte is a \r or \n, we've reached the end of the line and should break out of the loop c = peek(); if((c == 13) || (c == 10)) { newLineReached = true; break; } // Otherwise, append the next character to the string ret += getChar(); } // If a line termination was never reached, discard the result and conclude there are no more lines to parse if(!newLineReached) { setReadPos(startPos); // Reset the position to before the last line read that we are now discarding ret = ""; return ret; } // Increment the read position until the end of a CR or LF chain, so the read position will then point to the next line for(unsigned int i = getReadPos(); i < size(); i++) { c = getChar(); if((c != 13) && (c != 10)) { // Set the Read position back one because the retrived character wasn't a LF or CR setReadPos(getReadPos()-1); break; } } return ret; }
/** * getStrElement * Get a token from the current buffer, stopping at the delimiter. Returns the token as a string * * @param delim The delimiter to stop at when retriving the element. By default, it's a space * @return Token found in the buffer. Empty if delimiter wasn't reached */ string HTTPMessage::getStrElement(char delim) { string ret = ""; int startPos = getReadPos(); unsigned int size = 0; int endPos = find(delim, startPos); // Calculate the size based on the found ending position size = (endPos+1) - startPos; if((endPos == -1) || (size <= 0)) return ""; // Grab the string from the byte buffer up to the delimiter char *str = new char[size]; getBytes((byte*)str, size); str[size-1] = 0x00; // NULL termination ret.assign(str); // Increment the read position PAST the delimiter setReadPos(endPos+1); return ret; }