193 lines
4.1 KiB
C++
193 lines
4.1 KiB
C++
#include "StdAfx.h"
|
|
#include "FileTransferClient.h"
|
|
#include "Csock_cl.h"
|
|
//***********************************************************************************
|
|
FileTransferClient::FileTransferClient(void)
|
|
{
|
|
sc = NULL;
|
|
lisener =0;
|
|
}
|
|
//***********************************************************************************
|
|
|
|
FileTransferClient::~FileTransferClient(void)
|
|
{
|
|
if(sc)
|
|
delete sc;
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::conecta( char* ip, int puerto )
|
|
{
|
|
if(sc)
|
|
delete sc;
|
|
sc = new Csock_cl();
|
|
if( sc->conectar(ip,puerto))
|
|
return false;
|
|
return enviaOk();
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::Presenta( char *user, char *key )
|
|
{
|
|
if(!sc)
|
|
return false;
|
|
int *id;
|
|
id =(int*)buff;
|
|
Usuario_conx_0 *usu = (Usuario_conx_0*)&((int*)buff)[1];
|
|
strcpy(usu->nombre,user);
|
|
strcpy(usu->clave,key);
|
|
|
|
*id=PETICION_GENERAL_PRESENTA;
|
|
if(!sc->envia_package((BYTE*)buff, sizeof(int)+ sizeof(Usuario_conx_0)))
|
|
return false;
|
|
int nv =10;
|
|
|
|
while(!sc->recibe_package(0))
|
|
{
|
|
nv--;
|
|
if(nv<0)
|
|
return false;
|
|
}
|
|
|
|
return (*(int*)sc->buf)== PETICION_GENERAL_OK;
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::DescargaFile( char *pathOrig, char *pathDest )
|
|
{
|
|
porcentOld =0;
|
|
if(lisener)
|
|
lisener->setStatus(0);
|
|
if(!sc)
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
int *id;
|
|
id =(int*)buff;
|
|
char *str = (char*)&((int*)buff)[1];
|
|
if(!sc)
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
if(!dt.setData(sc,&f) || !dt.setMode(DataTransfer::MODE_RECEIVE))
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
if(!f.abre(pathDest,2,TRUE))
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
strcpy(str,pathOrig);
|
|
*id = PETICION_GENERAL_OK;
|
|
if(!sc->envia_package((BYTE*)buff, sizeof(int)+ 256))
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
|
|
if(!sc->recibe_package(0))
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
if(sc->nb<sizeof(int) || (*(int*)sc->buf)!= PETICION_GENERAL_OK)
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
if(!dt.start())
|
|
{
|
|
if(lisener)
|
|
lisener->finTransfer(false);
|
|
return false;
|
|
}
|
|
while(dt.currando())
|
|
{
|
|
if(lisener)
|
|
{
|
|
if(lisener->cancelTransfer())
|
|
{
|
|
dt.cancel();
|
|
continue;
|
|
}
|
|
|
|
double status= dt.getStatus();
|
|
//C_log::log("FileTransferClient","Porcent: %lf" status);
|
|
if(porcentOld<status)
|
|
{
|
|
porcentOld = status;
|
|
lisener->setStatus(porcentOld);
|
|
}
|
|
}
|
|
|
|
Sleep(10);
|
|
}
|
|
if(lisener)
|
|
lisener->finTransfer(!dt.isCanceled());
|
|
|
|
return !dt.isCanceled();
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::DescargaDir( char *dirOrig, char *dirDest )
|
|
{
|
|
if(!sc)
|
|
return false;
|
|
return false;
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::desconecta()
|
|
{
|
|
if(!sc)
|
|
return false;
|
|
int *id =(int*)buff;
|
|
*id = PETICION_GENERAL_FIN;
|
|
if(!sc->envia_package((BYTE*)buff, sizeof(int)))
|
|
return false;
|
|
|
|
delete sc;
|
|
sc = NULL;
|
|
return true;
|
|
}
|
|
//***********************************************************************************
|
|
|
|
bool FileTransferClient::enviaOk()
|
|
{
|
|
if(!sc)
|
|
return false;
|
|
int *id =(int*)buff;
|
|
*id = PETICION_GENERAL_NO;
|
|
if(!sc->envia_package((BYTE*)buff, sizeof(int)))
|
|
return false;
|
|
if(!sc->recibe_package(0))
|
|
return false;
|
|
return sc->nb==sizeof(int) &&(*(int*)sc->buf)== PETICION_GENERAL_OK;
|
|
}
|
|
//***********************************************************************************
|
|
double FileTransferClient::getPorcen()
|
|
{
|
|
if(dt.isCanceled() || !dt.currando())
|
|
return 100.;
|
|
return dt.getStatus();
|
|
|
|
}
|
|
//***********************************************************************************
|
|
void FileTransferClient::setLisener( FileTransferClientLisener*lis )
|
|
{
|
|
lisener = lis;
|
|
}
|
|
//***********************************************************************************
|
|
|