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.

SocketManager.cxx 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright (C) 2002-2005 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. // -=- SocketManager.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <winsock2.h>
  23. #include <list>
  24. #include <rfb/LogWriter.h>
  25. #include <rfb/Timer.h>
  26. #include <rfb_win32/SocketManager.h>
  27. using namespace rfb;
  28. using namespace rfb::win32;
  29. static LogWriter vlog("SocketManager");
  30. // -=- SocketManager
  31. SocketManager::SocketManager() {
  32. }
  33. SocketManager::~SocketManager() {
  34. }
  35. static void requestAddressChangeEvents(network::SocketListener* sock_) {
  36. DWORD dummy = 0;
  37. if (WSAIoctl(sock_->getFd(), SIO_ADDRESS_LIST_CHANGE, 0, 0, 0, 0, &dummy, 0, 0) == SOCKET_ERROR) {
  38. DWORD err = WSAGetLastError();
  39. if (err != WSAEWOULDBLOCK)
  40. vlog.error("Unable to track address changes: 0x%08x", (unsigned)err);
  41. }
  42. }
  43. void SocketManager::addListener(network::SocketListener* sock_,
  44. network::SocketServer* srvr,
  45. AddressChangeNotifier* acn) {
  46. WSAEVENT event = WSACreateEvent();
  47. long flags = FD_ACCEPT | FD_CLOSE;
  48. if (acn)
  49. flags |= FD_ADDRESS_LIST_CHANGE;
  50. try {
  51. if (event && (WSAEventSelect(sock_->getFd(), event, flags) == SOCKET_ERROR))
  52. throw rdr::SystemException("Unable to select on listener", WSAGetLastError());
  53. // requestAddressChangeEvents MUST happen after WSAEventSelect, so that the socket is non-blocking
  54. if (acn)
  55. requestAddressChangeEvents(sock_);
  56. // addEvent is the last thing we do, so that the event is NOT registered if previous steps fail
  57. if (!event || !addEvent(event, this))
  58. throw rdr::Exception("Unable to add listener");
  59. } catch (rdr::Exception& e) {
  60. if (event)
  61. WSACloseEvent(event);
  62. delete sock_;
  63. vlog.error("%s", e.str());
  64. throw;
  65. }
  66. ListenInfo li;
  67. li.sock = sock_;
  68. li.server = srvr;
  69. li.notifier = acn;
  70. li.disable = false;
  71. listeners[event] = li;
  72. }
  73. void SocketManager::remListener(network::SocketListener* sock) {
  74. std::map<HANDLE,ListenInfo>::iterator i;
  75. for (i=listeners.begin(); i!=listeners.end(); i++) {
  76. if (i->second.sock == sock) {
  77. removeEvent(i->first);
  78. WSACloseEvent(i->first);
  79. delete sock;
  80. listeners.erase(i);
  81. return;
  82. }
  83. }
  84. throw rdr::Exception("Listener not registered");
  85. }
  86. void SocketManager::addSocket(network::Socket* sock_, network::SocketServer* srvr, bool outgoing) {
  87. WSAEVENT event = WSACreateEvent();
  88. if (!event || !addEvent(event, this) ||
  89. (WSAEventSelect(sock_->getFd(), event, FD_READ | FD_CLOSE) == SOCKET_ERROR)) {
  90. if (event)
  91. WSACloseEvent(event);
  92. delete sock_;
  93. vlog.error("Unable to add connection");
  94. return;
  95. }
  96. ConnInfo ci;
  97. ci.sock = sock_;
  98. ci.server = srvr;
  99. connections[event] = ci;
  100. srvr->addSocket(sock_, outgoing);
  101. }
  102. void SocketManager::remSocket(network::Socket* sock_) {
  103. std::map<HANDLE,ConnInfo>::iterator i;
  104. for (i=connections.begin(); i!=connections.end(); i++) {
  105. if (i->second.sock == sock_) {
  106. i->second.server->removeSocket(sock_);
  107. removeEvent(i->first);
  108. WSACloseEvent(i->first);
  109. delete sock_;
  110. connections.erase(i);
  111. return;
  112. }
  113. }
  114. throw rdr::Exception("Socket not registered");
  115. }
  116. bool SocketManager::getDisable(network::SocketServer* srvr)
  117. {
  118. std::map<HANDLE,ListenInfo>::iterator i;
  119. for (i=listeners.begin(); i!=listeners.end(); i++) {
  120. if (i->second.server == srvr) {
  121. return i->second.disable;
  122. }
  123. }
  124. throw rdr::Exception("Listener not registered");
  125. }
  126. void SocketManager::setDisable(network::SocketServer* srvr, bool disable)
  127. {
  128. bool found = false;
  129. std::map<HANDLE,ListenInfo>::iterator i;
  130. for (i=listeners.begin(); i!=listeners.end(); i++) {
  131. if (i->second.server == srvr) {
  132. i->second.disable = disable;
  133. // There might be multiple sockets for the same server, so
  134. // continue iterating
  135. found = true;
  136. }
  137. }
  138. if (!found)
  139. throw rdr::Exception("Listener not registered");
  140. }
  141. int SocketManager::checkTimeouts() {
  142. int timeout = EventManager::checkTimeouts();
  143. std::map<HANDLE,ListenInfo>::iterator i;
  144. for (i=listeners.begin(); i!=listeners.end(); i++)
  145. soonestTimeout(&timeout, Timer::checkTimeouts());
  146. std::list<network::Socket*> shutdownSocks;
  147. std::map<HANDLE,ConnInfo>::iterator j, j_next;
  148. for (j=connections.begin(); j!=connections.end(); j=j_next) {
  149. j_next = j; j_next++;
  150. if (j->second.sock->isShutdown())
  151. shutdownSocks.push_back(j->second.sock);
  152. else {
  153. long eventMask = FD_READ | FD_CLOSE;
  154. if (j->second.sock->outStream().hasBufferedData())
  155. eventMask |= FD_WRITE;
  156. if (WSAEventSelect(j->second.sock->getFd(), j->first, eventMask) == SOCKET_ERROR)
  157. throw rdr::SystemException("unable to adjust WSAEventSelect:%u", WSAGetLastError());
  158. }
  159. }
  160. std::list<network::Socket*>::iterator k;
  161. for (k=shutdownSocks.begin(); k!=shutdownSocks.end(); k++)
  162. remSocket(*k);
  163. return timeout;
  164. }
  165. void SocketManager::processEvent(HANDLE event) {
  166. if (listeners.count(event)) {
  167. ListenInfo li = listeners[event];
  168. // Accept an incoming connection
  169. vlog.debug("accepting incoming connection");
  170. // What kind of event is this?
  171. WSANETWORKEVENTS network_events;
  172. WSAEnumNetworkEvents(li.sock->getFd(), event, &network_events);
  173. if (network_events.lNetworkEvents & FD_ACCEPT) {
  174. network::Socket* new_sock = li.sock->accept();
  175. if (new_sock && li.disable) {
  176. delete new_sock;
  177. new_sock = 0;
  178. }
  179. if (new_sock)
  180. addSocket(new_sock, li.server, false);
  181. } else if (network_events.lNetworkEvents & FD_CLOSE) {
  182. vlog.info("deleting listening socket");
  183. remListener(li.sock);
  184. } else if (network_events.lNetworkEvents & FD_ADDRESS_LIST_CHANGE) {
  185. li.notifier->processAddressChange();
  186. requestAddressChangeEvents(li.sock);
  187. } else {
  188. vlog.error("unknown listener event: %lx", network_events.lNetworkEvents);
  189. }
  190. } else if (connections.count(event)) {
  191. ConnInfo ci = connections[event];
  192. try {
  193. // Process data from an active connection
  194. WSANETWORKEVENTS events;
  195. long eventMask;
  196. // Fetch why this event notification triggered
  197. if (WSAEnumNetworkEvents(ci.sock->getFd(), event, &events) == SOCKET_ERROR)
  198. throw rdr::SystemException("unable to get WSAEnumNetworkEvents:%u", WSAGetLastError());
  199. // Cancel event notification for this socket
  200. if (WSAEventSelect(ci.sock->getFd(), event, 0) == SOCKET_ERROR)
  201. throw rdr::SystemException("unable to disable WSAEventSelect:%u", WSAGetLastError());
  202. // Reset the event object
  203. WSAResetEvent(event);
  204. // Call the socket server to process the event
  205. if (events.lNetworkEvents & FD_WRITE) {
  206. ci.server->processSocketWriteEvent(ci.sock);
  207. if (ci.sock->isShutdown()) {
  208. remSocket(ci.sock);
  209. return;
  210. }
  211. }
  212. if (events.lNetworkEvents & (FD_READ | FD_CLOSE)) {
  213. ci.server->processSocketReadEvent(ci.sock);
  214. if (ci.sock->isShutdown()) {
  215. remSocket(ci.sock);
  216. return;
  217. }
  218. }
  219. // Re-instate the required socket event
  220. // If the read event is still valid, the event object gets set here
  221. eventMask = FD_READ | FD_CLOSE;
  222. if (ci.sock->outStream().hasBufferedData())
  223. eventMask |= FD_WRITE;
  224. if (WSAEventSelect(ci.sock->getFd(), event, eventMask) == SOCKET_ERROR)
  225. throw rdr::SystemException("unable to re-enable WSAEventSelect:%u", WSAGetLastError());
  226. } catch (rdr::Exception& e) {
  227. vlog.error("%s", e.str());
  228. remSocket(ci.sock);
  229. }
  230. }
  231. }