summaryrefslogtreecommitdiffstats
path: root/win/winvnc/VNCServerWin32.h
blob: 1feae3680da79ed5acd0b5a25c33aac00ea867e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifndef __VNCSERVER_WIN32_H__
#define __VNCSERVER_WIN32_H__

#include <winsock2.h>
#include <network/TcpSocket.h>
#include <rfb/VNCServerST.h>
#include <rfb_win32/RegConfig.h>
#include <rfb_win32/SDisplay.h>
#include <rfb_win32/SocketManager.h>
#include <rfb_win32/TCharArray.h>
#include <winvnc/QueryConnectDialog.h>
#include <winvnc/JavaViewer.h>
#include <winvnc/ManagedListener.h>

namespace winvnc {

  class STrayIconThread;

  class VNCServerWin32 : rfb::VNCServerST::QueryConnectionHandler,
                         rfb::win32::SocketManager::AddressChangeNotifier,
                         rfb::win32::RegConfig::Callback,
                         rfb::win32::EventHandler {
  public:
    VNCServerWin32();
    virtual ~VNCServerWin32();

    // Run the server in the current thread
    int run();

    // Cause the run() call to return
    // THREAD-SAFE
    void stop();

    // Determine whether a viewer is active
    // THREAD-SAFE
    bool isServerInUse() const {return isDesktopStarted;}

    // Connect out to the specified VNC Viewer
    // THREAD-SAFE
    bool addNewClient(const char* client);

    // Disconnect all connected clients
    // THREAD-SAFE
    bool disconnectClients(const char* reason=0);

    // Call used to notify VNCServerST of user accept/reject query completion
    // CALLED FROM AcceptConnectDialog THREAD
    void queryConnectionComplete();

    // Where to read the configuration settings from
    static const TCHAR* RegConfigPath;

    bool getClientsInfo(rfb::ListConnInfo* LCInfo);

    bool setClientsStatus(rfb::ListConnInfo* LCInfo);

  protected:
    // VNCServerST::QueryConnectionHandler interface
    // Callback used to prompt user to accept or reject a connection.
    // CALLBACK IN VNCServerST "HOST" THREAD
    virtual rfb::VNCServerST::queryResult queryConnection(network::Socket* sock,
                                                          const char* userName,
                                                          char** reason);

    // SocketManager::AddressChangeNotifier interface
    // Used to keep tray icon up to date
    virtual void processAddressChange(network::SocketListener* sl);

    // RegConfig::Callback interface
    // Called via the EventManager whenver RegConfig sees the registry change
    virtual void regConfigChanged();

    // EventHandler interface
    // Used to perform queued commands
    virtual void processEvent(HANDLE event);

  protected:
    // Perform a particular internal function in the server thread
    typedef enum {NoCommand, DisconnectClients, AddClient, QueryConnectionComplete, SetClientsStatus, GetClientsInfo} Command;
    bool queueCommand(Command cmd, const void* data, int len, bool wait=true);
    Command command;
    const void* commandData;
    int commandDataLen;
    rfb::Mutex commandLock;
    rfb::Condition commandSig;
    rfb::win32::Handle commandEvent;
    rfb::win32::Handle sessionEvent;

    // VNCServerWin32 Server-internal state
    rfb::win32::SDisplay desktop;
    rfb::VNCServerST vncServer;
    rfb::Mutex runLock;
    rfb::Thread* hostThread;
    bool runServer;
    bool isDesktopStarted;
    JavaViewerServer httpServer;
    rfb::win32::SocketManager sockMgr;
    rfb::win32::RegConfig config;

    ManagedListener rfbSock;
    ManagedListener httpSock;
    STrayIconThread* trayIcon;

    QueryConnectDialog* queryConnectDialog;
  };

};

#endif