Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::AsyncFileSend Struct Reference

Sends file contents to a socket using zero-copy when available (sendfile, TransmitFile). More...

#include <Async.h>

Inheritance diagram for SC::AsyncFileSend:
SC::AsyncRequest

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
AsyncRequestnext = nullptr
 
AsyncRequestprev = 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)
 
AsyncTaskSequencegetTask ()
 
- Protected Attributes inherited from SC::AsyncRequest
AsyncSequencesequence = nullptr
 

Detailed Description

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.

Note
On epoll/macOS/Windows this runs synchronously when scheduled and can optionally use a thread pool for better concurrency.
On io_uring this uses native async sendfile (IORING_OP_SEND_ZC when available).

File Opening Requirements

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.

Member Function Documentation

◆ start() [1/2]

Result SC::AsyncRequest::start ( AsyncEventLoop & eventLoop)

Shortcut for AsyncEventLoop::start.

◆ start() [2/2]

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.

Parameters
eventLoopThe event loop to use
fileThe 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.
socketThe socket descriptor (must be connected)
offsetFile offset to start reading from (0 for beginning)
lengthNumber of bytes to send (0 means send until EOF)
pipeSizeOptional size of the splice pipe (0 means default)

Member Data Documentation

◆ bytesSent

size_t SC::AsyncFileSend::bytesSent = 0

Internal progress tracking.

◆ callback

Function<void(Result&)> SC::AsyncFileSend::callback

Called when send completes or fails.


The documentation for this struct was generated from the following file: