MainThread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // MainThread.cpp: Implementierungsdatei
  15. //
  16. #include "stdafx.h"
  17. #include "MainThread.h"
  18. #ifndef MPEXT
  19. #include "fileexistsdlg.h"
  20. #include "resource.h"
  21. #include "entersomething.h"
  22. #endif
  23. #ifndef MPEXT_NO_CACHE
  24. #include "directorycache.h"
  25. #endif
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. #define ECS m_CriticalSection.Lock()
  32. #define LCS m_CriticalSection.Unlock()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMainThread
  35. CMainThread::CMainThread()
  36. {
  37. m_hOwnerWnd = 0;
  38. m_pTools = NULL;
  39. m_nReplyMessageID = 0;
  40. m_nInternalMessageID = 0;
  41. m_pPostKeepAliveCommand = 0;
  42. m_nTimerID = 0;
  43. m_pControlSocket = NULL;
  44. m_pFtpControlSocket = NULL;
  45. #ifndef MPEXT_NO_SFTP
  46. m_pSFtpControlSocket = NULL;
  47. #endif
  48. m_bBusy = FALSE;
  49. m_bConnected = FALSE;
  50. #ifndef MPEXT_NO_CACHE
  51. m_pDirectoryCache = 0;
  52. #endif
  53. m_pWorkingDir = 0;
  54. m_nAsyncRequestID = 0;
  55. m_bQuit = FALSE;
  56. #ifndef MPEXT_NO_IDENT
  57. m_pIdentServer = 0;
  58. #endif
  59. m_hThread = 0;
  60. m_dwThreadId = 0;
  61. }
  62. CMainThread::~CMainThread()
  63. {
  64. delete m_pWorkingDir;
  65. CloseHandle(m_hThread);
  66. }
  67. BOOL CMainThread::InitInstance()
  68. {
  69. m_nTimerID=SetTimer(0,1,1000,0);
  70. #ifndef MPEXT_NO_CACHE
  71. m_pDirectoryCache=new CDirectoryCache;
  72. #endif
  73. m_pPostKeepAliveCommand=0;
  74. // initialize Winsock library
  75. BOOL res=TRUE;
  76. WSADATA wsaData;
  77. WORD wVersionRequested = MAKEWORD(1, 1);
  78. int nResult = WSAStartup(wVersionRequested, &wsaData);
  79. if (nResult != 0)
  80. res=FALSE;
  81. else if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
  82. {
  83. WSACleanup();
  84. res=FALSE;
  85. }
  86. m_pFtpControlSocket=new CFtpControlSocket(this, m_pTools);
  87. #ifndef MPEXT_NO_SFTP
  88. m_pSFtpControlSocket=new CSFtpControlSocket(this);
  89. #endif
  90. m_pControlSocket=m_pFtpControlSocket;
  91. m_pFtpControlSocket->InitLog(this);
  92. #ifndef MPEXT_NO_SFTP
  93. m_pSFtpControlSocket->InitLog(this);
  94. #endif
  95. #ifndef MPEXT_NO_IDENT
  96. if (COptions::GetOptionVal(OPTION_IDENT) && !COptions::GetOptionVal(OPTION_IDENTCONNECT))
  97. m_pIdentServer=new CIdentServerControl(this);
  98. #endif
  99. return TRUE;
  100. }
  101. DWORD CMainThread::ExitInstance()
  102. {
  103. KillTimer(0,m_nTimerID);
  104. if (m_pFtpControlSocket)
  105. delete m_pFtpControlSocket;
  106. #ifndef MPEXT_NO_SFTP
  107. if (m_pSFtpControlSocket)
  108. delete m_pSFtpControlSocket;
  109. #endif
  110. #ifndef MPEXT_NO_CACHE
  111. if (m_pDirectoryCache)
  112. delete m_pDirectoryCache;
  113. #endif
  114. #ifndef MPEXT_NO_IDENT
  115. if (m_pIdentServer)
  116. delete m_pIdentServer;
  117. #endif
  118. return 1;
  119. }
  120. BOOL CMainThread::IsConnected()
  121. {
  122. BOOL bConnected;
  123. ECS;
  124. bConnected=m_bConnected;
  125. LCS;
  126. return bConnected;
  127. }
  128. void CMainThread::OnTimer(WPARAM wParam, LPARAM lParam)
  129. {
  130. if (!m_pControlSocket)
  131. return;
  132. if (wParam==m_nTimerID)
  133. m_pControlSocket->OnTimer();
  134. return;
  135. }
  136. void CMainThread::ShowStatus(CString status, int type)
  137. {
  138. ECS;
  139. if (m_bQuit)
  140. {
  141. LCS;
  142. return;
  143. }
  144. LCS;
  145. //Displays a message in the message log
  146. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  147. pStatus->post = TRUE;
  148. pStatus->status = status;
  149. pStatus->type = type;
  150. if (!PostMessage(m_hOwnerWnd, m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  151. delete pStatus;
  152. }
  153. void CMainThread::ShowStatus(UINT nID, int type)
  154. {
  155. ECS;
  156. if (m_bQuit)
  157. {
  158. LCS;
  159. return;
  160. }
  161. LCS;
  162. CString str;
  163. str.LoadString(nID);
  164. ShowStatus(str,type);
  165. }
  166. BOOL CMainThread::OnThreadMessage(UINT Msg, WPARAM wParam, LPARAM lParam)
  167. {
  168. if (Msg==m_nInternalMessageID)
  169. {
  170. if (wParam==FZAPI_THREADMSG_COMMAND)
  171. {
  172. if (m_pControlSocket && !m_pControlSocket->IsReady())
  173. m_pPostKeepAliveCommand=(t_command *)lParam;
  174. else
  175. {
  176. t_command *pCommand=(t_command *)lParam;
  177. switch(pCommand->id)
  178. {
  179. case FZ_COMMAND_CONNECT:
  180. ASSERT(!IsConnected());
  181. SetCurrentPath(CServerPath());
  182. #ifndef MPEXT_NO_SFTP
  183. if (pCommand->server.nServerType&FZ_SERVERTYPE_SUB_FTP_SFTP)
  184. m_pControlSocket=m_pSFtpControlSocket;
  185. else
  186. #endif
  187. m_pControlSocket=m_pFtpControlSocket;
  188. ASSERT(m_pControlSocket);
  189. m_pControlSocket->Connect(pCommand->server);
  190. break;
  191. case FZ_COMMAND_LIST:
  192. ASSERT(m_pControlSocket);
  193. m_pControlSocket->List(FALSE, 0, pCommand->path, pCommand->param1, pCommand->param4);
  194. break;
  195. case FZ_COMMAND_LISTFILE:
  196. ASSERT(m_pControlSocket);
  197. m_pControlSocket->ListFile(pCommand->path, pCommand->param1);
  198. break;
  199. case FZ_COMMAND_FILETRANSFER:
  200. ASSERT(m_pControlSocket);
  201. m_pControlSocket->FileTransfer(&pCommand->transferfile);
  202. break;
  203. case FZ_COMMAND_CUSTOMCOMMAND:
  204. ASSERT(m_pControlSocket);
  205. m_pControlSocket->FtpCommand(pCommand->param1);
  206. break;
  207. case FZ_COMMAND_DELETE:
  208. ASSERT(m_pControlSocket);
  209. m_pControlSocket->Delete(pCommand->param1, pCommand->path);
  210. break;
  211. case FZ_COMMAND_REMOVEDIR:
  212. ASSERT(m_pControlSocket);
  213. m_pControlSocket->RemoveDir(pCommand->param1, pCommand->path);
  214. break;
  215. case FZ_COMMAND_MAKEDIR:
  216. ASSERT(m_pControlSocket);
  217. m_pControlSocket->MakeDir(pCommand->path);
  218. break;
  219. case FZ_COMMAND_RENAME:
  220. ASSERT(m_pControlSocket);
  221. m_pControlSocket->Rename(pCommand->param1, pCommand->param2, pCommand->path, pCommand->newPath);
  222. break;
  223. case FZ_COMMAND_CHMOD:
  224. ASSERT(m_pControlSocket);
  225. m_pControlSocket->Chmod(pCommand->param1, pCommand->path, pCommand->param4);
  226. break;
  227. }
  228. delete pCommand;
  229. }
  230. }
  231. else if (wParam==FZAPI_THREADMSG_PROCESSREPLY)
  232. m_pControlSocket->ProcessReply();
  233. else if (wParam==FZAPI_THREADMSG_TRANSFEREND)
  234. m_pControlSocket->TransferEnd(lParam);
  235. else if (wParam==FZAPI_THREADMSG_CANCEL)
  236. m_pControlSocket->Cancel(lParam);
  237. else if (wParam==FZAPI_THREADMSG_DISCONNECT)
  238. m_pControlSocket->Disconnect();
  239. else if (wParam==FZAPI_THREADMSG_POSTKEEPALIVE)
  240. {
  241. if (m_pPostKeepAliveCommand)
  242. {
  243. PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_COMMAND,(LPARAM)m_pPostKeepAliveCommand);
  244. m_pPostKeepAliveCommand=0;
  245. }
  246. }
  247. else if (wParam==FZAPI_THREADMSG_ASYNCREQUESTREPLY)
  248. {
  249. CAsyncRequestData *pData=(CAsyncRequestData *)lParam;
  250. if (pData)
  251. {
  252. if (pData->nRequestID!=GetAsyncRequestID())
  253. LogMessage(__FILE__, __LINE__, this,FZ_LOG_INFO, _T("Ignoring old request ID"));
  254. else
  255. m_pControlSocket->SetAsyncRequestResult(pData->nRequestResult, pData);
  256. delete pData;
  257. }
  258. else
  259. LogMessage(__FILE__, __LINE__, this,FZ_LOG_WARNING, _T("Request reply without data"));
  260. }
  261. return TRUE;
  262. }
  263. else if (Msg==WM_TIMER)
  264. {
  265. OnTimer(wParam, lParam);
  266. }
  267. return TRUE;
  268. }
  269. BOOL CMainThread::IsBusy()
  270. {
  271. BOOL bBusy;
  272. ECS;
  273. bBusy=m_bBusy;
  274. LCS;
  275. return bBusy;
  276. }
  277. void CMainThread::Command(const t_command &command)
  278. {
  279. ASSERT(!IsBusy());
  280. ECS;
  281. if (m_bQuit)
  282. {
  283. LCS;
  284. return;
  285. }
  286. m_bBusy=TRUE;
  287. t_command *pCommand=new t_command;
  288. *pCommand=command;
  289. VERIFY(PostThreadMessage(m_nInternalMessageID,FZAPI_THREADMSG_COMMAND,(LPARAM)pCommand));
  290. m_LastCommand=command;
  291. LCS;
  292. }
  293. BOOL CMainThread::LastOperationSuccessful()
  294. {
  295. return TRUE;
  296. }
  297. void CMainThread::SetBusy(BOOL bBusy)
  298. {
  299. ECS;
  300. m_bBusy=bBusy;
  301. LCS;
  302. }
  303. void CMainThread::SetConnected(BOOL bConnected /*=TRUE*/)
  304. {
  305. ECS;
  306. m_bConnected=bConnected;
  307. #ifdef MPEXT
  308. if (!bConnected)
  309. {
  310. // when we loose connection
  311. // reset pending commands as we cannot fulfill them anyway
  312. m_pPostKeepAliveCommand = 0;
  313. }
  314. #endif
  315. LCS;
  316. }
  317. bool CMainThread::GetCurrentPath(CServerPath &dir)
  318. {
  319. if (!IsConnected())
  320. return false;
  321. ECS;
  322. dir=m_CurrentPath;
  323. LCS;
  324. return true;
  325. }
  326. CServerPath CMainThread::GetCurrentPath()
  327. {
  328. CServerPath path;
  329. bool res = GetCurrentPath(path);
  330. if (!res)
  331. return CServerPath();
  332. return path;
  333. }
  334. BOOL CMainThread::GetCurrentServer(t_server &server)
  335. {
  336. if (!IsConnected())
  337. return FALSE;
  338. ECS;
  339. server=m_pControlSocket->GetCurrentServer();
  340. LCS;
  341. return TRUE;
  342. }
  343. void CMainThread::Quit()
  344. {
  345. ECS;
  346. m_bQuit=TRUE;
  347. LCS;
  348. if (IsBusy())
  349. PostThreadMessage(m_nInternalMessageID, FZAPI_THREADMSG_CANCEL, 1);
  350. PostThreadMessage(WM_QUIT, 0, 0);
  351. }
  352. void CMainThread::SetCurrentPath(CServerPath path)
  353. {
  354. ECS;
  355. m_CurrentPath=path;
  356. LCS;
  357. return;
  358. }
  359. #ifndef MPEXT
  360. int CMainThread::GetOption(int nOption)
  361. {
  362. int nValue=0;
  363. ECS;
  364. std::map<int, int>::iterator iter=m_Options.find(nOption);
  365. if (iter!=m_Options.end())
  366. nValue=iter->second;
  367. LCS;
  368. return nValue;
  369. }
  370. void CMainThread::SetOption(int nOption, int nValue)
  371. {
  372. ECS;
  373. m_Options[nOption]=nValue;
  374. LCS;
  375. }
  376. #else
  377. bool CMainThread::UsingMlsd()
  378. {
  379. if (!IsConnected())
  380. return false;
  381. return m_pControlSocket->UsingMlsd();
  382. }
  383. std::string CMainThread::GetTlsVersionStr()
  384. {
  385. if (!IsConnected())
  386. return std::string();
  387. return m_pControlSocket->GetTlsVersionStr();
  388. }
  389. std::string CMainThread::GetCipherName()
  390. {
  391. if (!IsConnected())
  392. return std::string();
  393. return m_pControlSocket->GetCipherName();
  394. }
  395. #endif
  396. BOOL CMainThread::GetWorkingDir(t_directory *pWorkingDir)
  397. {
  398. ECS;
  399. if (m_pWorkingDir)
  400. {
  401. *pWorkingDir=*m_pWorkingDir;
  402. LCS;
  403. return TRUE;
  404. }
  405. LCS;
  406. return FALSE;
  407. }
  408. void CMainThread::SetWorkingDir(t_directory *pWorkingDir)
  409. {
  410. if (!pWorkingDir)
  411. {
  412. ECS;
  413. delete m_pWorkingDir;
  414. m_pWorkingDir=0;
  415. LCS;
  416. }
  417. else
  418. {
  419. ECS;
  420. if (!m_pWorkingDir)
  421. m_pWorkingDir=new t_directory;
  422. *m_pWorkingDir=*pWorkingDir;
  423. LCS;
  424. }
  425. if (pWorkingDir)
  426. {
  427. t_directory *pDirectoryToSend=new t_directory;
  428. *pDirectoryToSend=*pWorkingDir;
  429. if (!PostMessage(m_hOwnerWnd, m_nReplyMessageID, FZ_MSG_MAKEMSG(FZ_MSG_LISTDATA, 0), (LPARAM)pDirectoryToSend))
  430. delete pDirectoryToSend;
  431. }
  432. return;
  433. }
  434. bool CMainThread::GetWorkingDirPath(CServerPath &path)
  435. {
  436. ECS;
  437. if (m_pWorkingDir)
  438. {
  439. path = m_pWorkingDir->path;
  440. LCS;
  441. return true;
  442. }
  443. LCS;
  444. return false;
  445. }
  446. __int64 CMainThread::GetAsyncRequestID() const
  447. {
  448. return m_nAsyncRequestID;
  449. }
  450. __int64 CMainThread::GetNextAsyncRequestID()
  451. {
  452. return ++m_nAsyncRequestID;
  453. }
  454. CMainThread* CMainThread::Create(int nPriority /*=THREAD_PRIORITY_NORMAL*/, DWORD dwCreateFlags /*=0*/)
  455. {
  456. CMainThread *pMainThread=new CMainThread();
  457. pMainThread->m_hThread=CreateThread(0, 0, ThreadProc, pMainThread, dwCreateFlags, &pMainThread->m_dwThreadId);
  458. if (!pMainThread->m_hThread)
  459. {
  460. delete pMainThread;
  461. return NULL;
  462. }
  463. ::SetThreadPriority(pMainThread->m_hThread, nPriority);
  464. return pMainThread;
  465. }
  466. BOOL CMainThread::PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam)
  467. {
  468. return ::PostThreadMessage(m_dwThreadId, message, wParam, lParam);
  469. }
  470. DWORD CMainThread::ResumeThread()
  471. {
  472. BOOL res=::ResumeThread(m_hThread);
  473. if (res)
  474. {
  475. m_EventStarted.Lock();
  476. m_EventStarted.Unlock();
  477. }
  478. return res;
  479. }
  480. DWORD WINAPI CMainThread::ThreadProc(LPVOID lpParameter)
  481. {
  482. return ((CMainThread *)lpParameter)->Run();
  483. }
  484. DWORD CMainThread::Run()
  485. {
  486. ECS;
  487. InitInstance();
  488. m_EventStarted.SetEvent();
  489. LCS;
  490. MSG msg;
  491. while (GetMessage(&msg, 0, 0, 0))
  492. {
  493. TranslateMessage(&msg);
  494. if (!msg.hwnd)
  495. OnThreadMessage(msg.message, msg.wParam, msg.lParam);
  496. DispatchMessage(&msg);
  497. }
  498. DWORD res = ExitInstance();
  499. delete this;
  500. return res;
  501. }
  502. BOOL CMainThread::IsValid() const
  503. {
  504. if (!this)
  505. return FALSE;
  506. if (IsBadWritePtr((VOID *)this, sizeof(CMainThread)) )
  507. return FALSE;
  508. if (m_pControlSocket &&
  509. m_pControlSocket != m_pFtpControlSocket
  510. #ifndef MPEXT_NO_SFTP
  511. &&
  512. m_pControlSocket != m_pSFtpControlSocket
  513. #endif
  514. )
  515. return FALSE;
  516. return TRUE;
  517. }