MainThread.cpp 12 KB

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