| 63 | } |
| 64 | |
| 65 | void |
| 66 | GenericDataWorker::onCommit(void) |
| 67 | { |
| 68 | if (!this->writerPrepared) { |
| 69 | // Silently ignore this buffer |
| 70 | this->instance->bufferReady = true; |
| 71 | } else if (!this->failed) { |
| 72 | QMutexLocker locker(&this->instance->dataMutex); |
| 73 | struct timeval tv, otv, sub; |
| 74 | ssize_t dumped; |
| 75 | size_t allocation = this->instance->allocation; |
| 76 | std::vector<uint8_t> *thisBuf = |
| 77 | &this->instance->buffers[1 - this->instance->buffer]; |
| 78 | uint8_t *buffer = thisBuf->data(); |
| 79 | int remaining = static_cast<int>(this->instance->commitedSize); |
| 80 | |
| 81 | locker.unlock(); |
| 82 | |
| 83 | gettimeofday(&otv, nullptr); |
| 84 | |
| 85 | while (remaining > 0) { |
| 86 | dumped = this->instance->writer->write( |
| 87 | buffer, |
| 88 | static_cast<unsigned>(remaining)); |
| 89 | |
| 90 | if (dumped < 1) { |
| 91 | this->failed = true; |
| 92 | emit error(QString::fromStdString(this->instance->writer->getError())); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | remaining -= dumped; |
| 97 | buffer += dumped; |
| 98 | } |
| 99 | |
| 100 | gettimeofday(&tv, nullptr); |
| 101 | |
| 102 | // Requested allocation does not match buffer size. |
| 103 | if (thisBuf->size() != allocation) { |
| 104 | try { |
| 105 | thisBuf->resize(allocation); |
| 106 | } catch (std::exception &) { |
| 107 | this->failed = true; |
| 108 | emit error("Memory allocation error"); |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | this->instance->bufferReady = true; |
| 114 | timersub(&tv, &otv, &sub); |
| 115 | |
| 116 | emit writeFinished(static_cast<quint64>( |
| 117 | sub.tv_usec + sub.tv_sec * 1000000l)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | GenericDataSaver::GenericDataSaver( |
| 122 | GenericDataWriter *writer, |