bool ReadTask::fillReadBuffer() { char buffer[100000]; #if defined(_MSC_VER) int nr = recv(readSocket, buffer, sizeof(buffer), 0); #else #if defined(__APPLE__) || defined(macintosh) int nr = recv(readSocket, buffer, sizeof(buffer), SO_NOSIGPIPE); #else int nr = recv(readSocket, buffer, sizeof(buffer), MSG_NOSIGNAL); #endif #endif if (nr > 0) { readBuffer.appendText(buffer, nr); } else if (nr == 0 || errno_socket == ECONNRESET_SOCKET) { return false; } else if (errno_socket == EINTR_SOCKET) { return fillReadBuffer(); } else if (errno_socket != EWOULDBLOCK_SOCKET) { Logger::debug << "read failed in " << __FUNCTION__ << "(" << __FILE__ << "@" << __LINE__ << ")" << " with " << errno_socket << " (" << strerror_socket(errno_socket) << ")" << endl; return false; } return true; }
inline void splitFile() { while (1) { int nextNlPos = getNextNlPos(); char* offset = (char *) (_rb + _rbBeginPos); if (nextNlPos != -1) { /* We have found a newline. */ int count = nextNlPos - _rbBeginPos + 1; fillChunk(offset, count, _outputFileIndex, 1); _newlinesFound++; _outputFileIndex = (_newlinesFound / _chunkSize) % _numOutputFiles; _rbBeginPos = nextNlPos + 1; } else { if (_rbBeginPos <= _rbEndPos) { /* We have a line fragment left in the buffer. */ int count = _rbEndPos - _rbBeginPos + 1; fillChunk(offset, count, _outputFileIndex, 0); } if (!fillReadBuffer()) { /* End of the file. Drain the chunk buffers and finish up. */ writeData(); break; } } } }
bool HttpServerTask::handleRead() { bool res = fillReadBuffer(); if (!res) { return false; } return processRead(); }
inline void skipLines() { while (_linesSkipped < _numLinesToSkip) { int nextNlPos = getNextNlPos(); if (nextNlPos != -1) { _linesSkipped++; _rbBeginPos = nextNlPos + 1; } else { if (!fillReadBuffer()) { fprintf(stdout, "WARNING: All lines in the file have been skipped.\n"); break; } } } }
bool Worker::readLine(string& line, time_t timeout) { while (true) { const char * ptr = readBuffer.c_str() + readPosition; const char * end = readBuffer.end(); while (ptr < end) { for (; ptr < end; ptr++) { if (*ptr == '\n') { break; } } if (ptr < end) { readPosition = ptr - readBuffer.c_str(); size_t lineLength = readPosition; if (lineLength > 0 && *(ptr - 1) == '\r') { lineLength--; } string result(readBuffer.c_str(), lineLength); Logger::trace << "got result '" << result << "' from worker" << endl; readBuffer.erase_front(readPosition + 1); readPosition = 0; line = result; return true; } } bool ok = fillReadBuffer(timeout); if (!ok) { return false; } } }
bool Worker::fillReadBuffer(time_t timeout) { char buffer[1024]; // with timeout if (timeout != 0) { #ifdef USE_POLL pollfd fds[1]; fds[0].fd = readPipe; fds[0].events = POLLRDNORM | POLLHUP; fds[0].revents = 0; int np = ::poll(fds, 1, timeout); if (np < 0) { Logger::trace << "terminating worker, poll failed" << endl; return false; } else if (np == 0) { return fillReadBuffer(timeout); } else if (fds[0].revents & POLLHUP) { Logger::trace << "terminating worker, poll returned POLLHUP" << endl; return false; } else if (!(fds[0].revents & POLLRDNORM)) { return fillReadBuffer(timeout); } #else fd_set readFds; FD_ZERO(&readFds); FD_SET(readPipe, &readFds); fd_set exceptFds; FD_ZERO(&exceptFds); FD_SET(readPipe, &exceptFds); timeval tv; tv.tv_sec = 0; tv.tv_usec = timeout * 1000; int np = (int) select((int) readPipe + 1, &readFds, 0, &exceptFds, &tv); if (np < 0) { Logger::trace << "terminating worker, select failed" << endl; return false; } else if (np == 0) { return fillReadBuffer(timeout); } else if (FD_ISSET(readPipe, &exceptFds)) { Logger::trace << "terminating worker, select returned exception" << endl; return false; } else if (! FD_ISSET(readPipe, &readFds)) { return fillReadBuffer(timeout); } #endif } // start reading int nr = read(readPipe, buffer, sizeof(buffer)); if (nr > 0) { readBuffer.appendText(buffer, nr); return true; } else if (nr == 0) { return false; } else if (errno_socket == EINTR_SOCKET) { return fillReadBuffer(timeout); } else if (errno_socket != EWOULDBLOCK_SOCKET) { Logger::debug << "read failed in " << __FUNCTION__ << "(" << __FILE__ << "@" << __LINE__ << ")" << " with " << errno_socket << " (" << strerror_socket(errno_socket) << ")" << endl; return false; } else { return fillReadBuffer(timeout); } }
int AudioPlaySdAac::play(void){ lastError = ERR_CODEC_NONE; initVars(); sd_buf = allocBuffer(AAC_SD_BUF_SIZE); if (!sd_buf) return ERR_CODEC_OUT_OF_MEMORY; aacobjptr = this; buf[0] = (short *) malloc(AAC_BUF_SIZE * sizeof(int16_t)); buf[1] = (short *) malloc(AAC_BUF_SIZE * sizeof(int16_t)); hAACDecoder = AACInitDecoder(); if (!buf[0] || !buf[1] || !hAACDecoder) { lastError = ERR_CODEC_OUT_OF_MEMORY; stop(); return lastError; } isRAW = true; duration = 0; sd_left = 0; sd_p = sd_buf; if (setupMp4()) { fseek(firstChunk); sd_left = 0; isRAW = false; //Serial.print("mp4"); } else { //NO MP4. Do we have an ID3TAG ? fseek(0); //Read-ahead 10 Bytes to detect ID3 sd_left = fread(sd_buf, 10); //Skip ID3, if existent uint32_t skip = skipID3(sd_buf); if (skip) { size_id3 = skip; int b = skip & 0xfffffe00; fseek(b); sd_left = 0; //Serial.print("ID3"); } else size_id3 = 0; } //Fill buffer from the beginning with fresh data sd_left = fillReadBuffer(file, sd_buf, sd_buf, sd_left, AAC_SD_BUF_SIZE); if (!sd_left) { lastError = ERR_CODEC_FILE_NOT_FOUND; stop(); return lastError; } _VectorsRam[IRQ_AUDIOCODEC + 16] = &decodeAac; initSwi(); decoded_length[0] = 0; decoded_length[1] = 0; decoding_state = 0; decoding_block = 0; for (int i=0; i< DECODE_NUM_STATES; i++) decodeAac(); if((aacFrameInfo.sampRateOut != AUDIOCODECS_SAMPLE_RATE ) || (aacFrameInfo.nChans > 2)) { //Serial.println("incompatible AAC file."); lastError = ERR_CODEC_FORMAT; stop(); return lastError; } decoding_block = 1; playing = codec_playing; #ifdef CODEC_DEBUG // Serial.printf("RAM: %d\r\n",ram-freeRam()); #endif return lastError; }