Reading or Writing

Microsoft SQL Server Virtual Backup

Microsoft SQL Server Virtual Backup Device Specification

Reading or Writing

With pipes, the application will loop:

while(1) {
   if( readingPipe)
       ReadFile(h, buf, maxBytes, &bytesRead)
   else
       WriteFile(h, buf, numBytes, &bytesWritten)

   switch (error) {
case ERROR_BROKEN_PIPE:
   // the server closed its end, so we're done
   goto exit;  // break while loop

   case ERROR_MORE_DATA
   // more data to read; a normal situation
   break;

   default:
   // unexpected error
   // break out of while loop
   goto exit;
}

// deal with buffer, either writing it somewhere,
// or read the next chunk from somewhere
}
exit: // proceed with termination

With the VDI:

while(1) {
   errCode = vds->GetCommand( &cmd )
   switch (errCode) {
      case NOERROR:  // we got a command
         break;

      case VD_E_CLOSE:   // time to close
         // break out of loop
         goto exit;

      default: // unexpected
         goto exit;
   }

   compCode = ERROR_SUCCESS;
   bytesTransferred = 0;
      
   switch (cmd.commandCode) {
      case Read:
         // read bytes into cmd.Buffer
         break;

      case Write:
         // write bytes from cmd.Buffer
         break;


      case Flush:
         // flush the real output device
         break;

      case ClearError:
      // simply acknowledging the command
         // is sufficient
         break;

      default:
         // unexpected, so abort
         vds->SignalAbort();
         goto exit;
   }

   vds->CompleteCommand (cmd, compCode, bytesTransferred, 0 );
}
exit:
// continue with termination

The main phase of processing is slightly more complicated with the VDI than with pipes. No complex asynchronous I/O processing is required. A simple fetch, execute, and complete loop is sufficient. Higher performance is possible by exploiting an asynchronous model.