\label{api_pull_md_doc_pull}%
\Hypertarget{api_pull_md_doc_pull}%
 As an alternative to \mbox{\hyperlink{api_streaming}{Streaming API}}, librsync provides a \char`\"{}pull-\/mode\char`\"{} callback interface where it will repeatedly call application-\/provided callbacks to get more input data and to accept output data.

Pull jobs are also created using \mbox{\hyperlink{librsync_8h_a226f57577fcad2e4c97f3e4d612650d3}{rs\+\_\+sig\+\_\+begin()}}, rs\+\_\+loadsig\+\_\+begin,() \mbox{\hyperlink{delta_8c_a59fe8536218632417097d084606bb675}{rs\+\_\+delta\+\_\+begin()}}, \mbox{\hyperlink{librsync_8h_a15efa6e180d239fee2c195874618b0ea}{rs\+\_\+patch\+\_\+begin()}}.

However, rather than calling \mbox{\hyperlink{librsync_8h_aede8e0f42424b9aa29093f94b59ea029}{rs\+\_\+job\+\_\+iter()}}, the application should then call \mbox{\hyperlink{librsync_8h_a85d48e8216b848b9757a076f829ac4a8}{rs\+\_\+job\+\_\+drive()}}, passing an input and an output callback. \mbox{\hyperlink{librsync_8h_a85d48e8216b848b9757a076f829ac4a8}{rs\+\_\+job\+\_\+drive()}} takes an opaque pointer for both the input and output callback\+: this could be a {\ttfamily FILE$\ast$} or some similar object telling them what to read and write.\hypertarget{api_pull_autotoc_md67}{}\doxysubsection{Non-\/blocking IO}\label{api_pull_autotoc_md67}
The librsync interface allows non-\/blocking streaming processing of data. This means that the library will accept input and produce output when it suits the application. If nonblocking file IO is used and the IO callbacks support it, then librsync will never block waiting for IO.

Normally callbacks will read/write the whole buffer when they\textquotesingle{}re called, but in some cases they might not be able to process all of it, or perhaps not process any at all. This might happen if the callbacks are connected to a nonblocking socket. Either of two things can happen in this case. If the callback returns \mbox{\hyperlink{librsync_8h_a7feb858ceba3b8f3cf048d49be108253af2d289bbc65678b4b00f56a2e6632957}{RS\+\_\+\+BLOCKED}}, then \mbox{\hyperlink{librsync_8h_aede8e0f42424b9aa29093f94b59ea029}{rs\+\_\+job\+\_\+iter()}} will also return \mbox{\hyperlink{librsync_8h_a7feb858ceba3b8f3cf048d49be108253af2d289bbc65678b4b00f56a2e6632957}{RS\+\_\+\+BLOCKED}} shortly.

When an IO callback blocks, it is the responsibility of the application to work out when it will be able to make progress and therefore when it is worth calling \mbox{\hyperlink{librsync_8h_aede8e0f42424b9aa29093f94b59ea029}{rs\+\_\+job\+\_\+iter()}} again. Typically this involves a mechanism like {\ttfamily poll} or {\ttfamily select} to wait for the file descriptor to be ready.\hypertarget{api_pull_autotoc_md68}{}\doxysubsection{Blocking IO}\label{api_pull_autotoc_md68}
The IO callbacks are allowed to block. This will of course mean that the application\textquotesingle{}s call to \mbox{\hyperlink{librsync_8h_a85d48e8216b848b9757a076f829ac4a8}{rs\+\_\+job\+\_\+drive()}} will also block.\hypertarget{api_pull_autotoc_md69}{}\doxysubsection{Partial completion}\label{api_pull_autotoc_md69}
IO callbacks are also allowed to process or provide only part of the requested data, as will commonly happen with socket IO.

The library might not get as much input as it wanted when it is first called. If it gets a partial read, it needs to hold onto that valuable and irreplaceable data.

It cannot keep it on the stack, because it will be lost if the read blocks. It needs to be kept in the job structure, or in somewhere referenced from there.

The state function probably cannot proceed until it has all the needed input. So possibly this can be expressed at a high level of the job structure. Or perhaps it should just be done by each particular state function.

When the library has output to write out, the callback might not be able to accept all of it at the time it is called. Deferred outgoing data needs to be stored in a buffer referenced from the job structure.

I think it\textquotesingle{}s always OK to try to flush this when entering rs\+\_\+job\+\_\+iter. I think it\textquotesingle{}s OK to not do anything else until all the outgoing data has been flushed.

In many cases we would like to pass a pointer into the input (or pread) buffer straight to the output callback. In other cases, we need a different buffer to build up literal outgoing data.

librsync deals with short, bounded-\/size headers and checksums, and with arbitrarily-\/large streaming data. Although the commands are of bounded size, they are not of fixed size, because there are different encodings to suit different situations.

The situation is very similar to fetching variable-\/length headers from a socket. We cannot read the whole command in a single input, because we don\textquotesingle{}t know how long it is. As a general principle I think we should {\itshape not} read in too much data and buffer it, because this complicates things. Therefore we need to read the type byte first, and then possibly read some parameters. 