You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CViewManager.cxx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <winsock2.h>
  19. #include <vncviewer/CViewManager.h>
  20. #include <vncviewer/CView.h>
  21. #include <vncviewer/ConnectionDialog.h>
  22. #include <vncviewer/ConnectingDialog.h>
  23. #include <rfb/Hostname.h>
  24. #include <rfb/util.h>
  25. #include <rfb/LogWriter.h>
  26. #include <rfb/vncAuth.h>
  27. #include <rdr/HexInStream.h>
  28. #include <network/TcpSocket.h>
  29. using namespace rfb;
  30. using namespace win32;
  31. static LogWriter vlog("CViewManager");
  32. // -=- Custom thread class used internally
  33. class CViewThread : public Thread {
  34. public:
  35. CViewThread(network::Socket* s, CViewManager& cvm);
  36. CViewThread(const char* conninfo, CViewManager& cvm, bool infoIsConfigFile);
  37. virtual ~CViewThread();
  38. virtual void run();
  39. protected:
  40. void setSocket(network::Socket* sock);
  41. network::Socket* sock;
  42. CharArray hostname;
  43. CViewManager& manager;
  44. bool useConfigFile;
  45. };
  46. CViewThread::CViewThread(network::Socket* s, CViewManager& cvm)
  47. : Thread("CView"), sock(s), manager(cvm) {
  48. setDeleteAfterRun();
  49. }
  50. CViewThread::CViewThread(const char* h, CViewManager& cvm, bool hIsConfigFile)
  51. : Thread("CView"), sock(0), manager(cvm), useConfigFile(hIsConfigFile) {
  52. setDeleteAfterRun();
  53. if (h) hostname.buf = strDup(h);
  54. }
  55. CViewThread::~CViewThread() {
  56. vlog.debug("~CViewThread");
  57. manager.remThread(this);
  58. delete sock;
  59. }
  60. void CViewThread::run() {
  61. try {
  62. CView view;
  63. view.setManager(&manager);
  64. if (!sock) {
  65. try {
  66. // If the hostname is actually a config filename then read it
  67. if (useConfigFile) {
  68. CharArray filename = hostname.takeBuf();
  69. CViewOptions options;
  70. options.readFromFile(filename.buf);
  71. if (options.host.buf)
  72. hostname.buf = strDup(options.host.buf);
  73. view.applyOptions(options);
  74. }
  75. // If there is no hostname then present the connection
  76. // dialog
  77. if (!hostname.buf) {
  78. ConnectionDialog conn(&view);
  79. if (!conn.showDialog())
  80. return;
  81. hostname.buf = strDup(conn.hostname.buf);
  82. // *** hack - Tell the view object the hostname
  83. CViewOptions opt(view.getOptions());
  84. opt.setHost(hostname.buf);
  85. view.applyOptions(opt);
  86. }
  87. // Parse the host name & port
  88. CharArray host;
  89. int port;
  90. getHostAndPort(hostname.buf, &host.buf, &port);
  91. // Attempt to connect
  92. ConnectingDialog dlg;
  93. // this is a nasty hack to get round a Win2K and later "feature" which
  94. // puts your second window in the background unless the first window
  95. // you put up actually gets some input. Just generate a fake shift
  96. // event, which seems to do the trick.
  97. keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);
  98. keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0);
  99. sock = new network::TcpSocket(host.buf, port);
  100. } catch(rdr::Exception& e) {
  101. vlog.error("unable to connect to %s (%s)", hostname, e.str());
  102. MsgBox(NULL, TStr(e.str()), MB_ICONERROR | MB_OK);
  103. return;
  104. }
  105. // Try to add the caller to the MRU
  106. MRU::addToMRU(hostname.buf);
  107. }
  108. view.initialise(sock);
  109. try {
  110. view.postQuitOnDestroy(true);
  111. while (true) {
  112. // - processMsg is designed to be callable in response to select().
  113. // As a result, it can be called when FdInStream data is available,
  114. // BUT there may be no actual RFB data available. This is the case
  115. // for example when reading data over an encrypted stream - an
  116. // entire block must be read from the FdInStream before any data
  117. // becomes available through the top-level encrypted stream.
  118. // Since we are using blockCallback and not doing a select() here,
  119. // we simply check() for some data on the top-level RFB stream.
  120. // This ensures that processMsg will only be called when there is
  121. // actually something to do. In the meantime, blockCallback()
  122. // will be called, keeping the user interface responsive.
  123. view.getInStream()->check(1,1);
  124. view.processMsg();
  125. }
  126. } catch (CView::QuitMessage& e) {
  127. // - Cope silently with WM_QUIT messages
  128. vlog.debug("QuitMessage received (wParam=%d)", e.wParam);
  129. } catch (rdr::EndOfStream& e) {
  130. // - Copy silently with disconnection if in NORMAL state
  131. if (view.state() == CConnection::RFBSTATE_NORMAL)
  132. vlog.debug(e.str());
  133. else {
  134. view.postQuitOnDestroy(false);
  135. throw rfb::Exception("server closed connection unexpectedly");
  136. }
  137. } catch (rdr::Exception&) {
  138. // - We MUST do this, otherwise ~CView will cause a
  139. // PostQuitMessage and any MessageBox call will quit immediately.
  140. view.postQuitOnDestroy(false);
  141. throw;
  142. }
  143. } catch(rdr::Exception& e) {
  144. // - Something went wrong - display the error
  145. vlog.error("error: %s", e.str());
  146. MsgBox(NULL, TStr(e.str()), MB_ICONERROR | MB_OK);
  147. }
  148. }
  149. // -=- CViewManager itself
  150. CViewManager::CViewManager()
  151. : MsgWindow(_T("CViewManager")), threadsSig(threadsMutex),
  152. mainThread(Thread::self()) {
  153. }
  154. CViewManager::~CViewManager() {
  155. while (!socks.empty()) {
  156. network::SocketListener* sock = socks.front();
  157. delete sock;
  158. socks.pop_front();
  159. }
  160. awaitEmpty();
  161. }
  162. void CViewManager::awaitEmpty() {
  163. Lock l(threadsMutex);
  164. while (!threads.empty()) {
  165. threadsSig.wait(true);
  166. }
  167. }
  168. void CViewManager::addThread(Thread* t) {
  169. Lock l(threadsMutex);
  170. threads.push_front(t);
  171. }
  172. void CViewManager::remThread(Thread* t) {
  173. Lock l(threadsMutex);
  174. threads.remove(t);
  175. threadsSig.signal();
  176. // If there are no listening sockets then post a quit message when the
  177. // last client disconnects
  178. if (socks.empty())
  179. PostThreadMessage(mainThread->getThreadId(), WM_QUIT, 0, 0);
  180. }
  181. bool CViewManager::addClient(const char* hostinfo, bool isConfigFile) {
  182. CViewThread* thread = new CViewThread(hostinfo, *this, isConfigFile);
  183. addThread(thread);
  184. thread->start();
  185. return true;
  186. }
  187. bool CViewManager::addListener(network::SocketListener* sock) {
  188. socks.push_back(sock);
  189. WSAAsyncSelect(sock->getFd(), getHandle(), WM_USER, FD_ACCEPT);
  190. return true;
  191. }
  192. bool CViewManager::addDefaultTCPListener(int port) {
  193. return addListener(new network::TcpListener(port));
  194. }
  195. LRESULT CViewManager::processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
  196. switch (msg) {
  197. case WM_USER:
  198. std::list<network::SocketListener*>::iterator i;
  199. for (i=socks.begin(); i!=socks.end(); i++) {
  200. if (wParam == (*i)->getFd()) {
  201. network::Socket* new_sock = (*i)->accept();
  202. CharArray connname;
  203. connname.buf = new_sock->getPeerEndpoint();
  204. vlog.debug("accepted connection: %s", connname);
  205. CViewThread* thread = new CViewThread(new_sock, *this);
  206. addThread(thread);
  207. thread->start();
  208. break;
  209. }
  210. }
  211. break;
  212. }
  213. return MsgWindow::processMessage(msg, wParam, lParam);
  214. }