bool NetGameConnectionImpl::read_connection_data(DataBuffer &receive_buffer, int &bytes_received) { while (true) { int bytes = connection->read(receive_buffer.data() + bytes_received, receive_buffer.size() - bytes_received); if (bytes < 0) return false; if (bytes == 0) { connection->close(); return true; } bytes_received += bytes; int bytes_consumed = 0; bool exit = read_data(receive_buffer.data(), bytes_received, bytes_consumed); if (bytes_consumed >= 0) { memmove(receive_buffer.data(), receive_buffer.data() + bytes_consumed, bytes_received - bytes_consumed); bytes_received -= bytes_consumed; } if (exit) return exit; } }
void ErrorRequestProcessor::process(HttpRequest& req, DataBuffer<uint8_t>& buffer) { DataBuffer<char> content; HttpStatus status; if (code_ == EACCES) { status = FORBIDDEN; const std::string path = std::string("../html/403.html"); FilePro::File file(path, path); file.content(content); } else if (code_ == ENOENT) { status = NOT_FOUND; const std::string path = std::string("../html/404.html"); FilePro::File file(path, path); file.content(content); } else { status = INTERNAL_ERROR; const std::string path = std::string("../html/500.html"); FilePro::File file(path, path); file.content(content); } makeHttpResponse(req, content.data(), content.size(), buffer, status); (void)req; }
bool NetGameConnectionImpl::write_connection_data(DataBuffer &send_buffer, int &bytes_sent, bool &send_graceful_close) { while (true) { int bytes = connection->write(send_buffer.data() + bytes_sent, send_buffer.size() - bytes_sent); if (bytes < 0) return false; bytes_sent += bytes; if (bytes_sent == send_buffer.size()) { if (send_graceful_close) { connection->close(); return true; } else { bytes_sent = 0; send_buffer.set_size(0); send_graceful_close = write_data(send_buffer); if (send_buffer.size() == 0) return false; } } } }
bool StreamSoundSource::fillBufferAndQueue(uint buffer) { if(m_waitingFile) return false; // fill buffer static DataBuffer<char> bufferData(2*STREAM_FRAGMENT_SIZE); ALenum format = m_soundFile->getSampleFormat(); int maxRead = STREAM_FRAGMENT_SIZE; if(m_downMix != NoDownMix) maxRead *= 2; int bytesRead = 0; do { bytesRead += m_soundFile->read(&bufferData[bytesRead], maxRead - bytesRead); // end of sound file if(bytesRead < maxRead) { if(m_looping) m_soundFile->reset(); else { m_eof = true; break; } } } while(bytesRead < maxRead); if(bytesRead > 0) { if(m_downMix != NoDownMix) { if(format == AL_FORMAT_STEREO16) { assert(bytesRead % 2 == 0); bytesRead /= 2; uint16_t *data = (uint16_t*)bufferData.data(); for(int i=0;i<bytesRead/2;i++) data[i] = data[2*i + (m_downMix == DownMixLeft ? 0 : 1)]; format = AL_FORMAT_MONO16; } } alBufferData(buffer, format, &bufferData[0], bytesRead, m_soundFile->getRate()); ALenum err = alGetError(); if(err != AL_NO_ERROR) g_logger.error(stdext::format("unable to refill audio buffer for '%s': %s", m_soundFile->getName(), alGetString(err))); alSourceQueueBuffers(m_sourceId, 1, &buffer); err = alGetError(); if(err != AL_NO_ERROR) g_logger.error(stdext::format("unable to queue audio buffer for '%s': %s", m_soundFile->getName(), alGetString(err))); } // return false if there aren't more buffers to fill return (bytesRead >= STREAM_FRAGMENT_SIZE && !m_eof); }
void Message::setData(DataBuffer db) { //kill the previous buffer buffer.clear(); buffer.resize(db.size()); //copy the data buffer buffer.write(db.data(), db.size()); msgHasReadLength = true; msgLength = buffer.size(); }
bool NetGameConnectionImpl::write_data(DataBuffer &buffer) { std::unique_lock<std::mutex> mutex_lock(mutex); std::vector<Message> new_send_queue; send_queue.swap(new_send_queue); mutex_lock.unlock(); for (auto & elem : new_send_queue) { if (elem.type == Message::type_message) { DataBufferPtr packet = NetGameNetworkData::send_data(elem.event); int pos = buffer.size(); buffer.set_size(pos + packet->size()); memcpy(buffer.data() + pos, packet->data(), packet->size()); } else if (elem.type == Message::type_disconnect) { return true; } } return false; }