blob: ef3597497ed68cf2ec447f7b9f05490437bc99c9 [file] [log] [blame]
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000033#include <map>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000034#include <network/Socket.h>
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000035#include <rfb_win32/EventManager.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000036
37namespace rfb {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000038 namespace win32 {
39
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000040 class SocketManager : public EventManager, EventHandler {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000041 public:
42 SocketManager();
43 virtual ~SocketManager();
44
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000045 // AddressChangeNotifier callback interface
46 // If an object implementing this is passed to addListener then it will be
47 // called whenever the SocketListener's address list changes
48 class AddressChangeNotifier {
49 public:
50 virtual ~AddressChangeNotifier() {}
51 virtual void processAddressChange(network::SocketListener* sl) = 0;
52 };
53
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000054 // Add a listening socket. Incoming connections will be added to the supplied
55 // SocketServer.
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000056 void addListener(network::SocketListener* sock_,
57 network::SocketServer* srvr,
58 AddressChangeNotifier* acn = 0);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000059
60 // Remove and delete a listening socket.
61 void remListener(network::SocketListener* sock);
62
63 // Add an already-connected socket. Socket events will cause the supplied
64 // SocketServer to be called. The socket must ALREADY BE REGISTERED with
65 // the SocketServer.
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000066 void addSocket(network::Socket* sock_, network::SocketServer* srvr, bool outgoing=true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000067
68 protected:
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000069 virtual int checkTimeouts();
70 virtual void processEvent(HANDLE event);
71 virtual void remSocket(network::Socket* sock);
72
73 struct ConnInfo {
74 network::Socket* sock;
75 network::SocketServer* server;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000076 };
Constantin Kaplinsky0a1eca12006-04-16 07:28:14 +000077 struct ListenInfo {
78 network::SocketListener* sock;
79 network::SocketServer* server;
80 AddressChangeNotifier* notifier;
81 };
82 std::map<HANDLE, ListenInfo> listeners;
83 std::map<HANDLE, ConnInfo> connections;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000084 };
85
86 }
87
88}
89
90#endif