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.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2002-2004 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.h
  19. // Socket manager class for Win32.
  20. // Passed a network::SocketListener and a network::SocketServer when
  21. // constructed. Uses WSAAsyncSelect to get notifications of network
  22. // connection attempts. When an incoming connection is received,
  23. // the manager will call network::SocketServer::addClient(). If
  24. // addClient returns true then the manager registers interest in
  25. // network events on that socket, and calls
  26. // network::SocketServer::processSocketEvent().
  27. #ifndef __RFB_WIN32_SOCKET_MGR_H__
  28. #define __RFB_WIN32_SOCKET_MGR_H__
  29. #include <list>
  30. #include <network/Socket.h>
  31. #include <rfb_win32/MsgWindow.h>
  32. namespace rfb {
  33. namespace win32 {
  34. class SocketManager {
  35. public:
  36. SocketManager();
  37. virtual ~SocketManager();
  38. // Add a listening socket. Incoming connections will be added to the supplied
  39. // SocketServer.
  40. void addListener(network::SocketListener* sock_, network::SocketServer* srvr);
  41. // Remove and delete a listening socket.
  42. void remListener(network::SocketListener* sock);
  43. // Add an already-connected socket. Socket events will cause the supplied
  44. // SocketServer to be called. The socket must ALREADY BE REGISTERED with
  45. // the SocketServer.
  46. void addSocket(network::Socket* sock_, network::SocketServer* srvr);
  47. // Add a Win32 event & handler for it to the SocketManager
  48. // This event will be blocked on along with the registered Sockets, and the
  49. // handler called whenever it is discovered to be set.
  50. // NB: SocketManager does NOT call ResetEvent on the event!
  51. // NB: If processEvent returns false then the event is no longer registered,
  52. // and the event object is assumed to have been closed by processEvent()
  53. struct EventHandler {
  54. virtual ~EventHandler() {}
  55. virtual bool processEvent(HANDLE event) = 0;
  56. };
  57. void addEvent(HANDLE event, EventHandler* ecb);
  58. // getMessage
  59. //
  60. // Either return a message from the thread's message queue or process a socket
  61. // event.
  62. // Returns whenever a message needs processing. Returns false if message is
  63. // WM_QUIT, true for all other messages.
  64. BOOL getMessage(MSG* msg, HWND hwnd, UINT minMsg, UINT maxMsg);
  65. protected:
  66. void addListener(network::SocketListener* sock, HANDLE event, network::SocketServer* server);
  67. void addSocket(network::Socket* sock, HANDLE event, network::SocketServer* server);
  68. void resizeArrays(int numSockets);
  69. void removeSocket(int index);
  70. struct SocketInfo {
  71. union {
  72. network::Socket* conn;
  73. network::SocketListener* listener;
  74. } sock;
  75. SOCKET fd;
  76. bool is_conn;
  77. bool is_event;
  78. union {
  79. network::SocketServer* server;
  80. EventHandler* handler;
  81. };
  82. };
  83. SocketInfo* sockets;
  84. HANDLE* events;
  85. int nSockets;
  86. int nAvail;
  87. };
  88. }
  89. }
  90. #endif