Sends file contents to a socket using zero-copy when available (sendfile, TransmitFile). More...
#include <Async.h>
Classes | |
| struct | CompletionData |
| struct | Result |
Public Member Functions | |
| SC::Result | start (AsyncEventLoop &eventLoop, const FileDescriptor &file, const SocketDescriptor &socket, int64_t offset=0, size_t length=0, size_t pipeSize=0) |
| Start the file send operation. | |
| Result | start (AsyncEventLoop &eventLoop) |
| Shortcut for AsyncEventLoop::start. | |
Public Member Functions inherited from SC::AsyncRequest | |
| void | setDebugName (const char *newDebugName) |
| void | executeOn (AsyncSequence &sequence) |
| Adds the request to be executed on a specific AsyncSequence. | |
| Result | executeOn (AsyncTaskSequence &task, ThreadPool &pool) |
| Adds the request to be executed on a specific AsyncTaskSequence. | |
| void | disableThreadPool () |
| Disables the thread-pool usage for this request. | |
| AsyncRequest (Type type) | |
| Constructs a free async request of given type. | |
| Result | stop (AsyncEventLoop &eventLoop, Function< void(AsyncResult &)> *afterStopped=nullptr) |
| Ask to stop current async operation. | |
| bool | isFree () const |
Returns true if this request is free. | |
| bool | isCancelling () const |
Returns true if this request is being cancelled. | |
| bool | isActive () const |
Returns true if this request is active or being reactivated. | |
| Type | getType () const |
| Returns request type. | |
| Result | start (AsyncEventLoop &eventLoop) |
| Shortcut for AsyncEventLoop::start. | |
| void | setUserFlags (uint16_t externalFlags) |
| Sets user flags, holding some meaningful data for the caller. | |
| uint16_t | getUserFlags () const |
| Gets user flags, holding some meaningful data for the caller. | |
| Function< void(AsyncResult &)> * | getCloseCallback () |
| Returns currently set close callback (if any) passed to AsyncRequest::stop. | |
| const Function< void(AsyncResult &)> * | getCloseCallback () const |
Public Attributes | |
| Function< void(Result &)> | callback |
| Called when send completes or fails. | |
| FileDescriptor::Handle | fileHandle = FileDescriptor::Invalid |
| SocketDescriptor::Handle | socketHandle = SocketDescriptor::Invalid |
| int64_t | offset = 0 |
| size_t | length = 0 |
| size_t | bytesSent = 0 |
| Internal progress tracking. | |
Public Attributes inherited from SC::AsyncRequest | |
| AsyncRequest * | next = nullptr |
| AsyncRequest * | prev = nullptr |
Friends | |
| struct | AsyncEventLoop |
Additional Inherited Members | |
Public Types inherited from SC::AsyncRequest | |
| enum class | Type : uint8_t { LoopTimeout , LoopWakeUp , LoopWork , ProcessExit , SocketAccept , SocketConnect , SocketSend , SocketSendTo , SocketReceive , SocketReceiveFrom , FileRead , FileWrite , FileSend , FilePoll , FileSystemOperation } |
| Type of async request. More... | |
Protected Member Functions inherited from SC::AsyncRequest | |
| Result | checkState () |
| void | queueSubmission (AsyncEventLoop &eventLoop) |
| AsyncTaskSequence * | getTask () |
Protected Attributes inherited from SC::AsyncRequest | |
| AsyncSequence * | sequence = nullptr |
Sends file contents to a socket using zero-copy when available (sendfile, TransmitFile).
Falls back to read/write loop on platforms or configurations where zero-copy is unavailable. Callback will be called when the file has been sent or an error occurred.
Windows: Files must be opened with FileOpen::blocking = true because the Windows implementation uses synchronous ReadFile/WSASend internally. Opening files with blocking = false (which adds FILE_FLAG_OVERLAPPED) will cause ReadFile to fail or return 0 bytes. The async aspect comes from being scheduled within the event loop, not from overlapped I/O.
Posix (macOS/Linux with epoll): Files should be opened with FileOpen::blocking = false to enable non-blocking sendfile. If opened with blocking = true, sendfile will block the event loop thread until completion.
Linux with io_uring: Files can be opened with any mode (blocking or non-blocking). The implementation uses IORING_OP_SPLICE for efficient zero-copy transfer. Pipe used for splice is automatically created as non-blocking.
| Result SC::AsyncRequest::start | ( | AsyncEventLoop & | eventLoop | ) |
Shortcut for AsyncEventLoop::start.
| SC::Result SC::AsyncFileSend::start | ( | AsyncEventLoop & | eventLoop, |
| const FileDescriptor & | file, | ||
| const SocketDescriptor & | socket, | ||
| int64_t | offset = 0, | ||
| size_t | length = 0, | ||
| size_t | pipeSize = 0 ) |
Start the file send operation.
| eventLoop | The event loop to use |
| file | The file descriptor (must be open for reading). For true async I/O (without thread pool), open with FileOpen::blocking = false. When using thread pool, either blocking mode works. |
| socket | The socket descriptor (must be connected) |
| offset | File offset to start reading from (0 for beginning) |
| length | Number of bytes to send (0 means send until EOF) |
| pipeSize | Optional size of the splice pipe (0 means default) |
| size_t SC::AsyncFileSend::bytesSent = 0 |
Internal progress tracking.