1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
//the header file
class AsynFileCopy
{
struct m_ov{
OVERLAPPED ov;
AsynFileCopy* maddr;
} *Rov,* Wov;
HANDLE fin;
HANDLE fout;
unsigned long Rstart;
unsigned long Wstart;
unsigned long fileSize;
bool done;
unsigned char* buf;
int bufMax;
unsigned long t1;
....
public:
AsynFileCopy(HANDLE in,HANDLE ou ,unsigned long s1,unsigned long s2,unsigned long ds):fin(in),fout(ou),Rstart(s1),Wstart(s2),fileSize(ds),done(false),Rov(0),Wov(0),buf(0),bufMax(4096){};
~AsynFileCopy(void);
unsigned long go();
bool isDone(){ return done;}
unsigned long runTime() {return t1;}
bool readBuf();
static void WINAPI readCompFuc(DWORD,DWORD, LPOVERLAPPED);
void writeBuf(DWORD);
static void WINAPI writeCompFuc(DWORD,DWORD, LPOVERLAPPED);
};
//cpp.file
void WINAPI AsynFileCopy::readCompFuc(DWORD dwErr,DWORD dwBytes, LPOVERLAPPED pOv){
m_ov* Ov1 = (m_ov*)pOv;
AsynFileCopy* me= Ov1->maddr;
if(dwErr ==0){
me->writeBuf(dwBytes);
}
else if(dwErr == ERROR_HANDLE_EOF)
me->done= true;
else
std::cout <<"Error in Read Completion!\n";
}
void WINAPI AsynFileCopy::writeCompFuc(DWORD dwErr,DWORD dwBytes, LPOVERLAPPED pOv){
m_ov* Ov1= (m_ov*)pOv;
AsynFileCopy* me= Ov1->maddr;
if(dwErr ==0){
Ov1->ov.Offset += me->bufMax;
me->readBuf();
}
else if(dwErr == ERROR_HANDLE_EOF)
me->done= true;
else
std::cout <<"Error in Write Completion!\n";
}
..........
void AsynFileCopy::writeBuf(DWORD dwBytes){
.........
BOOL wret=WriteFileEx(fout,buf,dwBytes,(LPOVERLAPPED)Wov,writeCompFuc);
.........
}
bool AsynFileCopy::readBuf(){
.......
BOOL fRead = ReadFileEx(fin,buf,bufMax,(LPOVERLAPPED)Rov,readCompFuc);
.........
}
| |