資源簡介
iocp 作為windows提供一種告訴I/O機制,應用的很廣泛. 本代碼就是簡單實現如何使用iocp機制來進行文件傳輸

代碼片段和文件信息
/*++
THIS?CODE?AND?INFORMATION?IS?PROVIDED?“AS?IS“?WITHOUT?WARRANTY?OF
ANY?KIND?EITHER?EXPRESSED?OR?IMPLIED?INCLUDING?BUT?NOT?LIMITED
TO?THE?IMPLIED?WARRANTIES?OF?MERCHANTABILITY?AND/OR?FITNESS?FOR?A
PARTICULAR?PURPOSE.
Copyright?(C)?1994?-?2000.??Microsoft?Corporation.??All?rights?reserved.
Module?Name:
????unbufcp2.c
Abstract:
????Intended?to?demonstrate?how?to?complete?I/O?in?a?different?thread
????asynchronously?than?the?I/O?was?started?from.??This?is?useful?for
????people?who?want?a?more?powerful?mechanism?of?asynchronous?completion
????callbacks?than?Win32?provides?(i.e.?VMS?developers?who?are?used?to?ASTs).
????Two?threads?are?used.?The?first?thread?posts?overlapped?reads?from?the
????source?file.?These?reads?complete?to?an?I/O?completion?port?the?second
????thread?is?waiting?on.?The?second?thread?sees?the?I/O?completion?and
????posts?an?overlapped?write?to?the?destination?file.?The?write?completes
????to?another?I/O?completion?port?that?the?first?thread?is?waiting?on.
????The?first?thread?sees?the?I/O?completion?and?posts?another?overlapped
????read.
????Thread?1????????????????????????????????????????Thread?2
???????|???????????????????????????????????????????????|
???????|???????????????????????????????????????????????|
????kick?off?a?few???????????????????????????-->GetQueuedCompletionStatus(ReadPort)
????overlapped?reads?????????????????????????|?????????|
???????|?????????????????????????????????????|?????????|
???????|?????????????????????????????????????|??read?has?completed
??->GetQueuedCompletionStatus(WritePort)?????|??kick?off?the?corresponding
??|????|?????????????????????????????????????|??write.
??|?write?has?completed?????????????????????|?????????|
??|?kick?off?another?????????????????????????|_________|
??|?read
??|????|
??|____|
--*/
#ifdef?_IA64_
#pragma?warning(disable:4100?4127)
#endif
#include?
#include?
#include?
#include?
//
//?File?handles?for?the?copy?operation.?All?read?operations?are
//?from?SourceFile.?All?write?operations?are?to?DestFile.
//
HANDLE?SourceFile;
HANDLE?DestFile;
//
//?I/O?completion?ports.?All?reads?from?the?source?file?complete
//?to?ReadPort.?All?writes?to?the?destination?file?complete?to
//?WritePort.
//
HANDLE?ReadPort;
HANDLE?WritePort;
//
//version?information
//
OSVERSIONINFO?????ver;
//
//?Structure?used?to?track?each?outstanding?I/O.?The?maximum
//?number?of?I/Os?that?will?be?outstanding?at?any?time?is
//?controllable?by?the?MAX_CONCURRENT_IO?definition.
//
#define?MAX_CONCURRENT_IO?20
typedef?struct?_COPY_CHUNK?{
????OVERLAPPED?Overlapped;
????LPVOID?Buffer;
}?COPY_CHUNK?*PCOPY_CHUNK;
COPY_CHUNK?CopyChunk[MAX_CONCURRENT_IO];
//
//?Define?the?size?of?the?buffers?used?to?do?the?I/O.
//?64K?is?a?nice?number.
//
#define?BUFFER_SIZE?(64*1024)
//
//?The?system‘s?page?size?will?always?be?a?multiple?of?the
//?sector?size.?Do?all?I/Os?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????2958336??2011-10-19?13:56??iocp_file\Debug\iocp_file.bsc
?????文件?????196667??2011-10-19?13:59??iocp_file\Debug\iocp_file.exe
?????文件?????211592??2011-10-19?13:59??iocp_file\Debug\iocp_file.ilk
?????文件??????26702??2011-10-19?13:58??iocp_file\Debug\iocp_file.obj
?????文件????5669892??2011-10-19?13:05??iocp_file\Debug\iocp_file.pch
?????文件?????467968??2011-10-19?13:59??iocp_file\Debug\iocp_file.pdb
?????文件????1508818??2011-10-19?13:58??iocp_file\Debug\iocp_file.sbr
?????文件??????41984??2011-10-19?13:58??iocp_file\Debug\vc60.idb
?????文件??????69632??2011-10-19?13:56??iocp_file\Debug\vc60.pdb
?????文件??????19117??2011-10-19?13:59??iocp_file\iocp_file.c
?????文件???????4322??2011-10-19?14:11??iocp_file\iocp_file.dsp
?????文件????????541??2011-10-19?11:23??iocp_file\iocp_file.dsw
?????文件??????33792??2011-10-19?14:11??iocp_file\iocp_file.ncb
?????文件??????49664??2011-10-19?14:11??iocp_file\iocp_file.opt
?????文件???????1447??2011-10-19?13:56??iocp_file\iocp_file.plg
?????目錄??????????0??2011-10-19?13:58??iocp_file\Debug
?????目錄??????????0??2011-10-19?14:11??iocp_file
-----------?---------??----------?-----??----
?????????????11260474????????????????????17
評論
共有 條評論