Преглед изворни кода

-Handle the case where the only file being requested is a folder
-Set a network read timeout of 30 seconds, before we would sit there indefinitely trying to read the network, now we will timeout after 30 seconds

git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@756 595ec19a-5cb4-439b-94a8-42fb3063c22c

sabrogden пре 11 година
родитељ
комит
b64ec9590a
10 измењених фајлова са 172 додато и 40 уклоњено
  1. 11 1
      Client.cpp
  2. 1 0
      FileRecieve.cpp
  3. 4 4
      FileSend.cpp
  4. 7 0
      FileTransferProgressDlg.cpp
  5. 1 0
      FileTransferProgressDlg.h
  6. 10 0
      Options.cpp
  7. 3 0
      Options.h
  8. 125 35
      RecieveSocket.cpp
  9. 5 0
      RecieveSocket.h
  10. 5 0
      SendSocket.h

+ 11 - 1
Client.cpp

@@ -313,6 +313,7 @@ HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CStr
 		}
 
 		m_SendSocket.SetSocket(m_Connection);
+		m_SendSocket.SetProgressBar(pProgress);
 
 		if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
 			break;
@@ -326,6 +327,8 @@ HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CStr
 		if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
 			break;
 			
+		pProgress->SetMessage(StrF(_T("Requesting Files from %s (%s)"), csComputerName, csIP));
+
 		if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE)
 			break;
 
@@ -337,7 +340,14 @@ HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CStr
 		}
 		else if(lRet == FALSE)
 		{
-			csErrorString = _T("Error recieving files.");
+			if(pProgress != NULL && pProgress->Cancelled())
+			{
+				//Don't show an error message the user canceled things
+			}
+			else	
+			{
+				csErrorString = _T("Error recieving files.");
+			}
 		}
 
 	} while(false);

+ 1 - 0
FileRecieve.cpp

@@ -37,6 +37,7 @@ long CFileRecieve::RecieveFiles(SOCKET sock, CString csIP, CFileTransferProgress
 	m_pProgress = pProgress;
 	m_csRecievingFromIP = csIP;
 	m_Sock.SetSocket(sock);
+	m_Sock.SetProgressBar(pProgress);
 
 	while(true)
 	{

+ 4 - 4
FileSend.cpp

@@ -75,12 +75,12 @@ BOOL CFileSend::SendClientFiles(SOCKET sock, CClipList *pClipList)
 			{
 				SendFile(CopyFiles[nFile]);
 			}
-
-			if(m_Send.SendCSendData(Info, MyEnums::END))
-				bRet = TRUE;
 		}
 	}
-
+	
+	if(m_Send.SendCSendData(Info, MyEnums::END))
+			bRet = TRUE;
+	
 	return bRet;
 }
 

+ 7 - 0
FileTransferProgressDlg.cpp

@@ -74,6 +74,13 @@ void CFileTransferProgressDlg::SetMessage(CString &cs)
 	m_Message.SetWindowText(cs);
 }
 
+CString CFileTransferProgressDlg::GetMessage()
+{
+	CString cs;
+	m_Message.GetWindowText(cs);
+	return cs;
+}
+
 void CFileTransferProgressDlg::SetFileMessage(CString &cs)
 {
 	m_CopyingFile.SetWindowText(cs);

+ 1 - 0
FileTransferProgressDlg.h

@@ -29,6 +29,7 @@ public:
 	//}}AFX_DATA
 
 	void SetMessage(CString &cs);
+	CString GetMessage();
 	void SetFileMessage(CString &cs);
 
 	void SetNumFiles(int nFiles);

+ 10 - 0
Options.cpp

@@ -2172,4 +2172,14 @@ void CGetSetOptions::SetQRCodeBorderPixels(int val)
 int CGetSetOptions::GetQRCodeBorderPixels()
 {
 	return GetProfileLong(_T("QRCodeBorderPixels"), 30);
+}
+
+void CGetSetOptions::SetNetworkReadTimeoutMS(int val)
+{
+	SetProfileLong(_T("NetworkReadTimeoutMS"), val);
+}
+
+int CGetSetOptions::GetNetworkReadTimeoutMS()
+{
+	return GetProfileLong(_T("NetworkReadTimeoutMS"), 30000);
 }

+ 3 - 0
Options.h

@@ -467,6 +467,9 @@ public:
 	static BOOL GetRegExTextSearch();
 	static void SetRegExTextSearch(BOOL val);
 
+	static void SetNetworkReadTimeoutMS(int val);
+	static int GetNetworkReadTimeoutMS();
+
 
 };
 

+ 125 - 35
RecieveSocket.cpp

@@ -10,6 +10,7 @@ CRecieveSocket::CRecieveSocket(SOCKET sock)
 	m_pDataReturnedFromDecrypt = NULL;
 	m_Sock = sock;
 	m_pEncryptor = new CEncryption; //CreateEncryptionInterface("encryptdecrypt.dll");
+	m_pProgress = NULL;
 }
 
 CRecieveSocket::~CRecieveSocket()
@@ -53,51 +54,57 @@ LPVOID CRecieveSocket::ReceiveEncryptedData(long lInSize, long &lOutSize)
 	
 	if(pInput)
 	{
-		RecieveExactSize(pInput, lInSize);
-
 		int nOut = 0;
-		CStringA csPassword;
-		INT_PTR count = g_Opt.m_csNetworkPasswordArray.GetSize();
-		INT_PTR nIndex;
-		for(int i = -2; i < count; i++)
-		{
-			csPassword.Empty();
-			nIndex = i;
 
-			//First time through try the last index that was valid
-			if(i == -2)
+		if(RecieveExactSize(pInput, lInSize))
+		{
+			CStringA csPassword;
+			INT_PTR count = g_Opt.m_csNetworkPasswordArray.GetSize();
+			INT_PTR nIndex;
+			for(int i = -2; i < count; i++)
 			{
-				nIndex = theApp.m_lLastGoodIndexForNextworkPassword;
-				if(nIndex == -2)
-					continue;
-			}
+				csPassword.Empty();
+				nIndex = i;
 
-			if(nIndex == -1)
-			{
-				csPassword = g_Opt.m_csPassword;
-			}
-			else
-			{
-				if(nIndex >= 0 && nIndex < count)
+				//First time through try the last index that was valid
+				if(i == -2)
+				{
+					nIndex = theApp.m_lLastGoodIndexForNextworkPassword;
+					if(nIndex == -2)
+						continue;
+				}
+
+				if(nIndex == -1)
 				{
-					CTextConvert::ConvertToUTF8(g_Opt.m_csNetworkPasswordArray[nIndex], csPassword);
+					csPassword = g_Opt.m_csPassword;
 				}
 				else
 				{
-					continue;
+					if(nIndex >= 0 && nIndex < count)
+					{
+						CTextConvert::ConvertToUTF8(g_Opt.m_csNetworkPasswordArray[nIndex], csPassword);
+					}
+					else
+					{
+						continue;
+					}
 				}
-			}
 
-			if(m_pEncryptor->Decrypt((UCHAR*)pInput, lInSize, csPassword, pOutput, nOut) == FALSE)
-			{
-				LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to Decrypt data password = %s"), g_Opt.m_csPassword));
-			}
-			else
-			{
-				theApp.m_lLastGoodIndexForNextworkPassword = (long)nIndex;
-				break;
+				if(m_pEncryptor->Decrypt((UCHAR*)pInput, lInSize, csPassword, pOutput, nOut) == FALSE)
+				{
+					LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to Decrypt data password = %s"), g_Opt.m_csPassword));
+				}
+				else
+				{
+					theApp.m_lLastGoodIndexForNextworkPassword = (long)nIndex;
+					break;
+				}
 			}
 		}
+		else
+		{
+			LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: FAILED"), lInSize));
+		}
 
 		lOutSize = nOut;
 
@@ -115,6 +122,40 @@ LPVOID CRecieveSocket::ReceiveEncryptedData(long lInSize, long &lOutSize)
 	return pOutput;
 }
 
+int recv_to(int fd, char *buffer, int len, int flags, int to) 
+{
+	fd_set readset;
+	int result, iof = -1;
+	struct timeval tv;
+
+	// Initialize the set
+	FD_ZERO(&readset);
+	FD_SET(fd, &readset);
+
+	// Initialize time out struct
+	tv.tv_sec = 0;
+	tv.tv_usec = to * 1000;
+	// select()
+	result = select(fd+1, &readset, NULL, NULL, &tv);
+
+	// Check status
+	if (result < 0)
+		return -1;
+	else if (result > 0 && FD_ISSET(fd, &readset)) 
+	{
+		// Set non-blocking mode
+		//if ((iof = fcntl(fd, F_GETFL, 0)) != -1)
+		//	fcntl(fd, F_SETFL, iof | O_NONBLOCK);
+		// receive
+		result = recv(fd, buffer, len, flags);
+		// set as before
+		//if (iof != -1)
+		//	fcntl(fd, F_SETFL, iof);
+		return result;
+	}
+	return -2;
+}
+
 BOOL CRecieveSocket::RecieveExactSize(char *pData, long lSize)
 {
 	LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------ Start wanted size %d"), lSize));
@@ -123,10 +164,53 @@ BOOL CRecieveSocket::RecieveExactSize(char *pData, long lSize)
 
 	long lWanted = lSize;
 	long lOffset = 0;
+	CString originalText = _T("");
 
 	while(lWanted > 0)
-	{
-		lReceiveCount = recv(m_Sock, pData + lOffset, lWanted, 0);
+	{		
+		fd_set readset;
+		int res;
+
+		int timeoutMs = CGetSetOptions::GetNetworkReadTimeoutMS();
+		int loops100msEach = (timeoutMs/100);
+
+		for(int i = 0; i < loops100msEach; i++)
+		{
+			lReceiveCount = recv_to(m_Sock, pData + lOffset, lWanted, 0, 100);
+
+			if(lReceiveCount >= 0)
+			{
+				break;
+			}
+			else if(lReceiveCount == SOCKET_ERROR)
+			{
+				ASSERT(FALSE);
+				LogSendRecieveInfo(StrF(_T("RecieveExactSize:: Socket Error")));
+				return FALSE;
+			}
+
+			if(lReceiveCount == -2 && i > 15)
+			{
+				if(m_pProgress != NULL)
+				{
+					originalText = m_pProgress->GetMessage();
+					m_pProgress->SetMessage(StrF(_T("Requesting data from Server")));					
+					m_pProgress->PumpMessages();
+					if(m_pProgress->Cancelled())
+					{
+						return FALSE;
+					}
+				}
+			}
+		}
+
+		if(lReceiveCount == -2)
+		{
+			ASSERT(FALSE);
+			LogSendRecieveInfo(StrF(_T("RecieveExactSize:: Timeout waiting for server")));
+			return FALSE;
+		}
+
 		if(lReceiveCount == SOCKET_ERROR)
 		{
 			LogSendRecieveInfo("RecieveExactSize:: ********ERROR if(lReceiveCount == SOCKET_ERROR)*******");
@@ -138,6 +222,12 @@ BOOL CRecieveSocket::RecieveExactSize(char *pData, long lSize)
 			return FALSE;
 		}
 
+		if(m_pProgress != NULL &&
+			originalText != _T(""))
+		{
+			m_pProgress->SetMessage(originalText);					
+		}
+
 		lWanted -= lReceiveCount;
 		lOffset += lReceiveCount;
 

+ 5 - 0
RecieveSocket.h

@@ -3,6 +3,7 @@
 #include "Winsock2.h"
 #include "EncryptDecrypt\Encryption.h"
 #include "ServerDefines.h"
+#include "FileTransferProgressDlg.h"
 
 class CRecieveSocket
 {
@@ -20,8 +21,12 @@ public:
 	SOCKET	GetSocket()				{ return m_Sock;	}
 	void	SetSocket(SOCKET sock)	{ m_Sock = sock;	}
 
+	void SetProgressBar(CFileTransferProgressDlg *pDlg) { m_pProgress = pDlg; }
+
 protected:
 	CEncryption *m_pEncryptor;
 	SOCKET m_Sock;
 	UCHAR *m_pDataReturnedFromDecrypt;	
+
+	CFileTransferProgressDlg *m_pProgress;
 };

+ 5 - 0
SendSocket.h

@@ -11,6 +11,8 @@
 
 #include "EncryptDecrypt\Encryption.h"
 #include "ServerDefines.h"
+#include "FileTransferProgressDlg.h"
+
 
 class CSendSocket  
 {
@@ -19,6 +21,7 @@ public:
 	virtual ~CSendSocket();
 
 	void SetSocket(SOCKET sock)		{ m_Connection = sock;	}
+	void SetProgressBar(CFileTransferProgressDlg *pDlg) { m_pProgress = pDlg; }
 
 	CEncryption *m_pEncryptor;
 
@@ -27,6 +30,8 @@ public:
 
 protected:
 	SOCKET m_Connection;
+
+	CFileTransferProgressDlg *m_pProgress;
 };
 
 #endif // !defined(AFX_SENDSOCKET_H__4A4A5B96_8123_413C_8F86_A2875C9E5E48__INCLUDED_)