///////////////////////////////////////////////////////////////////////////// // Rev 11/12/2003 // // // AIOTEST.C // --------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // Open aiotest.c and copy it into aiotest.out, using the Aio*() api // Work under win32 (NT) and unix supporting the aio_*() api // // ///////////////////////////////////////////////////////////////////////////// #ifdef _WIN32 #include #else #include #endif #include #include #include "aioport.h" /* Rq In this example, we use 3 AioOperations, but **the copy could be done with only one AioOperation */ #define READ_INDEX 0 #define WRITE_INDEX 1 #define POST_INDEX 2 int main() { AIOHANDLE hReadFile, hWriteFile; AioOperation aAioOp[3]; char Buffer[10]; int Offset; /* Open the file for reading */ hReadFile = AioOpenFile( "./aiotest.c", AIO_FILE_READ_WRITE, 0 ); if( hReadFile == AIO_INVALID_HANDLE ) { printf( "AioOpenFile() : Unable to open the file (%d) (%s)\n", errno, strerror(errno) ); return 1; } /* Create the file for reading */ hWriteFile = AioOpenFile( "./aiotest.out", AIO_FILE_READ_WRITE | AIO_FILE_CREAT, 0644 ); if( hWriteFile == AIO_INVALID_HANDLE ) { printf( "AioOpenFile() : Unable to create the file (%d)\n", errno ); return 1; } /* Initialize a read operation */ Offset = 0; aAioOp[READ_INDEX].pUser = (void *)READ_INDEX; if( AioRead( hReadFile, Buffer, sizeof(Buffer), Offset, &aAioOp[READ_INDEX] ) ) { printf( "AioRead() error (%d)\n", errno ); return 1; } #if 0 /*Win32 test */ // LockFileEx() seems to work with GetQueuedCompletionStatus() SetLastError(0); memset( (char *)&aAioOp[WRITE_INDEX].Ovl, 0, sizeof( OVERLAPPED) ); if( LockFileEx( hWriteFile, LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK, 0, 0, 10, (LPOVERLAPPED)&aAioOp[WRITE_INDEX] ) == 0 ) { printf( "LockFileEx() error %d\n", GetLastError() ); } #endif /* Initialize pUser*/ aAioOp[WRITE_INDEX].pUser = (void *)WRITE_INDEX; /* Post a message */ aAioOp[POST_INDEX].pUser = (void *)POST_INDEX; if( AioPostCompletion( &aAioOp[POST_INDEX] ) ) { printf( "( AioPostCompletion() error (%d)\n", errno ); return 1; } /* Copy file */ for(;;) { AioOperation * paioOp; unsigned long dwBytes = 0; unsigned long dwMilliseconds = -1; int nResult; /* Waiting for one completion */ nResult = AioGetCompletion( &paioOp, &dwBytes, dwMilliseconds ); if( nResult == -1 ) { if( errno == EAGAIN ) { printf( "AioGetCompletion() : Timeout\n" ); break; } printf( "AioGetCompletion() : Error (%d)\n", errno ); break; } /* Testing the type */ if( (int)paioOp->pUser == READ_INDEX ) { /* End of file ? */ if( dwBytes == 0 ) { printf( "File copied\n" ); break; } /* Initialise next write, using the same buffer */ if( AioWrite( hWriteFile, Buffer, dwBytes, Offset, &aAioOp[WRITE_INDEX] ) ) { printf( "AioWrite() error (%d)\n", errno ); break; } } else if( (int)paioOp->pUser == WRITE_INDEX ) { /* Increment Offset */ Offset += dwBytes; /* Initialise next read, using the same buffer */ if( AioRead( hReadFile, Buffer, sizeof(Buffer), Offset, &aAioOp[READ_INDEX] ) ) { printf( "AioRead() error (%d)\n", errno ); break; } } else if( (int)paioOp->pUser == POST_INDEX ) { /* User message */ printf( "Receive user message\n" ); } } /* Close */ AioClose( hReadFile ); AioClose( hWriteFile ); printf( "Exiting ...\n" ); return 0; }