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