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

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