void DataSocket::SetTransferType(int nType, BOOL bWaitForAccept) { m_nTransferType = nType; if (bWaitForAccept && !m_bConnected) { m_bInitialized = FALSE; return; } if (m_bConnected && m_nTransferType != -1) m_pConnectSocket->SendResponse("150 Connection accepted"); m_bInitialized = TRUE; switch(m_nTransferType) { case 0: // List Directory m_nStatus = XFERMODE_LIST; OnSend(0); break; case 1: // Send File if (PrepareSendFile(m_strData)) { m_nStatus = XFERMODE_SEND; m_bConnected = TRUE; OnSend(0); } else { Close(); } break; case 2: // Receive File if (PrepareReceiveFile(m_strData)) { m_nStatus = XFERMODE_RECEIVE; m_bConnected = TRUE; OnSend(0); } else { Close(); m_pConnectSocket->SendResponse("450 Can't access file."); // destroy this socket AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0); // upload failed ((ConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED); } break; default: m_bInitialized = FALSE; break; } }
void DataSocket::OnConnect(int nErrorCode) { if (nErrorCode) { m_nStatus = XFERMODE_ERROR; m_pConnectSocket->SendResponse("425 Can't open data connection."); // destroy this socket AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0); } else { switch (m_nTransferType) { case 0: // List Directory m_nStatus = XFERMODE_LIST; m_bConnected = TRUE; OnSend(0); break; case 1: // Send File if (PrepareSendFile(m_strData)) { m_nStatus = XFERMODE_SEND; m_bConnected = TRUE; } else { Close(); } break; case 2: // Receive File if (PrepareReceiveFile(m_strData)) { m_nStatus = XFERMODE_RECEIVE; m_bConnected = TRUE; } else { Close(); m_pConnectSocket->SendResponse("450 can't access file."); // destroy this socket AfxGetThread()->PostThreadMessage(WM_THREADMSG, 0, 0); // upload failed ((ConnectThread *)AfxGetThread())->UpdateStatistic(FTPSTAT_UPLOADFAILED); } break; } } CAsyncSocket::OnConnect(nErrorCode); }
/* * This function is called when the remote connection, whant to send a file. * */ void MyIOCP::PackageFileTransfer(CIOCPBuffer *pOverlapBuff,int nSize,ClientContext *pContext) { #ifdef TRANSFERFILEFUNCTIONALITY CString sFileName=""; UINT iMaxFileSize=0; BYTE dummy=0; if(pOverlapBuff->GetPackageInfo(dummy,iMaxFileSize,sFileName)) { // Get The Current Path name and application name. CString sFilePath=""; TCHAR drive[_MAX_DRIVE]; TCHAR dir[_MAX_DIR]; TCHAR fname[_MAX_FNAME]; TCHAR ext[_MAX_EXT]; CString strTemp; GetModuleFileName(NULL,strTemp.GetBuffer(MAX_PATH),MAX_PATH); strTemp.ReleaseBuffer(); #ifdef _UNICODE _wsplitpath_s(strTemp,drive, _MAX_DRIVE,dir,_MAX_DIR,fname,_MAX_FNAME,ext,_MAX_EXT); #else _splitpath( strTemp, drive, dir, fname, ext ); #endif sFilePath=drive; //Drive sFilePath+=dir; //dir sFilePath+=sFileName; //name.. TRACE("Incoming File >%s.\r\n",sFilePath); // Perpare for Receive if(PrepareReceiveFile(pContext->m_Socket,sFilePath,iMaxFileSize)) { // Send start file transfer.. CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_StartFileTransfer)) ASend(pContext,pOverlapBuff); } }else { // Abort Transfer. CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_AbortFileTransfer)) ASend(pContext,pOverlapBuff); } } // to be sure that pcontext Suddenly does not dissapear.. m_ContextMapLock.Lock(); pContext->m_ContextLock.Lock(); pContext->m_sReceived=sFilePath; // Update that we have data pContext->m_iNumberOfReceivedMsg++; pContext->m_ContextLock.Unlock(); m_ContextMapLock.Unlock(); // Update Statistics. m_StatusLock.Lock(); m_iNumberOfMsg++; m_StatusLock.Unlock(); } #endif }