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 7.1KB

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