Sane C++ Libraries: SC::AsyncFileRead Struct Reference (original) (raw)

Starts a file read operation, reading bytes from a file (or pipe).

Callback will be called when the data read from the file (or pipe) is available.
Call AsyncRequest::executeOn to set a thread pool if this is a buffered file and not a pipe. This is important on APIs with blocking behaviour on buffered file I/O (all apis with the exception of io_uring).

File library can be used to open the file and obtain a file (or pipe) descriptor handle.

Note

Pipes or files opened using Posix O_DIRECT or Windows FILE_FLAG_WRITE_THROUGH & FILE_FLAG_NO_BUFFERING should instead avoid using the Task parameter for best performance.

When not using the Task remember to:

Additional notes:

FileDescriptor fd;

FileOpen openMode;

openMode.blocking = true;

SC_TRY(fd.open("MyFile.txt", openMode));

AsyncFileRead asyncReadFile;

asyncReadFile.callback = [&](AsyncFileRead::Result& res)

{

Span readData;

if(res.get(readData))

{

if(res.completionData.endOfFile)

{

console.print("End of file reached");

}

else

{

console.print("Read {} bytes from file", readData.sizeInBytes());

res.getAsync().setOffset(res.getAsync().getOffset() + readData.sizeInBytes());

res.reactivateRequest(true);

}

}

else

{

}

};

char buffer[100] = {0};

asyncReadFile.buffer = {buffer, sizeof(buffer)};

AsyncTaskSequence asyncFileTask;

SC_TRY(asyncReadFile.executeOn(asyncFileTask, threadPool));

SC_TRY(asyncReadFile.start(eventLoop));