system/FileDescriptor: add method FullWrite()
This commit is contained in:
parent
540919f256
commit
ae23682372
@ -298,6 +298,24 @@ FileDescriptor::FullRead(void *_buffer, size_t length)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
FileDescriptor::FullWrite(const void *_buffer, size_t length)
|
||||||
|
{
|
||||||
|
const uint8_t *buffer = (const uint8_t *)_buffer;
|
||||||
|
|
||||||
|
while (length > 0) {
|
||||||
|
ssize_t nbytes = Write(buffer, length);
|
||||||
|
if (nbytes <= 0) {
|
||||||
|
if (nbytes < 0)
|
||||||
|
throw MakeErrno("Failed to write");
|
||||||
|
throw std::runtime_error("Failed to write");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer += nbytes;
|
||||||
|
length -= nbytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -237,6 +237,12 @@ public:
|
|||||||
return ::write(fd, buffer, length);
|
return ::write(fd, buffer, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write until all of the given buffer has been written.
|
||||||
|
* Throws on error.
|
||||||
|
*/
|
||||||
|
void FullWrite(const void *buffer, size_t length);
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
int Poll(short events, int timeout) const noexcept;
|
int Poll(short events, int timeout) const noexcept;
|
||||||
|
|
||||||
|
@ -70,34 +70,6 @@ ReadOrThrow(FileDescriptor fd, void *buffer, size_t size)
|
|||||||
return nbytes;
|
return nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
|
||||||
WriteOrThrow(FileDescriptor fd, const void *buffer, size_t size)
|
|
||||||
{
|
|
||||||
auto nbytes = fd.Write(buffer, size);
|
|
||||||
if (nbytes < 0)
|
|
||||||
throw MakeErrno("Write failed");
|
|
||||||
|
|
||||||
return nbytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
FullWrite(FileDescriptor fd, ConstBuffer<uint8_t> src)
|
|
||||||
{
|
|
||||||
while (!src.empty()) {
|
|
||||||
size_t nbytes = WriteOrThrow(fd, src.data, src.size);
|
|
||||||
if (nbytes == 0)
|
|
||||||
throw std::runtime_error("Write failed");
|
|
||||||
|
|
||||||
src.skip_front(nbytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
FullWrite(FileDescriptor fd, ConstBuffer<void> src)
|
|
||||||
{
|
|
||||||
FullWrite(fd, ConstBuffer<uint8_t>::FromVoid(src));
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
ReadFrames(FileDescriptor fd, void *_buffer, size_t size, size_t frame_size)
|
ReadFrames(FileDescriptor fd, void *_buffer, size_t size, size_t frame_size)
|
||||||
{
|
{
|
||||||
@ -166,14 +138,14 @@ try {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
|
auto dest = filter->FilterPCM({(const void *)buffer, (size_t)nbytes});
|
||||||
FullWrite(output_fd, dest);
|
output_fd.FullWrite(dest.data, dest.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto dest = filter->Flush();
|
auto dest = filter->Flush();
|
||||||
if (dest.IsNull())
|
if (dest.IsNull())
|
||||||
break;
|
break;
|
||||||
FullWrite(output_fd, dest);
|
output_fd.FullWrite(dest.data, dest.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cleanup and exit */
|
/* cleanup and exit */
|
||||||
|
Loading…
Reference in New Issue
Block a user